0

I am struggling to make the GeoDataFrame functions: intersects/contains work between GeoDataFrames instances.

Let t be a GeoDataFrame with contents:

    id                                          geom
0  230  POINT (154860.3254511474 -468176.1141802836)
1  231  POINT (149247.4170036312 -436634.9002944119)
2  232  POINT (192530.4468407042 -465274.3331522844)
3  233  POINT (138125.4083732415 -419237.1333624315)
4  234  POINT (195314.0952219047 -468429.8881146177)
5  235  POINT (196320.0493430012 -479448.5877129403)
6  236  POINT (199780.1489409541 -446899.1169124786)
7  237  POINT (161437.1203666388 -440485.6615666826)
8  238  POINT (192413.1253873391 -465423.3888242305)
9  239  POINT (196039.9555616449 -473696.9492107867)

I extract the 5 last points in another GeoDataFrame named prt:

    id                                          geom
5  235  POINT (196320.0493430012 -479448.5877129403)
6  236  POINT (199780.1489409541 -446899.1169124786)
7  237  POINT (161437.1203666388 -440485.6615666826)
8  238  POINT (192413.1253873391 -465423.3888242305)
9  239  POINT (196039.9555616449 -473696.9492107867)

executing the command:

t.intersects(prt)
Out:
0    False
1    False
2    False
3    False
4    False
5     True
6     True
7     True
8     True
9     True

However when I create a polygon from the points of prt and repeat the process it fails:

import shapely.geometry as sh
polygon = sh.polygon.Polygon([[p.x, p.y] for p in prt.geometry.tolist()])

import geopandas as gpd
pds = gpd.GeoDataFrame([polygon], columns=['geom'], geometry = 'geom', crs = {'init':'epsg:3309'})


pds.geom_equals(polygon)
Out[873]: 
0    True
dtype: bool

t.intersects(polygon)
Out[875]: 
0    False
1    False
2     True
3    False
4     True
5     True
6     True
7     True
8     True
9     True
dtype: bool

t.intersects(pds)
Out[874]: 
0    False
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False
dtype: bool

Can you please provide me some help/guidelines? I am pretty new in Python, so any advice is welcome.

  • Look at How to find which points intersect with a polygon in geopandas? (the solution works with your data) – gene Jun 12 at 15:26
  • Thank you for the comment. I tested the code that was listed in the suggested link and it works. When I am trying it with my data it fails however. I tried additional relations, as well, such as within or contains (in order to avoid problems with the boundaries) but the result was the same. – Manos Ppd Jun 12 at 18:21
  • Look below with your data – gene Jun 12 at 20:03
0

With the solution of How to find which points intersect with a polygon in geopandas? and your data

....
polygon = sh.polygon.Polygon([[p.x, p.y] for p in prt.geometry.tolist()])
polygon.wkt
'POLYGON ((196320.0493430012 -479448.5877129403, 199780.1489409541 -446899.1169124786, 161437.1203666388 -440485.6615666826, 192413.1253873391 -465423.3888242305, 196039.9555616449 -473696.9492107867, 196320.0493430012 -479448.5877129403))'
t.intersects(polygon)
0    False
1    False
2     True
3    False
4     True
5     True
6     True
7     True
8     True
9     True
dtype: bool
pds = gpd.GeoDataFrame([polygon], columns=['geometry'], geometry = 'geometry', crs = {'init':'epsg:3309'}
t.intersects(pds.unary_union)
index_left
0    False
1    False
2     True
3    False
4     True
5     True
6     True
7     True
8     True
9     True
dtype: bool
t.intersects(pds.geometry.ix[0]) 
index_left
0    False
1    False
2     True
3    False
4     True
5     True
6     True
7     True
8     True
9     True
dtype: bool

And

from geopandas.tools import sjoin
sjoin(t, pds, how='inner')
    FID               geometry                         index_right
2    2  POINT (192530.4468407042 -465274.3331522844)            0
4    4  POINT (195314.0952219047 -468429.8881146177)            0
5    5  POINT (196320.0493430012 -479448.5877129403)            0
6    6  POINT (199780.1489409541 -446899.1169124786)            0
7    7  POINT (161437.1203666388 -440485.6615666826)            0
8    8  POINT (192413.1253873391 -465423.3888242305)            0
9    9  POINT (196039.9555616449 -473696.9492107867)            0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.