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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Apr 24, 2014 at 19:48

1 Answer 1

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)
answered Apr 24, 2014 at 20:12
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.