3

Situation

I am currently helping a friend with a ArcGIS problem. She is familiar with ArcGIS but not with programming and I am familiar with programming but not with ArcGIS :-) .

We have a layer LAYER1 that was created via "Create Layer from Selected features" from another layer LAYER0, of which some features were selected via a SQL query 'Select by Attribute' (Open Attribute Table -> Select by Attribute).

Question

How do I extract the original SQL query, which was applied on LAYER0, from LAYER1 via Python/arcpy?

Background

(Why do I want to know it)

The servers of network drives have changed. Solutions to updated the path names are described here. We updated the data sources' path names via appying findAndReplaceWorkspacePaths on the mxd file, which was successful. Unfortunately, all feature selections of layers created by "Create Layer from Selected features" got lost for an unknown reason. We also tried applying replaceDataSource on each layer, which suchseccessfully updated the data path but also removed the selections. In the referenced article it is suggested to "update Layer object's definitionQuery". However, when I get the definitionQuery of one layer variable LAYER (LAYER.definitionQuery), I only get an empty string ... . Now, my plan is to extract the SQL queries and recreated the layers via arcpy.SelectLayerByAttribute_management from the old SQL queries.

  • 4
    I don't see anywhere that IQueryTableDataSourceDescription is exposed in arcpy. – Kirk Kuykendall Aug 8 '16 at 20:57
  • 5
    You can't recover that information from the data itself. In the future, I would recommend using definition queries instead of selecting data. Or you can store the original query in the log of a script run. – Tom Aug 8 '16 at 21:57
2

As Kirk and Tom have both pointed out in the comments, there is no way to recover those queries. Creating layers from selections should never be used for any long term solution. The layers will eventually fail, even if you hadn't changed network drives. I have been burned by this in the past. Always use definition queries or create new datasets from your selections. Definition queries are ideal because they don't create additional data, and you can save them both in the mxd and as a separate text file.
Please note that FID or OBJECTID are poor fields to use for definition queries. These IDs can change when the data is modified, rendering your queries useless. The best solution is to use or create a unique ID field that will not change when data is added or removed from the dataset and query on this field.

Since you are a programmer, you can use arcpy and the da module search cursor to grab your unique attributes from the selected features and format them into a definition query. There is extensive documentation available on the ArcGIS site explaining how to do this.

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.