ogr2ogrgeopackage

Does ogr2ogr support selecting from gpkg using shp polygons as filter?


I have two input sources:

  1. region.shp. Geometry type MultiPolygon. Currently contains 3 regions.

  2. roads.gpkg. One layer, "road" with geometry field named "geom" with type LineString.

I want to create a new gpkg with all the roads that are within any of areas in region.shp. Preferably using ogr2ogr in a shell script.

I have found examples online that shows how to do this with two shape files. But I cant figure out how to do it with two dissimilar formats like gpkg and shp. Will I need to convert to the same format before doing any spatial operations/queries involving both data sources?


Solution

  • I reproduced your input creating a Geopackage file named "roads.gpkg" with only one Linestring layer named "road" with a couple of streets downloaded from OpenStreetMap. Also created a shapefile named "region.shp" with a one multipart polygon which has two parts and another singlepart polygon, which one of them representing surroudings of parks within a city downtown.

    According to the ogr2ogr's documentation, you can clip your input file using another input file as a filter with this command:

    ogr2ogr -nlt LINESTRING -clipsrc region.shp dst.gpkg roads.gpkg
    

    The file outputted by the command is shown in the image below and I think it fits what you needed.

    Snapshot of three geospatial data layers. The base layer is represented by roads in a city. The second layer is a filter composed by a set of green polygons representing greens areas such as parks. The last layer contains red lines that were outputted by the command that tells ogr2ogr that the the roads layer must be clipped by the polygons layers.