Select to view content in your preferred language

looping through rasters in an mxd

2806
12
Jump to solution
01-13-2013 05:16 PM
RosieBell
Emerging Contributor
Hi all, newbie question here. I'd like to use the Python window to write a piece of code which loops through all the rasters in an mxd, and creates a polygon of the outline of each raster.

The Raster Domain tool in the 3D analyst extension works well for this task. However, when I try and use the code snippet in the Python window, I encounter the following problem. When I go to type in the In Raster ("dtm_grd") in the example below, I get a drop down list of all the rasters in my mxd. If I choose one from this list, I then get an error message that the raster "does not exist or is not supported". However, if I change the environment to the folder where the raster is currently located, and copy the source of the raster from the Properties box, the tool executes successfully.

Is there a way of looping through these layers, without having to manually change the address for each one? I know (very roughly) how to loop through a list of layers in python, but there doesn't seem much point trying if I can't get the Python snippet to work on even one layer.

nb not using the Raster to Polygon tool as most of the rasters are floating point and would need to be converted to integer before this tool would work.

arcpy.CheckOutExtension("3D") env.workspace = "C:/data" arcpy.RasterDomain_3d("dtm_grd", "raster_domain.shp", "POLYGON")
Tags (2)
0 Kudos
12 Replies
RosieBell
Emerging Contributor
I added this to the beginning of my code to  disable M and Z values:

#disable M and Z values in output
arcpy.env.outputZflag = "Disabled"
arcpy.env.outputMflag = "Disabled"


unfortunately it didn't affect the size.

I also tried using the tool manually and disabling everything I could think of in the environments - again, didn't make any difference.

Saving it as LINE rather than POLYGON did the trick though! Thanks Wayne 🙂 Now I wonder if I should bother trying to convert this line to polygon through my script, or if it's not necessary.
0 Kudos
T__WayneWhitley
Honored Contributor
Well, depends what you need it for.  I would say now that you have it in line form, you can experiment with some of the generalization tools....because if you don't need vertices so 'densely packed', you can make some adjustments now, saving time in the conversion to polygon process - should you decide you need polygons.

Don't know how well this will work, but it may be worth looking into:

Simplify Line (Cartography)
Desktop » Geoprocessing » Tool reference » Cartography toolbox
http://resources.arcgis.com/en/help/main/10.1/index.html#/Simplify_Line/007000000010000000/

Just curious, but did you know you could export that extent raster polygon fc to a table and use that result in ArcMap as a 'legacy' raster catalog?  In other words, in table form, ArcMap reads and recognizes the IMAGE, XMIN, YMIN, XMAX, YMAX format as a catalog and will auto-load your image tiles (in the view extent - and this is adjustable).  I find that valuable sometimes, esp. when sharing multiple images without preprocessing a mosaic, etc.  That is, 10.0 still 'recognizes' it; I don't know about 10.1, but I wouldn't be surprised if it still is supported.

Enjoy,
Wayne
0 Kudos
RosieBell
Emerging Contributor
Ah, I'm with you now. I hadn't realised that my polygon (and line) had so many vertices - about 50,000! Simplifying brought it down to under 20, much more manageable. I think I do need the polygons so have gone back to using POLYGON in the Raster Domain tool, and then simplifying.

This is my script to date (just in case anyone is trying something similar). It's a bit clumsy but it works. I included Delete_management to get rid of my large polygons, once I had a simplified version.

#Script to loop through rasters in an mxd, and print a shape file of their extent (exact boundary, not rectangle).

import arcpy
from arcpy import env

#disable M and Z values in output
arcpy.env.outputZflag = "Disabled"
arcpy.env.outputMflag = "Disabled"

arcpy.CheckOutExtension("3D")

mxd = arcpy.mapping.MapDocument(<insert path to mxd here>)

env.workspace = <insert path to workspace here>(mxd, "_*")

for raster in rasters:
    
    #Generate a unique name for each polygon/ shapefile. Have given them the prefix "del" so I can find them and delete them later
    outPoly = "del" + str(raster) + ".shp"
    #Execute Raster Domain Tool
    arcpy.RasterDomain_3d(raster, outPoly, "POLYGON")

#get the feature classes from this folder
fcList = arcpy.ListFeatureClasses()

for fc in fcList:
    #Generate a unique name for each simplified polygon
    simpPoly = fc.lstrip("del_")+ "simp_" + fc.rstrip(".shp")
    #Execute Simplify Polygon tool
    arcpy.SimplifyPolygon_cartography(fc, simpPoly, "POINT_REMOVE", 1, "", "", "NO_KEEP")

#get feature classes for deletion from same folder
deleteBigPolys = arcpy.ListFeatureClasses("del*")

#delete these features classes (too large)
for deleteBigPoly in deleteBigPolys:
    arcpy.Delete_management(deleteBigPoly)


0 Kudos