from shapely.geometry import Polygon
from geopandas import GeoSeries, GeoDataFrame
# Create three simple polygons
p1 = Polygon([(0, 0), (1, 0), (1, 1)])
p2 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1)])
p3 = Polygon([(2, 0), (3, 0), (3, 1), (2, 1)])
s = GeoSeries([p1, p2, p3])
s
Some geographic operations return a normal pandas object. The area
property of a GeoSeries
will return a pandas Series
containing the area of each item in the GeoSeries
:
print(s.area)
# Specify file name
import os
boro_file = os.path.join("..", "data", "nybb", "nybb.shp")
# Create from file (one line)
boros = GeoDataFrame.from_file(boro_file)
# Do some pandas stuff
boros.set_index('BoroCode', inplace=True)
boros.sort()
boros
import matplotlib.pyplot as plt
%matplotlib inline
f, (ax1, ax2) = plt.subplots(1, 2, figsize=(15, 5), sharey=True)
s.plot(axes=ax1)
ax1.set_title("Original Polygons")
ax1.set_xlim(-0.5, 3.5)
s.buffer(0.4).plot(axes=ax2)
ax2.set_title("Buffered Polygons")
ax2.set_ylim(-0.5, 1.5)
plt.show()
fig = plt.figure(figsize=(8, 8))
ax = boros.plot() # Regular plot
# We can access and plot the geometries directly as well
ax = boros.geometry.convex_hull.plot(axes=ax)
plt.show()
You can access PostGIS
and other spatially aware databases with a similar API
to Pandas. The added bonus here is that GeoPandas understands geospatial data (such as WKB
):
It automatically converts WKB
to the appropriate Shapely geometry
type.
Note: If you're following along, you can use the included shapefile instead of a database
import geopandas as gpd
wifi_file = os.path.join("..", "data", "wifi", "wifi.shp")
df_geo = gpd.read_file(wifi_file)
df_geo.head()
# What is the total area of all 5 NYC boroughs?
print(boros.area.sum())
...change the CRS:
# Convert from NY State Plane to WGS84 (long/lat)
df_wgs84 = df_geo.to_crs(epsg=4326) # 4326 is the EPSG code for WGS84
print(df_geo.crs)
print(df_wgs84.crs)
...and much more... plus anything else that Pandas can do
Here's a super simple map, which shows Free
and Fee-based
WiFi hotspots in NYC:
ax = df_wgs84.plot() # This is all that's needed, but...
# ...here's a hack to plot things a bit more nicely (GeoPandas is still new!)
free = df_wgs84["type"] == "Free" # Is it free?
col = {True: "green", False: "blue"} # If free, green, otherwise blue
for i, l in enumerate(ax.lines):
l.set_markersize(6)
l.set_color(col[free[i]])
ax.axis('off') # Turn off axes
plt.show()
import mplleaflet
mplleaflet.display(fig=ax.figure, crs=df_wgs8.crs)
mplleaflet.show(fig=ax.figure, crs=df_wgs84.crs)
geojson.io
.¶Note, your map should really be reprojected to EPSG:4326 (WGS84 lon/lat) for this to align properly.
import geojsonio
res = geojsonio.display(boros.to_crs(epsg=4326).to_json(), force_gist=True)
print(res)
Don't forget to pop on over to geojson.io and play around/edit your GeoJSON layer...
[Working with Vectors in Python
](../exercises/Working with Vectors in Python.ipynb)