Using Python to get Manhattan OSM Data

Jiacheng Zhou
2 min readApr 26, 2020

1. Install and Import the dependencies

import pandas as pd
import geopandas as gpd
import osmnx as ox
import folium
import matplotlib.pyplot as plt
import seaborn as sns

2. Define the place of interest (POI)

place = "Manhattan, United States"
graph = ox.graph_from_place(place, network_type="drive")
len(graph)

3. Convert the data type

#Streets
nodes, streets = ox.graph_to_gdfs(graph)
streets.head()
#Building Footprints
bldg = ox.footprints_from_place(place)
bldg.shape
cols = ['amenity', 'building', 'name', 'tourism']
bldg[cols].head()
#Cafe https://wiki.openstreetmap.org/wiki/Key:amenity
cafe = ox.pois_from_place(place, amenities = ["cafe"])
cafe_pt = cafe[cafe.geom_type == "Point"]
cafe.head()

4. Data Visualization

Street stats

#street
street_type = pd.DataFrame(streets["highway"].apply(pd.Series)[0].value_counts().reset_index())
street_type.columns=["type", "count"]
fig, ax = plt.subplots(figsize=(12,10))sns.barplot(y="type", x="count", data=street_type, ax=ax)
plt.tight_layout()

Map projection of Street

style = {'color': '#F7DC6F', 'weight':'1'}
m = folium.Map([40.730610, -73.935242],
zoom_start=15,
tiles="CartoDb dark_matter")
folium.GeoJson(streets, style_function=lambda x: style).add_to(m)
m.save("streets.html")
m

Building stats

bldg_type = pd.DataFrame(bldg["amenity"].apply(pd.Series)[0].value_counts().reset_index())bldg_type.columns=["type", "count"]fig, ax = plt.subplots(figsize=(12,10))sns.barplot(y="type", x="count", data=bldg_type, ax=ax)
plt.tight_layout()

Map of Building

style_buildings = {'color':'#6C3483 ', 'fillColor': '#6C3483 ', 'weight':'1', 'fillOpacity' : 1}b = folium.Map([40.730610, -73.935242],
zoom_start=15,
tiles="Stamen Toner")
folium.GeoJson(bldg[:1000], style_function=lambda x: style_buildings).add_to(b)
b.save("buildings.html")
b

Map of Cafe

c = folium.Map([40.730610, -73.935242], 
zoom_start=10,
tiles="CartoDb dark_matter")
c_loc = zip(cafe_pt.geometry.y, cafe_pt.geometry.x)

for location in c_loc:
folium.CircleMarker(location=location,
color = "#F4F6F7", radius=2).add_to(c)
c.save("cafes.html")
c

Note: The interactive map can be moved around to make sure that it is at the right position to show POI.

--

--