Commit 0b275567 authored by Sarah Abrishami's avatar Sarah Abrishami

debugged center, added visualization file

parent 4eedffb2
import json import json
import geopandas as gpd import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon, Point from shapely.geometry import Polygon, MultiPolygon, Point
import fiona
def find_center(mlp): def find_center(mlp, how='json'):
if isinstance(mlp, MultiPolygon): if isinstance(mlp, MultiPolygon):
area = 0 area = 0
for p in mlp.geoms: for p in mlp.geoms:
if p.area > area: if p.area > area:
center = json.dumps([p.centroid.x, p.centroid.y]) area = p.area
center = [p.centroid.x, p.centroid.y]
elif isinstance(mlp, Polygon): elif isinstance(mlp, Polygon):
center = json.dumps([mlp.centroid.x, mlp.centroid.y]) center = [mlp.centroid.x, mlp.centroid.y]
else: else:
raise TypeError('object not polygon nor multipolygon') raise TypeError('object not polygon nor multipolygon')
if how == 'json':
center = json.dumps(center)
elif how == 'point':
center = Point(center)
return center return center
df = gpd.read_file('Iran-Districts-Test.json') if __name__ == '__main__':
df = df.loc[df['geometry'].notna()] df = gpd.read_file('Iran-Districts-Test.json')
gdf = gpd.GeoDataFrame(columns=['geometry', 'clmd_id', 'center']) df = df.loc[df['geometry'].notna()]
for climate in df['clmd_id'].unique(): gdf = gpd.GeoDataFrame(columns=['geometry', 'clmd_id', 'center'])
a = {'geometry': MultiPolygon(), 'clmd_id': climate} for climate in df['clmd_id'].unique():
df.loc[df['clmd_id'] == climate, 'geometry'].apply(lambda x: a.update({'geometry': a['geometry'].union(x)})) a = {'geometry': MultiPolygon(), 'clmd_id': climate}
gdf = gdf.append(a, ignore_index=True) df.loc[df['clmd_id'] == climate, 'geometry'].apply(lambda x: a.update({'geometry': a['geometry'].union(x)}))
gdf = gdf.append(a, ignore_index=True)
gdf['center'] = gdf['geometry'].apply(find_center) gdf['center'] = gdf['geometry'].apply(find_center)
gdf.to_file('output.json', driver='GeoJSON') gdf.to_file('output.json', driver='GeoJSON')
This diff is collapsed.
import plotly.express as px
import plotly.graph_objs as go
import geopandas as gpd
from create_multi_polygon import find_center
dist = gpd.read_file('output.json')
dist['center'] = dist['geometry'].apply(lambda x: find_center(x, 'point'))
dist['lat'] = dist['center'].y
dist['lon'] = dist['center'].x
dist.append(gpd.GeoDataFrame())
counties = {'type': 'FeatureCollection', 'features': list(dist[['clmd_id', 'geometry']].iterfeatures())}
fig = go.Figure(go.Choroplethmapbox(geojson=counties, locations=dist.reset_index()['index'].values,
hovertext=dist['clmd_id'].values,
z=dist['clmd_id'].values, colorscale="Cividis_r", zmin=0))
fig.add_trace(
px.scatter_mapbox(dist, lat='lat', lon='lon', hover_name='clmd_id', color_discrete_sequence=["blue"]).data[0])
fig.update_layout(mapbox_style="carto-positron", margin={"r": 0, "t": 0, "l": 0, "b": 0},
mapbox_zoom=5, mapbox_center={"lat": 35.7, "lon": 51.4})
fig.show()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment