diff options
| -rw-r--r-- | archaeological_finds/tests.py | 599 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/blocks/sheet_geo_items.html | 43 | ||||
| -rw-r--r-- | ishtar_common/templates/ishtar/blocks/sheet_simple_map.html | 31 | ||||
| -rw-r--r-- | ishtar_common/tests.py | 177 | 
4 files changed, 320 insertions, 530 deletions
| diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index 955b1aedc..00e14307c 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -2520,466 +2520,159 @@ class GeomaticTest(FindInit, TestCase):          self.assertEqual(base_find.point_2d.ewkt, "SRID=4326;POINT (42 3)")          self.assertEqual(base_find.point_source, "P") -    @staticmethod -    def create_cr_with_bfs(ope, geom_ope, geom_cr, list_geom_bf, label_cr): -        wgs84 = SpatialReferenceSystem.objects.get(srid=4326) -        poly_ope = ("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," -                    "((6 3,9 2,9 4,6 3)))") -        point_ope = GEOSGeometry("POINT({} {} {})".format(3.8, 3, 1), srid=4326) -        point_2d_ope = GEOSGeometry("POINT({} {})".format(3.8, 3), srid=4326) -        xy_ope = 3.8, 3 -        poly_cr = ("MULTIPOLYGON(((2 1,5 1,5 5,1 5,2 1),(2 2,2 3,3 3,3 2,2 2))," -                   "((6 3,9 2,9 4,6 3)))") -        point_cr = GEOSGeometry("POINT({} {} {})".format(3.9, 3, 1), srid=4326) -        point_2d_cr = GEOSGeometry("POINT({} {})".format(3.9, 3), srid=4326) -        xy_cr = 3.9, 3 -        poly_bf = ("MULTIPOLYGON(((2 1,4 1,4 4,1 4,2 1),(2 2,2 3,3 3,3 2,2 2))," -                    "((6 3,9 2,9 4,6 3)))") -        point_bf = GEOSGeometry("POINT({} {} {})".format(3.9, 2.9, 1), srid=4326) -        point_2d_bf = GEOSGeometry("POINT({} {})".format(3.9, 2.9), srid=4326) -        xy_bf = 3.9, 2.9 - -        pks = {} - -        ope.multi_polygon = "SRID=4326;" + poly_ope -        ope.multi_polygon_source = "P" -        ope.multi_polygon_source_item = str(ope._meta.verbose_name) -        ope.spatial_reference_system = wgs84 -        if geom_ope == "Point": -            ope.point = point_ope -            ope.point_source = "P" -            ope.point_source_item = str(ope._meta.verbose_name) -            ope.x, ope.y = xy_ope -            ope.point_2d = point_2d_ope -        ope.save()  # needs 2 saves because point_2d is none for multi_polygon -        ope = Operation.objects.get(pk=ope.pk) -        ope.save() -        ope = Operation.objects.get(pk=ope.pk) - -        default = { -            "label": label_cr, -            "operation": ope, -        } -        cr = ContextRecord.objects.create(**default) -        cr = ContextRecord.objects.get(pk=cr.pk) -        cr.multi_polygon = "SRID=4326;" + poly_cr -        cr.multi_polygon_source = "P" -        cr.multi_polygon_source_item = str(cr._meta.verbose_name) -        cr.spatial_reference_system = wgs84 -        if geom_cr == "Point": -            cr.point = point_cr -            cr.point_source = "P" -            cr.point_source_item = str(cr._meta.verbose_name) -            cr.x, cr.y = xy_cr -            cr.point_2d = point_2d_cr -        cr.save() -        cr = ContextRecord.objects.get(pk=cr.pk) -        pks[label_cr] = cr.pk - -        bfs = [] -        for bf_geom in list_geom_bf: -            default = { -                "label": "Find " + bf_geom + " from " + label_cr, -                "context_record": cr, -            } -            bf = models.BaseFind.objects.create(**default) -            bf.multi_polygon = "SRID=4326;" + poly_bf -            bf.multi_polygon_source = "P" -            bf.multi_polygon_source_item = str(bf._meta.verbose_name) -            bf.spatial_reference_system = wgs84 -            if bf_geom == "Point": -                bf.point = point_bf -                bf.point_source = "P" -                bf.point_source_item = str(bf._meta.verbose_name) -                bf.x, bf.y = xy_bf -                bf.point_2d = point_2d_bf -            bf.save() -            bf = models.BaseFind.objects.get(pk=bf.pk) -            bfs.append(bf) -            pks[default['label']] = bf.pk - -            find = models.Find.objects.create() -            find.base_finds.add(bf) -            find.save() -        return pks -      def test_get_geo_items(self): -        wgs84 = SpatialReferenceSystem.objects.get(srid=4326) +        from archaeological_operations.models import Operation +        from archaeological_context_records.models import ContextRecord +        from archaeological_finds.models import BaseFind +        from ishtar_common.tests import GeomaticTest as BaseGeomaticTest + +        #profile = models.get_current_profile()          profile = get_current_profile()          profile.mapping = True          profile.save() -        base_find = models.BaseFind.objects.all()[0] -        context_record = ContextRecord.objects.get(pk=base_find.context_record.pk) -        operation = self.create_operation()[0] -        ope = self.create_operation()[1] -        limit = ( -            "MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," -            "((6 3,9 2,9 4,6 3)))" -        ) -        geom = GEOSGeometry("POINT({} {} {})".format(2, 43, 1), srid=4326) -        geom_2d = GEOSGeometry("POINT({} {})".format(2, 43), srid=4326) -        xy = 2, 43 -        geom_params = [limit, geom, geom_2d, xy, wgs84] - -        # with point -        cr_pt_pt, bfs = self.create_cr_with_bfs(operation, "Point", "Point", ["Point"], "CR Pt Pt", geom_params) -        base_find_pt = bfs[0] - -        dic_pt_t = { -            "type": "Feature", -            "geometry": {"type": "MultiPolygon", "coordinates": []}, -            "properties": {"label": "Find 1 from CR Pt Pt"}, -        } -        res = base_find_pt.get_geo_items(get_polygons=True) -        self.assertEqual(dic_pt_t, res) -        dic_pt_f = copy.deepcopy(dic_pt_t) -        dic_pt_f["geometry"] = {"type": "Point", "coordinates": [2.0, 43.0]} -        res = base_find_pt.get_geo_items(get_polygons=False) -        self.assertEqual(dic_pt_f, res) - -        # test API with point -        url_pts = reverse("api-get-geo-points") -        url_polys = reverse("api-get-geo-polygons") -        response = self.client.get(url_pts + "?pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_pts + "?pk=" + str(base_find_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertIn(json.dumps(dic_pt_f).encode("utf-8"), response.content) - -        # test context record with point as base find -        dic_pt_f_copy = copy.deepcopy(dic_pt_f) -        dic_pt_pt_f = copy.deepcopy(dic_pt_f) -        dic_pt_pt_f["properties"] = { -            "label": "CR Pt Pt", -            "base-finds": {"type": "FeatureCollection", "features": [dic_pt_f_copy]}, -        } -        self.assertEqual(dic_pt_pt_f, cr_pt_pt.get_geo_items(False)) - -        dic_pt_t_copy = copy.deepcopy(dic_pt_t) -        dic_pt_pt_t = { -            "type": "Feature", -            "geometry": {"type": "MultiPolygon", "coordinates": []}, -            "properties": { -                "label": "CR Pt Pt", -                "base-finds": { -                    "type": "FeatureCollection", -                    "features": [dic_pt_t_copy], -                }, -            }, -        } -        self.assertEqual(dic_pt_pt_t, cr_pt_pt.get_geo_items(True)) - -        # test of API for point context record with point as base find -        response = self.client.get(url_pts + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_pts + "?context_record_pk=" + str(cr_pt_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertIn(json.dumps(dic_pt_pt_f).encode("utf-8"), response.content) - -        response = self.client.get(url_polys + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_polys + "?context_record_pk=" + str(cr_pt_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertIn(json.dumps(dic_pt_pt_t).encode("utf-8"), response.content) - -        # the context record is a multi-polygon -        cr_poly_pt, bfs = self.create_cr_with_bfs( -            operation, "Polygon", ["Point"], "CR Poly Pt", geom_params -        ) -        base_find_pt = bfs[0] -        dic_poly_t = { -            "type": "Feature", -            "geometry": { -                "type": "MultiPolygon", -                "coordinates": [ -                    [ -                        [[1.0, 1.0], [5.0, 1.0], [5.0, 5.0], [1.0, 5.0], [1.0, 1.0]], -                        [[2.0, 2.0], [2.0, 3.0], [3.0, 3.0], [3.0, 2.0], [2.0, 2.0]], -                    ], -                    [[[6.0, 3.0], [9.0, 2.0], [9.0, 4.0], [6.0, 3.0]]], -                ], -            }, -            "properties": {"label": "Find 1 from CR Poly Pt"}, -        } - -        dic_poly_pt_t = copy.deepcopy(dic_poly_t) -        dic_pt_t_from_poly = copy.deepcopy(dic_pt_t_copy) -        dic_pt_t_from_poly["properties"]["label"] = "Find 1 from CR Poly Pt" -        dic_poly_pt_t["properties"] = { -            "label": "CR Poly Pt", -            "base-finds": { -                "type": "FeatureCollection", -                "features": [dic_pt_t_from_poly], -            }, -        } -        self.assertEqual(dic_poly_pt_t, cr_poly_pt.get_geo_items(True)) - -        dic_poly_f = { -            "type": "Feature", -            "geometry": {"type": "Point", "coordinates": [3.86111, 3.02778]}, -            "properties": {"label": "Find 1 from CR Poly Pt"}, -        } -        dic_pt_f_from_poly = copy.deepcopy(dic_pt_f) -        dic_pt_f_from_poly["properties"]["label"] = "Find 1 from CR Poly Pt" -        dic_poly_pt_f = copy.deepcopy(dic_poly_f) -        dic_poly_pt_f["properties"] = { -            "label": "CR Poly Pt", -            "base-finds": { -                "type": "FeatureCollection", -                "features": [dic_pt_f_from_poly], -            }, -        } -        self.assertEqual(dic_poly_pt_f, cr_poly_pt.get_geo_items(False)) - -        # test of API for point context record with multipolygon as base find -        response = self.client.get(url_pts + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_pts + "?context_record_pk=" + str(cr_poly_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertIn(json.dumps(dic_poly_pt_f).encode("utf-8"), response.content) - -        response = self.client.get(url_polys + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get( -            url_polys + "?context_record_pk=" + str(cr_poly_pt.pk) -        ) -        self.assertEqual(response.status_code, 200) -        self.assertIn(json.dumps(dic_poly_pt_t).encode("utf-8"), response.content) - -        # we add a base find which is a multi-polygon -        cr_poly_poly_and_pt, bfs = self.create_cr_with_bfs( -            ope, "Polygon", ["Polygon", "Point"], "CR Poly Poly Pt", geom_params -        ) -        base_find_poly, base_find_pt = bfs - -        dic_poly_t_from_poly = copy.deepcopy(dic_poly_t) -        dic_poly_t_from_poly["properties"]["label"] = "Find 1 from CR Poly Poly Pt" -        dic_poly_f_from_poly = copy.deepcopy(dic_poly_f) -        dic_poly_f_from_poly["properties"]["label"] = "Find 1 from CR Poly Poly Pt" - -        res_poly = base_find_poly.get_geo_items(get_polygons=True) -        self.assertEqual(dic_poly_t_from_poly, res_poly) -        res_centroid = base_find_poly.get_geo_items(get_polygons=False) -        self.assertEqual(dic_poly_f_from_poly, res_centroid) +        '''username, password, user = create_superuser() +        client = Client() +        client.login(username=username, password=password) +        print("is_authenticated") +        print(user.is_authenticated())''' -        # test API with polygon -        response = self.client.get(url_polys + "?pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_polys + "?pk=" + str(base_find_poly.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_poly_t_from_poly, json.loads(response.content)) -        response = self.client.get(url_pts + "?pk=" + str(base_find_poly.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_poly_f_from_poly, json.loads(response.content)) +        geoms, pks = BaseGeomaticTest.setUpDefaultGeoItems(self.user) -        # context record is a polygon -        # (and one base find is a point and the other is a polygon) -        self.assertEqual( -            base_find_pt.context_record.pk, base_find_poly.context_record.pk -        ) -        dic_poly_t_both = copy.deepcopy(dic_poly_t_from_poly) -        dic_poly_t_both["properties"]["label"] = "Find 1 from CR Poly Poly Pt" -        dic_poly_f_both = copy.deepcopy(dic_poly_f_from_poly) -        dic_poly_f_both["properties"]["label"] = "Find 1 from CR Poly Poly Pt" -        dic_pt_t_both = copy.deepcopy(dic_pt_t_from_poly) -        dic_pt_t_both["properties"]["label"] = "Find 2 from CR Poly Poly Pt" -        dic_pt_f_both = copy.deepcopy(dic_pt_f_from_poly) -        dic_pt_f_both["properties"]["label"] = "Find 2 from CR Poly Poly Pt" - -        dic_poly_poly_and_pt_t = copy.deepcopy(dic_poly_t) -        dic_poly_poly_and_pt_t["properties"] = { -            "label": "CR Poly Poly Pt", -            "base-finds": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_t_both, -                    dic_pt_t_both, -                ], -            }, -        } -        self.assertEqual( -            dic_poly_poly_and_pt_t, cr_poly_poly_and_pt.get_geo_items(True) -        ) - -        dic_poly_poly_and_pt_f = copy.deepcopy(dic_poly_f) -        dic_poly_poly_and_pt_f["properties"] = { -            "base-finds": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_f_both, -                    dic_pt_f_both, -                ], -            }, -            "label": "CR Poly Poly Pt", -        } -        self.assertEqual( -            dic_poly_poly_and_pt_f, cr_poly_poly_and_pt.get_geo_items(False) -        ) - -        # test of API for multipolygon context record with a multipolygon and a point as base find -        response = self.client.get(url_pts + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get( -            url_pts + "?context_record_pk=" + str(cr_poly_poly_and_pt.pk) -        ) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_poly_poly_and_pt_f, json.loads(response.content)) - -        response = self.client.get(url_polys + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get( -            url_polys + "?context_record_pk=" + str(cr_poly_poly_and_pt.pk) -        ) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_poly_poly_and_pt_t, json.loads(response.content)) - -        # context record is a point, base find 1 a polygon and base find 2 a point -        cr_pt_poly_and_pt, bfs = self.create_cr_with_bfs( -            ope, "Point", ["Polygon", "Point"], "CR Pt Poly Pt", geom_params -        ) -        base_find_poly, base_find_pt = bfs - -        dic_poly_t_pt_based = copy.deepcopy(dic_poly_t_from_poly) -        dic_poly_t_pt_based["properties"]["label"] = "Find 1 from CR Pt Poly Pt" -        dic_poly_f_pt_based = copy.deepcopy(dic_poly_f_from_poly) -        dic_poly_f_pt_based["properties"]["label"] = "Find 1 from CR Pt Poly Pt" -        dic_pt_t_pt_based = copy.deepcopy(dic_pt_t_from_poly) -        dic_pt_t_pt_based["properties"]["label"] = "Find 2 from CR Pt Poly Pt" -        dic_pt_f_pt_based = copy.deepcopy(dic_pt_f_from_poly) -        dic_pt_f_pt_based["properties"]["label"] = "Find 2 from CR Pt Poly Pt" - -        dic_pt_poly_and_pt_t = copy.deepcopy(dic_pt_pt_t) -        dic_pt_poly_and_pt_t["properties"] = { -            "base-finds": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_t_pt_based, -                    dic_pt_t_pt_based, -                ], -            }, -            "label": "CR Pt Poly Pt", -        } -        self.assertEqual(dic_pt_poly_and_pt_t, cr_pt_poly_and_pt.get_geo_items(True)) - -        dic_pt_poly_and_pt_f = copy.deepcopy(dic_pt_pt_f) -        dic_pt_poly_and_pt_f["properties"] = { -            "base-finds": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_f_pt_based, -                    dic_pt_f_pt_based, -                ], -            }, -            "label": "CR Pt Poly Pt", -        } -        self.assertEqual(dic_pt_poly_and_pt_f, cr_pt_poly_and_pt.get_geo_items(False)) - -        # test of API for point context record with multipolygon as base find -        response = self.client.get(url_pts + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get( -            url_pts + "?context_record_pk=" + str(cr_pt_poly_and_pt.pk) -        ) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_pt_poly_and_pt_f, json.loads(response.content)) - -        response = self.client.get(url_polys + "?context_record_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get( -            url_polys + "?context_record_pk=" + str(cr_pt_poly_and_pt.pk) -        ) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_pt_poly_and_pt_t, json.loads(response.content)) - -        # test of operation - -        dic_pt_pt_and_poly_pt_and_poly_f = copy.deepcopy(dic_pt_f) -        dic_pt_pt_and_poly_pt_and_poly_f["properties"] = { -            "label": "OA2", -            "context-records": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_poly_and_pt_f, -                    dic_pt_poly_and_pt_f, -                ], -            }, -        } -        dic_pt_pt_and_poly_pt_and_poly_t = copy.deepcopy(dic_pt_t) -        dic_pt_pt_and_poly_pt_and_poly_t["properties"] = { -            "context-records": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_poly_and_pt_t, -                    dic_pt_poly_and_pt_t, -                ], -            }, -            "label": "OA2", -        } -        dic_poly_pt_and_poly_pt_and_poly_t = copy.deepcopy(dic_poly_t) -        dic_poly_pt_and_poly_pt_and_poly_t["properties"] = { -            "context-records": { -                "type": "FeatureCollection", -                "features": [ -                    dic_poly_poly_and_pt_t, -                    dic_pt_poly_and_pt_t, -                ], -            }, -            "label": "OA2", -        } - -        ope_pt = Operation.objects.get(pk=ope.pk) -        ope_pt.point = geom -        ope_pt.multi_polygon = None -        ope_pt.multi_polygon_source = None -        ope_pt.multi_polygon_source_item = None -        ope_pt.x, ope_pt.y = xy -        ope_pt.point_2d = geom_2d -        ope_pt.spatial_reference_system = wgs84 -        ope_pt.save() -        ope_pt = Operation.objects.get(pk=ope_pt.pk) -        self.assertEqual(dic_pt_pt_and_poly_pt_and_poly_f, ope_pt.get_geo_items(False)) -        self.assertEqual(dic_pt_pt_and_poly_pt_and_poly_t, ope_pt.get_geo_items(True)) - -        response = self.client.get(url_polys + "?operation_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_polys + "?operation_pk=" + str(ope_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_pt_pt_and_poly_pt_and_poly_t, json.loads(response.content)) - -        response = self.client.get(url_pts + "?operation_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_pts + "?operation_pk=" + str(ope_pt.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual(dic_pt_pt_and_poly_pt_and_poly_f, json.loads(response.content)) - -        # ope is now a multipolygon -        ope_poly = Operation.objects.get(pk=ope.pk) -        ope_poly.multi_polygon = "SRID=4326;" + limit -        ope_poly.save() -        ope_poly = Operation.objects.get(pk=ope_poly.pk) - -        self.assertEqual( -            dic_pt_pt_and_poly_pt_and_poly_f, ope_poly.get_geo_items(False) -        ) -        self.assertEqual( -            dic_poly_pt_and_poly_pt_and_poly_t, ope_poly.get_geo_items(True) -        ) +        url_pts = reverse("api-get-geo-points") +        url_polys = reverse("api-get-geo-polygons") -        response = self.client.get(url_polys + "?operation_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_polys + "?operation_pk=" + str(ope_poly.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual( -            dic_poly_pt_and_poly_pt_and_poly_t, json.loads(response.content) -        ) +        cache_dics_t, cache_dics_f = {}, {} -        response = self.client.get(url_pts + "?operation_pk=0") -        self.assertEqual(response.status_code, 404) -        response = self.client.get(url_pts + "?operation_pk=" + str(ope_poly.pk)) -        self.assertEqual(response.status_code, 200) -        self.assertEqual( -            dic_pt_pt_and_poly_pt_and_poly_f, json.loads(response.content) -        ) +        def finds_first(x): +            if x[:2] == "CR": +                return 0 +            if x[:4] == "Find": +                return -1 +            else: +                return 1 + +        labels = list(pks.keys()) +        labels.sort(key=finds_first) +        for label in labels: +            pk = pks[label] +            if label[:4] == "Find": +                dic_t = { +                    "type": "Feature", +                    "geometry": { +                        "type": "MultiPolygon", "coordinates": geoms[2][0] +                    }, +                    "properties": {"label": label}, +                } +                dic_f = { +                    "type": "Feature", +                    "geometry": { +                        "type": "Point", "coordinates": BaseGeomaticTest.pt_coords_from_label(label, geoms), +                    }, +                    "properties": {"label": label}, +                } +                cache_dics_t[label] = dic_t +                cache_dics_f[label] = dic_f +                res_t = BaseFind.objects.get(pk=pk).get_geo_items(get_polygons=True) +                res_f = BaseFind.objects.get(pk=pk).get_geo_items(get_polygons=False) +                get_pk = "?pk=" + +            elif label[:2] == "CR": +                cache_key = lambda x: "Find " + x + " from " + label +                dic_t = { +                    "type": "Feature", +                    "geometry": { +                        "type": "MultiPolygon", "coordinates": geoms[1][0] +                    }, +                    "properties": { +                        "label": label, +                        "base-finds": { +                            'type': 'FeatureCollection', +                            'features': [ +                                cache_dics_t[cache_key("Polygon")], +                                cache_dics_t[cache_key("Point")], +                            ] +                        } +                    }, +                } +                dic_f = { +                    "type": "Feature", +                    "geometry": { +                        "type": "Point", "coordinates": BaseGeomaticTest.pt_coords_from_label(label, geoms), +                    }, +                    "properties": { +                        "label": label, +                        "base-finds": { +                            'type': 'FeatureCollection', +                            'features': [ +                                cache_dics_f[cache_key("Polygon")], +                                cache_dics_f[cache_key("Point")], +                            ] +                        } +                    }, +                } +                cache_dics_t[label] = dic_t +                cache_dics_f[label] = dic_f +                res_t = ContextRecord.objects.get(pk=pk).get_geo_items(get_polygons=True) +                res_f = ContextRecord.objects.get(pk=pk).get_geo_items(get_polygons=False) +                get_pk = "?context_record_pk=" +            else: +                real_label = "OA3" +                cache_key_prefix = "CR " + label + " " +                if label == "Pt": +                    real_label = "OA2" +                dic_t = { +                    "type": "Feature", +                    "geometry": { +                        "type": "MultiPolygon", "coordinates": geoms[0][0] +                    }, +                    "properties": { +                        "label": real_label, +                        "context-records": { +                            'type': 'FeatureCollection', +                            'features': [ +                                cache_dics_t[cache_key_prefix + "Poly"], +                                cache_dics_t[cache_key_prefix + "Pt"], +                            ] +                        } +                    }, +                } +                dic_f = { +                    "type": "Feature", +                    "geometry": { +                        "type": "Point", "coordinates": BaseGeomaticTest.pt_coords_from_label(label, geoms), +                    }, +                    "properties": { +                        "label": real_label, +                        "context-records": { +                            'type': 'FeatureCollection', +                            'features': [ +                                cache_dics_f[cache_key_prefix + "Poly"], +                                cache_dics_f[cache_key_prefix + "Pt"], +                            ] +                        } +                    }, +                } +                res_t = Operation.objects.get(pk=pk).get_geo_items(get_polygons=True) +                res_f = Operation.objects.get(pk=pk).get_geo_items(get_polygons=False) +                get_pk = "?operation_pk=" + +            self.assertEqual(res_t, dic_t) +            print("url") +            print(url_polys + get_pk + "0") +            response = self.client.get(url_polys + get_pk + "0") +            print('response') +            print(response) +            self.assertEqual(response.status_code, 404) +            response = self.client.get(url_polys + get_pk + str(pk)) +            self.assertEqual(response.status_code, 200) +            self.assertEqual(dic_t, json.loads(response.content)) + +            self.assertEqual(dic_f, res_f) +            response = self.client.get(url_pts + get_pk + "0") +            self.assertEqual(response.status_code, 404) +            response = self.client.get(url_pts + get_pk + str(pk)) +            self.assertEqual(response.status_code, 200) +            self.assertEqual(dic_f, json.loads(response.content)) @@ -3307,7 +3000,7 @@ class SeleniumTestsBaseFinds(SeleniumTests):      def test_geo_items(self):          self.selenium.implicitly_wait(5) -        geo_elements, ope_pks = self.setUpDefaultGeoItems() +        geo_elements, pks = self.setUpDefaultGeoItems()          poly_bf, centr_bf, pt_bf = geo_elements[2]          dic_polygon = { diff --git a/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html b/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html index 3d76a5aa5..1c45b5d9f 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_geo_items.html @@ -1,60 +1,43 @@  {% load i18n window_field %}{% if PROFILE.mapping %}  {% if geo_item.point_2d or geo_item.multi_polygon %} +{% load ids_display_geo_items %}  <dl class="col-12">      <dt>{% trans "Display geo items" %}</dt>      <dd>          <fieldset>              {% if geo_item.SLUG == "operation" %} -            <input type="checkbox" name="display-cr-for-ope" id="display-cr-for-ope"> -            <label for="display-cr-for-ope">{% trans "Display context records" %}</label> -            <input type="checkbox" name="display-bf-for-ope" id="display-bf-for-ope"> -            <label for="display-bf-for-ope">{% trans "Display base finds" %}</label> -            {% else %} -            <input type="checkbox" name="display-bf-for-cr" id="display-bf-for-cr"> -            <label for="display-bf-for-cr">{% trans "Display base finds" %}</label> +            <input type="checkbox" id="disp-cr-for-{{geo_item.SLUG}}-{{geo_item.pk}}"> +            <label for="disp-cr-for-{{geo_item.SLUG}}-{{geo_item.pk}}">{% trans "Display context records" %}</label>              {% endif %} +            <input type="checkbox" id="disp-bf-for-{{geo_item.SLUG}}-{{geo_item.pk}}"> +            <label for="disp-bf-for-{{geo_item.SLUG}}-{{geo_item.pk}}">{% trans "Display base finds" %}</label>          </fieldset>      </dd>      <dd> -        {% if geo_item.SLUG == "operation" %} -        <label for="get-poly-ope" id="label-get-poly-ope">{% trans "Geometry: " %}</label> -        <select name="get-poly-ope" id="get-poly-ope"> +        <label for="get-poly-for-{{geo_item.SLUG}}-{{geo_item.pk}}" id="get-poly-label-for-{{geo_item.SLUG}}-{{geo_item.pk}}">{% trans "Geometry: " %}</label> +        <select id="get-poly-for-{{geo_item.SLUG}}-{{geo_item.pk}}">              <option value="polygons" selected="selected">{% trans "Display polygons" %}</option>              <option value="points">{% trans "Display points" %}</option>          </select> -        {% else %} -        <label for="get-poly-cr" id="label-get-poly-cr">{% trans "Geometry: " %}</label> -        <select name="get-poly-cr" id="get-poly-cr"> -            <option value="polygons" selected="selected">{% trans "Display polygons" %}</option> -            <option value="points">{% trans "Display points" %}</option> -        </select> -        {% endif %}      </dd>  </dl>  <script>  const hide_get_polygons = function() { -    {% if geo_item.SLUG == "operation" %} -    const disp_cr = $("#display-cr-for-ope"); -    const disp_bf = $("#display-bf-for-ope"); -    const get_poly = $("#get-poly-ope"); -    const label = $("#label-get-poly-ope"); -    {% else %} -    const disp_cr = null; -    const disp_bf = $("#display-bf-for-cr"); -    const get_poly = $("#get-poly-cr"); -    const label = $("#label-get-poly-cr"); -    {% endif %} +    const disp_cr = $("#disp-cr-for-{{geo_item.SLUG}}-{{geo_item.pk}}"); +    const disp_bf = $("#disp-bf-for-{{geo_item.SLUG}}-{{geo_item.pk}}"); +    const get_poly = $("#get-poly-for-{{geo_item.SLUG}}-{{geo_item.pk}}"); +    const label = $("#get-poly-label-for-{{geo_item.SLUG}}-{{geo_item.pk}}");      get_poly.hide();      label.hide();      const display_get_poly = function () { -        if (disp_bf.prop('checked') || ((disp_cr != null) && disp_cr.prop('checked'))) { +        if (disp_bf.prop('checked') || ((disp_cr.length > 0) && disp_cr.prop('checked'))) {              get_poly.show();              label.show();          } else {              get_poly.hide();              label.hide();}      } -    if (disp_cr != null) { +    if (disp_cr.length > 0) {          disp_cr.change( display_get_poly );      }      disp_bf.change( display_get_poly ) diff --git a/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html b/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html index b44eb5724..a472c8b07 100644 --- a/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html +++ b/ishtar_common/templates/ishtar/blocks/sheet_simple_map.html @@ -1,5 +1,6 @@  {% if PROFILE.mapping %}  {% if geo_item.point_2d or geo_item.multi_polygon %} +{% load ids_display_geo_items %}  <div class="col-12 col-lg-6 flex-wrap">  <div class="window-map" id="map-{{window_id}}-{{geo_item.SLUG}}-{{geo_item.pk}}">  </div> @@ -21,23 +22,25 @@ disp_map();  {% if geo_item.SLUG == "operation" or geo_item.SLUG == "contextrecord" %}  const disp_geo_items = function(disp_cr, disp_bf, get_polygons) { -    const get_poly = (get_polygons === "polygons"); -    let url = null; -    if (get_poly) { -        url = "{% url 'api-get-geo-polygons' %}"; +    if (disp_cr || disp_bf) { +        const get_poly = (get_polygons === "polygons"); +        let url = null; +        if (get_poly) { +            url = "{% url 'api-get-geo-polygons' %}"; +        } +        else { +            url = "{% url 'api-get-geo-points' %}"; +        } +        display_geo_items("map-{{window_id}}-{{geo_item.SLUG}}-{{geo_item.pk}}", url, "{{geo_item.SLUG}}", +                       "{{geo_item.pk}}", disp_cr, disp_bf, get_poly);      } -    else { -        url = "{% url 'api-get-geo-points' %}"; -    } -    display_geo_items("map-{{window_id}}-{{geo_item.SLUG}}-{{geo_item.pk}}", url, "{{geo_item.SLUG}}", -                   "{{geo_item.pk}}", disp_cr, disp_bf, get_poly);  } -  let geo_items_displayed_once = false; +const disp_bf = $("#disp-bf-for-{{geo_item.SLUG}}-{{geo_item.pk}}"); +const get_polygons = $("#get-poly-for-{{geo_item.SLUG}}-{{geo_item.pk}}"); +  {% if geo_item.SLUG == "operation" %} -const disp_cr = $("#display-cr-for-ope"); -const disp_bf = $("#display-bf-for-ope"); -const get_polygons = $("#get-poly-ope"); +const disp_cr = $("#disp-cr-for-{{geo_item.SLUG}}-{{geo_item.pk}}");  const event_func_disp_geo_items = function () {      if (geo_items_displayed_once) {          disp_map(); @@ -52,8 +55,6 @@ get_polygons.change( function () {      disp_geo_items(disp_cr.prop('checked'), disp_bf.prop('checked'), get_polygons.val());  })  {% else %} -const disp_bf = $("#display-bf-for-cr"); -const get_polygons = $("#get-poly-cr");  const event_func_disp_geo_items = function () {      if (geo_items_displayed_once) {          disp_map(); diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index bebb58eac..d8087163d 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -2701,6 +2701,8 @@ class IshtarBasicTest(TestCase):  class GeomaticTest(TestCase): +    fixtures = FIND_FIXTURES +      def test_post_save_point(self):          class FakeGeomaticObject(object):              _meta = models.GeoItem._meta @@ -2734,6 +2736,148 @@ class GeomaticTest(TestCase):          self.assertIsNotNone(obj.point_2d)          self.assertIsNotNone(obj.point) +    @staticmethod +    def create_cr_with_bfs(ope, geom_ope, geom_cr, list_geom_bf, label_cr): +        from archaeological_operations.models import Operation +        from archaeological_context_records.models import ContextRecord +        from archaeological_finds.models import BaseFind, Find +        from django.contrib.gis.geos import GEOSGeometry +        from ishtar_common.models_common import SpatialReferenceSystem + +        wgs84 = SpatialReferenceSystem.objects.get(srid=4326) +        poly_ope = ("MULTIPOLYGON(((1 1,5 1,5 5,1 5,1 1),(2 2,2 3,3 3,3 2,2 2))," +                    "((6 3,9 2,9 4,6 3)))") +        point_ope = GEOSGeometry("POINT({} {} {})".format(3.8, 3, 1), srid=4326) +        point_2d_ope = GEOSGeometry("POINT({} {})".format(3.8, 3), srid=4326) +        xy_ope = 3.8, 3 +        poly_cr = ("MULTIPOLYGON(((2 1,5 1,5 5,1 5,2 1),(2 2,2 3,3 3,3 2,2 2))," +                   "((6 3,9 2,9 4,6 3)))") +        point_cr = GEOSGeometry("POINT({} {} {})".format(3.9, 3, 1), srid=4326) +        point_2d_cr = GEOSGeometry("POINT({} {})".format(3.9, 3), srid=4326) +        xy_cr = 3.9, 3 +        poly_bf = ("MULTIPOLYGON(((2 1,4 1,4 4,1 4,2 1),(2 2,2 3,3 3,3 2,2 2))," +                   "((6 3,9 2,9 4,6 3)))") +        point_bf = GEOSGeometry("POINT({} {} {})".format(3.9, 2.9, 1), srid=4326) +        point_2d_bf = GEOSGeometry("POINT({} {})".format(3.9, 2.9), srid=4326) +        xy_bf = 3.9, 2.9 + +        pks = {} + +        ope.multi_polygon = "SRID=4326;" + poly_ope +        ope.multi_polygon_source = "P" +        ope.multi_polygon_source_item = str(ope._meta.verbose_name) +        ope.spatial_reference_system = wgs84 +        if geom_ope == "Point": +            ope.point = point_ope +            ope.point_source = "P" +            ope.point_source_item = str(ope._meta.verbose_name) +            ope.x, ope.y = xy_ope +            ope.point_2d = point_2d_ope +        ope.save()  # needs 2 saves because point_2d is none for multi_polygon +        ope = Operation.objects.get(pk=ope.pk) +        ope.save() +        ope = Operation.objects.get(pk=ope.pk) + +        default = { +            "label": label_cr, +            "operation": ope, +        } +        cr = ContextRecord.objects.create(**default) +        cr = ContextRecord.objects.get(pk=cr.pk) +        cr.multi_polygon = "SRID=4326;" + poly_cr +        cr.multi_polygon_source = "P" +        cr.multi_polygon_source_item = str(cr._meta.verbose_name) +        cr.spatial_reference_system = wgs84 +        if geom_cr == "Point": +            cr.point = point_cr +            cr.point_source = "P" +            cr.point_source_item = str(cr._meta.verbose_name) +            cr.x, cr.y = xy_cr +            cr.point_2d = point_2d_cr +        cr.save() +        cr = ContextRecord.objects.get(pk=cr.pk) +        pks[label_cr] = cr.pk + +        bfs = [] +        for bf_geom in list_geom_bf: +            default = { +                "label": "Find " + bf_geom + " from " + label_cr, +                "context_record": cr, +            } +            bf = BaseFind.objects.create(**default) +            bf.multi_polygon = "SRID=4326;" + poly_bf +            bf.multi_polygon_source = "P" +            bf.multi_polygon_source_item = str(bf._meta.verbose_name) +            bf.spatial_reference_system = wgs84 +            if bf_geom == "Point": +                bf.point = point_bf +                bf.point_source = "P" +                bf.point_source_item = str(bf._meta.verbose_name) +                bf.x, bf.y = xy_bf +                bf.point_2d = point_2d_bf +            bf.save() +            bf = BaseFind.objects.get(pk=bf.pk) +            bfs.append(bf) +            pks[default['label']] = bf.pk + +            find = Find.objects.create() +            find.base_finds.add(bf) +            find.save() +        return pks + +    @staticmethod +    def setUpDefaultGeoItems(user): +        from archaeological_operations.tests import create_operation + +        ope1 = create_operation(user) +        ope2 = create_operation(user) +        pks = {'Pt': ope1.pk, 'Poly': ope2.pk} + +        pks.update(GeomaticTest.create_cr_with_bfs(ope1, "Point", "Point", ["Polygon", "Point"], "CR Pt Pt")) +        pks.update(GeomaticTest.create_cr_with_bfs(ope1, "Point", "Polygon", ["Polygon", "Point"], "CR Pt Poly")) +        pks.update(GeomaticTest.create_cr_with_bfs(ope2, "Polygon", "Point", ["Polygon", "Point"], "CR Poly Pt")) +        pks.update(GeomaticTest.create_cr_with_bfs(ope2, "Polygon", "Polygon", ["Polygon", "Point"], "CR Poly Poly")) + +        geom_ope = [ +            [[[[1, 1], [5, 1], [5, 5], [1, 5], [1, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], +             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], +            [3.86111, 3.02778], +            [3.8, 3] +        ] +        geom_cr = [ +            [[[[2, 1], [5, 1], [5, 5], [1, 5], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], +             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], +            [4.17708, 3.11458], +            [3.9, 3] +        ] +        geom_bf = [ +            [[[[2, 1], [4, 1], [4, 4], [1, 4], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], +             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], +            [4.42105, 2.73684], +            [3.9, 2.9] +        ] +        return [geom_ope, geom_cr, geom_bf], pks + +    @staticmethod +    def pt_coords_from_label(label, geoms): +        if label[:2] == "Pt": +            return geoms[0][2] +        if label[:4] == "Poly": +            return geoms[0][1] +        if label[:2] == "CR": +            if label[-2:] == "Pt": +                return geoms[1][2] +            if label[3:5] == "Pt": +                return geoms[0][2] +            return geoms[1][1] +        if label[5:10] == "Point": +            return geoms[2][2] +        # label has shape Find Polygon from CR xxx xxx +        if label[-2:] == "Pt": +            return geoms[1][2] +        if label[21:23] == "Pt": +            return geoms[0][2] +        return geoms[2][1]  class NewItems(TestCase):      fixtures = COMMON_FIXTURES @@ -3251,38 +3395,7 @@ class SeleniumTests(StaticLiveServerTestCase):          self.selenium.find_element_by_xpath('//button[@type="submit"][@class="btn btn-primary"]').click()      def setUpDefaultGeoItems(self): -        from archaeological_operations.tests import create_operation -        from archaeological_finds.tests import GeomaticTest as GeomaticFindsTest - -        ope1 = create_operation(self.user) -        ope2 = create_operation(self.user) -        pks = {'Pt': ope1.pk, 'Poly': ope2.pk} - -        pks.update(GeomaticFindsTest.create_cr_with_bfs(ope1, "Point", "Point", ["Polygon", "Point"], "CR Pt Pt")) -        pks.update(GeomaticFindsTest.create_cr_with_bfs(ope1, "Point", "Polygon", ["Polygon", "Point"], "CR Pt Poly")) -        pks.update(GeomaticFindsTest.create_cr_with_bfs(ope2, "Polygon", "Point", ["Polygon", "Point"], "CR Poly Pt")) -        pks.update(GeomaticFindsTest.create_cr_with_bfs(ope2, "Polygon", "Polygon", ["Polygon", "Point"], "CR Poly Poly")) - -        geom_ope = [ -            [[[[1, 1], [5, 1], [5, 5], [1, 5], [1, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], -             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], -            [3.86111, 3.02778], -            [3.8, 3] -        ] -        geom_cr = [ -            [[[[2, 1], [5, 1], [5, 5], [1, 5], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], -             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], -            [4.17708, 3.11458], -            [3.9, 3] -        ] -        geom_bf = [ -            [[[[2, 1], [4, 1], [4, 4], [1, 4], [2, 1]], [[2, 2], [2, 3], [3, 3], [3, 2], [2, 2]]], -             [[[6, 3], [9, 2], [9, 4], [6, 3]]]], -            [4.42105, 2.73684], -            [3.9, 2.9] -        ] -        return [geom_ope, geom_cr, geom_bf], pks - +        return GeomaticTest.setUpDefaultGeoItems(self.user)      def scroll(self, element):          self.selenium.execute_script( | 
