Arcpy | 套合差代码

1.计算河网套合差代码

import arcpy
import os
from arcpy.sa import *
def GetFiles(D,Path):
    F = []
    files = os.listdir(Path)
    for i in range(len(files)):
        suffix = files[i].split(".")[-1] 
        if suffix == D:
            F.append(files[i])
    return F
def mkdir(path):
	folder = os.path.exists(path)
	if not folder:                   #判断是否存在文件夹如果不存在则创建为文件夹
		os.makedirs(path)  
def R_Difference(folder,originalfolder,threshold):
    dirfolder = folder + "\\" +"dir"
    accfolder = folder + "\\" +"acc" 
    streamfolder = folder + "\\" +"stream" + str(threshold)
    Sshpfolder = folder + "\\" +"S" + str(threshold)
    Mergefolder = folder + "\\" +"M" + str(threshold)
    Polygonfolder = folder + "\\" +"P" + str(threshold)

    Odirfolder = dirfolder.replace(folder,originalfolder)
    Oaccfolder = accfolder.replace(folder,originalfolder)
    Ostreamfolder = streamfolder.replace(folder,originalfolder)
    OSshpfolder = Sshpfolder.replace(folder,originalfolder)

    mkdir(accfolder)
    mkdir(streamfolder)
    mkdir(Sshpfolder)
    mkdir(Mergefolder)
    mkdir(Polygonfolder)
    mkdir(Oaccfolder)
    mkdir(Ostreamfolder)
    mkdir(OSshpfolder)

    dirFiles = GetFiles("tif",r"E:\超分test\demo\dir")
    Count = len(dirFiles)
    for i in range(Count):
        #fileName = dirFiles[i].split(".")[0]
        dir_temp = dirfolder+"\\"+ dirFiles[i]
        acc_temp = accfolder+"\\acc_"+ dirFiles[i]
        outFlowAccumulation = FlowAccumulation(dir_temp)
        outFlowAccumulation.save(acc_temp)

        Odir_temp = Odirfolder +"\\"+ dirFiles[i]
        Oacc_temp = Oaccfolder +"\\acc_"+ dirFiles[i]
        OoutFlowAccumulation = FlowAccumulation(Odir_temp)
        OoutFlowAccumulation.save(Oacc_temp)

        inRaster = arcpy.Raster(acc_temp)
        stream_temp = streamfolder+"\\S_"+str(threshold)+"_"+ dirFiles[i]
        out_raster=arcpy.sa.Con(inRaster>threshold,1)
        out_raster.save(stream_temp)

        OinRaster = arcpy.Raster(Oacc_temp)
        Ostream_temp = Ostreamfolder+"\\S_"+ str(threshold) + "_" + dirFiles[i]
        Oout_raster=arcpy.sa.Con(OinRaster>threshold,1)
        Oout_raster.save(Ostream_temp)

        feature_temp = Sshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i]
        StreamToFeature(stream_temp, dir_temp, feature_temp, "NO_SIMPLIFY")

        Ofeature_temp = OSshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i]
        StreamToFeature(Ostream_temp, dir_temp, feature_temp, "NO_SIMPLIFY")

        feature_temp = Sshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        StreamToFeature(stream_temp, dir_temp, feature_temp, "NO_SIMPLIFY")

        Ofeature_temp = OSshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        StreamToFeature(Ostream_temp, Odir_temp, Ofeature_temp, "NO_SIMPLIFY")

        arcpy.management.AddField(feature_temp, "Type", "TEXT", "", "", "20")
        arcpy.management.CalculateField(feature_temp, "Type", "\"method\"", "PYTHON3")
        arcpy.management.AddField(Ofeature_temp, "Type", "TEXT", "", "", "20")
        arcpy.management.CalculateField(Ofeature_temp, "Type","\"origin\"", "PYTHON3")

        outshp = Mergefolder + "\\M_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.Merge([feature_temp,Ofeature_temp], outshp)

        outpolygon = Polygonfolder + "\\P_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.FeatureToPolygon(outshp,outpolygon)

2.流域边界套合差

def R_Difference_W(folder,originalfolder,threshold):
    dirfolder = folder + "\\" +"dir"
    Sshpfolder = folder + "\\" +"S" + str(threshold)

    Odirfolder = dirfolder.replace(folder,originalfolder)
    OSshpfolder = Sshpfolder.replace(folder,originalfolder)

    wfolder = folder + "\\" +"w_" + str(threshold)
    Owfolder = wfolder.replace(folder,originalfolder)

    wpofolder = folder + "\\" +"wpo_" + str(threshold)
    Owpofolder = wpofolder.replace(folder,originalfolder)

    wlinefolder = folder + "\\" +"wl_" + str(threshold)
    Owlinefolder = wlinefolder.replace(folder,originalfolder)

    MergeWfolder = folder + "\\" +"M_wl_" + str(threshold)

    PolygonWfolder = folder + "\\" +"P_w_" + str(threshold)

    mkdir(wfolder)
    mkdir(Owfolder)
    mkdir(wpofolder )
    mkdir(Owpofolder )
    mkdir(wlinefolder)
    mkdir(Owlinefolder)
    mkdir(MergeWfolder)
    mkdir(PolygonWfolder)

    dirFiles = GetFiles("tif", dirfolder)
    Count = len(dirFiles)

    for i in range(Count):
        dir_temp = dirfolder+"\\"+ dirFiles[i]
        feature_temp = Sshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        watershed_temp = wfolder + "\\W_" + str(threshold) + "_" + dirFiles[i]
        outWatershed = Watershed(dir_temp, feature_temp, "FID")
        outWatershed.save(watershed_temp)

        Odir_temp = Odirfolder +"\\"+ dirFiles[i]
        Ofeature_temp = OSshpfolder + "\\Shp_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        Owatershed_temp = Owfolder + "\\W_" + str(threshold) + "_" + dirFiles[i]
        outWatershed = Watershed(Odir_temp, Ofeature_temp, "FID")
        outWatershed.save(Owatershed_temp)

        w_polygon_temp = wpofolder +"\\Wpo_"+ dirFiles[i].split(".")[0] + ".shp"
        arcpy.RasterToPolygon_conversion(watershed_temp, w_polygon_temp, "NO_SIMPLIFY", "VALUE")

        Ow_polygon_temp = Owpofolder +"\\Wpo_"+ dirFiles[i].split(".")[0] + ".shp"
        arcpy.RasterToPolygon_conversion(Owatershed_temp, Ow_polygon_temp, "NO_SIMPLIFY", "VALUE")

        wline_temp = wlinefolder + "\\Wl_"+ dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.PolygonToLine(w_polygon_temp, wline_temp, "IDENTIFY_NEIGHBORS")

        Owline_temp = Owlinefolder + "\\Wl_"+ dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.PolygonToLine(Ow_polygon_temp, Owline_temp, "IDENTIFY_NEIGHBORS")

        arcpy.management.AddField(wline_temp, "Type", "TEXT", "", "", "20")
        arcpy.management.CalculateField(wline_temp, "Type", "\"method\"", "PYTHON3")
        arcpy.management.AddField(Owline_temp, "Type", "TEXT", "", "", "20")
        arcpy.management.CalculateField(Owline_temp, "Type","\"origin\"", "PYTHON3")

        outshp = MergeWfolder + "\\Mw_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.Merge([wline_temp,Owline_temp], outshp)

        outpolygon = PolygonWfolder + "\\PW_" + str(threshold) + "_" + dirFiles[i].split(".")[0] + ".shp"
        arcpy.management.FeatureToPolygon(outshp,outpolygon)
threshold = 2000 
folder = r"E:\超分test\Interpolation"
originalfolder = r"E:\超分test\data"
R_Difference_W(folder,originalfolder,threshold)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值