当前位置:   article > 正文

代码分享 | Arcgis批量栅格值提取至点(自定义工具箱或代码)_批量栅格转点

批量栅格转点

任务举例:文件夹有20年某区域影像,需要提取该区域某点或多点值。

数据准备:1.矢量点文件 2.需要处理的栅格影像(存在同一文件夹) 3.存储影像文件夹

方法1

工具准备ARCGIS
操作:自定义脚本工具

1.我的工具箱添加脚本文件(后缀为.py文件)。

2.注意存储相对路径记得打钩。

3.导入脚本文件。

4.脚本文件中的代码如下。

  1. # coding=gbk
  2. import arcpy
  3. from arcpy import env #定义env
  4. from arcpy.sa import *
  5. point = arcpy.GetParameterAsText(0) # 用于提取点的shp
  6. inputwork = arcpy.GetParameterAsText(1) # 输入的要提取点的数据
  7. workPath = arcpy.GetParameterAsText(2) # 输出的位置
  8. arcpy.env.workspace = inputwork
  9. rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
  10. for raster in rasters:
  11. outPath=workPath+ u"\\" + str(raster).replace('.tif','')+".shp"
  12. ExtractValuesToPoints(point,raster,outPath)

5.设置输入数据的一个属性,这个顺序对应代码中的0、1、2,非常重要,其中输入路径和输出数据是对应文件夹。

6.结果如下,分别输入要处理文件即可。

 也可直接用Python arcpy 环境或 ARCGIS内置代码编辑器 ,代码如下:

  1. # coding=gbk
  2. import arcpy
  3. from arcpy import env #定义env
  4. from arcpy.sa import *
  5. arcpy.CheckOutExtension("spatial")
  6. arcpy.gp.overwriteOutput=1
  7. arcpy.env.workspace = r"G:\PRE2007/" #环境
  8. outpath = r"G:\site\POINT_PRE2007/" #存储
  9. inMask = r"G:\site\site.shp" #需提取shp点
  10. rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
  11. for raster in rasters:
  12. outPath=outpath+ u"\\" + str(raster).replace('.tif','')+".shp"
  13. ExtractValuesToPoints(inMask,raster,outPath)
  14. print("All project is OK")

7.结果为一个文件夹下各年的点shp影像,提取点值还需对属性值进行导出处理。下列代码为批量shp 属性表导出为csv文件:

  1. # coding:utf-8
  2. import os
  3. import arcpy
  4. from arcpy import env
  5. from arcpy.sa import *
  6. import string
  7. #矢量图像属性表导出csv
  8. arcpy.CheckOutExtension("spatial")
  9. arcpy.gp.overwriteOutput=1
  10. arcpy.env.workspace = r"G:\site\POINT_PRE2007/"
  11. featureclasses = arcpy.ListFeatureClasses () #遍历工作空间中的shp格式数据
  12. outpath = r"G:\site\POINT_PRE2007/"
  13. for fc in featureclasses:
  14. outputName = (str(fc).strip('.shp'))+ ".csv"
  15. table=arcpy.TableToTable_conversion(fc, outpath, outputName)
  16. print("All project is OK!")

方法2

任务举例:上述方法对于提取较少点的情况未免过于麻烦。假如是提取站点值,在站点过少而栅格数量较多的情况下需要进行提取点shp-栅格值导出-excel表整理三步。下述代码结合ArcMap值提取至点与属性表提取操作轻松实现批量处理。
操作:替换代码中的路径即可。

  1. # coding=gbk
  2. import arcpy
  3. from arcpy import env #定义env
  4. from arcpy.sa import *
  5. arcpy.CheckOutExtension("spatial")
  6. arcpy.gp.overwriteOutput=1
  7. arcpy.env.workspace = r"G:\PRE2007/"
  8. outpath = r"G:\site\1/"
  9. inMask = r"G:\site\site.shp"
  10. OutputFile = open(outpath+'PRE2007.txt', 'w')
  11. rasters = arcpy.ListRasters("*", "tif")#不加tiff()括号内为空默认所有类型栅格文件
  12. for raster in rasters:
  13. outPath=outpath+ u"\\" + str(raster).replace('.tif','')+".shp"
  14. ExtractValuesToPoints(inMask,raster,outPath)
  15. # 提取shp文件中的'FID', 'POINT_X', 'POINT_Y'字段
  16. shpfields = ['Lat', 'Lon', 'RASTERVALU']
  17. shp_Lat = []
  18. shp_Lon = []
  19. shp_RASTERVALU = []
  20. # 使用SearchCursor(搜索指针)遍历Row对象并提取字段值
  21. shprows = arcpy.SearchCursor(outPath, shpfields)
  22. # 使用WHILE进行迭代搜索游标,通过游标的next返回下一行
  23. # 当属性表中有多行时,这种遍历很关键,但对于点元素其实不用遍历也行
  24. while True:
  25. shprow = shprows.next()
  26. if not shprow:
  27. break
  28. shp_Lat.append(shprow.Lat) # append()方法用于在列表末尾添加新的对象
  29. shp_Lon.append(shprow.Lon)
  30. shp_RASTERVALU.append(shprow.RASTERVALU)
  31. OutputFile.write(str(raster).strip('.tif') + "," + str(shp_RASTERVALU).strip('[]') + '\n')
  32. # 将shp_RASTERVALU写入txt文件中,并换行
  33. print shp_RASTERVALU
  34. print("All project is OK")

提取点属性表如图所示:

 

生成的txt数据如下图所示:可直接导入excel表与站点真实测量值对比。

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/310870
推荐阅读
相关标签
  

闽ICP备14008679号