How to set layer definition query equal to data driven page number?

3683
19
Jump to solution
12-20-2013 07:32 AM
MatthewBaker2
Occasional Contributor II
All,

I'd like to make maps of students based on the school they're attending, what otherwise might be called a 'scatter-plot map'.

Is it possible to set a definition query on the student and school point layers to only display what is set as the data driven page number (in this case, the school number?)

The definition query would use the data driven page number, which is the school number, to display on the map the current school, and all students attending that school.

I hope I'm explaining myself correctly! Any ideas or pointers on how to better describe this problem are all welcome.

Thanks!

-m
0 Kudos
1 Solution

Accepted Solutions
MatthewBaker2
Occasional Contributor II
The solution proved to be page definitions, rather than python scripting (but see below for how I used python).

Page Definitions, in combination with data driven pages, were all I needed to ensure that other layers in the map were displaying the same SCHOOL_ID as the index layer of the data driven pages.

With the Index layer set to a copy of the schools layer, the layer turned off, and the SORT and NAME fields of DDP set to SCHOOL_ID.

Then enable page definitions of a 2nd schools layer (used for display) and set the page name field to SCHOOL_ID.

Do the same thing with the students layer, page name field set to SCHOOL_ID

Now when you toggle between data driven pages (on the hidden schools index layer), the 2nd schools layer displays the school being referenced by the index layer, the students show those that match the index layer as well, and we're all set.

Many thanks to Wayne Whitley for all the help!!!

Python: Many of our output maps are for powerpoint, rather than print, so the simple python script found here made it easy to export the data driven pages to PNG!

Thanks, Jeff for getting me started on this problem!

View solution in original post

0 Kudos
19 Replies
JeffBarrette
Esri Regular Contributor
You should be able to do this.  Try something like:

import arcpy
mxd = arcpy.mapping.MapDocument(r"C:\Project\ParcelAtlas.mxd")
lyr1 = arcpy.mapping.ListLayers(mxd, "my layer1")[0]
lyr2 = arcpy.mapping.ListLayers(mxd, "my layer2")[0]
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    lyr1.definitionQuery =   "SchoolNum = " + pageNum
    lyr2.definitionQuery =   "SchoolNum = " + pageNum
    print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPNG(mxd, r"C:\Project\OutPut\ParcelAtlas_Page" + str(pageNum) + ".png")
del mxd


Jeff
0 Kudos
MatthewBaker2
Occasional Contributor II
Hi Jeff,

Thank you - this looks really promising...

I keep getting the error:

lyr1.definitionQuery =   "SCHOOL_NUM = " + pageNum
TypeError: cannot concatenate 'str' and 'int' objects

I've tried setting the SCHOOL_NUM field to a string field, and also tried setting the 'Page Number' option in the MXD to the string field as well, both with no luck.

I'm running the script from Aptana Studio 3

Any thoughts?

-m
0 Kudos
T__WayneWhitley
Frequent Contributor
Probably you've solved this already...but your pageNum must be an integer (that's what the error is reporting), and of course you cannot just 'slam' them together - that sends Python off in a corner to sit and fret....and you want Python busy working on more labor-intensive things, so try this instead:
defQry = "SCHOOL_NUM = {0}".format(pageNum)
lyr1.definitionQuery = defQry


-Wayne

PS - oh, but your problem is you aren't sure what your "SCHOOL_NUM" field type is?...then if it really contains text strings, then you'd simply do it this way:
defQry = "SCHOOL_NUM = '{0}'".format(pageNum)
lyr1.definitionQuery = defQry
0 Kudos
MatthewBaker2
Occasional Contributor II
Thank you Wayne!

So the error I'm getting now is:

Exporting page 1 of 11
Runtime error
Traceback (most recent call last):
  File "<string>", line 6, in <module>
NameError: name 'mxd' is not defined


And here is the code I'm using:

import arcpy

mxd = arcpy.mapping.MapDocument(r"C:\scatterplots\mxd\scatterplotsdenver.mxd")
lyr1 = arcpy.mapping.ListLayers(mxd, "students")[0]
lyr2 = arcpy.mapping.ListLayers(mxd, "schools")[0] 
for pageNum in range(1, mxd.dataDrivenPages.pageCount + 1):
    mxd.dataDrivenPages.currentPageID = pageNum
    defQry = "SCHOOL_NUM = {0}".format(pageNum)
    lyr1.definitionQuery = defQry
    lyr2.definitionQuery = defQry
    print "Exporting page {0} of {1}".format(str(mxd.dataDrivenPages.currentPageID), str(mxd.dataDrivenPages.pageCount))
    arcpy.mapping.ExportToPNG(mxd, r"C:\scatterplots\outputs" + str(pageNum) + ".png") 
    del mxd
0 Kudos
T__WayneWhitley
Frequent Contributor
Well, you should probably post your entire code...the var for your mxd doesn't look like it is set properly - I'd check that.
If your error is on line 6 (as it says) and the code doesn't choke using the mxd var until it gets to 'mxd.dataDrivenPages.pageCount' then it sounds like DDP isn't enabled...so make sure you have the correct map doc and the DDP is already set up.

Wayne
0 Kudos
MatthewBaker2
Occasional Contributor II
Hi Wayne,

Edited the path the the MXD in my previous post... I think that's right.

And yes, the DDP is enabled in the MXD.

Here are the DDP settings:

[ATTACH=CONFIG]30194[/ATTACH]
0 Kudos
T__WayneWhitley
Frequent Contributor
Okay, so it looks like you've set up DDP from a 'scatterplots' data frame...when you list layers, I think it defaults to the 1st dataframe.  So how many data frames do you have?  If you have multiple ones (and your desired one isn't the 1st in the map), then you can set that obj before you feed that into ListLayers.
0 Kudos
MatthewBaker2
Occasional Contributor II
I had two, but this MXD only has 1 data frame called "Scatterplots"...
0 Kudos
T__WayneWhitley
Frequent Contributor
post your current code along with the specific error msg you are getting now - you can just paste it (use the code tags)...meanwhile check your data frame name and layer names to make sure that's not the problem.
I'll edit your code quickly and send it back to you.  How are you running this code?...standalone, within the open mxd, or what?
0 Kudos