赞
踩
任务举例:文件夹有20年某区域影像,需要提取该区域某点或多点值。
数据准备:1.矢量点文件 2.需要处理的栅格影像(存在同一文件夹) 3.存储影像文件夹
工具准备:ARCGIS
操作:自定义脚本工具
1.我的工具箱添加脚本文件(后缀为.py文件)。
2.注意存储相对路径记得打钩。
3.导入脚本文件。
4.脚本文件中的代码如下。
- # coding=gbk
- import arcpy
- from arcpy import env #定义env
- from arcpy.sa import *
- point = arcpy.GetParameterAsText(0) # 用于提取点的shp
-
- inputwork = arcpy.GetParameterAsText(1) # 输入的要提取点的数据
-
- workPath = arcpy.GetParameterAsText(2) # 输出的位置
-
- arcpy.env.workspace = inputwork
- rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
-
- for raster in rasters:
- outPath=workPath+ u"\\" + str(raster).replace('.tif','')+".shp"
- ExtractValuesToPoints(point,raster,outPath)
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
5.设置输入数据的一个属性,这个顺序对应代码中的0、1、2,非常重要,其中输入路径和输出数据是对应文件夹。
6.结果如下,分别输入要处理文件即可。
也可直接用Python arcpy 环境或 ARCGIS内置代码编辑器 ,代码如下:
- # coding=gbk
- import arcpy
- from arcpy import env #定义env
- from arcpy.sa import *
-
- arcpy.CheckOutExtension("spatial")
- arcpy.gp.overwriteOutput=1
-
- arcpy.env.workspace = r"G:\PRE2007/" #环境
- outpath = r"G:\site\POINT_PRE2007/" #存储
- inMask = r"G:\site\site.shp" #需提取shp点
-
- rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
- for raster in rasters:
- outPath=outpath+ u"\\" + str(raster).replace('.tif','')+".shp"
- ExtractValuesToPoints(inMask,raster,outPath)
-
- print("All project is OK")
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
7.结果为一个文件夹下各年的点shp影像,提取点值还需对属性值进行导出处理。下列代码为批量shp 属性表导出为csv文件:
- # coding:utf-8
- import os
- import arcpy
- from arcpy import env
- from arcpy.sa import *
- import string
- #矢量图像属性表导出csv
- arcpy.CheckOutExtension("spatial")
- arcpy.gp.overwriteOutput=1
-
- arcpy.env.workspace = r"G:\site\POINT_PRE2007/"
- featureclasses = arcpy.ListFeatureClasses () #遍历工作空间中的shp格式数据
-
- outpath = r"G:\site\POINT_PRE2007/"
- for fc in featureclasses:
- outputName = (str(fc).strip('.shp'))+ ".csv"
- table=arcpy.TableToTable_conversion(fc, outpath, outputName)
- print("All project is OK!")
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
任务举例:上述方法对于提取较少点的情况未免过于麻烦。假如是提取站点值,在站点过少而栅格数量较多的情况下需要进行提取点shp-栅格值导出-excel表整理三步。下述代码结合ArcMap值提取至点与属性表提取操作轻松实现批量处理。
操作:替换代码中的路径即可。
- # coding=gbk
- import arcpy
- from arcpy import env #定义env
- from arcpy.sa import *
-
- arcpy.CheckOutExtension("spatial")
- arcpy.gp.overwriteOutput=1
-
- arcpy.env.workspace = r"G:\PRE2007/"
- outpath = r"G:\site\1/"
- inMask = r"G:\site\site.shp"
- OutputFile = open(outpath+'PRE2007.txt', 'w')
-
- rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
- for raster in rasters:
- outPath=outpath+ u"\\" + str(raster).replace('.tif','')+".shp"
- ExtractValuesToPoints(inMask,raster,outPath)
- # 提取shp文件中的'FID', 'POINT_X', 'POINT_Y'字段
- shpfields = ['Lat', 'Lon', 'RASTERVALU']
- shp_Lat = []
- shp_Lon = []
- shp_RASTERVALU = []
- # 使用SearchCursor(搜索指针)遍历Row对象并提取字段值
- shprows = arcpy.SearchCursor(outPath, shpfields)
- # 使用WHILE进行迭代搜索游标,通过游标的next返回下一行
- # 当属性表中有多行时,这种遍历很关键,但对于点元素其实不用遍历也行
- while True:
- shprow = shprows.next()
- if not shprow:
- break
- shp_Lat.append(shprow.Lat) # append()方法用于在列表末尾添加新的对象
- shp_Lon.append(shprow.Lon)
- shp_RASTERVALU.append(shprow.RASTERVALU)
-
- OutputFile.write(str(raster).strip('.tif') + "," + str(shp_RASTERVALU).strip('[]') + '\n')
- # 将shp_RASTERVALU写入txt文件中,并换行
- print shp_RASTERVALU
- print("All project is OK")
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
提取点属性表如图所示:
生成的txt数据如下图所示:可直接导入excel表与站点真实测量值对比。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。