5

I'm trying to mask a group of raster files and save the new rasters in a different folder using the same name of the original files.

It doesn't sound as a difficult task, but I'm having trouble saving-naming each masked raster inside the "for" loop. Here is what I have so far:

import arcpy
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/rasters/threshold"
mask = "C:/GIS/mesoamerica.tif"

rasterlist = arcpy.ListDatasets("*", "Raster")
    for i in rasterlist:
        outExtractByMask = ExtractByMask(i, mask)
        outExtractByMask.save("C:/SIG/MelasCA_30runs_avg/threshold/mesoamerica/" i)

I need to find a way to use the "i" variable in the outExtractByMask.save. Any idea?

8

Here is one way to accomplish the task. The general idea here is to create a full output file path using os.path.join() and input that into the .save command

import arcpy, os
from arcpy import env
from arcpy.sa import *

env.workspace = "C:/rasters/threshold"
outws = "C:/SIG/MelasCA_30runs_avg/threshold/mesoamerica"
mask = "C:/GIS/mesoamerica.tif"

rasterlist = arcpy.ListDatasets("*", "Raster")
for i in rasterlist:
    outExtractByMask = ExtractByMask(i, mask)
    outname = os.path.join(outws, str(i)) # Create the full out path
    outExtractByMask.save(outname)
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.