从草gis导出多个光栅

时间:2015-08-11 04:41:21

标签: export gis tile grass

我使用r.tile(http://grass.osgeo.org/grass71/manuals/r.tile.html)从巨大的地理位置创建平铺。现在我想将每个磁贴导出到一个文件,但还没有找到一个简单的方法: 使用r.out.gdal我一次只能导出一个瓦片,这使得它无法使用,因为我有100个瓦片......

我还没有找到任何其他解决方案......任何想法?

2 个答案:

答案 0 :(得分:2)

您只需使用某种脚本语言在for循环中调用 r.out.gdal

这是Bash的一个例子。这是一个简单但完整的脚本。它期望栅格地图名称作为命令行参数,如果参数不存在,则失败并显示使用建议。

#!/usr/bin/env bash

if [ $# -eq 0 ]
then
    >&2 echo "No arguments supplied"
    >&2 echo "Usage: $0 raster1 raster2 ..."
fi

for RASTER in "$@"
do
    r.out.gdal input=$RASTER output=$RASTER.tiff format=GTiff
done

要运行它,首先必须将其设置为可执行文件(chmod u+x export.sh),然后才能在命令行中运行它:

./export.sh tile1 tile2

这是Python的一个例子,它更适用于更大的任务。代码与Bash脚本完全相同。此外,它使用GRASS Python Scripting Library来调用 r.in.gdal 模块,print函数与Python 3向前兼容。

#!/usr/bin/env python

from __future__ import print_function
import sys
import grass.script as gscript

if len(sys.argv) == 1:
    print("No arguments supplied", file=sys.stderr)
    print("Usage: {} raster1 raster2 ...".format(sys.argv[0]), file=sys.stderr)

for raster in sys.argv[1:]:
    gscript.run_command('r.out.gdal', input=raster,
                        output=raster + '.tiff', format='GTiff')

同样,要运行它,首先必须将其设置为可执行文件(chmod u+x export.py),然后可以在命令行中运行它:

./export.py tile1 tile2

这两个示例都假设您使用的是Linux,Mac OS X或类似的东西,并在GRASS GIS中的系统命令行中运行该脚本。在MS Windows上,您应该使用Python版本并运行它,例如从GRASS GIS GUI到 File 菜单中的 Launch script 项目。图层管理器​​

答案 1 :(得分:0)

The above answers require you to input the name of each raster layer. If you want to automate the whole process you can use the following Python script. This script first creates a list of all rasters that are available to grass, it then exports each as a Tiff file individually. To run it, either copy and paste the code into the GRASS python shell (accessible as a tab on the GRASS GUI layer manager window) or, save it as a .py file and, again from the Python shell, use the command execfile("foo.py").

import grass.script as grass

# create list of all rasters

rastlist=grass.read_command("g.list",type="rast")
rastlist1= rastlist.split(':', 1)[1]
rastlist2= rastlist1.split('-', 1)[0]
RastListClean=rastlist2.split()

# export all rasters

for raster in RastListClean:
    grass.run_command('r.out.gdal', input=raster,
                        output=raster + '.tiff', format='GTiff')