What is the most direct and straightforward, hopefully interactive, method to conduct a For each item Do... loop, where the input list is a selection of polygons?

A word about "interactive". I'm not opposed to a script solution, but it's not the ideal. The basic idea is that the whole process be as dynamic and immediate as possible. Point and click at some stuff, do something with it, point and click at the results, do something else, and so on for an hour or so. Then save the final results and forget about everything in the middle, it was all adhoc and doesn't need to be repeated exactly.

Software at hand is ArcGIS 10 (with 3D and Spatial Analyst), Arcinfo Workstation, and QGIS 1.7 (though don't feel like a solution from a different product is off-topic). In pseudo code the idea is:

for each selected poly in "index_layer" do:
   clip "raster_layer" by poly geometry to "d:\out\raster_poly[$ID].tif"

In the following image the selected set is outlined in heavy black and with halo text.

selected polygons over shaded relief

  • I had a hard time selecting a tag for this. Feel free to expand/replace. Jun 29, 2011 at 21:31
  • Could we assume the user has already selected index polygons with out-of-the-box tools? Jun 29, 2011 at 21:54
  • 2
    Going along with what @Kirk asked, if it's a known feature layer with a selection on it, you could write a Python script that does "stuff" with that selection, make it into a script tool with no parameters and drag it to a toolbar. In ArcGIS 10 you can put script tools on toolbars, and as long as they have no parameters they will execute immediately with no dialog asking for parameters. You could also write an Add-In to do something like this. It really is more the "do stuff" part that dictates the exact implementation.
    – blah238
    Jun 29, 2011 at 22:42
  • @kirk, yes we can assume the selection is already in place, whether by pointing and clicking or select-by-attributes or some other means. Jun 29, 2011 at 23:55
  • Along with what blah238 suggested, you can do this with Model Builder including iterating and substituting variables for unique output file names.
    – Sean
    Jun 30, 2011 at 2:00

3 Answers 3


Here's how to do it Model Builder in ArcGIS 10. This will take the selected features of VectorTiles and clip the InputRaster by each selected feature saving it to the Default.gdb with the TileID as part of the new raster name.

Iterate Feature Selection is added from the Insert menu. TileVectors is the Input Features and the group field is a unique tile id field. Clip is added from ArcToolbox. The blue oval are layers from the map document.

Note the variable substitution syntax in the final output. "output_%TileID%" where TileID is being substituted from the Input Feature Selection group variable.

After saving the model, the user would use the usual selection tools on TileVectors and run the model. You can get fancier by adding parameters for the inputs or output locations as needed.

enter image description here

  • thanks Sean. This route is not as direct as one might wish (I'm pretty good at wishing!) but it did get the job done. Jun 30, 2011 at 17:33
  • @matt wilkie, you can add the model to a toolbar to make it as simple as select, click, done. Here's an ESRI forum thread with the procedure to add it to a toolbar forums.arcgis.com/threads/…
    – Sean
    Jul 1, 2011 at 2:13
  • thanks Sean, I'm sure to use that toolbar tip! It's only useful after one has built the model though, and in that respect is little different from script. The "not as direct" comment is pointed more to the initial part of the process. For example if I want to make thumbnails of a directory of rasters I'll drop to command prompt and for %a in (*.tif) do gdal_translate -outsize 10% 10% %a previews\%a and be done with it in few seconds. The same could be done by creating a batch file and then running it, but that's extra steps. I was hoping an analogous system was available inside the GIS. Jul 1, 2011 at 2:39
  • @matt wilkie, yeah, what you want to do is a slightly more involved than creating thumbnails. But scripting it in a reusable way will get you what you want. Keep an eye on this GDAL ticket, though, trac.osgeo.org/gdal/ticket/1599
    – Sean
    Jul 1, 2011 at 13:01

If you have ArcInfo workstation, there is a module called Librarian, which has an interesting command called VISIT. I'm not suggesting that you actually use Librarian, but consider looking at what VISIT does. It's been many years, and I can't find any online documentation for it, but as I recall VISIT allows you to loop through a selected set of tiles (index polygons) and run some arbitrary aml.

The thing to notice about the Visit command is that it lets you focus on writing the aml that gets run for each tile and not the mechanics of visiting tiles.
visitor pattern

I can't remember if the Visit command follows the Visitor Pattern to the letter, but it is conceptually similar. A script that performs the Clip would be an example of a concrete visitor in this case, while an object containing the tile polygon would be the ConcreteElement. Your requirement to "do something with it" implies you want an abstraction layer. The visitor pattern provides this. It would allow you to present a UI to the user that allows them to pick from a list of somethings (concrete visitors), e.g. Clip, Dissolve etc.

Implementing this pattern takes more initial effort, but as new requirements arise, it will pay off: new visitors can be written without having to re-invent the visitation logic.

  • thank you for reminding me about Librarian! In years past I spent many many hours constructing and using Arcinfo libraries, and visit was a blessing. In this case I ended up building a model as per Sean's answer. It would have been faster to use the library though as I actually have one already built with this index. Doh! Jun 30, 2011 at 17:26
  • Don't let anyone know you use Librarian - they might start calling you a paleogeographer :) Jul 1, 2011 at 16:04

If you are familiar with Python, you could use Qgis Python console with something like that:

#get a pointer to active layer
layer=iface.activeLayer ()

#get a copy of selected features
features=layer.selectedFeatures ()
#create a new feature and a new geometry to hold the output
newgeometry = QgsGeometry()
# loop
for feature in features:
    #do something with geometry (see Qgis API: http://doc.qgis.org/head/classQgsGeometry.html)
    #example that combine all geometries:
# geometry->feature->layer
  • +1 for command line option. ArcGIS 10 also has a Python console but QGIS has a better API, in my opinion.
    – Sean
    Jun 30, 2011 at 12:58
  • I needed to change iface to qgis.utils.iface (maybe that's new/specific to qgis 1.7?), but that's as far as I could get as the "QgsGeometry Class Reference" is over my head. It does answer the nub of the Q though, thanks Pablo. Jun 30, 2011 at 17:18
  • I wrote an example in the answer that may help. regards.
    – Pablo
    Jun 30, 2011 at 17:40

Your Answer

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

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