Select to view content in your preferred language

Help with using definition query

654
4
02-20-2012 11:26 PM
ClaireParsons1
Emerging Contributor
Hi All
I'd like some help using definition query with layer.

Basically, I want to clip a raster several times by using polygons within a feature class. My idea was to use Searchcursor to loop through each row - set the definitionquery for the featureclass, clip the raster, delete the definitionquery then repeat.
Would this work?  Has anyone got any other ideas?

thanks
Claire
Tags (2)
0 Kudos
4 Replies
DuncanHornby
MVP Notable Contributor
Claire,

Instead of writing a Python script, why not use model builder? You could have a feature iterator that spews out a polygon one for each row (or even grouped by a case field) and this simply feeds into a clip raster tool as the clipping polygon?

Duncan
0 Kudos
ClaireParsons1
Emerging Contributor
Thanks for your reply - model builder is a good idea but it is part of a bigger script, so would prefer to have it in Python.
I've managed to sort it now and the definitionquery works!!:D
0 Kudos
JeffBarrette
Esri Regular Contributor
Have you tried the layer object via arcpy.mapping?

http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#/Layer/00s300000008000000/

Once you reference a layer in a map document you can modify its query definition, for example:

mxd = arcpy.mapping.MapDocument(r"path/to/map.mxd")
lyr = arcpy.mapping.ListLayers(mxd, "some layer name")[0]
lyr.definitionQuery = "[query] = 'here'"

#Do you raster clip here


I'm not clear on your requirements but you may not even need to use a SearchCursor.  Simply change the def query in some sort of loop.

Jeff
0 Kudos
ChrisSnyder
Honored Contributor
I didn't test this code, but it should work something like:

searchRows = arcpy.SearchCursor(myFC)
for searchRow in searchRow:
    myIdValue = searchRow.MY_FIELD_NAME
    outRaster = r"C:\temp\test_" + str(myIdValue)
    arcpy.MakeFeatureLayer_management(myFC, "fl", "MY_FIELD_NAME = " + str(myIdValue))
    arcpy.Clip_management(inputRaster, "", outRaster, "fl", "", "ClippingGeometry")
0 Kudos