Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

In arcgis 10 and python I want to get the extent (xmax, ymax, xmin, ymin) info of each of the polygons in a shape file.

I can get the extent of the whole shape file using

file=r"D:\SCRATCH\ARCGIS\100k_trc_tiles_TVM.shp"
desc=arcpy.Describe(file)
print desc.extent.Xmax

394551.52085039532

But I can't seem to figure out how to get the same info for each row in the dataset.

rows = arcpy.SearchCursor("100k_trc_tiles_TVM")
for row in rows:
 print row

prints the 31 rows in the dataset but

for row in rows:
 desc=arcpy.Describe(row)
 print desc.extent.Xmax

gives an error.

Runtime error : Object: Describe input value is not valid type

I was thinking of adding the extent values to the table using "calculate geometry" but this only gives the centroid. Then I guess we can use something like row.GetValue("xmax").

That being said I know that we can create the X/Y, max/min using the function from http://www.ian-ko.com/free/free_arcgis.htm but it would be best if we can avoid having to add fields, especially if arcpy can get these values.

Basically I need to get the extents to feed into the clip tool to clip out 30 areas of data (according to the 1:100,000 map sheets) for geoprocessing since the Split tool fails due to the large size of the dataset (see Issues with large datasets). I want to automate this as it is repeated on a number of datasets.

=== working script ===

# Emulates Arc Info SPLIT tool by using Clip but
# Requires a FC from which each row is used as the input clip feature.
# Each row must be rectangular.
# Used on 12GB FGDB with 100 million records.


#Licence: Creative Commons
#Created by: George Corea; georgec@atgis.com.au, coreagc@gmail.com
import arcpy, string

#inFrame=arcpy.GetParameterAsText(0) # Input dataframe FC
#inFile=arcpy.GetParameterAsText(1) # Input FC for splitting
#outDir=arcpy.GetParameterAsText(2) # Output FGDB

inFrame=r"D:\SCRATCH\ARCGIS\100k_trc_tiles_TVM.shp"
inFile=r"c:\junk\106\data\7_Merge.gdb\FullRez_m2b"
outDir=r"D:\SCRATCH\Projects\206\datasplit\test_slaasp.gdb"
#NameField="Name_1"

#arcpy.env.workspace = r"C:/Workspace"
arcpy.env.overwriteOutput = True

rows = arcpy.SearchCursor(inFrame)
shapeName = arcpy.Describe(inFrame).shapeFieldName
for row in rows:
    feat = row.getValue(shapeName)
    Name = row.Name_1
    print "Executing clip on: "+str(Name)
    extent = feat.extent
    #print extent.XMin,extent.YMin,extent.XMax,extent.YMax
# Create an in_memory polygon
    XMAX = extent.XMax
    XMIN = extent.XMin
    YMAX = extent.YMax
    YMIN = extent.YMin
    pnt1 = arcpy.Point(XMIN, YMIN)
    pnt2 = arcpy.Point(XMIN, YMAX)
    pnt3 = arcpy.Point(XMAX, YMAX)
    pnt4 = arcpy.Point(XMAX, YMIN)
    array = arcpy.Array()
    array.add(pnt1)
    array.add(pnt2)
    array.add(pnt3)
    array.add(pnt4)
    array.add(pnt1)
    polygon = arcpy.Polygon(array)
    ShapeFile = outDir+"\\temp_poly"
    arcpy.CopyFeatures_management(polygon, ShapeFile)

    #print Name
### Set local variables
    in_features = inFile
    clip_features = ShapeFile
    out_feature_class = outDir+"\\"+Name
    xy_tolerance = "0.22"

    # Execute Clip

    try:
        arcpy.Clip_analysis(in_features, clip_features, out_feature_class, xy_tolerance)
        print "Completed: "+str(Name)
    except:
        error = arcpy.GetMessages()
        print "Failed on: "+str(Name)+" due to "+str(error)
share|improve this question
2  
You don't need to write the clip feature to disk, just use the in memory polygon, eg: polygon = arcpy.Polygon(array) arcpy.Clip_analysis(in_features, polygon, out_feature_class, xy_tolerance) –  Luke Nov 9 '11 at 23:12
    
tks. Any idea how I can export the row to a new shape file so that it used, rather than just the extent of the row? This is so that it can handle non-rectangular clips as well. –  GeorgeC Nov 10 '11 at 20:47
1  
Well if you just want to clip by that feature, in the same script, just use the feature object. Again, no need to export to shape file, e.g: arcpy.Clip_analysis(in_features, feat, out_feature_class, xy_tolerance) –  Luke Nov 10 '11 at 23:14

5 Answers 5

up vote 16 down vote accepted

Get the shape object in your cursor and access its extent property. See ArcGIS Help Working with geometry in Python:

shapeName = arcpy.Describe(inFeatures).shapeFieldName
for row in rows:
    feat = row.getValue(shapeName)
    extent = feat.extent
    print extent.XMin,extent.YMin,extent.XMax,extent.YMax
share|improve this answer
    
Thanks Luke. This worked great. I will edit my question to have the new working code if anyone wants to use a tool which - Uses the Clip toolset itieratively to clip out rectangular regions of a large feature class. Emulates functionality of the arc info Split tool without crashing with datasets as large as 10GB FGDB and 100 million records. –  GeorgeC Nov 7 '11 at 3:06
    
One thing - I had to hard code the name of the column to get the equivalent of Name=row.Name_1 by trying the name attribute as ; NameField="Name_1" ; Name=row.NameField ; or Name=row+"."+NameField where NameField=arcpy.GetParameterAsText(2) and the Name is held in Name_1 column. Any ideas? Note I;ve used ";" to denote a new line. –  GeorgeC Nov 7 '11 at 3:11
1  
figured the above out - row.GetValue(xxx) from gis.stackexchange.com/questions/16586/… –  GeorgeC Nov 8 '11 at 0:44

The Bounding Container toolset does exactly what you want. Should you just want code snippets, examine the functions within the scripts, one deals explicitly with extent.

EDIT

I should add that the script will add values to a Left, Right, Top and Bottom field in the created output file which can be used for subsequent processing

share|improve this answer
    
thanks. Will check it out. Hopefully I can use the code within my python scripts/models. –  GeorgeC Nov 4 '11 at 12:52

I just tried the Minimum Bounding Geometry (Envelope) (in Data Management) in ArcGIS 10 and it seems to do exactly the same, for all the fields.

HTH.

share|improve this answer

Did you try capitalizing the "M" in "XMax"? I think it's supposed to be:

print desc.extent.XMax

instead of

print desc.extent.Xmax

according to the documentation. Of course that makes me wonder how your first code snippet worked. Either way, give it a shot!

share|improve this answer

Another way would be to do a SearchCursor() on the shapefile, then you can use row.shape.extent:

rows = arcpy.SearchCursor(shapefileName)

for row in rows:
   extent = row.shape.extent
   ...
   ...
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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