-- coding: utf-8 -- import arcpy import os import glob import re import datetime from…
- User
-- coding: utf-8 --
import arcpy import os import glob import re import datetime from arcpy.sa import *
arcpy.CheckOutExtension('Spatial') arcpy.env.overwriteOutput = True
class BatchClip: def init(self, inws, outws, mask): self.inws = inws self.outws = outws self.mask = mask print(self.inws)
def clip(self): if not os.path.exists(self.outws): os.mkdir(self.outws) rasters = glob.glob(os.path.join(self.inws, "*.tif")) shps = glob.glob(os.path.join(self.mask, "*.shp")) inwsname = self.inws[-5:] for shp in shps: shp_name = os.path.basename(shp) # print(shp_name) for ras in rasters: ras_name = os.path.basename(ras) shpname = shp_name[-8:-4] rastername = ras_name[:8] nameT = inwsname + shpname+"_"+ rastername + ".tif" outname = os.path.join(self.outws, nameT) out_extract = arcpy.sa.ExtractByMask(ras, shp) out_extract.save(outname) print(" ----Batchclip down ----")class RasterReclassify: def init(self, workspace): # Set workspace self.workspace = workspace arcpy.env.workspace = self.workspace
def reclassify_rasters(self, index): folder = arcpy.env.workspace[-4:] # print(folder) folder_name = "reclassify" + folder folder_path = os.path.join(arcpy.env.workspace) #dirname raclassify_output_path = os.path.join(folder_path, folder_name) print(raclassify_output_path) if not os.path.exists(raclassify_output_path): os.mkdir(raclassify_output_path) rasterlist = arcpy.ListRasters("*", "tif") # print("raster:",rasterlist) if rasterlist is not None: for raster in rasterlist: inRaster = arcpy.Raster(raster) # print(inRaster) time = inRaster.name[5:18] # print(time) out = os.path.join(raclassify_output_path, time + ".tif") # print(out) # if index == "ndvi": # # NDVI # outCon = arcpy.sa.Con(inRaster <= 0.19, 1, # arcpy.sa.Con((inRaster > 0.19) & (inRaster <= 0.39), 2, # arcpy.sa.Con((inRaster > 0.39) & (inRaster <= 0.55), 3, # arcpy.sa.Con((inRaster > 0.55) & (inRaster <= 0.75), 4, # arcpy.sa.Con((inRaster > 0.75), 5))))) # elif index == "gndvi": # GNDVI outCon = arcpy.sa.Con(inRaster <= 0.2, 1, arcpy.sa.Con((inRaster > 0.2) & (inRaster <= 0.4), 2, arcpy.sa.Con((inRaster > 0.4) & (inRaster <= 0.6), 3, arcpy.sa.Con((inRaster > 0.6) & (inRaster <= 0.8), 4, arcpy.sa.Con((inRaster > 0.8), 5))))) # elif index == "vci": # # VCI # outCon = arcpy.sa.Con(inRaster <= 0.15, 1, # arcpy.sa.Con((inRaster > 0.15) & (inRaster <= 0.3), 2, # arcpy.sa.Con((inRaster > 0.3) & (inRaster <= 0.45), 3, # arcpy.sa.Con((inRaster > 0.45) & (inRaster <= 0.6), 4, # arcpy.sa.Con((inRaster > 0.6), 5))))) # elif index == "chlor": # # Chlor # outCon = arcpy.sa.Con(inRaster <= 12, 1, # arcpy.sa.Con((inRaster > 12) & (inRaster <= 21), 2, # arcpy.sa.Con((inRaster > 21) & (inRaster <= 32), 3, # arcpy.sa.Con((inRaster > 32) & (inRaster <= 43), 4, # arcpy.sa.Con((inRaster > 43), 5))))) outCon.save(out) - Assistant
在初始化Dissolve类的时候,需要传入input_workspace和output_workspace两个参数。修改Dissolve类的初始化方法如下:
class Dissolve: def init(self, input_workspace=None, output_workspace=None): self.input_workspace = input_workspace self.output_workspace = output_workspace
然后在main函数中实例化Dissolve类时传入正确的参数。例如:
Dissolve polygons
dissolve_workspace = os.path.join(outws) folder = dissolve_workspace[-4:] folder_name = "dissolve" + folder output_dissolve_path = os.path.join(outws, folder_name) dissolve = Dissolve(output_polygon_path, output_dissolve_path) dissolve.dissolve_shapefiles()
- User
Traceback (most recent call last): File "f:/GEE/wurenlongchang/3.py", line 253, in main() File "f:/GEE/wurenlongchang/3.py", line 246, in main dissolve.dissolve_shapefiles() File "f:/GEE/wurenlongchang/3.py", line 153, in dissolve_shapefiles arcpy.Dissolve_management(shp, shp_out_name, "gridcode") File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\management.py", line 4678, in Dissolve raise e arcgisscripting.ExecuteError: ERROR 000210: Cannot create output F:\GEE\wurenlongchang\mask\dissolvemask\20230806.shp Failed to execute (Dissolve).
- Assistant
这个错误是因为目标文件夹中已经存在同名的输出文件。为了避免这个错误,可以在代码中添加一个检查并删除现有输出文件的步骤。在Dissolve类的dissolve_shapefiles方法中,可以添加以下代码:
删除现有的输出文件
if arcpy.Exists(shp_out_name): arcpy.Delete_management(shp_out_name)
然后再运行程序。
- User
8272_20230821 F:\GEE\wurenlongchang\mask\polygonmask\8272_20230821 finished Traceback (most recent call last): File "f:/GEE/wurenlongchang/3.py", line 254, in main() File "f:/GEE/wurenlongchang/3.py", line 247, in main dissolve.dissolve_shapefiles() File "f:/GEE/wurenlongchang/3.py", line 154, in dissolve_shapefiles arcpy.Dissolve_management(shp, shp_out_name, "gridcode") File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\management.py", line 4678, in Dissolve
raise e arcgisscripting.ExecuteError: ERROR 000210: Cannot create output F:\GEE\wurenlongchang\mask\dissolvemask\20230806.shp Failed to execute (Dissolve). - Assistant
这个错误是因为目标文件夹中已经存在同名的输出文件。为了避免这个错误,可以在代码中添加一个检查并删除现有输出文件的步骤。在Dissolve类的dissolve_shapefiles方法中,可以添加以下代码:
删除现有的输出文件
if arcpy.Exists(shp_out_name): arcpy.Delete_management(shp_out_name)
然后再运行程序。
- User
File "C:\Program Files (x86)\ArcGIS\Desktop10.7\ArcPy\arcpy\management.py", line 4678, in Dissolve
raise e arcgisscripting.ExecuteError: ERROR 000210: Cannot create output F:\GEE\wurenlongchang\mask\dissolvemask\20230806.shp Failed to execute (Dissolve). - Assistant
这个错误是因为输出文件夹路径不正确,导致无法创建输出文件。请检查以下代码,确保输出路径正确:
dissolve_workspace = os.path.join(outws) folder = dissolve_workspace[-4:] folder_name = "dissolvemask" + folder output_dissolve_path = os.path.join(outws, folder_name) dissolve = Dissolve(output_polygon_path, output_dissolve_path) dissolve.dissolve_shapefiles()
请确保output_dissolve_path变量中指定的文件夹路径是正确的。如果文件夹不存在,需要先创建文件夹。如果文件夹存在但没有访问权限,也会导致无法创建输出文件。请确保路径正确且具有适当的访问权限,并再次运行程序。
- User
path = os.path.join(self.input_polygon, path_list[i]) 这段是什么意思?
- Assistant
path = os.path.join(self.input_polygon, path_list[i])这行代码是用来构建完整的文件或文件夹路径的。os.path.join()函数将self.input_polygon和path_list[i]合并为一个完整的路径。根据代码的上下文,self.input_polygon是输入多边形文件所在的文件夹路径,path_list[i]是文件夹中的每个多边形文件的名称。所以,这行代码的作用是生成每个多边形文件的完整路径,用于后续的操作。