[gdal-dev] Appending to a single feature class in a gdb via ogr2ogr

Eli Adam eadam at co.lincoln.or.us
Tue May 12 12:09:04 PDT 2020


Hi Maggie,

On Mon, Apr 27, 2020 at 9:03 AM Maggie Wooten <mwoo391 at gmail.com> wrote:
>
> First, I apologize if I am using this mailing list improperly. This is my first time here.

Asking questions is an appropriate use of the list.

>
> I am trying to iterate over some shapefiles (109 shapefiles to be exact) and append them to a *single* feature class in an output geodatabase, but the resulting output geodatabase is one with 100 feature classes that looks like this:
>

Do all the shapefiles have (exactly) the same fields?  If not, it
probably won't work.

> INFO: Open of `output.gdb' using driver `FileGDB' successful.
> 1: output (Multi Polygon)
> 2: output_1 (Multi Polygon)
> 3: output_1_2 (Multi Polygon)
> 4: output_1_2_3 (Multi Polygon)
> 5: output_1_2_3_4 (Multi Polygon)
> .....
> 99: output_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_25_26_27_28_29_30_31_32_33_34_35_36_37_38_39_40_41_42_43_44_45_98 (Multi Polygon)
> 100: output_1_2_3_4_5_6_7_8_9_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_25_26_27_28_29_30_31_32_33_34_35_36_37_38_39_40_41_42_43_44_45_99 (Multi Polygon)
>
>
> These are the commands I'm running in the iteration:
>
> if i == 0: "ogr2ogr -nln output -a_srs EPSG:4326 -t_srs EPSG:4326 -f "FileGDB" output.gdb input1.shp"
>
> else: "ogr2ogr -nln output -a_srs EPSG:4326 -t_srs EPSG:4326 -update -append -f "FileGDB" output.gdb inputX.shp"
>
>
> What am I doing wrong here? How can I get the desired result? I tried a bunch of different things and flags and still could not get it all to append to one feature class. Any advice would be greatly appreciated and I'd be happy to provide whatever information may be necessary.
>
I'm not quite sure what is going wrong there.  I didn't try with an
output of FileGDB but did something similar all with shapefiles.  The
final resulting combined shapefile could probably be manually
ogr2ogr'ed into a FileGDB.

I did this on Ubuntu using Bash.  If you're using a different shell,
you will need different command syntax.  Specifically variables,
filenames without extensions, quoting, escape characters and other
things all need adjustment.  Complicated filenames with spaces or
other symbols can make this more difficult too.

mkdir merged

This is to make an "empty" shapefile with the correct fields and types
and an additional field named s_file to put the source file name in
later:
ogr2ogr -sql "select *, '' as s_file from
Surveys_Invalid_ManualFix_7of7 where SURVEYID='fake'"
merged/surveys_v1.shp Surveys_Invalid_ManualFix_7of7.shp -nln
surveys_v1

This is to check that my empty shapefile turned out okay:
ogrinfo merged/surveys_v1.shp -al

(I worked in a directory where I was selecting ALL the shapefiles, all
of which have the same fields):
FILES=*.shp

for f in $FILES; do g=${f%%.*}; ogr2ogr -sql "select *, '$g' as s_file
from $g" -update -append merged/surveys_v1.shp $f -nln surveys_v1;
done

This is to check that my shapefile is no longer empty and turned out okay:
ogrinfo merged/surveys_v1.shp -al

If you are using DOS on windows, here is a note (untested) from some
previous work.  I don't have windows readily available to test it (the
% might need to be %%):

for %f in (*.shp) do ogr2ogr -sql "select *, '%~nf' as s_file from
%~nf" -update -append merged\output.shp %f -nln output


Hope that helps.

Best regards, Eli

> Thank you!
> Ma
> _______________________________________________
> gdal-dev mailing list
> gdal-dev at lists.osgeo.org
> https://lists.osgeo.org/mailman/listinfo/gdal-dev


More information about the gdal-dev mailing list