Select to view content in your preferred language

Help with layer file definition query in arcpy.mapping

1028
5
08-20-2013 11:20 AM
GerryGabrisch
Frequent Contributor
Everything seems to work just fine with the code below except that no features draw in the layer that is being queried.  All I want to do is create a jpeg for each record in my geodatabase feature class of point locations.

Is there something wrong with my query syntax?  I cannot figure it out!

try:
    import arcpy, sys, traceback
    print "Working..."
    
    mxdPath = r"Z:\GISpublic\GerryG\WaterResources\WaterSampleSiteAutoMap\WaterSampleSiteAutoMap2.mxd"
    mxd = arcpy.mapping.MapDocument(mxdPath)
    
    df = arcpy.mapping.ListDataFrames(mxd)[0]
    lyr = arcpy.mapping.ListLayers(mxd, "WaterSampleSiteMaster", df)[0]
    print lyr.dataSource
    theAttribute = "SiteID"
    #GetAListOfEachAttribute
    listOfAttributes = []
    print "Start cursor"
    rows = arcpy.SearchCursor(lyr.dataSource)
    for row in rows:
        listOfAttributes.append(row.getValue(theAttribute))
                                                         
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if elm.name == 'gbglabeler':
            elm = elm
            print elm.name
            break
    
    for item in listOfAttributes:
        #GBGquery = "\""  + theAttribute + "\" = " + "'" + item + "'"
        GBGquery = '"SiteID" =' + '\''+ item + '\''
        print  "Working on: ",item
        print GBGquery
        lyr.definitionQuery = GBGquery
        elm.text = "Site: " + item
        
        outputjpg = "Z:\GISpublic\GerryG\WaterResources\WaterSampleSiteAutoMap\SiteMaps\\" + item + ".jpg"
        arcpy.RefreshActiveView()
        arcpy.mapping.ExportToJPEG (mxd, outputjpg)
    print "done"
except arcpy.ExecuteError: 
    msgs = arcpy.GetMessages(2) 
    arcpy.AddError(msgs)  
#If the error is with the script, or with python, find error and report it to the screen...
except:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
Tags (2)
0 Kudos
5 Replies
MattSayler
Frequent Contributor
Long time no see!

Starting with the basics, is SiteID a text field (does the value need the single quotes)? Is the query working if you manually assign it to the layer?

Since it's running without error, I suspect this won't help, but you could also try using the Add Field Delimeters method for the field name:
GBGquery = arcpy.AddFieldDelimiters(lyr, theAttribute) + " = '" + item + "'"
0 Kudos
GerryGabrisch
Frequent Contributor
Thanks for the reply, Matt.  I hope you are doing well. 

Yes, SiteID is an attribute storing text. 

I tried the field delimeter but that just returned the same text string as the brute force way and nothing showed on the output jpeg.  Any other thoughts?
0 Kudos
GerryGabrisch
Frequent Contributor
Hmmm, Must have been some hang up between Arcpy, Komodo, ArcGIS or Citrix. 

I tried to save the original mxd, got an error that the file location folder was in use.  After shutting ArcMap and Komodo down the original code worked fine.
0 Kudos
MattSayler
Frequent Contributor
Is it just the JPEGs that aren't displaying the layer correctly? Step through using mxd = arcpy.mapping.MapDocument("CURRENT"), if you haven't already. Try exporting to pdf or one of the other formats too and see if you get the same results.
0 Kudos
MattSayler
Frequent Contributor
Hmmm, Must have been some hang up between Arcpy, Komodo, ArcGIS or Citrix. 

I tried to save the original mxd, got an error that the file location folder was in use.  After shutting ArcMap and Komodo down the original code worked fine.


Excellent!
0 Kudos