编程语言
首页 > 编程语言> > python程序调用arcgis生成反距离权重插值图

python程序调用arcgis生成反距离权重插值图

作者:互联网

一、导入插值区域

二、编写插值图调用python程序

# Import arcpy module
import arcpy
from arcpy.sa import *
import random
import os
import shutil
from arcpy import *
import sys
arcpy.CheckOutExtension("Spatial")
arcpy.env.overwriteOutput = True

'''
# excel完整路径
wrytInExcel_ = arcpy.GetParameterAsText(0)
# 中间数据的输出路径
tempOutFolder_ = arcpy.GetParameterAsText(1)
# 结果图片存放路径
resultPicPath_ = arcpy.GetParameterAsText(2)

行政区划

idwXzqh_ = arcpy.GetParameterAsText(3)
# 处理的网格大小 汾阳用0.0005 全国可以用0.008
Cellsize_ = arcpy.GetParameterAsText(4)
'''
'''
# 污染云图excel完整路径
wrytInExcel_ = sys.argv[1]
# 中间数据的输出路径
tempOutFolder_ = sys.argv[2]
# 结果图片存放路径
resultPicPath_ = sys.argv[3]

行政区划

idwXzqh_ = sys.argv[4]
# 处理的网格大小 汾阳用0.0005 全国可以用0.008
Cellsize_ = sys.argv[5]

'''

wrytInExcel_ = sys.argv[2]

tempOutFolder_ = sys.argv[1] + "/wrytgy/wryttool/wrytempresult"

resultPicPath_ = sys.argv[1] + "/wrytgy/wrytresultly"

20200106 改成范围边界

idwXzqh_ = sys.argv[1] + "/wrylaishanqu/wryttool/wrytempresult/sourcedata.gdb/gy"

wrytInExcel_ = "D:/gis/jingcheng/wrytexcel/202007291.xls"

wrytInExcel_ = sys.argv[2]
tempOutFolder_ = "D:/gis/hukou/wryttool/wrytempresult"
resultPicPath_ = "D:/gis/hukou/wrytresultly"

20200106 改成范围边界

idwXzqh_ = "D:/gis/hukou/wryttool/wrytempresult/sourcedata.gdb/hukou"
Cellsize_ = "0.0005"

def wrytCalcTool(wrytInExcel, tempOutFolder, resultPicPath, idwXzqh, Cellsize):
#设置环境变量
#范围
arcpy.env.workspace = tempOutFolder + "/wryttable.gdb"
arcpy.env.mask = idwXzqh
arcpy.env.extent = idwXzqh
tempGdbPath = tempOutFolder + "/wryttable.gdb"
tempTifPath = tempOutFolder + "/temptif"
tempTifPath1 = tempOutFolder + "/temptif"
try:
if os.path.isfile(wrytInExcel):
curTime = os.path.basename(wrytInExcel).split(".")[0]
#创建结果图片的文件夹
curDay = curTime[0:8]
resultPicPathCreate = resultPicPath + "/" + curDay
isExists = os.path.exists(resultPicPathCreate)
if not isExists:
os.makedirs(resultPicPathCreate)
#实时图片路径
currentPicPath = resultPicPath + "/current"
if not os.path.exists(currentPicPath):
os.makedirs(currentPicPath)
#获取行政区划的extent 9.22 10.82
xzqhExtent = arcpy.Extent(111.934148115, 36.067486125, 113.61960204, 35.18735391)
#第一步: 将excel转化为table
#当前数据的小时
wrytInTable = tempGdbPath + "/wrytTable"
arcpy.ExcelToTable_conversion(wrytInExcel, wrytInTable, Sheet="sheet1")
arcpy.AddMessage("污染云图excel转换成了table")
#第二步: 将table转化为图层
#根据table中的X和Y转成Layer,如果XY为经纬度,直接用下述方法,否则则进行投影转换
x_coords = "x"
y_coords = "y"
spRef = arcpy.SpatialReference(4326)
#20191227 增加
outspRef = arcpy.SpatialReference(3857)
wryOutLayer = "wrytLayer" + str(random.randint(0, 1000))
arcpy.MakeXYEventLayer_management(wrytInTable, x_coords, y_coords, wryOutLayer, spRef)
wrytPoint = tempGdbPath + "/wrytPoint"
#点转投影坐标
#arcpy.Project_management(wryOutLayer, wrytPoint, outspRef)
#arcpy.CopyFeatures_management(wryOutLayer, wrytPoint)
arcpy.AddMessage("chazhi图table转换成了点")
#第三步: IDW插值 需要给7个指标插值
idwFields = ['aqi','so2','pm25','pm10','no2','co','o3']
#idwFields = ['aqi','pm25','pm10','no2']
#idwFields = ['aqi']
for idwField in idwFields:
Power = "2"
Search_radius = "VARIABLE 12"
#tif 命名 aqi2019112810.tif
rasterName = idwField + curTime +".tif"
rasterName1 = idwField + ".tif"
wrytIdwRasterFile = os.path.join(tempTifPath,rasterName1)
wrytIdwRasterFile1 = os.path.join(tempTifPath1,rasterName1)
#需要把每个指标的零值过滤掉
exp = idwField + ">0"
arcpy.SelectLayerByAttribute_management(wryOutLayer, "NEW_SELECTION", exp)
if int(arcpy.GetCount_management(wryOutLayer).getOutput(0)) > 2:
#arcpy.gp.Idw_sa(wryOutLayer, idwField, wrytIdwRasterFile, Cellsize, Power, Search_radius, "")
arcpy.gp.Idw_sa(wryOutLayer, idwField, wrytIdwRasterFile1, Cellsize, Power, Search_radius, "")
arcpy.AddMessage(idwField + "IDW插值完成")
#第四步: 替换mxd数据源并出图 模板名称需要划分区域和城市
mxdPath = tempOutFolder + "/wryttemplately/"+ idwField+".mxd"
mxd = arcpy.mapping.MapDocument(mxdPath)
lyr=arcpy.mapping.ListLayers(mxd)[0]
arcpy.AddMessage(tempTifPath + "tif路径")
arcpy.AddMessage(rasterName + "tif名")
lyr.replaceDataSource(tempTifPath,"RASTER_WORKSPACE",rasterName1)
layerExtent = lyr.getExtent()
mxd.save()
outputPngFileName = idwField + '{}.png'.format(curTime)
OutputPngFile = os.path.join(resultPicPathCreate, outputPngFileName)
#df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
df = arcpy.mapping.ListDataFrames(mxd, "图层")[0]
#df.extent = xzqhExtent
arcpy.mapping.ExportToPNG(mxd, OutputPngFile,
df_export_width=1919,
df_export_height=1003,
resolution = 350,
world_file= False,
color_mode="24-BIT_TRUE_COLOR",
background_color="255, 255, 255",
transparent_color="255, 255, 255")
del mxd
arcpy.AddMessage(outputPngFileName + "图片生成完成")
# 复制图片到current目录 D:\EnvironmentApplication\zjn\wryt\wrytresult\current
if os.path.exists(OutputPngFile):
currentPicFile = os.path.join(currentPicPath,idwField +".png")
shutil.copyfile(OutputPngFile, currentPicFile)
arcpy.AddMessage(idwField + "图片生成完成")

except arcpy.ExecuteError:  
    arcpy.AddMessage(arcpy.GetMessages())  

wrytCalcTool(wrytInExcel_, tempOutFolder_, resultPicPath_, idwXzqh_, Cellsize_)

三、编写java程序生成Excel定时调用

springboot生成jar包自动运行

四、生成成果

五、系统调用

如果对您有帮忙,非常感谢您支持一下创造者的付出!

感谢支持技术分享, 请扫码点赞支持:

技术合作交流qq:2401315930

本文转自 https://blog.csdn.net/weixin_42496466/article/details/122375924?spm=1001.2014.3001.5502,如有侵权,请联系删除。

标签:python,arcpy,tempOutFolder,插值,argv,sys,arcgis,idwField,os
来源: https://www.cnblogs.com/hustshu/p/16282177.html