1

Let's say I have a GeoPackage file with 5000 lines. Each line with its own MULTIPOLYGON geometry. I know I can use spatial functions with it and generate a new file as the following:

ogr2ogr -f GPKG -sql "select st_buffer(geom,5) from database"  bufferedOutput.gpkg input.gpkg

In this example, the file bufferedOutput.gpkg is the result of my query in a new file... However, instead of generating a single file, I'd like to generate a different files for each geometry, something like getting the ogc_fid of my table and creating output files like 1.gpkg, 2.gpkg and so on.

Is there any way of doing it with ogr2ogr in a single line of code or would the only way be first splitting my original file into multiple files and then creating a second new file with the query that I want to make?

2
  • 2
    You'd have to add a bit more code. You would need to read the list of ids from the table, then loop through each id, running the ogr2ogr command and adding the id to the sql query in the where clause "where id = %id% "
    – jbalk
    Commented Dec 23, 2021 at 22:20
  • 2
    You could use ogrinfo to get the id list from the table
    – jbalk
    Commented Dec 23, 2021 at 22:29

1 Answer 1

3

I ended up writing a bash script for solving this problem as suggested in the comments. I'm not sure if that's the best way of solving the problem. But it's functional:

INPUT_FILE='input.gpkg'
OUTPUT_FOLDER='outputFolder'

if ! [ -d "$OUTPUT_FOLDER" ]; then mkdir -p "$OUTPUT_FOLDER" ; fi

layerName="$(ogrinfo -al "$INPUT_FILE" | grep 'OGRFeature' | sed 's/[0-9]*$//g' | uniq | sed 's/^.*(//g' | sed 's/).*$//g' | tr '\n' ' ')"
list="$(ogrinfo -al "$INPUT_FILE" | grep 'ogc_fid' | vim -e -c ':normal ggdd' -c ':g/./normal df=x' '+%p' -escq! /dev/stdin)"

for id in $list; do
    ogr2ogr -f GPKG -sql "select st_buffer(geom,5) from $layerName where qc_id = '$id'"  "$OUTPUT_FOLDER/$id.gpkg" "$INPUT_FILE"
done

Besides using ogr2ogr, I've also used Vim in a pipe to filter the ids that I needed.

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.