使用bash脚本将文件夹中的所有dbfs附加到第一个dbf

时间:2013-04-30 21:22:52

标签: bash gis shapefile dbf ogr

我尝试将文件夹中的所有dbfs追加到第一个dbf。 dbfs是我想要附加到一个文件中的ESRI shapefile的一部分。我得到了一个正常工作的代码,但我想我所做的是非常笨拙的(我是一个绝对的bash新手)...而且我省略了第一个文件,我的计数器在循环结束时计算一个过多的文件并产生错误.. 附加由ogr2ogr(GDAL / OGR库)

完成
mydir=C:/Users/Kay/Desktop/Test_GIS/new/
cd $mydir

dbfs=(*.dbf)                                # put dir to array

let i=1                                     # start with 1 omitting 1st file with index 0

for f in *.dbf 
  do       
  echo appending file ${dbfs[i]} to ${dbfs[0]}
  ogr2ogr -append ${dbfs[0]} ${dbfs[i]}
  let i=i+1                                 # counter + 1
done

2 个答案:

答案 0 :(得分:1)

版本A:您明确指定要追加的dbf

append_to="fff.dbf"
find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "^$append_to$" | xargs -0 -n1 -I % echo ogr2ogr -append "$append_to" "%"

变体B:附加到第1个dbf(第1个ls)

append_to=$(ls -1 *.dbf | head -1)
find . -maxdepth 1 -name \*.dbf -print0 | grep -zv "^$append_to$" | xargs -0 -n1 -I % echo ogr2ogr -append "$append_to" "%"

两者现在处于“干运行”模式 - 仅显示将要做的事情。满意后从xargs中删除echo。两个版本的第二行相同。

纯粹的bash

IFS=$'\t\n'       #don't need this line when your filenames doesn't contain spaces
declare -a dbfs=(*.dbf)
unset $IFS        #don't need this line when your filenames doesn't contain spaces
append_to=${dbfs[0]}
unset dbfs[0]
for dbf in ${dbfs[@]}
do
        echo ogr2ogr -append "$append_to" "$dbf"
done

答案 1 :(得分:1)

记录

如果使用ogr2​​ogr附加shape文件的dbfs,实际上事情要容易得多。如果传递一个未存在的shp-filename,它会动态创建一个空的shape文件并将数据附加到它。所以,这就足够了:

# working directory with shp-files to be appended into one file
mydir=D:/GIS_DataBase/CorineLC/shps_extracted
cd $mydir

# directory where final shp-file will be saved
mydir_final=D:/GIS_DataBase/CorineLC/shps_app_and_extr
mkdir $mydir_final

# get dbfs, which are the actual files to which append the data to
declare -a dbfs=(*.dbf)

# loop through dbfs in dir and append all to the dbf of shp-file
# extr_and_app.shp that will be created by ogr2ogr on the fly 
# and saved to {mydir_final}
for dbf in ${dbfs[@]}; do
  echo appending $dbf to $mydir_final/extr_and_app.dbf
  ogr2ogr -append $mydir_final/extr_and_app.dbf $dbf
done
相关问题