I am trying to loop through exporting data drive pages, select data from another layer based on the page getting exported, and write a row of the results of the selection to a text element. Each page should have different results in text element. For each page getting exported I I want to select all the parcels within the current page/feature getting exported and write the results to the text element. What I have so far only writes the results for the first page on every page. When it runs it does seem to select the parcels per page while exporting, but it does not re-write the results to the text element. Not sure what I am missing?
# Always run script in the mxd you are working on. mxd = arcpy.mapping.MapDocument("CURRENT") # Overwrites any existing features. arcpy.env.overwriteOutput = True # Uses the Dataframe called "Layers". df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] # Parcel Text Element ParcelText2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ParcelsText2")[0] # Data Driven Pages Export Sheets ExportSheets = arcpy.mapping.Layer("Sheets_11x17") # Parcels Parcels = arcpy.mapping.Layer("Parcels") # Data Driven Pages mxd abbreviation ddp = mxd.dataDrivenPages # Loop through DDP's for pageNumb in range(1, mxd.dataDrivenPages.pageCount + 1): ddp.currentPageID = pageNumb # DDP Export Sheet duplicate which has the page definition Query SelectedSheet = arcpy.mapping.Layer("Sheets_11x172") # Exports Row page ExportSheetsPage = ddp.pageRow.PLAN_NUMBER # Select Parcels withing my SelectedSheet arcpy.SelectLayerByLocation_management(Parcels, "WITHIN", SelectedSheet) # Use SearchCursor to get Parcels value and write to Parcel Text Element rows = arcpy.SearchCursor(Parcels) textValue = "" for row in rows: textValue += row.getValue("MAP_PARCEL") ParcelText2.text = textValue # Export Sheets ddp.exportToPDF(r"Y:\GIS\GIS_Work\Working\parcel_test\maps\test.mxd")
Solved! Go to Solution.
I don't use DDP at all, so I feel like I'm working with one hand behind my back here.
Your parameters for the export to pdf seem odd, the first parameter should be the outpdf name, yet you have a .mxd as the first parameter. Also, I would make sure you are using more parameters, namely you can select the page range and and page range. If you do not change the export parameters, it exports all page each time, not the single page.
have you tried deleting your cursor and rows after it set your text element in the loop? If the cursor isn't deleted, it could be using the same cursor each time instead of creating a new one maybe?
Also, out of curiousity why are you using the old cursors(arcpy.SearchCursor) instead of the new one arcpy.da.SearchCursor, the new ones are significantly faster.
I will try that. I was playing around with deleting the text element but not the cursor and rows. I will start using da, was just re-diving into an old script and also using what is familiar, time to upgrade my knowledge.
Delete row/cursor did not work. I also noticed two things, it is just writing the results of the last page on all the pages, not the first page results like I thought. Also while I watch it export each page it is updating the text element in arcmap when it goes to each page while exporting, but not in the pdf.
I don't use DDP at all, so I feel like I'm working with one hand behind my back here.
Your parameters for the export to pdf seem odd, the first parameter should be the outpdf name, yet you have a .mxd as the first parameter. Also, I would make sure you are using more parameters, namely you can select the page range and and page range. If you do not change the export parameters, it exports all page each time, not the single page.
I did not notice the .mxd rather than pdf, but it does not seem to effect it. I appreciate the advice, I think I will play around with my export parameters some. I am thinking about exporting the "current" individual pages then appending them at the end. If you think of anything else please let me know, I will post if I discover the answer and any other things that come up that might help. Thanks
Got it to work using your suggestions. Had to create a new pdf document before I ran the loop, export each sheet individually, and append. Thanks for the helping me think it through!
# Always run script in the mxd you are working on. mxd = arcpy.mapping.MapDocument("CURRENT") finalResult = arcpy.mapping.PDFDocumentCreate(r"Y:\GIS\GIS_Work\Working\parcel_test\maps\final.pdf") # Overwrites any existing features. arcpy.env.overwriteOutput = True # Uses the Dataframe called "Layers". df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] extent = df.extent ParcelText2 = arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT", "ParcelsText2")[0] ExportSheets = arcpy.mapping.Layer("Sheets_11x17") Parcels = arcpy.mapping.Layer("Parcels") ddp = mxd.dataDrivenPages for pageNumb in range(1, mxd.dataDrivenPages.pageCount + 1): ddp.currentPageID = pageNumb SelectedSheet = arcpy.mapping.Layer("Sheets_11x172") ExportSheetsPage = ddp.pageRow.PLAN_NUMBER arcpy.SelectLayerByLocation_management(Parcels, "WITHIN", SelectedSheet) rows = arcpy.SearchCursor(Parcels) textValue = "" for row in rows: textValue += row.getValue("MAP_PARCEL") ParcelText2.text = textValue del rows ddp.exportToPDF(r"Y:\GIS\GIS_Work\Working\parcel_test\maps\test.pdf", "CURRENT") finalResult.appendPages(r"Y:\GIS\GIS_Work\Working\parcel_test\maps\test.pdf") del textValue
I know this is an older post but I think your script could help with something similar I was trying to do.
I want to be able to use Data Driven Pages to give me a list of features from another layer (point) within each page.
Basically, I have a GRID polygon layer with a field called PAGE. PAGE is used for DDP with a range of 1-184.
I also have a point layer called TRAFFIC SIGNS that has a field with unique FACILITY_ID numbers that I use for inventory purposes.
For each PAGE I want to select all the TRAFFIC SIGNS within the page and write out each traffic sign's FACILITY_ID to a text file.
I am new to Python so bare with me and thanks for any advice ahead of time!
# I have the current sheet as a parameter. This is going to be your grid sheet/page. You will need a copy of the layer you are using for data driven pages, open the properties -> Definition Query and set up the Page Definition. CurrentSheet = arcpy.GetParameterAsText(0) Selection = arcpy.SelectLayerByLocation_management(TRAFFICSIGNS, "INTERSECT", CurrentSheet) # TextElement = the Element Name TextElement.text = Selection ddp.exportToPDF("C:LOCATION", "CURRENT") finalResult.appendPages("C:LOCATION")
Also, please don't double post, you can use the share function to share a single post in more than one area.