Spatial overlays allow you to compare two GeoDataFrames containing polygon or multipolygon geometries and create a new GeoDataFrame with the new geometries representing the spatial combination and merged properties. This allows you to answer questions like
What are the demographics of the census tracts within 1000 ft of the highway?
The basic idea is demonstrated by the graphic below but keep in mind that overlays operate at the dataframe level, not on individual geometries, and the properties from both are retained
From https://github.com/geopandas/geopandas/blob/master/examples/overlays.ipynb. Re-ran and tweaked by Emilio Mayorga, 12/10/2015, on my geopandas conda environment.
In [1]:
from IPython.core.display import Image
Image(url="http://docs.qgis.org/testing/en/_images/overlay_operations.png")
Out[1]:
Now we can load up two GeoDataFrames containing (multi)polygon geometries...
Downloaded sample NYC Boroughs shape file from http://www.nyc.gov/html/dcp/download/bytes/nybb_14aav.zip
In [2]:
%matplotlib inline
import os
from shapely.geometry import Point
from geopandas import GeoDataFrame, read_file
from geopandas.tools import overlay
# NYC Boros
#zippath = os.path.abspath('../examples/nybb_14aav.zip')
zippath = os.path.abspath('/home/mayorga/Desktop/MySciPythonApps/nybb_14aav.zip')
polydf = read_file('/nybb_14a_av/nybb.shp', vfs='zip://' + zippath)
# Generate some circles
b = [int(x) for x in polydf.total_bounds]
N = 10
polydf2 = GeoDataFrame([
{'geometry' : Point(x, y).buffer(10000), 'value1': x + y, 'value2': x - y}
for x, y in zip(range(b[0], b[2], int((b[2]-b[0])/N)),
range(b[1], b[3], int((b[3]-b[1])/N)))])
The first dataframe contains multipolygons of the NYC boros
In [3]:
polydf.plot();
And the second GeoDataFrame is a randomly generated set of circles in the same geographic space (TODO ...use real data)
In [4]:
polydf2.plot();
The geopandas.tools.overlay
function takes three arguments:
Where how
can be one of:
['intersection',
'union',
'identity',
'symmetric_difference',
'difference']
So let's identify the areas (and attributes) where both dataframes intersect using the overlay
tool.
In [5]:
newdf = overlay(polydf, polydf2, how="intersection")
newdf.plot();
And take a look at the attributes; we see that the attributes from both of the original GeoDataFrames are retained.
In [6]:
polydf.head()
Out[6]:
In [7]:
polydf2.head()
Out[7]:
In [8]:
newdf.head()
Out[8]:
Now let's look at the other how
operations:
In [9]:
newdf = overlay(polydf, polydf2, how="union")
newdf.plot();
In [10]:
newdf = overlay(polydf, polydf2, how="identity")
newdf.plot();
In [11]:
newdf = overlay(polydf, polydf2, how="symmetric_difference")
newdf.plot();
In [12]:
newdf = overlay(polydf, polydf2, how="difference")
newdf.plot();
In [ ]: