0

I am trying to set a definition query on a Application Lands layer in my project.mxd using arcpy. But it's not working for me. When I launch the project the query is in there but it doesn't get applied.

Can anyone help me out here?

import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\Users\Desktop\GIS PROGRAMMING\Project.mxd')
Dockets = arcpy.mapping.ListLayers(mxd, "Application Lands")
Applications= Dockets[0]
Applications.definitionQuery = '"Docket"' + " = " + "'1254-2017 '"
mxd.save()
del mxd
  • Please edit your question to include some more information about your data - what type of dataset (file/personal/enterprise geodatabase, shapefile), what type of field, maybe a screenshot of your attribute table – Midavalo Nov 25 '17 at 4:20
  • @Midavalo yes there is a space in the data. It still doesn't work. The text populates the query box and all of the polygons that belong to the shapefile disappear but it doesn't execute the query properly. – gisking Nov 25 '17 at 4:20
  • If everything disappears then the query doesn't match your data somehow – Midavalo Nov 25 '17 at 4:21
  • I have tested your code using a layer from file geodatabase (gdb), shapefile, and personal gdb. It works fine with the first two, but doesn't show anything (all features disappear) from the personal gdb feature class. If you are using a personal gdb the field name in the query should be '[Docket]', not '"Docket"' – Midavalo Nov 25 '17 at 4:27
  • @Midavalo okay now it is selecting the feature, but it is not querying it. And again none of the polygons are showing up. Do you think it's a bug? Also this is a shapefile I have saved on my desktop. – gisking Nov 25 '17 at 4:32
4

Unless the datasource of your layer is a Personal Geodatabase your query should work fine (if in fact there are features with that text as a value in your layer).

I recommend you write your code a bit tidier, using string formatting and arcpy.AddFieldDelimiters() to work around potential issues.

import arcpy
mxd = arcpy.mapping.MapDocument(r'C:\Users\Desktop\GIS PROGRAMMING\Project.mxd')
queryValue = '1254-2017 '
app = arcpy.mapping.ListLayers(mxd, "Application Lands")[0]
app.definitionQuery = "{} = '{}'".format(arcpy.AddFieldDelimiters(app, "Docket"), queryValue)
mxd.save()
del mxd
  • I tested the same code on a different shapefile and it works. So something is wrong with the initial shape file i was using. I have no idea what it could be – gisking Nov 25 '17 at 5:32
  • @gisking Tested my code or yours? What was the result with the code from my answer? Still the same? – Midavalo Nov 25 '17 at 7:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.