-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkunnat_bokeh.py
92 lines (78 loc) · 2.89 KB
/
kunnat_bokeh.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# -*- coding: utf-8 -*-
import os
from bokeh.plotting import figure, show
from bokeh.palettes import RdYlGn8 as palette
from bokeh.models import (
HoverTool,
GeoJSONDataSource,
LinearColorMapper,
Title
)
import geopandas as gpd
from shapely.geometry import Polygon, MultiPolygon
palette.reverse()
DATA_DIR = 'data'
def multipolygons_to_polygons(geodataframe, geometry_column='geometry', min_area=0):
"""
Turns rows with MultiPolygons into groups of rows with single Polygon each.
:param geodataframe:
:param geometry_column: Column containing Polygons and Multipolygons.
:param min_area: Size of Polygons to be removed
:return: enlargened geodataframed with each Polygon in its own row
"""
new_geodataframe = gpd.GeoDataFrame(columns=geodataframe.columns)
for _, row in geodataframe.iterrows():
geom = row[geometry_column]
if geom.type == 'Polygon':
new_geodataframe = new_geodataframe.append(row)
elif geom.type == 'MultiPolygon':
for poly in geom:
new_row = row.copy()
new_row[geometry_column] = poly
if poly.area >= min_area:
new_geodataframe = new_geodataframe.append(new_row)
return new_geodataframe.reset_index()
if __name__ == '__main__':
os.chdir(DATA_DIR)
kunnat = gpd.read_file('kuntienAvainluvut_2016.shp')
kunnat.to_crs(epsg=3067)
kunnat = multipolygons_to_polygons(kunnat, min_area=2000000)
kunnat['x'] = kunnat['geometry'].apply(lambda geom: tuple(geom.exterior.coords.xy[0]))
kunnat['y'] = kunnat['geometry'].apply(lambda geom: tuple(geom.exterior.coords.xy[1]))
kunnat['poly_area'] = kunnat.geometry.apply(lambda p: p.area)
# kunnat = kunnat[kunnat['poly_area'] > 2000000]
kunnat.geometry = kunnat.geometry.apply(lambda poly: poly.simplify(1000))
kunnat.columns = [w.replace(r'%', '_pct') for w in kunnat.columns]
kunnat_src = GeoJSONDataSource(geojson=kunnat.to_json())
color_mapper = LinearColorMapper(palette=palette)
fig = figure(
title='Muuttovoitto ja muuttotappio 2016',
x_axis_location=None,
y_axis_location=None,
plot_width=600,
plot_height=950
)
fig.title.text_font_size = "20px"
fig.grid.grid_line_color = None
fig.add_layout(Title(text="© Tilastokeskus, Kuntien avainluvut, 2016", align="left"), "below")
fig.patches(
xs='x',
ys='y',
source=kunnat_src,
fill_color={
'field': 'PopChange',
'transform': color_mapper,
},
fill_alpha=0.9,
line_color='black',
line_width=0.8
)
hover = HoverTool()
hover.tooltips = [
('Kunta', '@NIMI'),
('Väkiluku', '@Population'),
('Työttömyysaste', '@Tyottom_pct{0.0 a}'),
('Muuttovoitto tai -tappio', '@PopChange{0.0 a}'),
]
fig.add_tools(hover)
show(fig)