-- coding: utf-8 -- import arcpy import os import glob import re import datetime…
- User
-- coding: utf-8 --
import arcpy import os import glob import re import datetime
arcpy.CheckOutExtension('Spatial')
class BatchClip: def init(self, inws, outws, mask): self.inws = inws self.outws = outws self.mask = mask
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")) print(rasters,shps) for shp in shps: for ras in rasters: nameT = os.path.basename(ras) + ".tif" outname = os.path.join(self.outws, nameT) out_extract = arcpy.sa.ExtractByMask(ras, shp) out_extract.save(outname) print(os.path.basename(ras) + " ---- down") print(" ----Batchclip down ----")class RasterReclassify: def init(self, workspace): # Set workspace arcpy.env.workspace = workspace
def reclassify_rasters(self, index): folder = arcpy.env.workspace[-4:] folder_name = "reclassify" + folder folder_path = os.path.dirname(arcpy.env.workspace) output_path = os.path.join(folder_path, folder_name) if not os.path.exists(output_path): os.mkdir(output_path) print(output_path) rasterlist = arcpy.ListRasters("", "tif") if rasterlist is not None: for raster in rasterlist: inRaster = arcpy.Raster(raster) print(inRaster) time = inRaster.name[:8] print(time) out = os.path.join(output_path, time + "_" + index + ".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
If the files cannot be read, there are a few steps you can take to troubleshoot the issue:
-
Check if the file paths are correct: Make sure that the input and output file paths are correct and that the files actually exist in the specified locations.
-
Check file permissions: Ensure that you have the necessary permissions to read and write files in the specified locations. If not, try running the script as an administrator or changing the file permissions.
-
Check file formats: Verify that the files you are trying to read are in the appropriate format supported by arcpy. For example, arcpy can read raster files in formats such as .tif, .img, etc., and shapefiles in .shp format.
-
Check for file locks: Sometimes, files can be locked by other applications or processes, preventing them from being read. Make sure that no other application is currently using the files you are trying to read.
-
Check for errors: Check the error messages or traceback to see if any specific error is being raised that might provide insights into why the files cannot be read. This can help in troubleshooting the issue more effectively.
-