用shell脚本导入landuse数据到postgis

全国的土地利用数据,80年代末,1995和2000年3期数据,ARCINFO COVERAGE格式,打算直接转入POSTGIS库中。
写了一个shell文件,花费了2天时间。主要是shell的东西是边看边写的,特别是变量处理,还真是麻烦。
运行shell文件要先进入到landuse目录,postgis里应该已经有westdc库,并且已经
进行了POSTGIS初始化,导入过landuse表,并且已经增加了areacode和year字段,不能为NULL,且默认为空值。
从avcbin格式到postgis库,因为数据表的关系,作了两次转换。提出了ARCINFO中的cov#和cov-id字段。

#!/bin/sh
# cd /opt/to_reback/data/landuse
tmpdata=./tmpdata
YEAR=”80年代末 1995年 2000年”
for a in $YEAR; do
for b in $a/*; do #province, 目录判断
if [ -d $b ]; then
rm -fdr $tmpdata
for c in $b/ld*; do # areacode, 县级区域
bb=`echo $b | sed “s/\///g”` # 去除/特殊字符,否则sed罢工
areacode=`echo $c | sed “s/\///g” | sed “s/$bb//g” | sed “s/ld//g”` #主要时间都花费在这儿了
ogr2ogr $tmpdata $c
ogr2ogr $tmpdata/poly.shp $tmpdata/PAL.shp -select area,perimeter,r_area,ln71,ln72,ln73,ln74,aftln,tm20,tm30,tm40,tm50,tm60,afttm,ph41,ph52,ph53,net
shp2pgsql -a -g the_geom -i $tmpdata/poly.shp landuse | psql -d westdc
psql -d westdc -c “update landuse set areacode=’$areacode’ where areacode=””
psql -d westdc -c “update landuse set year=’$a’ where year=””
# exit
done
fi
done
done

This entry was posted in Linux, PostGIS and tagged , , . Bookmark the permalink.

4 Responses to 用shell脚本导入landuse数据到postgis

  1. wlx [微博 ] says:

    run了一下午,出现错误了。
    把脚本重新修改了一下,适应性更强。

    #!/bin/sh
    # cd /opt/to_reback/data/landuse
    #set -x
    d1=./tmpdata1
    d2=./tmpdata2
    YEAR=”80年代末 1995年 2000年”
    for a in $YEAR; do
    for b in $a/*; do #province, 目录判断
    if [ -d $b ]; then
    rm -fdr $d1
    rm -fdr $d2
    for c in $b/ld*; do # areacode, 县级区域
    bb=`echo $b | sed “s/\///g”` # 去除/特殊字符,否则sed罢工
    areacode=`echo $c | sed “s/\///g” | sed “s/$bb//g” | sed “s/ld//g”`
    echo $c
    ogr2ogr $d1 $c && ogr2ogr $d2 $d1/PAL.shp -f “ESRI Shapefile” -select area,perimeter,r_area,ln71,ln72,ln73,ln74,aftln,tm20,tm30,tm40,tm50,tm60,afttm,ph41,ph52,ph53,net || exit
    shp2pgsql -a -g the_geom -i $d2/PAL.shp landuse | psql -d westdc || exit
    psql -d westdc -c “update landuse set areacode=’$areacode’ where areacode=”” || exit
    psql -d westdc -c “update landuse set year=’$a’ where year=”” || exit
    # exit
    done
    fi
    done
    done
    #set +x

  2. wlx [微博 ] says:

    太慢了,不知道是什么原因。
    再修改一下:

    #!/bin/sh
    # cd /opt/to_reback/data/landuse
    #set -x
    d1=./tmpdata1
    d2=./tmpdata2
    YEAR=”80年代末 1995年 2000年”
    for a in $YEAR; do
    for b in $a/*; do #province, 目录判断
    if [ -d $b ]; then
    rm -fdr $d1
    rm -fdr $d2
    for c in $b/ld*; do # areacode, 县级区域
    bb=`echo $b | sed “s/\///g”` # 去除/特殊字符,否则sed罢工
    areacode=`echo $c | sed “s/\///g” | sed “s/$bb//g” | sed “s/ld//g”`
    echo $c
    ogr2ogr $d1 $c && ogr2ogr $d2 $d1/PAL.shp -f “ESRI Shapefile” -select area,perimeter,r_area,ln71,ln72,ln73,ln74,aftln,tm20,tm30,tm40,tm50,tm60,afttm,ph41,ph52,ph53,net || exit
    shp2pgsql -a -g the_geom -i $d2/PAL.shp landuse | psql -d westdc || exit
    psql -d westdc -c “update landuse set areacode=’$areacode’ where areacode=”” || exit
    # exit
    done
    fi
    done
    psql -d westdc -c “update landuse set year=’$a’ where year=”” || exit
    done
    #set +x

    或许,应该再设置一个临时文件,记录已经处理的文件(目录名),然后可以随时中断/继续?

  3. wlx [微博 ] says:

    或许,vacuumdb westdc命令应该插入在每次更新之后?

  4. Pingback: GIS RS Linux

发表评论

电子邮件地址不会被公开。 必填项已用 * 标注

*

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Please copy the string 9cGb6D to the field below:

以新浪微博帐号登录