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.
-
4I don't see anywhere that IQueryTableDataSourceDescription is exposed in arcpy.Kirk Kuykendall– Kirk Kuykendall2016年08月08日 20:57:52 +00:00Commented Aug 8, 2016 at 20:57
-
5You 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– Tom2016年08月08日 21:57:11 +00:00Commented Aug 8, 2016 at 21:57
1 Answer 1
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.
-
Thanks for the replay. Good to know that FID and OBJECTID may change. I expected something like this but I am struggling with the nomenclature (Features, Layers etc.). Is it a good start to use the ArcGIS Pro ArcPy Reference to get into arcpy or are there more reasonable resources?daniel.heydebreck– daniel.heydebreck2016年08月09日 09:20:08 +00:00Commented Aug 9, 2016 at 9:20
-
1@daniel.neumann Go with the ArcGIS for desktop 10.3 reference. You're interested in Search Cursor in the da module (data access module). You'll also need select by location management or select by attribute management. Here's some sites to get you started: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy/… desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy/… desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-data-access/…jbalk– jbalk2016年08月11日 00:59:39 +00:00Commented Aug 11, 2016 at 0:59