summaryrefslogtreecommitdiff
path: root/archaeological_operations/models.py
diff options
context:
space:
mode:
authorÉtienne Loks <etienne.loks@iggdrasil.net>2022-09-07 15:47:54 +0200
committerÉtienne Loks <etienne.loks@iggdrasil.net>2022-12-12 12:23:18 +0100
commit392a3508b2a5bfe266844b5be4ab58a01caff766 (patch)
tree8695edc626cdb32dacc5048730c08caa39266941 /archaeological_operations/models.py
parent4798e764adf21e259e6738eaaf18c0fa7ae18bac (diff)
downloadIshtar-392a3508b2a5bfe266844b5be4ab58a01caff766.tar.bz2
Ishtar-392a3508b2a5bfe266844b5be4ab58a01caff766.zip
Geo sheet: display context records
Diffstat (limited to 'archaeological_operations/models.py')
-rw-r--r--archaeological_operations/models.py95
1 files changed, 78 insertions, 17 deletions
diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py
index 080ddddab..69f8d4fdc 100644
--- a/archaeological_operations/models.py
+++ b/archaeological_operations/models.py
@@ -20,6 +20,8 @@
from collections import OrderedDict
import datetime
from itertools import groupby
+import json
+import re
import uuid
from django.apps import apps
@@ -45,6 +47,7 @@ from ishtar_common.models import (
GeneralRecordRelations,
GeneralRelationType,
GeneralType,
+ GeoVectorData,
IshtarUser,
LightHistorizedItem,
OperationType,
@@ -1726,30 +1729,88 @@ class Operation(
q = self.towns.filter(limit__isnull=False).annotate(poly=Union("limit")).all()
if not q.count():
return None
- return q.all()[0].poly, self._meta.verbose_name
+ return q.all()[0].poly, self._meta.verbose_namea
+
+ """
+ def _get_child_geodata(self, query, cr_list, collection_context_records_id,
+ current_geodata):
+ childs = []
+ for cr in query.exclude(id__in=cr_list).values(
+ "main_geodata", "id").distinct().all():
+ childs.append(cr["id"])
+ geodata_id = cr["main_geodata"]
+ if geodata_id not in collection_context_records_id \
+ and geodata_id not in current_geodata:
+ collection_context_records_id.append(geodata_id)
+ cr_list += childs
+ return childs
+ """
def get_geo_items(self, rounded=True):
dct = super(Operation, self).get_geo_items(rounded=rounded)
ContextRecord = apps.get_model(
"archaeological_context_records", "ContextRecord"
)
+
+ q = self.context_record.filter(main_geodata__isnull=False)
+
+ """
+ included = q.filter(
+ right_relations__relation_type__txt_idx='is_included')
+ q_upper = q.exclude(id__in=set(included.values_list("id", flat=True)))
+ cr_list = []
+
+ childs = self._get_child_geodata(
+ q_upper, cr_list, collection_context_records_id,
+ current_geodata)
+ while childs:
+ new_childs = []
+ for child_id in childs:
+ q_lower = q.filter(
+ right_relations__relation_type__txt_idx='is_included',
+ right_relations__right_record_id=child_id
+ )
+ new_childs += self._get_child_geodata(
+ q_lower, cr_list, collection_context_records_id,
+ current_geodata)
+ childs = new_childs
+
+
+
+ """
+
+ collection_context_records_id = []
+ current_geodata = self.geodata.values_list("id", flat=True)
+ q = q.values("main_geodata", "id")
+ for cr in q.distinct().all():
+ geodata_id = cr["main_geodata"]
+ if geodata_id not in collection_context_records_id \
+ and geodata_id not in current_geodata:
+ collection_context_records_id.append(geodata_id)
+
collection_context_records = []
- import datetime
- n = datetime.datetime.now()
- n1 = datetime.datetime.now()
- times = []
- print("archaeological_operations.models.py", "get_geo_items", 1741)
- for cr in self.context_record.distinct().all():
- try:
- geo_item = cr.get_geo_items(rounded=rounded)
- collection_context_records.append(geo_item)
- times.append((datetime.datetime.now() - n1).seconds)
- n1 = datetime.datetime.now()
- except ContextRecord.DoesNotExist:
- pass
- print("full", datetime.datetime.now() - n)
- if times:
- print("mean", sum(times)/len(times))
+ for cr_id in collection_context_records_id:
+ geo = json.loads(GeoVectorData.objects.get(pk=cr_id).geojson)
+ geo_type = geo.get("type", None)
+ if geo_type == "FeatureCollection":
+ collection_context_records += geo["features"]
+ elif geo_type:
+ collection_context_records.append(geo)
+
+ profile = get_current_profile()
+ precision = profile.point_precision
+ if not precision and rounded:
+ precision = 5
+ if precision is not None:
+ r = re.compile(r"(\d+)\.(\d{6})(\d*)")
+ for feat in collection_context_records:
+ geom_type = feat["geometry"].get("type", None)
+ if geom_type == "Point":
+ feat["geometry"]["coordinates"] = [
+ round(feat["geometry"]["coordinates"][0], precision),
+ round(feat["geometry"]["coordinates"][1], precision),
+ ]
+
dct["context-records"] = {
"type": "FeatureCollection",
"features": collection_context_records,