POST
|
I'm experiencing differences from exporting features to PDF from within ArcMap and using a stand alone python program. Basically, I'm selecting a feature from a layer, zooming to the selected extent, changing the scale of the dataframe, and exporting the image to a pdf from PAGE_LAYOUT view- something pretty simple. The issue that I'm having is that from within ArcMap, everything works fine. In the page layout view, everything looks as expected, and once exported, it looks great. I'm trying to automate this so after building a stand alone python program, it exports like it's supposed to, however the scale is off by about 200.0. Here's a snippet of my code: mxd = arcpy.mapping.MapDocument("C:\Path\To\MxdFile.mxd")
dataFrames = arcpy.mapping.ListDataFrames(mxd)
df = dataFrames[0]
layers = arcpy.mapping.ListLayers(mxd, "*", df)
for layer in layers:
if layer.name == "LayerIWantToUse":
break
if layer.name != "LayerIWantToUse":
raise Exception("Cannot find layer.")
arcpy.SelectLayerByAttribute_management(layer, "CLEAR_SELECTION")
query = "OBJECTID = 1"
layer.definitionQuery = query
arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION", query)
df.zoomToSelectedFeatures();
df.scale = 2400.0
fileName = r"Python_2400.pdf")
arcpy.mapping.ExportToPDF(mxd, fileName, "PAGE_LAYOUT", 1056, 1632)
The scale that it actually exports, appears to be 1:2200 instead of 1:2400 like it's supposed to. I've attached both images to show the differences. I have noticed that if I save the MXD with a different scale (1:100000 for example) and with ArcMap in "Data View", I have the scale issues. If I make sure "Layout View" is selected, change the scale to 1:2400, and save the MXD, I do not have the scale issues. I've seen quite a few forum posts regarding this, some saying it's a bug that should have been fixed in 10.1, but nobody with any real explanations on why there's scale issues. Has anyone else encountered something similar?
... View more
10-06-2014
02:22 PM
|
1
|
0
|
5833
|
POST
|
Hey there, I'm running into a slight issue when attempting to export images of features within a larger project. I've broken down my example to a much more simplistic example for readability. Basically, in this example, I'm attempting to select each feature individually, zoom to it's extent, and export the image to a certain sized image defined by the features attributes. The problem is the extent that I'm receiving from layer.getSelectedExtent() hugs the feature pretty well in some cases, but doesn't do a very good job in other cases. Now, I can't use data driven pages for this because this snippet is part of a larger project where I have to do more than just the below to get exactly what I want. Here's my sample code - note, this is an external python script, not run within ArcMap 10.2.1:
import arcpy
# Pixels per inch constant, used in determining the width and height of an image
PPI = 92
# retrieve the mxd, dataframe, and layer I want to export
mxd = arcpy.mapping.MapDocument(r"ExportImages.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
layer = None
layers = arcpy.mapping.ListLayers(mxd)
for lyr in layers:
if lyr.name.lower() == "layer_i_want":
layer = lyr
break
assert(layer != None)
# put the features in an easy to access container
arcpy.SelectLayerByAttribute_management(layer,"CLEAR_SELECTION")
recs = []
with arcpy.da.SearchCursor(layer,"*") as flCursor:
for flRow in flCursor:
recDict = {}
i = 0
for field in flCursor.fields:
recDict[field] = flRow
i += 1
recs.append(recDict)
for rec in recs:
print "Generating image for OBJECTID: {0}".format(rec["OBJECTID"])
query = "OBJECTID = {0}".format(rec["OBJECTID"])
arcpy.SelectLayerByAttribute_management(layer,"NEW_SELECTION", query)
#Determine the image width/hight needed based on attribute in feature.
recWidth = rec["WIDTH"]
recHeight = rec["HEIGHT"]
# Convert from feet to inches
recWidth *= 12
recHeight *= 12
# Convert inches to pixels
recWidth = int(recWidth * PPI)
recHeight = int(recHeight * PPI)
# Note - if I change the recWidth/recHeight to larger values, it doesn't change the scale,
# it changes the resolution of the image only
layer.definitionQuery = query
df.extent = layer.getSelectedExtent()
arcpy.mapping.ExportToPNG(mxd, r"C:\temp\{0}.png".format(rec["OBJECTID"]), df, recWidth, recHeight)
layer.definitionQuery = ""
Now, if the scale was off in a consistent way, I could multiply the dataFrame's scale by a multiplier to zoom in further, but because the shape of the features are variable, and the scale/extent works great in some cases, I would end up cutting off the features in many of the cases. Below are some sample images of what's generated: Bad Extents/Scales: [ATTACH=CONFIG]35037[/ATTACH] [ATTACH=CONFIG]35038[/ATTACH] Good Extents/Scales: [ATTACH=CONFIG]35040[/ATTACH] [ATTACH=CONFIG]35039[/ATTACH] If I open this mxd in ArcMap 10.2.1, I'm able to select these features and zoom to the selected feature just fine and the extent is perfect in ArcMap. Is there something that I'm missing? Should I need to modify the scale even though I have the selected feature's extent? I might be missing something basic here. Thanks, - C
... View more
07-02-2014
07:44 AM
|
0
|
0
|
3988
|
Title | Kudos | Posted |
---|---|---|
1 | 10-06-2014 02:22 PM |