-- 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.dirname(arcpy.env.workspace) raclassify_output_path = os.path.join(folder_path, folder_name) if not os.path.exists(raclassify_output_path): os.mkdir(raclassify_output_path) print(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
在main函数中,变量raclassify_output_path没有在converter的初始化中定义。你可以在main函数中定义这个变量并传递给RasterToPolygonConverter类的初始化方法。下面是修改后的代码示例:
def main(): # Set input and output paths inws = r"F:\GEE\wurenlongchang\tif\wurenlongchang_GDVI" outws = r"F:\GEE\wurenlongchang\mask" mask = r"F:\GEE\wurenlongchang\1_shp" # Batch clip rasters batch_clip = BatchClip(inws, outws, mask) batch_clip.clip() # Reclassify rasters reclassify_workspace = os.path.join(outws) rr = RasterReclassify(reclassify_workspace) rr.reclassify_rasters("ndvi") # rr.reclassify_rasters("gndvi") # # rr.reclassify_rasters("vci") # rr.reclassify_rasters("chlor") # Convert rasters to polygons polygon_workspace = os.path.join(outws) folder = polygon_workspace[-4:] folder_name = "polygon" + folder output_polygon_path = os.path.join(outws, folder_name) raclassify_output_path = os.path.join(outws, "reclassify" + folder) # 定义raclassify_output_path变量 converter = RasterToPolygonConverter(raclassify_output_path, output_polygon_path) converter.convert_raster_to_polygon() # 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() dissolve.dissolve_shapefiles(reclassify_workspace, output_dissolve_path) # Calculate fields field_calculator = FieldCalculator(output_dissolve_path) field_calculator.calculate_fields()