diff options
author | QuentinAndre <quentin.andre@imt-atlantique.net> | 2021-07-16 10:25:23 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-10-25 12:06:02 +0200 |
commit | c31d1773e5fe60baf3108aba736cd6538a8e4e73 (patch) | |
tree | 2e8805dc8faed655e4bc0dceec24cfb49599ff26 | |
parent | b7413fe72698e37d281b3507edcb203191ef8f1e (diff) | |
download | Ishtar-c31d1773e5fe60baf3108aba736cd6538a8e4e73.tar.bz2 Ishtar-c31d1773e5fe60baf3108aba736cd6538a8e4e73.zip |
format GeoJSON and getting properly related objects
-rw-r--r-- | archaeological_context_records/models.py | 15 | ||||
-rw-r--r-- | archaeological_finds/tests.py | 160 | ||||
-rw-r--r-- | archaeological_operations/models.py | 19 | ||||
-rw-r--r-- | ishtar_common/models_common.py | 9 |
4 files changed, 137 insertions, 66 deletions
diff --git a/archaeological_context_records/models.py b/archaeological_context_records/models.py index 01eb6e27b..50952d3a9 100644 --- a/archaeological_context_records/models.py +++ b/archaeological_context_records/models.py @@ -860,17 +860,18 @@ class ContextRecord( def get_geo_items(self, get_polygons, rounded=True): dict = super(ContextRecord, self).get_geo_items(get_polygons, rounded) - associated_geo_items = {"associated base finds": {}} BaseFind = apps.get_model("archaeological_finds", "BaseFind") - for find_label in self._get_associated_cached_labels(): + collection_base_finds = [] + for bf in self.base_finds.distinct().all(): try: - bf = BaseFind.objects.get(label=find_label) - associated_geo_items["associated base finds"][ - str(find_label) - ] = bf.get_geo_items(get_polygons, rounded) + geo_item = bf.get_geo_items(get_polygons, rounded) + collection_base_finds.append(geo_item) except BaseFind.DoesNotExist: pass - dict["properties"] = associated_geo_items + dict["properties"]["base-finds"] = { + "type": "FeatureCollection", + "features": collection_base_finds, + } return dict @classmethod diff --git a/archaeological_finds/tests.py b/archaeological_finds/tests.py index ddb362fd4..34d7e1b91 100644 --- a/archaeological_finds/tests.py +++ b/archaeological_finds/tests.py @@ -2592,6 +2592,7 @@ class GeomaticTest(FindInit, TestCase): 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) @@ -2613,7 +2614,8 @@ class GeomaticTest(FindInit, TestCase): dic_pt_f_copy = copy.deepcopy(dic_pt_f) dic_pt_pt_f = copy.deepcopy(dic_pt_f) dic_pt_pt_f["properties"] = { - "associated base finds": {"Find 1 from CR Pt Pt": dic_pt_f_copy} + "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)) @@ -2622,7 +2624,11 @@ class GeomaticTest(FindInit, TestCase): "type": "Feature", "geometry": {"type": "MultiPolygon", "coordinates": []}, "properties": { - "associated base finds": {"Find 1 from CR Pt Pt": dic_pt_t_copy} + "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)) @@ -2673,22 +2679,35 @@ class GeomaticTest(FindInit, TestCase): [[[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"] = { - "associated base finds": {"Find 1 from CR Poly Pt": dic_pt_t} + "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_copy = copy.deepcopy(dic_pt_f) + 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"] = { - "associated base finds": {"Find 1 from CR Poly Pt": dic_pt_f_copy} + "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)) @@ -2727,10 +2746,15 @@ class GeomaticTest(FindInit, TestCase): ) 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, res_poly) + 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, res_centroid) + self.assertEqual(dic_poly_f_from_poly, res_centroid) # test API with polygon response = self.client.get(reverse("api-get-geo-polygons", kwargs={"pk": 0})) @@ -2742,7 +2766,7 @@ class GeomaticTest(FindInit, TestCase): ) ) self.assertEqual(response.status_code, 200) - self.assertIn(json.dumps(dic_poly_t).encode("utf-8"), response.content) + self.assertEqual(dic_poly_t_from_poly, json.loads(response.content)) response = self.client.get( reverse( "api-get-geo-point", @@ -2750,32 +2774,47 @@ class GeomaticTest(FindInit, TestCase): ) ) self.assertEqual(response.status_code, 200) - self.assertIn(json.dumps(dic_poly_f).encode("utf-8"), response.content) + self.assertEqual(dic_poly_f_from_poly, json.loads(response.content)) # 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_copy = copy.deepcopy(dic_poly_t) + 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"] = { - "associated base finds": { - "Find 1 from CR Poly Poly Pt": dic_poly_t_copy, - "Find 2 from CR Poly Poly Pt": dic_pt_t_copy, - } + "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_f_copy = copy.deepcopy(dic_poly_f) dic_poly_poly_and_pt_f = copy.deepcopy(dic_poly_f) dic_poly_poly_and_pt_f["properties"] = { - "associated base finds": { - "Find 1 from CR Poly Poly Pt": dic_poly_f_copy, - "Find 2 from CR Poly Poly Pt": dic_pt_f_copy, - } + "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) @@ -2810,27 +2849,44 @@ class GeomaticTest(FindInit, TestCase): 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 point and base find 2 a polygon + # context record is a point, base find 1 a polygon and base find 2 a point cr_pt_poly_and_pt, bfs = create_cr_with_bfs( ope, "Point", ["Polygon", "Point"], "CR Pt Poly Pt" ) 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"] = { - "associated base finds": { - "Find 1 from CR Pt Poly Pt": dic_poly_t_copy, - "Find 2 from CR Pt Poly Pt": dic_pt_t_copy, - } + "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"] = { - "associated base finds": { - "Find 1 from CR Pt Poly Pt": dic_poly_f_copy, - "Find 2 from CR Pt Poly Pt": dic_pt_f_copy, - } + "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)) @@ -2867,31 +2923,47 @@ class GeomaticTest(FindInit, TestCase): dic_pt_pt_and_poly_pt_and_poly_f = copy.deepcopy(dic_pt_f) dic_pt_pt_and_poly_pt_and_poly_f["properties"] = { - "associated context records": { - "CR Pt Poly Pt": dic_pt_poly_and_pt_f, - "CR Poly Poly Pt": dic_poly_poly_and_pt_f, - } + "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"] = { - "associated context records": { - "CR Pt Poly Pt": dic_pt_poly_and_pt_t, - "CR Poly Poly Pt": dic_poly_poly_and_pt_t, - } + "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_f = copy.deepcopy(dic_poly_f) dic_poly_pt_and_poly_pt_and_poly_f["properties"] = { - "associated context records": { - "CR Pt Poly Pt": dic_pt_poly_and_pt_f, - "CR Poly Poly Pt": dic_poly_poly_and_pt_f, - } + "context-records": { + "type": "FeatureCollection", + "features": [ + dic_poly_poly_and_pt_f, + dic_pt_poly_and_pt_f, + ], + }, + "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"] = { - "associated context records": { - "CR Pt Poly Pt": dic_pt_poly_and_pt_t, - "CR Poly Poly Pt": dic_poly_poly_and_pt_t, - } + "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) diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 29b3eac91..277ca76d3 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -1607,23 +1607,20 @@ class Operation( def get_geo_items(self, get_polygons, rounded=True): dict = super(Operation, self).get_geo_items(get_polygons, rounded) - associated_geo_items = {"associated context records": {}} ContextRecord = apps.get_model( "archaeological_context_records", "ContextRecord" ) - for ( - cr - ) in ( - self._get_associated_cached_labels() - ): # malgré le nom, ce ne sont pas des labels ? + collection_context_records = [] + for cr in self.context_record.distinct().all(): try: - associated_geo_items["associated context records"][ - str(cr.label) - ] = cr.get_geo_items(get_polygons, rounded) + geo_item = cr.get_geo_items(get_polygons, rounded) + collection_context_records.append(geo_item) except ContextRecord.DoesNotExist: - print("except") pass - dict["properties"] = associated_geo_items + dict["properties"]["context-records"] = { + "type": "FeatureCollection", + "features": collection_context_records, + } return dict def context_record_relations_q(self): diff --git a/ishtar_common/models_common.py b/ishtar_common/models_common.py index fcd252577..5d1cd5d90 100644 --- a/ishtar_common/models_common.py +++ b/ishtar_common/models_common.py @@ -2848,10 +2848,11 @@ class GeoItem(models.Model): return self.multi_polygon, self.multi_polygon_source_item def get_geo_items(self, get_polygons, rounded=True): - dict = { - "type": "Feature", - "geometry": {}, - } + try: + label = self.label + except: + label = self.short_label + dict = {"type": "Feature", "geometry": {}, "properties": {"label": label}} if get_polygons: list_coords = [] if self.multi_polygon: |