I am trying to write a code in ARCpy that will batch process the PRISM Climate Data. As The PRISM data covers the whole US,I am needing to crop it to my required extent,that is texas boundary. When I am doing the cropping using the extract by mask tool manually,its cropping to my required boundary. But if I am doing it using the Arcpy,its croppimg to the outside boundary,not the inside. I am not sure why. How to fix this?Attaching both the manual & coding extract for example. Here is the code:
import os
# Set the workspace directory
workspace = r"C:UsersayantDocumentsprismtmp"
arcpy.env.workspace = workspace
# Set the shapefile path
shapefile = r"C:UsersayantDownloadsTexas_State_Boundary_9188475293773017791(1)State.shp"
# Set the output directory
output_dir = r"C:UsersayantDocumentsResearchoutput"
# Ensure the output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# List all .bil files in the workspace
bil_files = arcpy.ListRasters("*", "BIL")
# Check if there are any .bil files found
`if not bil_files:
print("No .bil files found in the workspace.")
else:
# Loop through each .bil file and crop it using the shapefile
for bil_file in bil_files:
# Print the file path to verify
full_path = os.path.join(workspace, bil_file)
print(f"Processing {full_path}...")
# Define the output file path
output_file = os.path.join(output_dir, os.path.basename(bil_file).replace('.bil', '_cropped.tif'))`
`try:
# Describe the raster to get its properties
desc = arcpy.Describe(full_path)
raster_sr = desc.spatialReference
cell_size = desc.meanCellWidth
# Reproject the shapefile to match the raster's spatial reference
reprojected_shapefile = os.path.join(workspace, "reprojected_shapefile.shp")
arcpy.management.Project(shapefile, reprojected_shapefile, raster_sr)
# Get the extent of the reprojected shapefile
shapefile_extent = arcpy.Describe(reprojected_shapefile).extent
extent_str = f"{shapefile_extent.XMin} {shapefile_extent.YMin} {shapefile_extent.XMax} {shapefile_extent.YMax}"`
` # Set environment settings to ensure the exact clipping extent
arcpy.env.extent = extent_str
arcpy.env.mask = reprojected_shapefile
arcpy.env.snapRaster = full_path
# Log the extent and mask settings for debugging
print(f"Shapefile extent: {extent_str}")
print(f"Mask: {arcpy.env.mask}")
print(f"Snap Raster: {arcpy.env.snapRaster}")
print(f"Processing Extent: {arcpy.env.extent}")
# Perform the extract by mask operation
print(f"Cropping {bil_file} with cell size {cell_size}...")
with arcpy.EnvManager(scratchWorkspace=workspace, workspace=workspace):
cropped_raster = arcpy.sa.ExtractByMask(
in_raster=full_path,
in_mask_data=reprojected_shapefile`
` )
cropped_raster.save(output_file)
print(f"Cropped {bil_file} and saved as {output_file}")
except arcpy.ExecuteError:
print(f"Failed to clip {bil_file}.")
print(arcpy.GetMessages())
except Exception as e:
print(f"An error occurred while processing {bil_file}: {e}")
print("Batch cropping completed.")```
Tried by setting the mask,snap raster & extent & its still the same.
Ayantika Bose is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.