ArcGIS Pro 3.2.1
A colleague has built a tool that converts a selection to a definition query.
Resulting definition query:
t_unique_id IN (122, 123, 124, 125, 126)
The tool works well, but it has a limitation: If there are multiple layers in the Contents that have the same name, then the tool has no way to differentiate between the two layers.
Note that since you can only get a layer reference in ArcPy by its name, I had to limit the tool to grabbing the first layer in the Map TOC that has the name [that corresponds to the name] in the tool. So if there are multiple layers or tables with the same name, it will operate on the first one found when looping through them.
In the following example, there are two layers that have the same name, and the second layer is the one that has the selection to be converted to a definition query. When I run the tool, it throws an error because it refers to the first layer, which doesn't have a selection.
As a non-Python person, that seems like an unfortunate limitation of ArcPy, especially since the Layer or Table parameter seems to be aware of the duplicates and refers to them by unique names: "species_records" and "species_records:1". If the parameter can refer to the duplicate layers by unique names, then is there a reason why the Python script can’t differentiate between the two layers as well?
Idea:
Could ArcPy be enhanced so that it can refer to a specific Contents layer, even if there are multiple layers with duplicate names?
Feel free to let me know if I've misunderstood something.
Related:
Selection2Definition - ArcGIS Pro 3.2.1.zip