Skip to content

Latest commit

 

History

History
109 lines (78 loc) · 2.05 KB

File metadata and controls

109 lines (78 loc) · 2.05 KB
jupyter
jupytext kernelspec
formats text_representation
ipynb,md
extension format_name format_version jupytext_version
.md
markdown
1.2
1.5.2
display_name language name
Python 3
python
python3

City centers

Reading files

Load the required modules

import geopandas as gpd
%matplotlib inline

Load 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_shape

Centroids

Although 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.centroid
belgium['center'] = belgium.geometry.centroid
belgium

Write 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.y
centers.info()
centers[['longitude', 'latitude']].to_csv('Data/city_centers.csv')