Select to view content in your preferred language

New at Python scripting (Definition query and Looping)

720
2
03-31-2011 05:08 AM
AdamTomko1
Occasional Contributor
I am trying to create a script that Loops through a layer and creates multiple defintion queries on that layer. The layer is building lines and i want to create multiple definition queries so that each floor can be queried out and then mapped. I am having trouble getting started. I tried creating a script that simply creates a definition query for a layer in a mxd. It says the script is executing but when i open up the project nothing changes. Any help  would be appreciated. Some of my scrip is below.

import arcpy, os

mxd = arcpy.mapping.MapDocument("M:/nih/AsbestosAbatement/Floorplans/BLDG_6A/Floorplanpdf.mxd")

for lyr in arcpy.mapping.ListLayers(mxd):
    if lyr.name == "BuildingFloorLInes":
        layer.definitionQuery =[FLR] = '02'
        layer.update
       
print 2
Tags (2)
0 Kudos
2 Replies
JasonScheirer
Esri Alum
You need to save the MXD when you're done.

import arcpy, os

mxd = arcpy.mapping.MapDocument("M:/nih/AsbestosAbatement/Floorplans/BLDG_6A/Floorplanpdf.mxd")

for lyr in arcpy.mapping.ListLayers(mxd, "BuildingFloorLines"):
    layer.definitionQuery = "[FLR] = '02'"

mxd.save()

print 2


If you're currently in arcmap and want to act on the active map:

import arcpy, os

mxd = arcpy.mapping.MapDocument("current")

for lyr in arcpy.mapping.ListLayers(mxd, "BuildingFloorLines"):
    layer.definitionQuery = "[FLR] = '02'"

arcpy.RefreshActiveView()
arcpy.RefreshTOC()

print 2
0 Kudos
StevenArebalo
Emerging Contributor
layer.definitionQuery =[FLR] = '02'


layer.definitionQuery='[FLR]="02"'

Your query isn't held together with outer quotes. Don't know how layer.definitionQuery reacts to ambiguous assignments, but it may be set to "" and would explain returning all records.

*beat to the punch*
0 Kudos