Skip to content

Commit 8055921

Browse files
committed
Add markdown version of notebook
1 parent 7e21877 commit 8055921

File tree

2 files changed

+160
-4
lines changed

2 files changed

+160
-4
lines changed

source-code/gis/city_center.ipynb

Lines changed: 51 additions & 4 deletions
Large diffs are not rendered by default.

source-code/gis/city_center.md

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
---
2+
jupyter:
3+
jupytext:
4+
formats: ipynb,md
5+
text_representation:
6+
extension: .md
7+
format_name: markdown
8+
format_version: '1.2'
9+
jupytext_version: 1.5.2
10+
kernelspec:
11+
display_name: Python 3
12+
language: python
13+
name: python3
14+
---
15+
16+
# City centers
17+
18+
19+
## Reading files
20+
21+
22+
Load the required modules
23+
24+
```python
25+
import geopandas as gpd
26+
%matplotlib inline
27+
```
28+
29+
Load a GeoJSON file containing GeoJSON file.
30+
31+
```python
32+
belgium = gpd.read_file('https://gist.githubusercontent.com/jandot/ba7eff2e15a38c6f809ba5e8bd8b6977/raw/eb49ce8dd2604e558e10e15d9a3806f114744e80/belgium_municipalities_topojson.json')
33+
```
34+
35+
Get info on the dataframe.
36+
37+
```python
38+
belgium.info()
39+
```
40+
41+
Set the municipaolities `CODE_INS` as the index for the dataframe.
42+
43+
```python
44+
belgium.set_index('CODE_INS', inplace=True)
45+
```
46+
47+
Find the entry for Hasselt.
48+
49+
```python
50+
belgium.query('ADMUNADU == "HASSELT"')
51+
```
52+
53+
Get the geomerty for Belgium. It is returned as a Shapely object, a `Polygon` in this case.
54+
55+
```python
56+
hasselt_shape = belgium.at['71022', 'geometry']
57+
```
58+
59+
```python
60+
hasselt_shape
61+
```
62+
63+
## Centroids
64+
65+
66+
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.
67+
68+
```python
69+
hasselt_shape.centroid
70+
```
71+
72+
```python
73+
belgium['center'] = belgium.geometry.centroid
74+
```
75+
76+
```python
77+
belgium
78+
```
79+
80+
Write the coordinates to a JSON file.
81+
82+
```python
83+
with open('Data/city_centers.json', 'w') as file:
84+
print(gpd.GeoSeries(belgium.center).to_json(), file=file)
85+
```
86+
87+
Plot the centers on top of the map of Belgium.
88+
89+
```python
90+
centers = gpd.GeoDataFrame(geometry=belgium.center)
91+
```
92+
93+
```python
94+
axes = centers.plot(markersize=1, color='red', figsize=(12, 15))
95+
_ = belgium.geometry.plot(ax=axes, color='white', edgecolor='black', alpha=0.5)
96+
```
97+
98+
```python
99+
centers['longitude'] = centers.geometry.x
100+
centers['latitude'] = centers.geometry.y
101+
```
102+
103+
```python
104+
centers.info()
105+
```
106+
107+
```python
108+
centers[['longitude', 'latitude']].to_csv('Data/city_centers.csv')
109+
```

0 commit comments

Comments
 (0)