Python - Using data driven pages to select features from another layer to get attributes to text

7806
12
Jump to solution
06-08-2015 09:55 AM
deleted-user-MS0lE1F_0-sy
Occasional Contributor

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")
0 Kudos
12 Replies
deleted-user-MS0lE1F_0-sy
Occasional Contributor

Sorry about that.  It seemed like I could only post in one area.  I will not double post in the future.

0 Kudos
WesMiller
Regular Contributor III

You could do a spatial join and use data driven text to accomplish what you are trying to do.

deleted-user-MS0lE1F_0-sy
Occasional Contributor

Spatial join will work in this case, but this is a small part of a larger script where I would run into problems with this.  I feel I am very close getting this way to work and would like to continue down this path.  I really appreciate the help.