python矢量裁剪栅格代码_Python 批量 Arcgis 通过矢量数据裁剪栅格数据

网上已经有批量裁剪的源代码,但是运行问题有很多,经过改动,成功运行,希望对他人有所启发。如果有问题,欢迎留言,希望能够回复帮忙。以下是从网上找到的源码:

#-*- encoding:UTF-8 -*-

# Tool Name: 遍历要素裁剪栅格

# Source Name: extractByMask.py

# Version: ArcGIS 10.1

# Author: GIScloud

# Created: 2012/11/1

import arcpy

import string

from arcpy.sa import *

try:

raster = arcpy.GetParameterAsText(0) #要裁剪的栅格

clip_feat = arcpy.GetParameterAsText(1) #裁剪要素类

field = arcpy.GetParameterAsText(2) #命名字段

outworkspace = arcpy.GetParameterAsText(3) #命名字段裁剪后输出目录

for row in arcpy.SearchCursor(clip_feat):

mask=row.getValue("Shape")

outPath=outworkspace+"\\"+str(row.getValue(field))

outExtractByMask = ExtractByMask(raster,mask)

outExtractByMask.save(outPath)

except arcpy.ExecuteError:

print arcpy.GetMessages()

运行过程中发现有很多问题,总结如下:

1)报错0100240,不能将运行好的文件保存到某路径。解决办法,更改environment中的workspace。此外,看看你的属性字段是不是有空格,字符是不是超过了13个,是不是有特殊符号,例如’之类,都会引起报错,如果这些都检查没有问题,再看看导入到arcgis工具箱中的脚本是不是有缩进的问题,因为python是由缩进控制作用域,很可能引起代码不循环,不判断等等。更改后代码如下:

#-*- encoding:UTF-8 -*-

# Tool Name: maskproperty

# Source Name: extractByMask.py

# Version: ArcGIS 10.1

# Author: GIScloud

# Created: 2012/11/1

import sys

reload(sys)

sys.setdefaultencoding( "utf-8" )

import arcpy

from arcpy import env

import string

from arcpy.sa import *

arcpy.CheckOutExtension("spatial")

env.workspace = r"G:\image_bach\origal_data"#enveriment setting

try:

raster = arcpy.GetParameterAsText(0)                                       #input roster

clip_feat = arcpy.GetParameterAsText(1)                                  #input vector

field = arcpy.GetParameterAsText(2)                                         #file 字段

outworkspace = arcpy.GetParameterAsText(3)                         #outputdectory

for row in arcpy.SearchCursor(clip_feat):

mask=row.getValue("Shape")

outpart=str(row.getValue(field))

n=len(outpart)

if n <= 13:

outPath=outworkspace+"\\"+''.join(outpart.split())       #由于python的文件命名不能超过13个字符,判断是否超了

else:

noblankstr=''.join(outpart.split())                                  #由于命名字段可能存在空格,去掉空格

outPath=outworkspace+"\\"+noblankstr[0:13]             #如果字段的字符超过13个,截取前13个

outExtractByMask = ExtractByMask(raster,mask)

outExtractByMask.save(outPath)

except arcpy.ExecuteError:

print arcpy.GetMessages()

运行时候是将此工具配置到arcgis中自己新建的script中运行的。

运行结果:

此为运行结果,文件名是属性字段命名的,如果有空格,将空格去掉作为文件名。

生成文件夹,以属性字段命名,生成的格式是arcgis的栅格文件。

此为原创,转载请注明出处。

转载自:https://blog.csdn.net/qq_42237611/article/details/81204832

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值