Select to view content in your preferred language

Multiple definition query

4595
7
Jump to solution
08-27-2012 08:51 PM
CPoynter
Frequent Contributor
Hi All,

I have three feature classes with a common attribute. Two of the FC are points (origin and destination) and the remaining FC represents connecting lines between points.

I would like to apply a common definition query to each FC and iterate over the attributes which would allow turning on/off of relevant features. What is the process to achieve this?

Regards,

Craig
0 Kudos
1 Solution

Accepted Solutions
JeffBarrette
Esri Regular Contributor
Craig,

I modified a bunch of things and I made some slight changes to work with my data.  See the code below.

1) Moved mxd variable out of loop.  It only needs to be referenced once.
2) I got rid of your "i" iterator lines.  Your for loop was good enough.
3) I modified your queryStr variable so it works with dyanmic data.  Because you are working with a fGDB, placing double quotes around double quotes requires using an escape character.  There are other methods.
4) I fixed your for lyr loop.  It was using "regional" as a layer name search and you were also using [0] - that won't work.
5a) Your if lyr.name == was looking for a workspace value, it needs to be the layer name the way it appears in the TOC.
5b) You'll want to add your additoinal searches since I only used one layer
6) arcpy.RefreshActiveView() wasn't really needed.

import arcpy  fc = r"C:\Temp\Test.gdb\SrcR" field = "NAME" # Field of interest values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))] uniqueValues = sorted(set(values))  mxd = arcpy.mapping.MapDocument(r"C:\Temp\defQuery.mxd")  for value in uniqueValues:   queryStr = "\"" + field + "\" = '" + str(value) + "'"    df = arcpy.mapping.ListDataFrames(mxd, "Regional")[0]   for lyr in arcpy.mapping.ListLayers(mxd, "", df):     if lyr.name == "SrcR":       lyr.definitionQuery = queryStr    arcpy.mapping.ExportToPDF(mxd, r"C:\Temp" + "\\" + value + ".pdf") # Output individual PDF pages  del mxd 


I hope this helps,
Jeff

View solution in original post

0 Kudos
7 Replies
JeffBarrette
Esri Regular Contributor
You can change the definition queries via the arcpy.mapping Layer class using the .definitionQuery property.  Check out the help:
http://resources.arcgis.com/en/help/main/10.1/#/Layer/00s300000008000000/

Jeff
0 Kudos
CPoynter
Frequent Contributor
Hi Jeff,

I will give the .definitionQuery property a try. I thought there must be something to do this in python, but wasn't sure how to structure my search query.

Regards,

Craig
0 Kudos
CPoynter
Frequent Contributor
Hi Jeff,

I have had a try at getting a python script together to do what I want. I had a play and was able to mimic the type of response I wanted to acheive, so I have tried to automate it into a script, but I am missing something in the process. Can you see any obvious mistakes I am making.

1) I want to access MXD
2) Create a denition query on a specified field
3) Output results to PDF pages for each value iteration value sourced from the attribute field (unique values identified and sorted)

import arcpy

fc = r"D:\test.gdb\srcR" # FC with field of interest
field = "Abbr" # Field of interest
values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))]
uniqueValues = sorted(set(values))

i = iter(uniqueValues)

value = i.next()

for value in uniqueValues:
        queryStr = 'str(field)' + "=" + 'str(value)'

        mxd = arcpy.mapping.MapDocument(r"D:\test.mxd") # MXD to analyse

        df = arcpy.mapping.ListDataFrames(mxd, "Regional")[0]
        for lyr in arcpy.mapping.ListLayers(mxd, "Regional", df)[0]:
                if lyr.name == r"D:\test.gdb\srcR" or lyr.name == r"D:\test.gdb\srcM" or lyr.name == r"D:\test.gdb\srcC": # Three FC with common field that definition query wll apply to
                        lyr.definitionQuery = queryStr

        arcpy.RefreshActiveView()

        arcpy.mapping.ExportToPDF(mxd, r"D:\Temp\value.pdf") # Output individual PDF pages

value = i.next()

del mxd


Regards,

Craig
0 Kudos
JeffBarrette
Esri Regular Contributor
Craig,

I modified a bunch of things and I made some slight changes to work with my data.  See the code below.

1) Moved mxd variable out of loop.  It only needs to be referenced once.
2) I got rid of your "i" iterator lines.  Your for loop was good enough.
3) I modified your queryStr variable so it works with dyanmic data.  Because you are working with a fGDB, placing double quotes around double quotes requires using an escape character.  There are other methods.
4) I fixed your for lyr loop.  It was using "regional" as a layer name search and you were also using [0] - that won't work.
5a) Your if lyr.name == was looking for a workspace value, it needs to be the layer name the way it appears in the TOC.
5b) You'll want to add your additoinal searches since I only used one layer
6) arcpy.RefreshActiveView() wasn't really needed.

import arcpy  fc = r"C:\Temp\Test.gdb\SrcR" field = "NAME" # Field of interest values = [row[0] for row in arcpy.da.SearchCursor(fc, (field))] uniqueValues = sorted(set(values))  mxd = arcpy.mapping.MapDocument(r"C:\Temp\defQuery.mxd")  for value in uniqueValues:   queryStr = "\"" + field + "\" = '" + str(value) + "'"    df = arcpy.mapping.ListDataFrames(mxd, "Regional")[0]   for lyr in arcpy.mapping.ListLayers(mxd, "", df):     if lyr.name == "SrcR":       lyr.definitionQuery = queryStr    arcpy.mapping.ExportToPDF(mxd, r"C:\Temp" + "\\" + value + ".pdf") # Output individual PDF pages  del mxd 


I hope this helps,
Jeff
0 Kudos
CPoynter
Frequent Contributor
Hi Jeff,

Script works a treat. Exported several pages based on variable. I then got adventurous and tried modifying it slightly to export all layers and attributes as well:

arcpy.mapping.ExportToPDF(mxd, r"C:\Temp" + "\\" + value + ".pdf", layers_attributes="LAYERS_AND_ATTRIBUTES")


This didn't result in the output PDFs having layers or attributes exported. I tested against the internal export function within ArcGIS 10.1 and it also didn't export layers or attributes. It seems that my mxd is exporting all of my point, polyline and polygon datasets as an image?

Is this a bug as it seems to work ok with other MXDs I have?

Also, now seeing a functioning script, I had a thought about adding a form of dynamic text to the map that represents the value being examined or references a value from another field related to the iterated value (e.g. iterated value in field 1 = a, dynamic text would read 'a' or if field1 = a = field2 = 'Site A', text would read 'Site A'). Is this possible?

Regards,

Craig
0 Kudos
JeffBarrette
Esri Regular Contributor
Here are two friendly forum post reminders:
1) this new question should be a new thread.  Try not to ask new questions within a thread because it makes it difficult for other users to search and learn from.
2) don't address the questions directly to a single person.  They are for the community to read and respond to.  Granted I do check this forum every day but I am allowed to take time off.  🙂

Concerning your new issue.  It could be that you have raster layers/graphics in your mxd.  When graphics are involved, all layers below the highest graphic layer are rasterized.  If there is a raster picture in the layout, it too will cause everything to rasterize.

Jeff
0 Kudos
CPoynter
Frequent Contributor
Hi All,

I was using a 3D symbol graphic for points and this was causing my part of my error when creating PDFs with ExportToPDF using the layers_attributes feature. I removed the graphics and also ungrouped my datasets. Point datasets are now providing attribute information, but polygon and polylines are not. Something to chase up with tech support.

Regards,

Craig
0 Kudos