Definition Query

1120
5
03-05-2013 04:30 AM
EmmaAlterton1
New Contributor III
Hello

I'm trying to use a python script tool to generate a number of maps showing walking routes and their starting points.

I've set up the data driven pages based on each of the walking routes, and have written a script that runs through each page and only displays the walking route in question. Then exports each map to a pdf.

This works fine except that all the starting point information shows all the time.
Each walking route record has the name of the starting point associated with it stored in a field. How can I script taking the value from the walking route field and use it to create a definition query in the starting point dataset? This definition query will need to be updated each time the script iterates through the data driven pages.

The script I'm using is below - where and what do I need to add to get the starting point layer working at the same time?

I hope this makes sense

Thanks
Emma



[INDENT]import arcpy
import os

#Get parameter for input project location
mxdName = arcpy.GetParameterAsText(0)
#Get parameter for output location
outPath = arcpy.GetParameterAsText(1)+"\\"
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + "HealthWalks.pdf")

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(mxdName)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]

#Get data driven index layer and name field
lyrName = mxd.dataDrivenPages.indexLayer
lyrField = mxd.dataDrivenPages.pageNameField.name

pLayer = arcpy.mapping.ListLayers(mxd, lyrName)[0]

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum
fldName = mxd.dataDrivenPages.pageRow.NAME  #Get field used in dynamic query
pLayer.definitionQuery = lyrField + " = '" + fldName + "'"

#Export each theme to a temporary PDF and append to the final PDF
tmpPdf = outPath + fldName + "_temp.pdf"
if os.path.exists(tmpPdf):
    os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
os.remove(tmpPdf)

del tmpPdf
del mxd, df, finalPdf[/INDENT]
Tags (2)
0 Kudos
5 Replies
JimCousins
MVP Regular Contributor
Hello, Emma.
What you want to do is set a "Page Definition Query" on your starting point dataset. This is not available for the layer that the data driven pages (DDD) are based on, but this does not sound like an issue for you.
Once you have your DDD defined, on your starting point layer: >>Layer Properties>>Definition Query there will be a new tab at the bottom called Page Definition. Use this to limit the display of your starting points.
If I am unclear, search "Using Page Definition Queries" in the help.
Regards,
Jim
0 Kudos
EmmaAlterton1
New Contributor III
Hello

Thanks for suggestion - I didn't know about the page def query.
I've tried it and this seems fine for a 1-1 relationship between route and starting point, but mine is a 1-many set up. A starting point can have a number of routes attached to it.

So I still think I need to put some extra code in my script that picks up the starting point name from a field in the routes dataset and uses it to populate a def query in the starting point dataset.

Whilst I get the theory of what I need to do - its actually putting the correct bits of python together to achieve this that I'm stumped on.

Thanks
Emma
0 Kudos
KariBuckvold
Occasional Contributor
Emma,

Did you ever figure out the one-to-many Field name page definition?  I have the same issue, for each page in the mapbook, there are several items that fall under each page that need to be shown.

-Kari
0 Kudos
by Anonymous User
Not applicable
Your question about running a page definition query when there is a many to one relation seems similar to one I have been exploring. With help from tech support I came up with this Python script which fulfilled part of my needs. Also, I have a suggestion at ArcGIS Ideas for extending the definition query to other fields. See at https://c.na9.visual.force.com/apex/ideaView?id=087E000000051RhIAI

#DDP: Set Definition Query on a secondary index 
 #Variables that need to be set by user:
 #mxdpath = r'C;\Users\Test.mxd' # string
 #outputfolder = 'PDFFolder' # string
 #namefield = 'THE FIELD CONTAINING THE NAME OF THE RESULTING PDF FILE' #string
 #queryfield = 'THE FIELD CONTAINING THE VALUE FOR THE DESIRED QUERY' #string
 #layers = ['LIST','OF','LAYERS'] # list ('Layer1_UpdateQuery', 'Layer2_UpdateQuery', 'etc')

#Import Modules
import arcpy, os

# Set Variables
mxdpath = r'D:\arcgis\Samples\DDP_Index2.mxd' # string
outputfolder = 'D:\PDF' # string
namefield = 'FIPS' # string
queryfield = 'STATE_NAME' # string
layers = ['CountySel_FIPS'] # list

mxd = arcpy.mapping.MapDocument(mxdpath)
# Start Loop through Data Driven Pages

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum # set the current page
    pageName = mxd.dataDrivenPages.pageRow.getValue(namefield) # get the pageName
    pageName += '.pdf' # add '.pdf' extension for file name
    pdf = os.path.join(outputfolder, pageName) # add pageName to output folder to get full output path
    # Start Loop through all layers of mxd file

    for lyr in arcpy.mapping.ListLayers(mxd): # for every layer in mxd's Layer List
            if lyr.name in layers: # if layer name in user input list above
                #value = mxd.DataDrivenPages.pageRow.getValue(queryfield) # get value of query field
                value = mxd.dataDrivenPages.pageRow.getValue(queryfield)
                print value
                if lyr.definitionQuery == True: # if a definitionQuery Exsts...
                    query = lyr.definitionQuery + "AND" + queryfield + "=" + "\"" + value + "\"" # Add new to old
                else:
                    query = queryfield + "=" + "\"" + value + "\"" # else create a new query
                    lyr.definitionQuery = query # set the layers defintionQuery
            else:
                print 'Next' # else print next, this isn't necassary, but its a good place holder

    print pageName
    arcpy.mapping.ExportToPDF(mxd, pdf) # export the current Page to pdf
del mxd, lyr, pageName, pageNum, query, value
0 Kudos
EmmaAlterton1
New Contributor III
Hello

I got the solution to my initial query - the following code will export each of my pages showing just one route and the meeting place for that particular route.


[INDENT]import arcpy
import os

#Get parameter for input project location
mxdName = arcpy.GetParameterAsText(0)
#Get parameter for output location
outPath = arcpy.GetParameterAsText(1)+"\\"
finalPdf = arcpy.mapping.PDFDocumentCreate(outPath + "HealthWalks.pdf")

#Specify the map document and the data frame
mxd = arcpy.mapping.MapDocument(mxdName)
df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0]
#Select the scale of the map
mxd.activeView='PAGE_LAYOUT'
df.scale = "15000"

#Get data driven index layer and name field
lyrName = mxd.dataDrivenPages.indexLayer
lyrField = mxd.dataDrivenPages.pageNameField.name

#Specify the meeting places within the mapping files
pLayer = arcpy.mapping.ListLayers(mxd, lyrName)[0]
pMLayer = arcpy.mapping.ListLayers(mxd, "Meeting Place")[0]

#Refresh the pages to display the 15000 scale and the meeting points
mxd.dataDrivenPages.refresh()

for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
mxd.dataDrivenPages.currentPageID = pageNum #Get the current page
fldName = mxd.dataDrivenPages.pageRow.NAME  #Get route name to be used in definition query
fld2= mxd.dataDrivenPages.pageRow.Meeting_Point #Get the name of the meeting point for this route
pLayer.definitionQuery = lyrField + " = '" + fldName + "'" #Display the current route (def. query)
arcpy.AddMessage(fld2)
pMLayer.definitionQuery= "Meeting_Point = '"+ fld2+"'" #Display the meeting point for the current route (def. query)
arcpy.AddMessage(pLayer.definitionQuery)
#Export each page to a temporary PDF and append to the final PDF
tmpPdf = outPath + fldName + "_temp.pdf"
if os.path.exists(tmpPdf):
    os.remove(tmpPdf)
arcpy.mapping.ExportToPDF(mxd, tmpPdf)
finalPdf.appendPages(tmpPdf)
os.remove(tmpPdf)

del tmpPdf
del mxd, df, finalPdf[/INDENT]


Hope this helps
Emma
0 Kudos