How can I get a value from a field and then do a definition query to SELECT A POLYGON?

2345
7
06-27-2016 10:48 AM
JeffErwin
New Contributor

Hi-

I'm trying to change the polygon selection based upon a field in it's own attribute table. I have 4 polygons in the feature class each representing a different lake levels and I would like to be able to change the polygon to the one that represents the data in the Pool_Status field. My code runs in pyscripter, but does not update the .mxd. Any help is appreciated.

Here is my code:

import arcpy

mxd = arcpy.mapping.MapDocument(r'\\swf-netapp1\RefGIS\FY-15_FLD_Support\Geospatial\Automation_Tools\LM_Test.mxd')

# Updates the color of the poly to reflect pool level

# Sets the field to take pool level from

df = arcpy.mapping.ListLayers(mxd, "Grapevine_Lake_Pool")

for lyr in arcpy.mapping.ListLayers(mxd, "Pool_Status", df):

            if ([Pool_Status] == "'SP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake SP'"

            elif ([Pool_Status] == "'CP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake CP'"

            elif ([Pool_Status] == "'FP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake FP'"

            elif ([Pool_Status] == "'SC'"):

               for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake SC'"

mxd.save()

del mxd

0 Kudos
7 Replies
DarrenWiens2
MVP Honored Contributor

You can only access field values with a cursor. This doesn't read the value in the field [Pool_Status]:

if ([Pool_Status] == "'SP'"):

Aside from that, it's somewhat unclear if you're trying to apply 4 different definition queries to the same layer (you can't, at least not at the same time).

DanPatterson_Retired
MVP Esteemed Contributor

just as a side note, group layers don't support definition queries, so you should provide checks

Layer—Help | ArcGIS for Desktop then there is that whole discussion on using saveACopy when the layer isn't loaded from file but I don't think that applies here, but I would do a check on the defn query just in case your files aren't stored locally to begin with.

Updating and fixing data sources with arcpy.mapping—Help | ArcGIS for Desktop see the comments in here as wel

JeffErwin
New Contributor

Thanks for the help on this. I'm trying to apply one definition query based upon what the data is in Pool_Status. This is in an effort to fully automate a lake level map. This is the only way I can think of to change the color of a text box without fooling with arc objects. I have 25 lakes that I'm creating 4 different polygons for each that change color if the pool level is raised. Each will be represented by the polygon selected in the definition query. I'm thinking a cursor is not the best approach here.Thanks again.

Jeff

0 Kudos
JeffErwin
New Contributor

Here is the cursor version if anyone has ideas....

FC = r"\\swf-netapp1\RefGIS\FY-15_FLD_Support\Geospatial\Automation_Tools\Daily_Lake_Map.gdb\Grapevine_Lake_Poly"

# Sets the field to take pool level from

Fields = ['Pool_Status']

with arcpy.da.UpdateCursor(FC, Fields, "NAME = 'Grapevine Lake'") as cursor:

    for row in cursor:

            if (row[0] == "'SP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake SP'"

            elif (row[0] == "'CP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake CP'"

            elif (row[0] == "'FP'"):

                for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake FP'"

            elif (row[0] == "'SC'"):

               for lyr in arcpy.mapping.ListLayers(mxd):

                    if lyr.name == 'Grapevine Lake Pool':

                        lyr.definitionQuery = "NAME = 'Grapevine Lake SC'"

            cursor.updateRow(row)

mxd.save()

del mxd

print '6 - Joined Lake_Flood_Point to Lakes Poly and set pool poly color'

0 Kudos
DarrenWiens2
MVP Honored Contributor

Is there something else going that limits FC to one row? How many features are in FC? Also, have you checked that the code actually gets into the if...elif clauses (i.e. does row[] ever equal "'SP'")? Are you using Data Driven Pages? Finally, can't you just do this with symbology?

0 Kudos
JeffErwin
New Contributor

Hey Darren,

No, I don't think the script limits the FC to one row. There are four separate polygons in the featureclass. I think the only way to change symbology in a script is by using arcobjects. I'm not using DDP. I will try to test the if elif clause. Thanks again for your help.

Jeff

0 Kudos
DarrenWiens2
MVP Honored Contributor

Okay, if I understand correctly (which I'm not sure I do), you should use Data Driven Pages to cycle through your lakes (it will make one map for each lake). Set up your symbology (once) in the map. Then, export all your maps. If that's not at all what you want, please elaborate.

You can't set different definition queries within the same feature class at the same time. You script seems to read through each feature changing the definition query on the layer each time. Essentially, you'll be left with with the last definition query, if it works at all.