| jupyter |
|
|---|
Load the required modules
import geopandas as gpd
%matplotlib inlineLoad a GeoJSON file containing GeoJSON file.
belgium = gpd.read_file('https://gist.githubusercontent.com/jandot/ba7eff2e15a38c6f809ba5e8bd8b6977/raw/eb49ce8dd2604e558e10e15d9a3806f114744e80/belgium_municipalities_topojson.json')Get info on the dataframe.
belgium.info()Set the municipaolities CODE_INS as the index for the dataframe.
belgium.set_index('CODE_INS', inplace=True)Find the entry for Hasselt.
belgium.query('ADMUNADU == "HASSELT"')Get the geomerty for Belgium. It is returned as a Shapely object, a Polygon in this case.
hasselt_shape = belgium.at['71022', 'geometry']hasselt_shapeAlthough it is possible to obtain the coordinates of city centers online, this data is not freely available. We can approximate the location by computing the centroid of the geometric shape associdatted with the municipality.
hasselt_shape.centroidbelgium['center'] = belgium.geometry.centroidbelgiumWrite the coordinates to a JSON file.
with open('Data/city_centers.json', 'w') as file:
print(gpd.GeoSeries(belgium.center).to_json(), file=file)Plot the centers on top of the map of Belgium.
centers = gpd.GeoDataFrame(geometry=belgium.center)axes = centers.plot(markersize=1, color='red', figsize=(12, 15))
_ = belgium.geometry.plot(ax=axes, color='white', edgecolor='black', alpha=0.5)centers['longitude'] = centers.geometry.x
centers['latitude'] = centers.geometry.ycenters.info()centers[['longitude', 'latitude']].to_csv('Data/city_centers.csv')