Skip to content

Commit

Permalink
adds resource geoutils #11507
Browse files Browse the repository at this point in the history
  • Loading branch information
chrabyrd committed Sep 27, 2024
1 parent 85e7606 commit 15ad41c
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 1 deletion.
1 change: 1 addition & 0 deletions arches/app/src/arches/declarations.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ declare module "arches";
// declare untyped modules that have been added to your project in `package.json`
declare module "@babel/runtime";
declare module "@mapbox/geojsonhint";
declare module "@mapbox/mapbox-gl-draw";
declare module "cross-env";
declare module "cytoscape-cola";
declare module "font-awesome";
Expand Down
97 changes: 96 additions & 1 deletion arches/app/utils/geo_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
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,
MultiPoint,
MultiLineString,
MultiPolygon,
)
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 +102,90 @@ 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 with and returns a dictionary of resource instances that intersect the geometries of it, grouped by graph.
"""
points = []
lines = []
polygons = []

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

if geom.geom_type == "Point":
points.append(geom)
elif geom.geom_type == "LineString":
lines.append(geom)
elif geom.geom_type == "Polygon":
polygons.append(geom)

combined_points = MultiPoint(points) if points else None
combined_lines = MultiLineString(lines) if lines else None
combined_polygons = MultiPolygon(polygons) if polygons else None

combined_geometries = [
geometry
for geometry in [combined_points, combined_lines, combined_polygons]
if geometry is not None
]

if len(combined_geometries) == 1:
combined_geometry = combined_geometries[0]
else:
combined_geometry = GeometryCollection(combined_geometries)

with connection.cursor() as cursor:
cursor.execute(
"""
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(
geom,
ST_Transform(
ST_SetSRID(ST_GeomFromGeoJSON(%s), 4326),
3857
)
)
AND resource_instances.graphid != %s
GROUP BY resource_instances.graphid;
""",
[combined_geometry.geojson, 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",
"properties": feature.get("properties", {}),
"geometry": json.loads(buffered_geom.geojson),
}
buffered_features.append(buffered_feature)

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

0 comments on commit 15ad41c

Please sign in to comment.