arpy.mapping - extent of feature set?

3764
5
10-22-2010 05:12 AM
BrentTucker
New Contributor
I am working on a tool that will eventually be used to create a pdf from multiple mxds (a thematic map book). The tool will run a a server geoprocessing service. The basic workflow is below

- user draws a polygon (feature set)
- for each mxd... zoom to extent of the feature set and export layout view of the mxd to pdf.
- compile into one pdf.

how do I access the values from the extent of the "feature set" so I can modify the data frame extent in each mxd?

Thanks
0 Kudos
5 Replies
JeffBarrette
Esri Regular Contributor
Hello Brent,

Have you tried using the GP Describe object?

desc = arcpy.Describe("PathToFC")

dfExtent = df.extent
dfExtent.XMin = desc.XMin
dfExtent.XMax = desc.XMax
dfExtent.YMin = desc.YMin
dfExtent.YMax = desc.YMax
df.extent = dfExtent

Jeff
0 Kudos
RobertBinckes
New Contributor II
Here is what I did.

featureLayer = "__featureLayer"

# get the parameter
aoiFeatureSet  = arcpy.GetParameter(0)

#convert featureset to featurelayer
arcpy.MakeFeatureLayer_management(aoiFeatureSet,featureLayer)
       
#set the symbology on the feature layer
symbologyLayer = toolDataDir + "__AoiSymbology.lyr"     
arcpy.ApplySymbologyFromLayer_management (featureLayer, symbologyLayer)
       
# Save feature layer to file  ( not sure why this is needed, however could not do it in memory)
arcpy.SaveToLayerFile_management(featureLayer, aoiLayerFilename, "ABSOLUTE")

...
#use 'CURRENT' if running from arcmap, when published use MXD on disk
mxd = arcpy.mapping.MapDocument("CURRENT")
                              
#open reference to data frame
dataFrame = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
...

#create layer object from layer file
fLayer = arcpy.mapping.Layer(aoiLayerFilename)

#add layer file to data frame
arcpy.mapping.AddLayer(dataFrame, fLayer, "AUTO_ARRANGE")

dataFrame.extent = fLayer.getExtent(True)

That is it.

-b
GerardoGarza
New Contributor III
Thanks for the code snippet Jeff.  Very helpful.
When I used it, however, the extent property of the describe object needed to be accessed before referring to the min & max coordinates, eg, desc.extent.XMin.
GG
0 Kudos
JeffBarrette
Esri Regular Contributor
Thanks - my bad! - that is what I get for typing code from memory.  Here is more complete code:

 
mxd = arcpy.mapping.MapDocument("current")

lyr = arcpy.mapping.ListLayers(mxd, "my layer")[0]
desc = arcpy.Describe(lyr.dataSource)

df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
dfExtent = df.extent
dfExtent.XMin = desc.extent.XMin
dfExtent.XMax = desc.extent.XMax
dfExtent.YMin = desc.extent.YMin
dfExtent.YMax = desc.extent.YMax
df.extent = dfExtent


Note - this code does work, I ran it. 🙂
Jeff
0 Kudos
SolanaFoo4
New Contributor III

I figured I would add this in case anyone else was interested, just another way to do it:

import arcpy, ast
# Get feature set object from user input parameter
extPoly=arcpy.GetParameter(0)

# Read JSON property of the feature set as a dictionary
jsonPoly=ast.literal_eval(extPoly.JSON)
feat= (jsonPoly['features'])
geom=((feat[0]['geometry']['rings']))

# Make separate list of x values and get min/max x
listX = []
for coord in geom[0]:
    listX.append(coord[0])
minx = min(listX)
maxx = max(listX)

# Make separate list of y values and get min/max y
listY = []
for coord in geom[0]:
    listY.append(coord[1])
miny = min(listY)
maxy = max(listY)

This was my initial JSON return:

{
"displayFieldName":"",
"fieldAliases":{"OBJECTID":"OBJECTID","SHAPE_Length":"SHAPE_Length","SHAPE_Area":"SHAPE_Area"},
"geometryType":"esriGeometryPolygon",
"spatialReference":{"wkid":102643,"latestWkid":2227},
"fields":[{"name":"OBJECTID","type":"esriFieldTypeOID","alias":"OBJECTID"},{"name":"SHAPE_Length","type":"esriFieldTypeDouble","alias":"SHAPE_Length"},{"name":"SHAPE_Area","type":"esriFieldTypeDouble","alias":"SHAPE_Area"}],

"features":[{"attributes":{"OBJECTID":1,"SHAPE_Length":898.13659879002444,"SHAPE_Area":48790.437030014538},
"geometry":{"rings":[[[6072538.7365812212,2099332.1891194731],[6072790.4726262987,2099361.7028399706],[6072792.2088433057,2099160.3141351491],[6072524.847829476,2099156.8417011499],[6072538.7365812212,2099332.1891194731]]]}

}]

}

0 Kudos