Last active
August 15, 2023 19:59
-
-
Save maptastik/616e4c05c962d263dae7 to your computer and use it in GitHub Desktop.
Clip a raster to the extent of another raster using arcpy and convert it to a TIFF
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Load arcpy | |
import arcpy | |
# Set workspace | |
arcpy.env.workspace = "<some filepath for your working environment>" | |
scratch = "<some scratch location for intermediate files>" | |
# Specify the data you'll be clipping and the area you want to clip that data by | |
myClip = "<location of raster to be clipped>" | |
clipArea = "<location of raster to clip by>" | |
# Get the extent of the clip area | |
# http://help.arcgis.com/En/Arcgisdesktop/10.0/Help/index.html#//0017000000m7000000 | |
# Values have to be converted to strings so they can be concatentated and used later | |
# https://docs.python.org/2/library/functions.html#str | |
clipAreaTop = str(arcpy.GetRasterProperties_management(clipArea, "TOP")) | |
clipAreaLeft = str(arcpy.GetRasterProperties_management(clipArea, "LEFT")) | |
clipAreaRight = str(arcpy.GetRasterProperties_management(clipArea, "RIGHT")) | |
clipAreaBottom = str(arcpy.GetRasterProperties_management(clipArea, "BOTTOM")) | |
# Concatentated stringified extent values | |
# http://www.pythonforbeginners.com/concatenation/string-concatenation-and-formatting-in-python | |
clipAreaExtent = clipAreaLeft + " " + clipAreaBottom + " " + clipAreaRight + " " + clipAreaTop | |
# Clip your raster by the extent of the clipAreaExtent derived from another raster | |
# http://resources.arcgis.com/EN/HELP/MAIN/10.1/index.html#//00170000009n000000 | |
print("clipping") | |
myClipScratch = arcpy.Clip_management(myClip, clipAreaExtent, scratch + "<file name>", "#", "#", "NONE") | |
# arcpy saves the output to an ESRI Grid format. | |
# You may want to work with a more common format. Here we convert the previous output to a TIFF | |
# http://help.arcgis.com/EN/arcgisdesktop/10.0/help/index.html#//001200000032000000 | |
print("converting") | |
arcpy.RasterToOtherFormat_conversion(aerialClipScratch, "<file path to folder wher you want to save output>", "TIFF") |
Would the Clip_management now be replaced with management.Clip?
From:
myClipScratch = arcpy.Clip_management(myClip, clipAreaExtent, scratch + "", "#", "#", "NONE")
To:
myClipScratch = arcpy.management.Clip(myClip, clipAreaExtent, scratch + "", "#", "#", "NONE")
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if I want to clip a set of multiple raster images saved in one folder using another set of multiple raster images in another folder. Now using the images with same dates I want to clip one image from the other. How can I acheive the clip results using arcpy.
For example the images to be clipped are by the names (solar_radiation_2017-10-04.tif) saved in one folder, the images to be used to clip the extent are by the name (udm2_Mosaic_20171014.tif) saved in another folder. The dates are same but the format is different can I still obatin the results I want?
Thank you for any suggestions.