TransWikia.com

Dynamically add features to map in ArcGIS Pro 2.7

Geographic Information Systems Asked by PCMitchell on March 24, 2021

I am trying to dynamically add the contents of a feature dataset to a map in Pro v 2.7.

To achieve this, my understanding of the workflow is that I need to list the features in the feature dataset (‘ResultsPath’ in code snippet below), then convert to layer, save as a layer file, then add layer. The code below runs without errors, but nothing happens–nothing is added to the map and no layer files are generated. I would try addDataFromPath, but since the lyrx is not generating in the save to layer file process I am stuck for the moment. Any pointers?

#Lists the feature classes in the Results Feature Dataset

#Dynamically convert to layer and add to map.

#Code working, but nothing happening? 

        m = aprx.listMaps()[0]
        for fc in arcpy.ListFeatureClasses(ResultsPath):
            OutFile = ScratchWS + os.sep + str(fc)
            lf = arcpy.MakeFeatureLayer_management(fc, OutFile, None, None).getOutput(0)
            lyf = arcpy.management.SaveToLayerFile(lf, OutFile)
            m.addLayer(lyf, "AUTO_ARRANGE")

Update: Problem solved, ListFeatureClasses was not referencing the feature dataset properly so was generating an empty list so nothing was happening. The updated code below does list the contents of the feature dataset.

    #Lists features from Results feature dataset
    #Makes feature layer from features, adds feature layers to map

    m = aprx.listMaps()[0]
    datasets = arcpy.ListDatasets(feature_type='feature')
    for ds in datasets:
        ResultList= arcpy.ListFeatureClasses("*", "ALL", ds)
        for fc in ResultList:
            OutFile = os.path.join(wkspc, str(fc))
            lf = arcpy.MakeFeatureLayer_management(fc, fc, None, None)

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP