Skip to content

Commit

Permalink
Add path stats as text to final path coord
Browse files Browse the repository at this point in the history
  • Loading branch information
aliaksei135 committed Mar 1, 2021
1 parent f6e4761 commit e8c2574
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions seedpod_ground_risk/layers/geojson_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import geopandas as gpd
import numpy as np
from holoviews.element import Geometry, Overlay
from holoviews.element import Overlay

from seedpod_ground_risk.layers.annotation_layer import AnnotationLayer

Expand All @@ -26,12 +26,15 @@ def preload_data(self) -> NoReturn:
self.buffer_poly = gpd.GeoDataFrame(
{'geometry': epsg27700_geom.buffer(self.buffer_dist).to_crs('EPSG:4326')}
)
self.endpoint = self.dataframe.iloc[0].geometry.coords[-1]

def annotate(self, data: List[gpd.GeoDataFrame], raster_data: Tuple[Dict[str, np.array], np.array],
**kwargs) -> Geometry:
**kwargs) -> Overlay:
import geoviews as gv

if self.buffer_poly is not None:
label_str = ''

annotation_layers = []
for gdf in data:
if not gdf.crs:
Expand All @@ -40,24 +43,28 @@ def annotate(self, data: List[gpd.GeoDataFrame], raster_data: Tuple[Dict[str, np
overlay = gpd.overlay(gdf, self.buffer_poly, how='intersection')

geom_type = overlay.geometry.geom_type.all()
if geom_type == 'Polygon':
if geom_type == 'Polygon' or geom_type == 'MultiPolygon':
if 'density' not in gdf:
continue
proj_gdf = overlay.to_crs('epsg:27700')
proj_gdf['population'] = proj_gdf.geometry.area * proj_gdf.density
print("Geometry Swept Population: ", proj_gdf['population'].sum())
label_str += f"Static Population: {proj_gdf['population'].sum():.2f}"
annotation_layers.append(gv.Polygons(overlay).opts(style={'alpha': 0.8, 'color': 'cyan'}))
elif geom_type == 'Point':
if 'population' in overlay:
mean_pop = overlay.population.mean()
print("Points mean ", mean_pop)
label_str += f'Dynamic Population: {mean_pop / 60:.2f}/min'
annotation_layers.append((gv.Points(overlay).opts(style={'alpha': 0.8, 'color': 'cyan'})))
elif geom_type == 'Line':
pass

label_str += '\n'

return Overlay([
gv.Contours(self.dataframe).opts(line_width=4, line_color='magenta'),
gv.Polygons(self.buffer_poly).opts(style={'alpha': 0.3, 'color': 'cyan'}),
# Add the path stats as a text annotation to the final path point
gv.Text(self.endpoint[0], self.endpoint[1], label_str),
*annotation_layers
])
else:
Expand Down

0 comments on commit e8c2574

Please sign in to comment.