Code
import pandas as pd
from matplotlib import pyplot as plt
import geopandas as gpd
import hvplot.pandas
import panel as pn
# Make sure plots show up in JupyterLab!
%matplotlib inline
import pandas as pd
from matplotlib import pyplot as plt
import geopandas as gpd
import hvplot.pandas
import panel as pn
# Make sure plots show up in JupyterLab!
%matplotlib inline
import holoviews as hv
import geoviews as gv
from holoviews.operation.datashader import datashade
'bokeh') hv.extension(
# please try again if run with error for first time
= (
district_url 'https://services3.arcgis.com/6j1KwZfY2fZrfNMR/arcgis/rest/services/Hong_Kong_18_Districts/FeatureServer/0/query?outFields=*&where=1%3D1&f=geojson'
)= gpd.read_file(district_url)
hk_limit hk_limit.head()
OBJECTID | ID | CNAME | CNAME_S | ENAME | Shape__Area | Shape__Length | geometry | |
---|---|---|---|---|---|---|---|---|
0 | 1 | 1 | 黃大仙區 | 黄大仙区 | WONG TAI SIN | 1.092784e+07 | 17995.640782 | POLYGON ((114.17942 22.34905, 114.17946 22.349... |
1 | 2 | 6 | 九龍城區 | 九龙城区 | KOWLOON CITY | 1.184286e+07 | 31834.409404 | MULTIPOLYGON (((114.17700 22.34904, 114.17702 ... |
2 | 3 | 7 | 觀塘區 | 观塘区 | KWUN TONG | 1.322124e+07 | 25496.700164 | POLYGON ((114.24371 22.28620, 114.24370 22.286... |
3 | 4 | 8 | 西貢區 | 西贡区 | SAI KUNG | 1.602944e+08 | 365545.476363 | MULTIPOLYGON (((114.22112 22.35318, 114.22114 ... |
4 | 5 | 11 | 北區 | 北区 | NORTH | 1.619184e+08 | 192815.532996 | MULTIPOLYGON (((114.33576 22.51003, 114.33576 ... |
= gpd.read_file(
building_raw "/Users/hangzhao/Documents/MUSA_550/Final Project/Data/Building.geojson")
building_raw.head()
OBJECTID | LASTUPDATEDATE | BUILDINGID | TYPEOFBUILDINGBLOCK | BASELEVEL | ROOFLEVEL | BASELEVELDATASOURCE | ROOFLEVELDATASOURCE | BUILDINGSTATUS | CERTAINTY | Shape__Area | Shape__Length | geometry | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 2014-07-01 00:00:00+00:00 | 1108522404 | T | 16.9 | 21.3 | 5 | 4 | E | 1 | 71.640625 | 34.756858 | POLYGON Z ((113.96350 22.40813 0.00000, 113.96... |
1 | 2 | 2014-07-01 00:00:00+00:00 | 1108523034 | T | 15.5 | 18.0 | 5 | 5 | E | 1 | 74.509766 | 40.051622 | POLYGON Z ((113.97897 22.40319 0.00000, 113.97... |
2 | 3 | 2014-07-01 00:00:00+00:00 | 1108523019 | T | 7.4 | 12.3 | 5 | 4 | E | 1 | 73.322266 | 36.024633 | POLYGON Z ((113.97356 22.40331 0.00000, 113.97... |
3 | 4 | 2014-07-01 00:00:00+00:00 | 1105710522 | OS | NaN | NaN | 4 | 4 | E | 1 | 75.222656 | 34.720149 | POLYGON Z ((113.98813 22.47048 0.00000, 113.98... |
4 | 5 | 2014-07-01 00:00:00+00:00 | 1105710546 | OS | NaN | NaN | 4 | 4 | E | 1 | 46.123047 | 28.230848 | POLYGON Z ((114.06630 22.47048 0.00000, 114.06... |
# Define some a default plot width & height
= 800
plot_width = int(plot_width*7.0/12) plot_height
'x'] = building_raw.geometry.centroid.x
building_raw['y'] = building_raw.geometry.centroid.y building_raw[
= hk_limit.hvplot.polygons(
districts_map =True,
geo=4326,
crs="white",
line_color=0,
fill_alpha=plot_width,
frame_width=plot_height,
frame_height )
= building_raw.hvplot.points(
points ="x",
x="y",
y=True, # NEW: tell hvplot to use datashader!
datashade=ds.count(), # NEW: how to aggregate
aggregator=fire,
cmap=True,
geo=4326,
crs=plot_width,
frame_width=plot_height,
frame_height="CartoDark"
tiles
)
#gv.tile_sources.CartoDark
points
= gpd.sjoin(building_raw, hk_limit, how='right')#, predicate="within") join
= join.groupby('ENAME')['Shape__Area_left'].sum().reset_index() building_area_by_district
= pd.merge(
gdf "geometry", "ENAME"]], building_area_by_district, on="ENAME") hk_limit[[
'building_density'] = gdf['Shape__Area_left'] / hk_limit['Shape__Area'] gdf[
"building_file.geojson", driver='GeoJSON') gdf.to_file(
gdf.hvplot(="building_density",
c=600,
frame_width=600,
frame_height="bone_r",
cmap=True,
geo="CartoDark"
tiles )