Select to view content in your preferred language

Make layer from selected features - using ArcPy

6361
12
04-05-2023 10:29 PM
ToyotaT18
Regular Contributor

In ArcGIS Pro GUI, after selecting some rows/features I can right-click the layer name in the Contents -> Selection -> Make Layer from Selected Features.

How can I achieve this using ArcPy? With the result added to the active map.

0 Kudos
12 Replies
JoshuaSharp-Heward
Frequent Contributor

Do you want to do the selection in ArcPy as well or will you be selecting them using the regular tools in the GUI and then running some python to create the layer? This page https://pro.arcgis.com/en/pro-app/latest/tool-reference/data-management/make-feature-layer.htm has the details of the make feature layer tool which is what you want, just click on "Python" in the Parameters section to see what the function is + what the inputs are. Pretty sure that by default if your input features into the tool have a selection on them that that will be honoured in any ArcPy tool.

0 Kudos
ToyotaT18
Regular Contributor

If I run MakeFeatureLayer, how do I add the output to the map?

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

Hi,

If you're running it from within Pro then MakeFeatureLayer should automatically add it to your active map. Although in testing this I've realised that MakeFeatureLayer doesn't honour the selection which is annoying. Does it have to be a temporary layer? If not then exporting to feature class should work https://pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/feature-class-to-feature-class.ht...

If not you will need to take the selected features on the input and add their ObjectIDs to the where_clause in the MakeFeatureLayer function. This should work:

 

inputLayer = 'yourlayername'
outputLayer = 'outputlayername'
OIDS = arcpy.Describe(inputLayer).FIDSet #returns a string of all the OBJECTIDs
sql = "OBJECTID IN ({})".format(OIDS.replace("; ", ",")) #formats the OBJECTiD string into the format of (1, 2, 3) required for SQL IN 
arcpy.management.MakeFeatureLayer(inputLayer, outputLayer, where_clause=sql)

 

 

ToyotaT18
Regular Contributor

I'm running it within a geoprocessing script. The MakeFeatureLayer function doesn't seem to 'do' anything, I can't work out how to add the resulting layer to the map, i.e. replicate the 'Make layer from selected features' GUI function.

Looking at the documentation, it only mentions that the temporary layer can be saved as a layer file (SaveToLayerFile). So I've implemented that, i.e. create a temporary .lyrx file in the project folder, then add that to the map, which works. The code then deletes the .lyrx file at the end to tidy up.

Thanks for your assistance.

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

Hi,

If it's not automatically adding it you may be able to sandwich it within the following code to get it to add to the map - haven't tried it myself but it's the manual way of adding a layer to a map.

myProject = arcpy.mp.ArcGISProject("CURRENT") #references current pro project
myMap = myProject.activeMap
#the rest of the code
myMap.addLayer(outputLayer)
0 Kudos
ToyotaT18
Regular Contributor

Yeah I tried that, it didn't work. Doesn't like the new 'virtual' layer, error message reports as if it doesn't exist.

0 Kudos
JoshuaSharp-Heward
Frequent Contributor

Weird, outputting the layer using feature class to feature class and storing it in the 'memory' workspace may be a decent option for temporary data, but if your process of outputting to a .lyrx is working then all good I guess!

0 Kudos
ToyotaT18
Regular Contributor

Yeah but I don't want it to be a forked copy of the data, I want it to refer back to the original data, like when adding the selected features as a layer.

And yes the .lyrx option works so I guess I'll just go with that. Thanks again.

0 Kudos
mmetzcar
Occasional Contributor

Ugh, yeah, I ran into the same thing w/ MakeFeatureLayer failing to honor the selection. It does add a new layer to my map, but that layer has 100% of the data from the source feature layer rather than only the ones I had selected (though it is kind of cute that it still has the selection applied so it does copy the knowledge of the selection into the new feature layer).

I'm thinking the issue has to do with the fact that the newly created layer doesn't have a "selection set" when I look at Properties > Selection.

For layers created via the UI "Make Layer from Selected Features" option, I see this in the Properties > Selection dialog:

"""

Selection definition set
This layer comprises a subset of features.
□ Clear selected features to access all features in the source

"""

But for layers created via the Python function, there is no mention of the "Selection definition set" in Properties > Selection.

0 Kudos