mirror of
https://github.com/wassname/geopandas.git
synced 2026-09-21 13:00:14 +08:00
5.4 KiB
5.4 KiB
In [ ]:
import geopandas as gpd
import folium
import matplotlib.pyplot as pltIn [ ]:
path = gpd.datasets.get_path('nybb')
df = gpd.read_file(path)
df.head()In [ ]:
df.plot(figsize=(6, 6))
plt.show()In [ ]:
df.crsIn [ ]:
# Use WGS 84 (epsg:4326) as the geographic coordinate system
df = df.to_crs(epsg=4326)
print(df.crs)
df.head()In [ ]:
df.plot(figsize=(6, 6))
plt.show()In [ ]:
m = folium.Map(location=[40.70, -73.94], zoom_start=10, tiles='CartoDB positron')
mIn [ ]:
for _, r in df.iterrows():
# Without simplifying the representation of each borough,
# the map might not be displayed
sim_geo = gpd.GeoSeries(r['geometry']).simplify(tolerance=0.001)
geo_j = sim_geo.to_json()
geo_j = folium.GeoJson(data=geo_j,
style_function=lambda x: {'fillColor': 'orange'})
folium.Popup(r['BoroName']).add_to(geo_j)
geo_j.add_to(m)
mIn [ ]:
# Project to NAD83 projected crs
df = df.to_crs(epsg=2263)
# Access the centroid attribute of each polygon
df['centroid'] = df.centroidIn [ ]:
# Project to WGS84 geographic crs
# geometry (active) column
df = df.to_crs(epsg=4326)
# Centroid column
df['centroid'] = df['centroid'].to_crs(epsg=4326)
df.head()In [ ]:
for _, r in df.iterrows():
lat = r['centroid'].y
lon = r['centroid'].x
folium.Marker(location=[lat, lon],
popup='length: {} <br> area: {}'.format(r['Shape_Leng'], r['Shape_Area'])).add_to(m)
m