import%20marimo%0A%0A__generated_with%20%3D%20%220.23.13%22%0Aapp%20%3D%20marimo.App()%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%0A%20%20%20%20return%20(mo%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Landuse%20Accessibility%20from%20GeoPandas%20Data%0A%0A%20%20%20%20Calculate%20landuse%20accessibilities%20from%20a%20%60geopandas%60%20%60GeoDataFrame%60.%0A%0A%20%20%20%20This%20recipe%20uses%20the%20high-level%20%5B%60CityNetwork%60%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fapi%2Fnetwork)%20class%2C%20which%20wraps%20network%20preparation%20and%20the%20accessibility%20computation%20behind%20a%20lean%20interface.%0A%0A%20%20%20%20The%20bundled%20datasets%20and%20their%20source%20attributions%20are%20documented%20on%20the%20%5Bdatasets%20page%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fexamples%2Fdatasets).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20import%20geopandas%20as%20gpd%0A%20%20%20%20import%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20cityseer.network%20import%20CityNetwork%0A%0A%20%20%20%20return%20CityNetwork%2C%20gpd%2C%20plt%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20start%2C%20build%20the%20network%20with%20the%20%5B%60from_geopandas%60%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fapi%2Fnetwork%23from_geopandas)%20constructor.%20The%20full%20bundled%20network%20is%20clipped%20to%20a%202km%20study%20area%20around%20the%20city%20centre%2C%20buffered%20by%20the%20maximum%20analysis%20distance%20so%20that%20nodes%20near%20the%20study%20edge%20keep%20their%20full%20catchments%3B%20accessibility%20is%20computed%20per%20node%20from%20local%20catchments%2C%20so%20a%20district%20gives%20the%20same%20results%20as%20the%20whole%20city%20at%20a%20fraction%20of%20the%20cost.%20The%20%60boundary%60%20argument%20marks%20the%20nodes%20inside%20the%20study%20area%20as%20%60live%60.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(CityNetwork%2C%20gpd)%3A%0A%20%20%20%20streets_gpd%20%3D%20gpd.read_file(%22..%2F..%2Fdata%2Fmadrid_streets%2Fstreet_network.gpkg%22)%0A%20%20%20%20streets_gpd%20%3D%20streets_gpd.explode(ignore_index%3DTrue)%0A%20%20%20%20%23%20the%20source%20data%20contains%20a%20small%20number%20of%20exact%20duplicate%20geometries%0A%20%20%20%20streets_gpd%20%3D%20streets_gpd.drop_duplicates(subset%3D%22geometry%22)%0A%20%20%20%20%23%202km%20study%20area%20around%20the%20city%20centre%2C%20with%20the%20network%20buffered%20a%20further%20800m%0A%20%20%20%20%23%20(the%20maximum%20analysis%20distance)%20to%20prevent%20edge%20rolloff%0A%20%20%20%20centre%20%3D%20gpd.GeoSeries.from_xy(%5B440300%5D%2C%20%5B4474300%5D%2C%20crs%3Dstreets_gpd.crs)%0A%20%20%20%20study_poly%20%3D%20centre.buffer(2000).iloc%5B0%5D%0A%20%20%20%20buffered_poly%20%3D%20centre.buffer(2800).iloc%5B0%5D%0A%20%20%20%20streets_clip%20%3D%20streets_gpd%5Bstreets_gpd.intersects(buffered_poly)%5D%0A%20%20%20%20cn%20%3D%20CityNetwork.from_geopandas(streets_clip%2C%20boundary%3Dstudy_poly)%0A%20%20%20%20return%20buffered_poly%2C%20cn%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Read%20the%20landuse%20dataset%20with%20GeoPandas%2C%20for%20example%20from%20a%20GeoPackage%20or%20Shapefile.%20The%20premises%20are%20clipped%20to%20the%20buffered%20study%20area%2C%20since%20only%20landuses%20within%20reach%20of%20the%20clipped%20network%20can%20contribute.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(buffered_poly%2C%20gpd)%3A%0A%20%20%20%20prems_gpd%20%3D%20gpd.read_file(%22..%2F..%2Fdata%2Fmadrid_premises%2Fmadrid_premises.gpkg%22)%0A%20%20%20%20prems_gpd%20%3D%20prems_gpd%5Bprems_gpd.within(buffered_poly)%5D%0A%20%20%20%20prems_gpd.head()%0A%20%20%20%20return%20(prems_gpd%2C)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Identify%20or%20prepare%20any%20columns%20and%20land%20uses%20of%20interest%2C%20for%20which%20you%20want%20to%20compute%20accessibilities.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(prems_gpd)%3A%0A%20%20%20%20prems_gpd.division_desc.unique()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Once%20the%20landuse%20and%20network%20data%20has%20been%20prepared%2C%20use%20the%20%5B%60compute_accessibilities%60%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fapi%2Fnetwork%23compute_accessibilities)%20method%20to%20compute%20accessibilities%20to%20landuses.%20The%20%60landuse_column_label%60%20and%20the%20target%20accessibility%20keys%20should%20correspond%20to%20the%20data%20in%20the%20input%20GeoDataFrame.%20%60to_geopandas%60%20returns%20the%20results%20joined%20to%20the%20original%20street%20LineString%20geometries.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(cn%2C%20prems_gpd)%3A%0A%20%20%20%20%23%20compute%20accessibility%0A%20%20%20%20distances%20%3D%20%5B100%2C%20200%2C%20400%2C%20800%5D%0A%20%20%20%20_cn%2C%20prems_gpd_1%20%3D%20cn.compute_accessibilities(%0A%20%20%20%20%20%20%20%20prems_gpd%2C%0A%20%20%20%20%20%20%20%20landuse_column_label%3D%22division_desc%22%2C%0A%20%20%20%20%20%20%20%20accessibility_keys%3D%5B%22food_bev%22%2C%20%22creat_entert%22%2C%20%22retail%22%5D%2C%0A%20%20%20%20%20%20%20%20distances%3Ddistances%2C%0A%20%20%20%20)%0A%20%20%20%20nodes_gdf_1%20%3D%20cn.to_geopandas()%0A%20%20%20%20return%20nodes_gdf_1%2C%20prems_gpd_1%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20The%20output%20columns%20are%20named%20%60cc_%7Bkey%7D_%7Bdistance%7D%60%2C%20where%20the%20keys%20correspond%20to%20the%20input%20accessibility%20keys%20and%20the%20distances%20to%20the%20input%20distances.%20By%20default%20the%20counts%20are%20unweighted%3B%20pass%20a%20%60decay_fn%60%20expression%20such%20as%20%60%22exp(-4%20*%20p)%22%60%20for%20distance-weighted%20counts.%20A%20further%20%60cc_%7Bkey%7D_nearest_max_%7Bdistance%7D%60%20column%20gives%20the%20distance%20to%20the%20nearest%20instance%20of%20each%20landuse.%0A%0A%20%20%20%20Standard%20GeoPandas%20functionality%20can%20be%20used%20to%20explore%2C%20visualise%2C%20or%20save%20the%20results.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nodes_gdf_1)%3A%0A%20%20%20%20nodes_gdf_1.columns%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nodes_gdf_1%2C%20plt%2C%20prems_gpd_1)%3A%0A%20%20%20%20_g%20%3D%20nodes_gdf_1%5Bnodes_gdf_1.live%5D.copy()%0A%20%20%20%20_g%5B%22_r%22%5D%20%3D%20_g%5B%22cc_retail_400%22%5D.rank(pct%3DTrue)%0A%20%20%20%20_g%20%3D%20_g.sort_values(%22_r%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(7%2C%207)%2C%20dpi%3D150)%0A%20%20%20%20_g.plot(ax%3D_ax%2C%20color%3Dplt.get_cmap(%22OrRd%22)(_g%5B%22_r%22%5D)%2C%20linewidth%3D0.15%20%2B%202.25%20*%20_g%5B%22_r%22%5D)%0A%20%20%20%20prems_gpd_1%5Bprems_gpd_1%5B%22division_desc%22%5D%20%3D%3D%20%22retail%22%5D.plot(%0A%20%20%20%20%20%20%20%20markersize%3D1%2C%20edgecolor%3DNone%2C%20color%3D%22%23333333%22%2C%20legend%3DFalse%2C%20ax%3D_ax%0A%20%20%20%20)%0A%20%20%20%20_ax.set_title(%22Retail%20accessibility%2C%20400%20m%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_ax.set_xlim(439000%2C%20439000%20%2B%202500)%0A%20%20%20%20_ax.set_ylim(4473000%2C%204473000%20%2B%202500)%0A%20%20%20%20_ax.set_axis_off()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nodes_gdf_1%2C%20plt%2C%20prems_gpd_1)%3A%0A%20%20%20%20_g%20%3D%20nodes_gdf_1%5Bnodes_gdf_1.live%5D.copy()%0A%20%20%20%20_g%5B%22_r%22%5D%20%3D%20_g%5B%22cc_food_bev_200%22%5D.rank(pct%3DTrue)%0A%20%20%20%20_g%20%3D%20_g.sort_values(%22_r%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(7%2C%207)%2C%20dpi%3D150)%0A%20%20%20%20_g.plot(ax%3D_ax%2C%20color%3Dplt.get_cmap(%22OrRd%22)(_g%5B%22_r%22%5D)%2C%20linewidth%3D0.15%20%2B%202.25%20*%20_g%5B%22_r%22%5D)%0A%20%20%20%20prems_gpd_1%5Bprems_gpd_1%5B%22division_desc%22%5D%20%3D%3D%20%22food_bev%22%5D.plot(%0A%20%20%20%20%20%20%20%20markersize%3D1%2C%20edgecolor%3DNone%2C%20color%3D%22%23333333%22%2C%20legend%3DFalse%2C%20ax%3D_ax%0A%20%20%20%20)%0A%20%20%20%20_ax.set_title(%22Food%20and%20beverage%20accessibility%2C%20200%20m%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_ax.set_xlim(439000%2C%20439000%20%2B%202500)%0A%20%20%20%20_ax.set_ylim(4473000%2C%204473000%20%2B%202500)%0A%20%20%20%20_ax.set_axis_off()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(nodes_gdf_1%2C%20plt%2C%20prems_gpd_1)%3A%0A%20%20%20%20nodes_gdf_1%5B%22cc_creat_entert_nearest_max_800%22%5D%20%3D%20nodes_gdf_1%5B%22cc_creat_entert_nearest_max_800%22%5D.fillna(800)%0A%20%20%20%20_g%20%3D%20nodes_gdf_1%5Bnodes_gdf_1.live%5D.copy()%0A%20%20%20%20%23%20proximity%20map%3A%20invert%20the%20rank%20so%20the%20nearest%20venues%20(smallest%20distance)%20read%20boldest%0A%20%20%20%20_g%5B%22_r%22%5D%20%3D%201%20-%20_g%5B%22cc_creat_entert_nearest_max_800%22%5D.rank(pct%3DTrue)%0A%20%20%20%20_g%20%3D%20_g.sort_values(%22_r%22)%0A%20%20%20%20_fig%2C%20_ax%20%3D%20plt.subplots(1%2C%201%2C%20figsize%3D(7%2C%207)%2C%20dpi%3D150)%0A%20%20%20%20_g.plot(ax%3D_ax%2C%20color%3Dplt.get_cmap(%22OrRd%22)(_g%5B%22_r%22%5D)%2C%20linewidth%3D0.15%20%2B%202.25%20*%20_g%5B%22_r%22%5D)%0A%20%20%20%20prems_gpd_1%5Bprems_gpd_1%5B%22division_desc%22%5D%20%3D%3D%20%22creat_entert%22%5D.plot(%0A%20%20%20%20%20%20%20%20markersize%3D2%2C%20edgecolor%3DNone%2C%20color%3D%22%23333333%22%2C%20legend%3DFalse%2C%20ax%3D_ax%0A%20%20%20%20)%0A%20%20%20%20_ax.set_title(%22Distance%20to%20nearest%20creative%20or%20entertainment%20venue%2C%20800%20m%20max%22%2C%20loc%3D%22left%22)%0A%20%20%20%20_ax.set_xlim(439000%2C%20439000%20%2B%202500)%0A%20%20%20%20_ax.set_ylim(4473000%2C%204473000%20%2B%202500)%0A%20%20%20%20_ax.set_axis_off()%0A%20%20%20%20_fig.tight_layout()%0A%20%20%20%20_fig%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Brighter%20areas%20indicate%20higher%20accessibility%3A%20more%20landuses%20of%20that%20type%20are%20reachable%20within%20the%20specified%20network%20distance.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_(mo)%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20Conclusion%0A%0A%20%20%20%20This%20notebook%20computed%20landuse%20accessibility%20metrics%20from%20a%20GeoDataFrame%20of%20premises%20data%20through%20the%20%60CityNetwork%60%20class%3A%20reachable%20counts%20and%20nearest-distance%20measures%20for%20food%20and%20beverage%2C%20creative%20entertainment%2C%20and%20retail%20categories%20at%20multiple%20distance%20thresholds.%20The%20results%20map%20the%20spatial%20distribution%20and%20intensity%20of%20different%20landuses%20across%20a%20central%20study%20area.%0A%0A%20%20%20%20**Next%20steps%3A**%20To%20compute%20accessibility%20from%20OSM%20data%20instead%2C%20see%20%5BOSM%20Accessibility%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fexamples%2Faccessibility%2Fosm-accessibility).%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
053967cc583a012ee1dc4abcfe2d9d1e