Search code examples
pythonpandasshapefileqgisgeopandas

different output between QGIS and Python(geopandas)


I'm a beginner of python and qgis.

I tried to intersect two shapefiles by using intersection. (polygons)

First, i tried intersection with QGIS, and it works. (EPSG : 4326) QGIS picture

But the problem is that when I tried to intersect at the python, just one value returns. what's wrong??

Here is my code

sb_4326 = buildings polygon
sg_4326 = city_polygon
import geopandas as gpd
###
###
sg4326 = gpd.read_file('sg_4326/sg4326.shp')
sb4326 = gpd.read_file('sb_4326/sb4326.shp')
mm = sb4326.intersects(sg4326)

Out[35]:
0        False
1        False
2        False
3        False
4        False
5        False
6        False
7        False
8        False
9        False
10       False
11       False
12       False
13       False
14       False
15       False
16       False
17       False
18       False
19        True
20       False
21       False
22       False
23       False
24       False
25       False
26       False
27       False
28       False
29       False

There are 27000 values.


Solution

  • I'm not sure what the behavior of geopandas is when it's handed two GeoSeries of unequal size (?).

    You probably want:

    sb4326.geometry.map(lambda building: any(sg4326.intersects(building))
    

    This will test each building against the entire set of tracts one at a time, and return True if any of the intersections are True.

    Note that this will run fairly slowly. There are some tricks that can be used to make it run faster (which QGIS probably uses), but this is a starting point.