4

I am trying to clip geometries within a GPKG file using a bounding box. I write an L-shaped line to a file:

import geopandas as gpd
import shapely.geometry

l = shapely.geometry.LineString([[0,2],[0,-2],[2,-2]])
gpd.GeoSeries(l).to_file("test.gpkg",layer="line")

I try to clip my L-shaped line using ogr2ogr:

ogr2ogr \
    -spat -1 -1 1 1 \
    -clipsrc spat_extent \
    test_cut.gpkg test.gpkg

Despite using the spat_extent keyword, test_cut.gpkg ends up containing the same geometry as the original test.gpkg.

To visualize my aim, I created this plot:

import matplotlib.pyplot as plt
ax = gpd.GeoSeries(l).plot(color="red")
gpd.GeoSeries(shapely.geometry.box(-1,-1,1,1)).plot(ax=ax)

enter image description here

I would like test_cut.gpkg only to contain the line which is within the -1,-1,1,1 bounding box, ie the part of the red line which is within the blue square.

I also tried:

ogr2ogr \
    -spat -1 -1 1 1 \
    -clipsrc spat_extent \
    -clipdstlayer line \
    test_cut.gpkg test.gpkg

but result is the same, the L-shaped line fully makes it to test_cut.gpkg.

How can I use the spat_extent keyword to clip a GPKG layer?

1 Answer 1

4

It looks like there is a regression in GDAL 3.8 that breaks the clipping. I filed a bug report for it here: https://github.com/OSGeo/gdal/issues/9013

As a workaround, you can disable usage of the arrow API by specifying config option OGR2OGR_USE_ARROW_API=NO. Using ogr2ogr this should be possible like this:

ogr2ogr \
    --config OGR2OGR_USE_ARROW_API NO \
    -spat -1 -1 1 1 \
    -clipsrc spat_extent \
    test_cut.gpkg test.gpkg
3
  • Thanks! Is there a way to figure this out without being very familiar with the GDAL codebase? The only place I find OGR2OGR_USE_ARROW_API is github.com/OSGeo/gdal/blob/master/autotest/utilities/….
    – zabop
    Commented Jan 2 at 15:19
  • 2
    No, I'm afraid not. Normally the parameter also should never be used because the only difference should be a difference in performance. But, it is still a very new change and apparently there are still some bugs popping up :-(.
    – Pieter
    Commented Jan 2 at 15:41
  • 1
    The issue has already been fixed by the GDAL developers and has just been released in GDAL 3.8.3.
    – Pieter
    Commented Jan 8 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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