|
POST
|
Tracey, A couple of comments: - I don't see an mxd.save() in your code so how is it that you expect to see changes? - If you are running this from the Python window, use "CURRENT" instead of the path to the file on disk. - it would be much easier to read your code if you posted it withing tags. Its the # button. You want to preserve indents. Jeff
... View more
11-29-2012
07:53 AM
|
0
|
0
|
1211
|
|
POST
|
I was wondering if you could edit the script for me so I could add a page range? The script example in the post is simply exporting each page in the for loop (with custom logic executed at each page). Rather than exporting all pages, modify the range value. Change: for DDP_Page in range(1, (mxd.dataDrivenPages.pageCount + 1)): To: for DDP_Page in range(2, 5)): This will print pages 2-4. The second sample script for the DDP help topic shows how you can export a list of page names. http://resources.arcgis.com/en/help/main/10.1/#/DataDrivenPages/00s300000030000000/ Jeff
... View more
11-29-2012
07:44 AM
|
2
|
5
|
3843
|
|
POST
|
An easy way would be to publish an MXD with the same data source (layer) mutliple times and depending on the scale, you would turn on/off the layer visibility. You can get the scale from the DataFrame class: http://resources.arcgis.com/en/help/main/10.1/#/DataFrame/00s300000003000000/ And set a layer's visibility from the Layer class: http://resources.arcgis.com/en/help/main/10.1/#/Layer/00s300000008000000/ If you are new to Python and arcpy.mapping I suggest you try the getting started tutorial: http://resources.arcgis.com/en/help/main/10.1/#/Getting_started_with_arcpy_mapping_tutorial/00s30000006w000000/ Jeff
... View more
11-26-2012
06:02 AM
|
0
|
0
|
1465
|
|
POST
|
After working with Alex's data, we were able to make the above code much more efficient. import arcpy, os #Specify map doc, data frame and visible layer mxd = arcpy.mapping.MapDocument(r"C:\Temp\Alex\Crossection_201210311a.mxd") df = arcpy.mapping.ListDataFrames(mxd, "Main")[0] visLyr = arcpy.mapping.ListLayers(mxd, "On-Site Mitigation Area_Python", df)[0] #Reference DDP object and iterate through pages ddp = mxd.dataDrivenPages for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1): print pageNum mxd.dataDrivenPages.currentPageID = pageNum visLyr.visible = False if ddp.pageRow.Source == "Willits": visLyr.visible = True ddp.exportToPDF(r"C:\Temp\Alex\Output\Page" + str(ddp.pageRow.Sorting_Field) + ".pdf", "CURRENT") del mxd Jeff
... View more
11-07-2012
09:16 AM
|
0
|
0
|
1261
|
|
POST
|
FYI - the issue appears to be addressed in 10.1 SP1. The issue would still need to be addressed in 10.0 if there is another planned service pack. Jeff
... View more
11-07-2012
05:11 AM
|
0
|
0
|
1731
|
|
POST
|
If you are on 10.1, have you considered using the new Data Access module (arcpy.UpdateCursor vs arcpy.da.UpdateCursor)? http://resources.arcgis.com/en/help/main/10.1/#/What_is_the_data_access_module/018w00000008000000/ The cursors are considerably faster. Jeff
... View more
11-07-2012
05:07 AM
|
0
|
0
|
2010
|
|
POST
|
Here are 3 sample apps on ArcGIS Online that use DDP and Python to display tabular data on the layout: http://www.arcgis.com/home/item.html?id=3a525b986b774a3f9cbbd8daf2435852 http://www.arcgis.com/home/item.html?id=af4fe32a93554eadbd3be3b0e55326be http://www.arcgis.com/home/item.html?id=0588e23e83f245afaa8501e84e7b25e5 Jeff
... View more
11-06-2012
05:45 AM
|
0
|
0
|
1144
|
|
POST
|
Your code looks pretty good. My immediate comment is that you are referencing an MXD on disk (i.e., you are not using "CURRENT") but your code doesn't do anything like save out a new MXD or export to PDF so you are not going to see the changes. I've added code below that includes the exportToPDF statement and also shows proper indentation (in case that is an issue too).
import arcpy, os
#Specify map doc, data frame and table
mxd = arcpy.mapping.MapDocument(r"K:\Projects_1\Caltrans\00543_09_Willits_from_URS\mapdoc\Crossection_2012\Crossection_20121024.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
mapLyr = arcpy.mapping.ListLayers(mxd, "Mapbook_table", df)[0]
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
rows = arcpy.SearchCursor(mapLyr)
for row in rows:
source = row.getValue ("Source")
if source == "Willits":
lyrName = "On-Site Mitigation Area_Python"
lyr = arcpy.mapping.ListLayers(mxd, lyrName, df)[0]
lyr.visible = True #This is turning the layer on based on if statement.
ddp.exportToPDF(r"C:\Temp\Page" + str(pageNum) + ".pdf", "CURRENT")
lyr.visible = False #This turns it back off for other maps.
del mxd, df, lyr The above code will create individual PDF files: Page1.pdf, Page2.pdf, etc Concerning your scale bars. This is a bit more trick but very doable. I would author the MXD with two scale bars (for each of the appropriate scale ranges), each with unique element names and one of them positioned just off the page. Then via arcpy.mapping I would move them on/off the page depending on the current page using .elementPositionX and/or .elementPositionY. I hope this helps, Jeff
... View more
10-31-2012
01:08 PM
|
0
|
0
|
1261
|
|
POST
|
If the objective is to fit variable length text into a limited area on a page layout and reduce the size of the text when necessary, it can be done using Python but requires some coding gymnastics. Here is a snippet of code that reads a string from a field and via the textwrap module, it breaks the code up into an appropriate number of lines.
def cloneText(txtElement, x, y, string):
condoText_clone = txtElement.clone()
condoText_clone.elementPositionX = x
condoText_clone.elementPositionY = y
condoText_clone.text = string
desc = row.getValue("LegalDesc")
wrapLines = textwrap.wrap(desc, 50)
for line in wrapLines:
cloneText(legalDesc, x, y, line)
y = y - legalDesc.elementHeight - y3
Where this can get more involved is if you want to test multiple variables ahead of time (e.g., total width vs total height) to optimize the text within the designated area. It can be an iterative process to modify the fontsize and retest for fitting strategies. Jeff
... View more
10-30-2012
07:53 AM
|
0
|
0
|
1731
|
|
POST
|
This is behavior is by design. The print and export options use DDP settings when DDP is enabled. As suggested above, temporarily disable DDP to export/print modified settings. Jeff
... View more
10-30-2012
06:20 AM
|
0
|
0
|
2023
|
|
POST
|
The code you had is was almost exactly what you needed.
mxd = arcpy.mapping.MapDocument( path to mxd )
df = arcpy.mapping.ListDataFrames(mxd)[0] ###UpdateLayer requires a data frame reference (returns 1st df)
lyrFile = arcpy.mapping.Layer( path to lyr file ) ###This is a lyr file with pre-authored symbology you will apply to layer
lyr = arcpy.mapping.ListLayers(mxd, "layer name", df)[0] ###A reference to the shapefile layer in MXD you want to update.
arcpy.mapping.UpdateLayer(df, lyr, lyrFile, True) ###This will update ONLY the symbology
For more help on UpdateLayer, please refer to: http://resources.arcgis.com/en/help/main/10.1/#/UpdateLayer/00s30000003p000000/ Jeff
... View more
10-23-2012
06:12 AM
|
0
|
0
|
853
|
|
POST
|
Dave, Again in your most recent post, you are referencing a shapefile via the Layer function. You must reference a layer file. To fix this, add your shapefile into ArcMap, symboloize it however and then save out to a layer file (*.lyr). Then replace the path to the shapefile with the path to the lyr file. UpdateLayer will update all layer properties, including dataSource, if your set symbology_only=False. Jeff
... View more
10-22-2012
08:07 AM
|
0
|
0
|
2663
|
|
POST
|
Chris, I could not reproduce on 10.0 SP5. What version/service pack are you running? Could you provide a script and a description of the type of datasets in your MXD? Jeff
... View more
10-22-2012
06:47 AM
|
0
|
0
|
2146
|
|
POST
|
After further evaluation, there is an error in the original code - it is easy to miss. The Layer function is being used to reference a feature class on disk, not a layer file. It is documented that the Layer function is for referencing layer files. The bug is that an error should have been thrown. It should not have worked in 10.0. Original code:
SubjectLyr = arcpy.mapping.Layer("Y:\Notification Radius Pkgs\Dave3\Shapefiles\Dave3Subject.shp")
mxd = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListDataFrames(mxd, "Radius Map")[0]
wPath = "Y:\Notification Radius Pkgs\\"
arcpy.mapping.AddLayer(df, SubjectLyr,"AUTO_ARRANGE")
sourceLayer = arcpy.mapping.Layer(wPath+"Setup10\LayerFiles\Subject.lyr")
arcpy.mapping.UpdateLayer(df, SubjectLyr, sourceLayer, True)
Thanks, Jeff
... View more
10-19-2012
09:48 AM
|
0
|
0
|
2663
|
|
POST
|
Ken, Contact me at [email protected]. Maybe we can come up with a new sample to place on ArcGIS online. Jeff
... View more
10-19-2012
06:22 AM
|
0
|
0
|
1964
|
| Title | Kudos | Posted |
|---|---|---|
| 1 | 06-05-2025 11:20 AM | |
| 3 | 06-05-2025 09:21 AM | |
| 1 | 05-14-2025 01:19 PM | |
| 2 | 04-24-2025 07:54 AM | |
| 1 | 03-15-2025 07:19 PM |
| Online Status |
Offline
|
| Date Last Visited |
06-23-2026
10:29 AM
|