Using this example dataset of country geometries, I seem to get a different result on whether or not the USA and Mexico intersect when I do the calculation with GeoPandas as opposed to directly with Fiona and Shapely (upon which Geopandas depends). I am reasonably experienced with Fiona and Shapely, but quite new to GeoPandas - does anyone know why this might be?
Consider the following code snippet:
from geopandas import read_file # load the shapefile of countries world = read_file('TM_WORLD_BORDERS-0.3.shp') # extract usa and mexico usa = world[ (world.ISO3 == 'USA') ] mexico = world[ (world.ISO3 == 'MEX') ] # test intersection print(usa.intersects(mexico))
119 False 208 False dtype: bool
However, if I do the same thing directly with Fiona and Shapely:
from fiona import open from shapely.geometry import shape # load the shapefile of countries with open('TM_WORLD_BORDERS-0.3.shp') as countries: # extract usa and mexico for feat in countries: if feat['properties']['ISO3'] == 'USA': usa = shape(feat['geometry']) elif feat['properties']['ISO3'] == 'MEX': mexico = shape(feat['geometry']) # test intersection print(usa.intersects(mexico))
Then I get:
Clearly, I would expect both code snippets to evaluate to
True. I had wondered if Geopandas handled MultiPolygons differently (as USA is a MultiPolygon, whereas Mexico is a Polygon), but I can't seem to get a positive intersection by isolating the individual polygon either.