question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

BUG: Clip not return GeoDataFrame or GeoSeries

See original GitHub issue
  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of geopandas.

  • (optional) I have confirmed this bug exists on the master branch of geopandas.


Example code to reproduce the bug

import geopandas as gpd
from shapely.geometry import Polygon

GEOPACKAGE = r'C:\temp\test.gpkg'
crs = 31982

layer_name_mask = 'polygon_area'
layer_name_toclip = 'polygon_to_clip'


def create_polygons():
    polygon_data = Polygon([
        (581762.720, 7276585.620),
        (581762.720, 7186208.392),
        (490411.599, 7186208.392),
        (490411.599, 7276585.620)
    ])
    polygon_gpd = gpd.GeoDataFrame(geometry=[polygon_data])
    polygon_gpd.to_file(GEOPACKAGE, layer=layer_name_mask, crs=crs,
                        driver='GPKG')

    pol_to_clip_data = Polygon([
        (552576.412, 7214657.225),
        (552576.412, 7257625.935),
        (595472.170, 7257625.935),
        (595472.170, 7214657.225)
    ])
    pol_to_clip_gpd = gpd.GeoDataFrame(geometry=[pol_to_clip_data])
    pol_to_clip_gpd.to_file(GEOPACKAGE, layer=layer_name_toclip, crs=crs,
                            driver='GPKG')


if __name__ == '__main__':
    create_polygons()

    layer_mask = gpd.read_file(GEOPACKAGE, layer=layer_name_mask)
    layer_toclip = gpd.read_file(GEOPACKAGE, layer=layer_name_toclip)

    # Here not return data and/or any error
    results = gpd.clip(layer_toclip, layer_mask)
    print(results)
    results.to_file(GEOPACKAGE, layer='results', crs=crs, driver='GPKG')

Problem description

I was writing some tests and I found this problem. If execute the code above, the expected output was a polygon results between the layer_mask and layer_toclip.

For my real data the code works fine, but I don’t sure I will have problems for future data. I tested with shapefile format and I haven’t sucess too, but the both cases (from Geopackage and shapefile) I can success cliping in QGis software.

Expected Output

Output of geopandas.show_versions()

SYSTEM INFO

python : 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 20:34:20) [MSC v.1916 64 bit (AMD64)] executable : D:\Desenvolvimento\Python\vantia\backend\venv\Scripts\python.exe machine : Windows-10-10.0.17134-SP0

GEOS, GDAL, PROJ INFO

GEOS : None GEOS lib : None GDAL : 3.1.2 GDAL data dir: None PROJ : 7.0.1 PROJ data dir: D:\Desenvolvimento\Python\vantia\backend\venv\lib\site-packages\pyproj\proj_dir\share\proj

PYTHON DEPENDENCIES

geopandas : 0.8.1 pandas : 1.1.0 fiona : 1.8.13 numpy : 1.19.1 shapely : 1.7.0 rtree : 0.9.4 pyproj : 2.6.1.post1 matplotlib : None mapclassify: None geopy : None psycopg2 : None geoalchemy2: None pyarrow : None

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
martinfleiscommented, Nov 26, 2021

This seems to be fixed now, likely in rtree. The Colab notebook now works as expected even when executed second time. Feel free to reopen the issue if that is not the case.

0reactions
byandercommented, Oct 7, 2020

I investigated further and I think that the problem can be Rtree package, when configuring the spatial index.

I created an example on Google Colab (Link below).

When I execute the code in the first time the results from clip it’s ok, but, I execute in the second time the same code, the kernel crash.

https://colab.research.google.com/drive/1zK7nBQ9bfYD62M-gXYmeGfZAlWzuu1uE?usp=sharing

Read more comments on GitHub >

github_iconTop Results From Across the Web

Changelog — GeoPandas 0.12.2+0.gefcb367.dirty ...
Accessing the crs of a GeoDataFrame without active geometry column is deprecated and will be removed in GeoPandas 0.12 (#2373). Bug fixes: GeoSeries.to_frame ......
Read more >
geopandas.clip
If False, return all resulting geometries (potentially mixed-types). Returns. GeoDataFrame or GeoSeries. Vector data (points, lines, polygons) from gdf clipped ...
Read more >
Data Structures — GeoPandas 0.12.2+0.gefcb367.dirty ...
GeoDataFrame # ; gdf.geometry will always return the active geometry column, not the column named ; geometry . If you wish to call...
Read more >
GeoPandas .clip() on example from the docs throws TypeError
This was a problem with geopandas versions. It turns out that geopandas.GeoDataFrame.clip() did not work the same way in v0.9.0.
Read more >
geopandas/geopandas: v0.8.0 | Zenodo
geopandas/geopandas: v0.8.0 · Any column of the GeoDataFrame that has a "geometry" dtype is now returned as a GeoSeries. · Multiple geometry ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found