Skip to content

Commit

Permalink
Add a function to consolidate nysdp features
Browse files Browse the repository at this point in the history
  • Loading branch information
annehaley committed Aug 2, 2024
1 parent 48e0a22 commit d499531
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 14 deletions.
2 changes: 2 additions & 0 deletions sample_data/use_cases/new_york_energy/contexts.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
],
"default_map_zoom": 8,
"datasets": [
"National Grid CompanyBoundary",
"National Grid Network",
"National Grid Substations",
"National Grid DistAssetsOverview",
"National Grid Electrification_Data",
Expand Down
23 changes: 10 additions & 13 deletions sample_data/use_cases/new_york_energy/datasets.json
Original file line number Diff line number Diff line change
@@ -1,22 +1,12 @@
[
{
"name": "National Grid Substations",
"description": "Substation vector data",
"name": "National Grid CompanyBoundary",
"description": "CompanyBoundary vector data",
"category": "energy",
"module": "nysdp",
"function": "create_vector_features",
"kwargs": {
"service_name": "Substations"
}
},
{
"name": "National Grid DistAssetsOverview",
"description": "DistAssetsOverview vector data",
"category": "energy",
"module": "nysdp",
"function": "create_vector_features",
"kwargs": {
"service_name": "DistAssetsOverview"
"service_name": "CompanyBoundary"
}
},
{
Expand Down Expand Up @@ -68,5 +58,12 @@
"kwargs": {
"service_name": "NY_SubT_SDP"
}
},
{
"name": "National Grid Network",
"description": "Consolidated network from several services' combined data",
"category": "energy",
"module": "nysdp",
"function": "create_consolidated_network"
}
]
54 changes: 53 additions & 1 deletion sample_data/use_cases/new_york_energy/nysdp.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import requests

from django.contrib.gis.geos import GEOSGeometry
from uvdat.core.models import VectorMapLayer, VectorFeature
from uvdat.core.models import Dataset, VectorMapLayer, VectorFeature


NYDSP_URL = 'https://systemdataportal.nationalgrid.com/arcgis/rest/services/NYSDP'
Expand Down Expand Up @@ -53,3 +53,55 @@ def create_vector_features(dataset, service_name=None):
)
)
VectorFeature.objects.bulk_create(vector_features)


def create_consolidated_network(dataset):
VectorMapLayer.objects.filter(dataset=dataset).delete()
map_layer = VectorMapLayer.objects.create(dataset=dataset, index=0)
include_services = [
"Substations",
"DistAssetsOverview",
"Electrification_Data",
"EV_Load_Serving_Capacity",
"Hosting_Capacity_Data",
"LSRV",
"NY_SubT_SDP"
]

features = []
for service_name in include_services:
try:
dataset = Dataset.objects.get(name=f'National Grid {service_name}')
print('\t', f'Using saved vector features for {service_name}...')
features += [
dict(
type='Feature',
geometry=json.loads(feature.geometry.json),
properties=feature.properties
)
for feature in VectorFeature.objects.filter(map_layer__dataset=dataset)
]
except Dataset.DoesNotExist:
print('\t', f'Querying {service_name}...')
feature_sets = fetch_vector_features(service_name=service_name)
for layer_id, feature_set in feature_sets.items():
features += feature_set
print('\t', len(features), 'features found. Combining like geometries...')

# Use geopandas to merge properties of duplicate features
gdf = geopandas.GeoDataFrame.from_features(features)
gdf["geometry"] = gdf.normalize()

grouped = gdf.groupby(gdf['geometry'].to_wkt()).first()
intersects = grouped.sjoin(grouped, how='left', predicate='intersects')
print('\t', len(grouped), 'consolidated features found. Saving to database...')

geojson = json.loads(grouped.to_json())
VectorFeature.objects.bulk_create([
VectorFeature(
map_layer=map_layer,
geometry=GEOSGeometry(json.dumps(feature['geometry'])),
properties=feature['properties'],
)
for feature in geojson.get('features')
])

0 comments on commit d499531

Please sign in to comment.