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%20OSM%20Data%0A%0A%20%20%20%20Calculate%20landuse%20accessibility%20to%20pubs%20and%20restaurants%20for%20London%20using%20OpenStreetMap%20data.%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%20whose%20%5B%60from_osm%60%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fapi%2Fnetwork%23from_osm)%20constructor%20wraps%20the%20OSM%20download%2C%20cleaning%2C%20and%20network%20preparation%20behind%20a%20single%20call.%0A%0A%20%20%20%20Network%20data%20%C2%A9%20%5BOpenStreetMap%5D(https%3A%2F%2Fwww.openstreetmap.org%2Fcopyright)%20contributors%2C%20available%20under%20the%20Open%20Database%20Licence.%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%20matplotlib.pyplot%20as%20plt%0A%20%20%20%20from%20cityseer.network%20import%20CityNetwork%0A%20%20%20%20from%20cityseer.tools%20import%20io%0A%20%20%20%20from%20osmnx%20import%20features%0A%0A%20%20%20%20return%20CityNetwork%2C%20features%2C%20io%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%20create%20the%20network%20from%20a%20buffered%20point%20polygon%20using%20the%20%60from_osm%60%20constructor.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(CityNetwork%2C%20io)%3A%0A%20%20%20%20lng%2C%20lat%20%3D%20-0.13396079424572427%2C%2051.51371088849723%0A%20%20%20%20buffer%20%3D%201500%0A%20%20%20%20poly_wgs%2C%20_epsg_code%20%3D%20io.buffered_point_poly(lng%2C%20lat%2C%20buffer)%0A%20%20%20%20cn%20%3D%20CityNetwork.from_osm(poly_wgs)%0A%20%20%20%20return%20cn%2C%20poly_wgs%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%20Prepare%20the%20amenities%20GeoDataFrame%20by%20downloading%20the%20data%20from%20OpenStreetMap.%20The%20%60osmnx%60%20%5B%60features_from_polygon%60%5D(https%3A%2F%2Fosmnx.readthedocs.io%2Fen%2Fstable%2Fuser-reference.html%23osmnx.features.features_from_polygon)%20works%20well%20for%20this%20purpose.%20In%20this%20instance%2C%20we%20are%20specifically%20targeting%20features%20that%20are%20labelled%20as%20an%20%60amenity%60%20type%20of%20either%20%60pub%60%20or%20%60restaurant%60.%20You%20can%20use%20the%20same%20idea%20to%20extract%20other%20features%20or%20land%20use%20types.%0A%0A%20%20%20%20It%20is%20important%20to%20convert%20the%20derivative%20GeoDataFrame%20to%20the%20same%20CRS%20as%20the%20network.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(cn%2C%20features%2C%20poly_wgs)%3A%0A%20%20%20%20data_gdf%20%3D%20features.features_from_polygon(poly_wgs%2C%20tags%3D%7B%22amenity%22%3A%20%5B%22pub%22%2C%20%22restaurant%22%5D%7D)%0A%20%20%20%20data_gdf%20%3D%20data_gdf.to_crs(cn.crs)%0A%20%20%20%20data_gdf%0A%20%20%20%20return%20(data_gdf%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%20Some%20preparatory%20data%20cleaning%20is%20typically%20necessary.%20This%20example%20extracts%20the%20particular%20rows%20and%20columns%20of%20interest%20for%20the%20subsequent%20steps%20of%20analysis.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(data_gdf)%3A%0A%20%20%20%20%23%20extract%20nodes%0A%20%20%20%20data_gdf_1%20%3D%20data_gdf.loc%5B%22node%22%5D%0A%20%20%20%20%23%20reset%20index%0A%20%20%20%20data_gdf_1%20%3D%20data_gdf_1.reset_index(level%3D0%2C%20drop%3DTrue)%0A%20%20%20%20%23%20extract%20relevant%20columns%0A%20%20%20%20data_gdf_1%20%3D%20data_gdf_1%5B%5B%22amenity%22%2C%20%22geometry%22%5D%5D%0A%20%20%20%20data_gdf_1.head()%0A%20%20%20%20return%20(data_gdf_1%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%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%20data_gdf_1)%3A%0A%20%20%20%20%23%20compute%20pub%20accessibility%0A%20%20%20%20distances%20%3D%20%5B100%2C%20200%2C%20400%2C%20800%5D%0A%20%20%20%20_cn%2C%20data_gdf_2%20%3D%20cn.compute_accessibilities(%0A%20%20%20%20%20%20%20%20data_gdf_1%2C%0A%20%20%20%20%20%20%20%20landuse_column_label%3D%22amenity%22%2C%0A%20%20%20%20%20%20%20%20accessibility_keys%3D%5B%22pub%22%2C%20%22restaurant%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%20data_gdf_2%2C%20nodes_gdf_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_(data_gdf_2%2C%20nodes_gdf_1%2C%20plt)%3A%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%20%3D%20nodes_gdf_1.copy()%0A%20%20%20%20_g%5B%22_r%22%5D%20%3D%20_g%5B%22cc_restaurant_400%22%5D.rank(pct%3DTrue)%0A%20%20%20%20_g%20%3D%20_g.sort_values(%22_r%22)%20%20%23%20strongest%20drawn%20last%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%20data_gdf_2%5Bdata_gdf_2%5B%22amenity%22%5D%20%3D%3D%20%22restaurant%22%5D.plot(markersize%3D2%2C%20edgecolor%3DNone%2C%20color%3D%22%23333333%22%2C%20ax%3D_ax)%0A%20%20%20%20_ax.set_title(%22Restaurant%20accessibility%2C%20400%20m%22%2C%20loc%3D%22left%22)%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_(data_gdf_2%2C%20nodes_gdf_1%2C%20plt)%3A%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%20%3D%20nodes_gdf_1.copy()%0A%20%20%20%20%23%20distance-to-nearest%3A%20lower%20is%20better%2C%20so%20invert%20the%20rank%20to%20draw%20the%20closest%20streets%20boldest%0A%20%20%20%20_g%5B%22_r%22%5D%20%3D%201%20-%20_g%5B%22cc_pub_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_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%20data_gdf_2%5Bdata_gdf_2%5B%22amenity%22%5D%20%3D%3D%20%22pub%22%5D.plot(markersize%3D2%2C%20edgecolor%3DNone%2C%20color%3D%22%23333333%22%2C%20ax%3D_ax)%0A%20%20%20%20_ax.set_title(%22Distance%20to%20nearest%20pub%2C%20800%20m%20max%22%2C%20loc%3D%22left%22)%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%20%23%23%20Conclusion%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%20This%20notebook%20computed%20landuse%20accessibility%20to%20pubs%20and%20restaurants%20in%20London%20using%20point%20features%20downloaded%20from%20OpenStreetMap%20via%20%60osmnx%60%20and%20the%20%60CityNetwork%60%20class.%20The%20workflow%20fetches%20amenity%20data%2C%20assigns%20it%20to%20the%20network%2C%20and%20produces%20reachable%20counts%20and%20nearest-distance%20columns%20that%20reveal%20the%20spatial%20concentration%20of%20hospitality%20venues.%0A%0A%20%20%20%20**Next%20steps%3A**%20For%20polygon-based%20accessibility%2C%20such%20as%20parks%2C%20see%20%5BPark%20Accessibility%5D(https%3A%2F%2Fcityseer.benchmarkurbanism.com%2Fexamples%2Faccessibility%2Fosm-accessibility-polys).%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
bc98652ce277da97c80bd2e7467ffece