Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds resource geoutils #11507 #11508

Closed
wants to merge 7 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 145 additions & 1 deletion arches/app/utils/geo_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import json
import uuid
from arcgis2geojson import arcgis2geojson
from django.contrib.gis.geos import GEOSGeometry, GeometryCollection, WKTWriter
from django.contrib.gis.geos import (
GEOSGeometry,
GeometryCollection,
)
from django.db import connection
from arches.app.models.system_settings import settings
from arches.app.utils.betterJSONSerializer import JSONSerializer, JSONDeserializer


Expand Down Expand Up @@ -94,3 +99,142 @@ def convert_geos_geom_collection_to_feature_collection(self, geometry):
arches_json_geometry["properties"] = {}
arches_geojson["features"].append(arches_json_geometry)
return arches_geojson

def get_resource_instances_within_feature_collection(self, feature_collection):
"""
Takes a FeatureCollection object and returns a dictionary of resource instances
that intersect the geometries of it, grouped by graph.
"""
with connection.cursor() as cursor:
cursor.execute(
"""
WITH combined_geom AS (
SELECT ST_Union(
ST_Transform(
ST_SetSRID(ST_GeomFromGeoJSON(feature->>'geometry'), 4326),
3857
)
) AS geom
FROM jsonb_array_elements(%s::jsonb->'features') AS feature
)
SELECT resource_instances.graphid,
array_agg(geojson_geometries.resourceinstanceid) AS resourceinstanceids
FROM geojson_geometries
JOIN resource_instances
ON geojson_geometries.resourceinstanceid = resource_instances.resourceinstanceid
WHERE ST_Intersects(
geojson_geometries.geom,
(SELECT geom FROM combined_geom)
)
AND resource_instances.graphid != %s
GROUP BY resource_instances.graphid;
""",
[
json.dumps(feature_collection),
settings.SYSTEM_SETTINGS_RESOURCE_MODEL_ID,
],
)

results = cursor.fetchall()

return dict(results)

def buffer_feature_collection(self, feature_collection, buffer_distance):
"""
Takes a FeatureCollection object and a value for the buffer distance in meters,
and returns a FeatureCollection of the original features with the buffer distance added.

"""

buffered_features = []

for feature in feature_collection["features"]:
geom = GEOSGeometry(json.dumps(feature["geometry"]))

geom.transform(settings.ANALYSIS_COORDINATE_SYSTEM_SRID)
buffered_geom = geom.buffer(buffer_distance)
buffered_geom.transform(4326)

buffered_feature = {
"type": "Feature",
"id": str(uuid.uuid4()),
"properties": feature.get("properties", {}),
"geometry": json.loads(buffered_geom.geojson),
}
buffered_features.append(buffered_feature)

return {
"type": "FeatureCollection",
"features": buffered_features,
}

def get_intersection_and_difference_of_feature_collections(
self, feature_collection_1, feature_collection_2
):
def _build_feature_collection(geometries):
chrabyrd marked this conversation as resolved.
Show resolved Hide resolved
features = []

if geometries is None or geometries.empty:
return {"type": "FeatureCollection", "features": []}

if geometries.geom_type in [
"GeometryCollection",
"MultiPolygon",
"MultiLineString",
"MultiPoint",
]:
for geometry in geometries:
if not geometry.empty:
features.append(
{
"type": "Feature",
"id": str(uuid.uuid4()),
"properties": {},
"geometry": json.loads(geometry.geojson),
}
)
else:
features.append(
{
"type": "Feature",
"id": str(uuid.uuid4()),
"properties": {},
"geometry": json.loads(geometries.geojson),
}
)

return {"type": "FeatureCollection", "features": features}

feature_collection_1_geometries = [
GEOSGeometry(json.dumps(feature["geometry"]))
for feature in feature_collection_1["features"]
]
feature_collection_1_geometries_union = GeometryCollection(
*feature_collection_1_geometries
).unary_union

feature_collection_2_geometries = [
GEOSGeometry(json.dumps(feature["geometry"]))
for feature in feature_collection_2["features"]
]
feature_collection_2_geometries_union = GeometryCollection(
*feature_collection_2_geometries
).unary_union

return {
chrabyrd marked this conversation as resolved.
Show resolved Hide resolved
"difference_derived_from_first_collection": _build_feature_collection(
feature_collection_1_geometries_union.difference(
feature_collection_2_geometries_union
)
),
"difference_derived_from_second_collection": _build_feature_collection(
feature_collection_2_geometries_union.difference(
feature_collection_1_geometries_union
)
),
"intersection_derived_from_both_collections": _build_feature_collection(
feature_collection_1_geometries_union.intersection(
feature_collection_2_geometries_union
)
),
}
Loading