3

Here is some example data:

import pandas as pd
import geopandas as gp
import shapely.geometry
from shapely.geometry import Polygon
from shapely.geometry import Point
import shapely.affinity
import matplotlib.pyplot as plt

df = gp.GeoDataFrame([['a', Polygon([(0,1), (1,1), (2,2), (1,2)])],
                     ['b', Polygon([(1.5,0.75), (2, 1.25), (3,0.25)])]],
                    columns = ['name', 'geometry'])

df = gp.GeoDataFrame(df, geometry = 'geometry')
df['area'] = df.area

points = gp.GeoDataFrame([['box', Point(1.2, 1.115), 4],
                         ['triangle', Point(2.5, 1.25), 8]],
                        columns = ['name', 'geometry', 'value'],
                        geometry = 'geometry')

buf = points.buffer(0.5, cap_style = 3)
points['buffer'] = buf
points = points.drop(['geometry'], axis = 1)
points = points.rename(columns = {'buffer': 'geometry'})

And it looks like this:

enter image description here

Basically I am trying to find the area of intersection between these objects.

I have done this so far with this code:

data = []
for index, geo in df.iterrows():
    for index2, poin in points.iterrows():
        if geo['geometry'].intersects(poin['geometry']):
          data.append({'geometry':geo['geometry'].intersection(poin['geometry']), 'area': geo['geometry'].intersection(poin['geometry']).area})

df2 = gp.GeoDataFrame(data, columns = ['geometry', 'area'])

However, the real data I'll be using this on has 100,000s of polygons, so this code would be incredibly time consuming. I know I could speed this up with the use of r-trees. However, I can't seem to implement it properly.

I have tried something like this:

spatial_index = df.sindex
results_list = []
for index, row in points.iterrows():
    buffer = row['geometry']
    possible_matches_index = list(spatial_index.intersection(buffer.bounds)) 
    possible_matches = df.iloc[possible_matches_index]
    results_list.append({'geometry':possible_matches['geometry'].intersection(row['geometry']), 'area': possible_matches['geometry'].intersection(row['geometry']).area})

df = gp.GeoDataFrame(results_list, columns = ['geometry', 'area'])

But this puts all the intersections for each square into a single line.

    geometry                                        area
0   name a POLYGON ((1.615 1.615, 1 1, 0.7 1, 0...  name a 0.000037 b 0.000003 dtype: float64
1                                    name a ...     name a 0.000000 b 0.000013 dtype: float64

How can I get this to produce a dataframe with a single line for each intersection with columns for its geometry and area?

tom91
  • 685
  • 7
  • 24
  • 1
    Would `geopandas.overlay(df, points, how='intersection')` help? That makes use of `rtree` spatial index under the hood, and will return the intersections of all combinations of the geometries of both datasets as a new GeoDataFrame (for which you can then calculate the areas). – joris May 07 '19 at 11:33
  • Yes! That is so helpful and so much cleaner than what I was trying to do, thank you! If you put it up as an answer I'll accept it. – tom91 May 07 '19 at 12:28

1 Answers1

3

To calculate the intersections between both GeoDataFrames, you can use the geopandas.overlay function:

geopandas.overlay(df, points, how='intersection')

That makes use of rtree spatial index under the hood (so should be more efficient that the brute force double for-loop), and will return the intersections of all combinations of the geometries of both datasets as a new GeoDataFrame (for which you can then calculate the areas).

See https://geopandas.readthedocs.io/en/latest/set_operations.html for the docs on this.

joris
  • 133,120
  • 36
  • 247
  • 202