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 inlineimport 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 inlineimport holoviews as hv
import geoviews as gvfrom holoviews.operation.datashader import datashade
hv.extension('bokeh')# 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'
)
hk_limit = gpd.read_file(district_url)
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 ... |
building_raw = gpd.read_file(
"/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
plot_width = 800
plot_height = int(plot_width*7.0/12)building_raw['x'] = building_raw.geometry.centroid.x
building_raw['y'] = building_raw.geometry.centroid.ydistricts_map = hk_limit.hvplot.polygons(
geo=True,
crs=4326,
line_color="white",
fill_alpha=0,
frame_width=plot_width,
frame_height=plot_height,
)points = building_raw.hvplot.points(
x="x",
y="y",
datashade=True, # NEW: tell hvplot to use datashader!
aggregator=ds.count(), # NEW: how to aggregate
cmap=fire,
geo=True,
crs=4326,
frame_width=plot_width,
frame_height=plot_height,
tiles="CartoDark"
)
#gv.tile_sources.CartoDark
pointsjoin = gpd.sjoin(building_raw, hk_limit, how='right')#, predicate="within")building_area_by_district = join.groupby('ENAME')['Shape__Area_left'].sum().reset_index()gdf = pd.merge(
hk_limit[["geometry", "ENAME"]], building_area_by_district, on="ENAME")gdf['building_density'] = gdf['Shape__Area_left'] / hk_limit['Shape__Area']gdf.to_file("building_file.geojson", driver='GeoJSON')gdf.hvplot(
c="building_density",
frame_width=600,
frame_height=600,
cmap="bone_r",
geo=True,
tiles="CartoDark"
)