2

I am using ogr2ogr to convert my huge geodatabase file (mygdb.gdb) into multiple shapefiles using one of the attributes (myattr). I have 100s of unique values for this attributes, so I am gonna do this on a loop. Based on the answer here, I created a list of these values in a text file (myattr.txt). I am going to do the following

mystring=$(cat myattr.txt)

ogr2ogr -f "ESRI Shapefile" -sql "SELECT * FROM my_layer WHERE "myattr" IN ($mystring)" myoutput mygdb.gdb

My question is how to dynamically name the output shape files i.e. what to write for myoutput? The linked answer suggets /output. I was wondering how does the dynamic naming of the output shapefiles works without any dynamic variable in this argument?

3
  • 2
    you should use a for loop, so you get one name at a time and use it for each shapefile.
    – Gery
    Feb 10, 2022 at 23:52
  • 1
    Could you please provide an example? I haven't used a for loop in bash before. Thanks.
    – rm167
    Feb 11, 2022 at 0:03
  • you will find a bunch of different examples in Google, try some of them and check if they work for you. Post your results and we will give some advices if necessary.
    – Gery
    Feb 11, 2022 at 1:30

1 Answer 1

5

Assuming you have a textfile myattr.txt with all your uniques values (one line per value).

You have to iterate over each line and create a Shapefile for each unique value :

cat myattr.txt | while read line 
do
   echo "$line"
   ogr2ogr -f "ESRI Shapefile" -sql "SELECT * FROM my_layer WHERE "myattr" ='$line'" $line.shp mygdb.gdb
done

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.