Hello everbody.
I'm facing a particular situation. Maybe you can help me.
I have 2 separete folders, One contains some vectors and the other contains some rasters. I just want to clip the rasters using the vector layers, (clip the raster_1 using the vector_1). I've tried to use model builder, but it's not possible to use two interator in the same model. Then I've tried to make to separete models (each one has an iterator and then embed one of them as a sub-model in the another model), but it's not working either. Maybe I have to use arcpy and try to solve it using a Python script. Does anyone have a Idea of how could I solve this particular situation? Thanks
Solved! Go to Solution.
This code is made to work on shapefiles, so I wanted to ensure that is what you had. You can try adding this as a Script tool and running it. It should do what you need. There is some code to split your file names based on the delimiter "_". This may need to change if your files are not structured "raster_1" and "vector_1".
# Import modules
import arcpy
from arcpy import env
import os
# Allow output to be overwritten
arcpy.env.overwriteOutput = True
# Assign raster folder to a parameter
rasterFolder = arcpy.GetParameterAsText(0)
# Assign vector folder to a parameter
vectorFolder = arcpy.GetParameterAsText(1)
# Set environment to raster folder
arcpy.env.workspace = rasterFolder
# Iterate through rasters in folder
for r in arcpy.ListRasters():
# Assign path of raster to variable
rPath = os.path.join(rasterFolder,str(r))
# Get value after the delimiter '_'
rNumber = r.partition('_')[2]
# Set environment to raster folder
arcpy.env.workspace = vectorFolder
# Iterate through vectors in folder
for v in arcpy.ListFiles():
vDesc = arcpy.Describe(v)
if vDesc.extension == "shp":
# Get value after the delimiter '_'
vSplit = v.partition('_')[2]
# Get value before the delimiter '.'
vNumber = vSplit.partition('.')[0]
# Compare raster number to vector number
if rNumber == vNumber:
# Clip raster to vector
arcpy.Clip_management(rPath,"#",os.path.join(rasterFolder,"outputRaster_"+str(rNumber)),v,"0","ClippingGeometry")
# Set environment back to raster folder
arcpy.env.workspace = rasterFolder
I'm guessing that the problem is your model doesn't contain any way to select the same number raster as vector (ie. vector1= raster1). You need to find some way to output a value from one iterator that will be used in the other iterator.
Hello, thanks for the answer.
Exactly, I'm trying to figure out a way to call the right mask for the right raster. Don't know how to procede
I don't use Model Builder much, but I think when people are in this position and need to use more than one iterator, they break them out into separate models and call them from the original model. Although that's what you're having trouble with, I think it's the right path.
Maybe this will help?
Thanks Blake.
I think I'm the right way, but I also think that it's a limitation of Model Builder, Maybe using Python I can solve the problem, just trying to figure out how....
Hi Ismar,
Are the vector files shapefiles or are they stored in a geodatabase?
Hi Jennifer, the vectors are all shapefiles. Do you think that could make some difference?
Thanks
This code is made to work on shapefiles, so I wanted to ensure that is what you had. You can try adding this as a Script tool and running it. It should do what you need. There is some code to split your file names based on the delimiter "_". This may need to change if your files are not structured "raster_1" and "vector_1".
# Import modules
import arcpy
from arcpy import env
import os
# Allow output to be overwritten
arcpy.env.overwriteOutput = True
# Assign raster folder to a parameter
rasterFolder = arcpy.GetParameterAsText(0)
# Assign vector folder to a parameter
vectorFolder = arcpy.GetParameterAsText(1)
# Set environment to raster folder
arcpy.env.workspace = rasterFolder
# Iterate through rasters in folder
for r in arcpy.ListRasters():
# Assign path of raster to variable
rPath = os.path.join(rasterFolder,str(r))
# Get value after the delimiter '_'
rNumber = r.partition('_')[2]
# Set environment to raster folder
arcpy.env.workspace = vectorFolder
# Iterate through vectors in folder
for v in arcpy.ListFiles():
vDesc = arcpy.Describe(v)
if vDesc.extension == "shp":
# Get value after the delimiter '_'
vSplit = v.partition('_')[2]
# Get value before the delimiter '.'
vNumber = vSplit.partition('.')[0]
# Compare raster number to vector number
if rNumber == vNumber:
# Clip raster to vector
arcpy.Clip_management(rPath,"#",os.path.join(rasterFolder,"outputRaster_"+str(rNumber)),v,"0","ClippingGeometry")
# Set environment back to raster folder
arcpy.env.workspace = rasterFolder
Thanks Jennifer, I think it 'going to work
I'll just have do make some adaptations, because the file names I have are more complex then just "raster_1" mostly of times ("2328706_2015_04_09_RE2_3A_302090", for example). In this case, I think I'll have to find a way to get the whole file name.
Thank you very much, now I'm sure I'm in the right way.
One way you can make this work with Modelbuilder is to make 3 models:
1. A model with Iterator #1
2. A model with Iterator #2
3. A model that links the two. You then run this one, which then runs the first and second models.
Here's an example (somewhat messy, I admit, but it shows the basics). The Iterator models are the ones called "Submodels". Note - use a Precondition to trigger the second model after the first completes.
Chris Donohue, GISP