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.
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 setThis 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.
Yep that's already ticked on
check your geoprocessing options in the Project backstage to ensure that results are added to the active map.
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.
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.htm
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)
If I run MakeFeatureLayer, how do I add the output to the map?
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.
Angemeldete Mitglieder können Beiträge verfassen, Updates folgen und mehr. Neu hier? Registriere ein kostenloses Konto.
Find useful guides, FAQs, and documents to help you navigate and make the most of Esri Community.