简体   繁体   中英

Geopandas overlay intersection gives incorrect result

I want to intersect two geodataframes (polygons) using gpd.overlay. When I intersect in QGIS the resulting shapefile is correct (I'm intersecting red ellipse and yellow shapefile, result is green in image below).

When I intersect using geopandas, the resulting geodataframe is empty. Here is the code:

import geopandas as gpd
gdf1 = gpd.read_file('... /gdf1.shp')   
gdf2 = gpd.read_file('... /gdf2.shp')   
gdf_inter = gpd.overlay(gdf1, gdf2, how='intersection')

> len(gdf_inter)
> 0

Link to shapefiles: https://www.dropbox.com/s/6e7frbev71phkpy/SHP_TEST.7z?dl=0

Image:图片

I imagine it could be related to the projection? Ideas? Thanks in advance,

Here's what I would try:


import geopandas as gpd
gdf1 = gpd.read_file('... /gdf1.shp')   
gdf2 = gpd.read_file('... /gdf2.shp').to_crs(gdf1.crs)
gdf_inter = gpd.overlay(gdf1, gdf2, how='intersection')

Thank you both. The crs was the same in both files. I set the keep_geom_type parameter to False, which solved the issue (returned a Geometrycollection comprised of a Linestring and polygon).

I had not tried this before as the information of the website was confusing: "In default settings, overlay returns only geometries of the same geometry type as df1 (left one) has, where Polygon and MultiPolygon is considered as a same type (other types likewise).[...]". I'm intersecting two polygons yet not receiving a polygon in return.

Anyway, it is solved now. Thank you so much.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM