summaryrefslogtreecommitdiff
path: root/ishtar_common/tests.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/tests.py')
-rw-r--r--ishtar_common/tests.py483
1 files changed, 0 insertions, 483 deletions
diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py
index dfae6c164..25d4d8e85 100644
--- a/ishtar_common/tests.py
+++ b/ishtar_common/tests.py
@@ -2839,489 +2839,6 @@ class IshtarBasicTest(TestCase):
self.assertEqual(town.cached_label, "Sin City - 99 (2050)")
-class GeomaticTest(TestCase):
- fixtures = FIND_FIXTURES
-
- def setUp(self):
- profile = models.get_current_profile()
- profile.mapping = True
- profile.save()
-
- def test_post_save_point(self):
- class FakeGeomaticObject(object):
- _meta = models.GeoItem._meta
-
- def __init__(
- self, x, y, z, spatial_reference_system, point=None, point_2d=None
- ):
- self.x = x
- self.y = y
- self.z = z
- self.spatial_reference_system = spatial_reference_system
- self.point_source = "P"
- self.point_source_item = ""
- self.point = point
- self.point_2d = point_2d
- self.pk = 42
-
- def save(self, *args, **kwargs):
- pass
-
- srs, __ = models.SpatialReferenceSystem.objects.get_or_create(
- srid=4326, defaults={"label": "WGS84", "txt_idx": "wgs84"}
- )
- obj = FakeGeomaticObject(x=2, y=3, z=4, spatial_reference_system=srs)
- self.assertIsNone(obj.point_2d)
- post_save_geo(FakeGeomaticObject, instance=obj)
- 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):
- Operation = apps.get_model("archaeological_operations", "Operation")
- ContextRecord = apps.get_model(
- "archaeological_context_records", "ContextRecord"
- )
- Find = apps.get_model("archaeological_finds", "Find")
- BaseFind = apps.get_model("archaeological_finds", "BaseFind")
-
- wgs84 = models_common.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], True
- if label[:4] == "Poly":
- return geoms[0][1], False
- if label[:2] == "CR":
- if label[-2:] == "Pt":
- return geoms[1][2], True
- if label[3:5] == "Pt":
- return geoms[0][2], True
- return geoms[1][1], False
- if label[5:10] == "Point":
- return geoms[2][2], True
- # label has shape Find Polygon from CR xxx xxx
- if label[-2:] == "Pt":
- return geoms[1][2], True
- if label[21:23] == "Pt":
- return geoms[0][2], True
- return geoms[2][1], False
-
- def assertPt(self, item, geom):
- self.assertEqual(item.multi_polygon.coords, geom[0].coords)
- self.assertEqual(item.point_2d.coords, geom[2].coords)
-
- def assertPoly(self, item, geom, real_point=None):
- self.assertEqual(item.multi_polygon.coords, geom[0].coords)
- if real_point:
- self.assertEqual((round(item.x, 5), round(item.y, 5)), real_point.coords)
- else:
- self.assertEqual((round(item.x, 5), round(item.y, 5)), geom[1].coords)
-
- def test_setUpDefaultGeoItems(self):
- # TODO: remove and clean
- Operation = apps.get_model("archaeological_operations", "Operation")
-
- """
- username, password, user = create_superuser()
- base_geoms, pks = self.setUpDefaultGeoItems(user)
- geoms = []
- for geom in base_geoms:
- poly = GEOSGeometry(
- json.dumps({"type": "MultiPolygon", "coordinates": geom[0]})
- )
- centr = GEOSGeometry(json.dumps({"type": "Point", "coordinates": geom[1]}))
- pt = GEOSGeometry(json.dumps({"type": "Point", "coordinates": geom[2]}))
- geoms.append([poly, centr, pt])
- ope_pt = Operation.objects.get(pk=pks["Pt"])
- self.assertPt(ope_pt, geoms[0])
- ope_poly = Operation.objects.get(pk=pks["Poly"])
- self.assertPoly(ope_poly, geoms[0])
-
- cr_poly_poly = ope_poly.context_record.get(pk=pks["CR Poly Poly"])
- self.assertPoly(cr_poly_poly, geoms[1])
- cr_poly_pt = ope_poly.context_record.get(pk=pks["CR Poly Pt"])
- self.assertPt(cr_poly_pt, geoms[1])
- cr_pt_poly = ope_pt.context_record.get(pk=pks["CR Pt Poly"])
- self.assertPoly(cr_pt_poly, geoms[1], geoms[0][2])
- cr_pt_pt = ope_pt.context_record.get(pk=pks["CR Pt Pt"])
- self.assertPt(cr_pt_pt, geoms[1])
-
- self.assertPt(
- cr_pt_pt.base_finds.get(pk=pks["Find Point from CR Pt Pt"]), geoms[2]
- )
- self.assertPoly(
- cr_pt_pt.base_finds.get(pk=pks["Find Polygon from CR Pt Pt"]),
- geoms[2],
- geoms[1][2],
- )
- self.assertPt(
- cr_poly_pt.base_finds.get(pk=pks["Find Point from CR Poly Pt"]), geoms[2]
- )
- self.assertPoly(
- cr_poly_pt.base_finds.get(pk=pks["Find Polygon from CR Poly Pt"]),
- geoms[2],
- geoms[1][2],
- )
- self.assertPt(
- cr_pt_poly.base_finds.get(pk=pks["Find Point from CR Pt Poly"]), geoms[2]
- )
- self.assertPoly(
- cr_pt_poly.base_finds.get(pk=pks["Find Polygon from CR Pt Poly"]),
- geoms[2],
- geoms[0][2],
- )
- self.assertPt(
- cr_poly_poly.base_finds.get(pk=pks["Find Point from CR Poly Poly"]),
- geoms[2],
- )
- self.assertPoly(
- cr_poly_poly.base_finds.get(pk=pks["Find Polygon from CR Poly Poly"]),
- geoms[2],
- )
- """
-
- @staticmethod
- def geojson_geo_items(geoms, pks, test_get_geo_items=False):
- Operation = apps.get_model("archaeological_operations", "Operation")
- ContextRecord = apps.get_model(
- "archaeological_context_records", "ContextRecord"
- )
- BaseFind = apps.get_model("archaeological_finds", "BaseFind")
-
- cache_dics_t, cache_dics_f = {}, {}
- res = {}
-
- 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]
- pt_coords, precise = GeomaticTest.pt_coords_from_label(label, geoms)
- 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": pt_coords,
- },
- "properties": {"label": label},
- }
- cache_dics_t[label] = dic_t
- cache_dics_f[label] = dic_f
- if test_get_geo_items:
- 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="
- else:
- get_polys = dic_t
- get_pts = dic_f
-
- 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": pt_coords,
- },
- "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
- if test_get_geo_items:
- 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:
- get_polys = copy.deepcopy(dic_t)
- get_polys["properties"]["base-finds"] = [
- cache_key("Polygon"),
- cache_key("Point"),
- ]
- get_pts = copy.deepcopy(dic_f)
- get_pts["properties"]["base-finds"] = [
- cache_key("Polygon"),
- cache_key("Point"),
- ]
- 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": pt_coords,
- },
- "properties": {
- "label": real_label,
- "context-records": {
- "type": "FeatureCollection",
- "features": [
- cache_dics_f[cache_key_prefix + "Poly"],
- cache_dics_f[cache_key_prefix + "Pt"],
- ],
- },
- },
- }
- if test_get_geo_items:
- 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="
- else:
- get_polys = copy.deepcopy(dic_t)
- bf_labels = []
- for cr in dic_t["properties"]["context-records"]["features"]:
- for bf in cr["properties"]["base-finds"]["features"]:
- bf_labels.append(bf["properties"]["label"])
- get_polys["properties"] = {
- "context-records": [
- cache_key_prefix + "Poly",
- cache_key_prefix + "Pt",
- ],
- "base-finds": bf_labels,
- }
- get_pts = copy.deepcopy(dic_f)
- bf_labels = []
- for cr in dic_f["properties"]["context-records"]["features"]:
- for bf in cr["properties"]["base-finds"]["features"]:
- bf_labels.append(bf["properties"]["label"])
- get_pts["properties"] = {
- "context-records": [
- cache_key_prefix + "Poly",
- cache_key_prefix + "Pt",
- ],
- "base-finds": bf_labels,
- }
-
- if test_get_geo_items:
- res[label] = {
- "get_polys": dic_t,
- "get_pts": dic_f,
- "res_polys": res_t,
- "res_pts": res_f,
- "get_pk": get_pk,
- }
- else:
- res[label] = {
- "get_polys": get_polys,
- "get_pts": get_pts,
- "precise": precise,
- }
- return res
-
-
class NewItems(TestCase):
fixtures = COMMON_FIXTURES