CÓMO
Las entidades se agregan al mapa para su visualización al realizar cualquier proceso en ArcGIS Pro. Cuando se agregan a un mapa abierto, estas entidades se convierten automáticamente en un archivo de capa. Para que una entidad de una geodatabase se agregue a un mapa utilizando un script de Python, la entidad se debe convertir en un archivo de capa. Por tanto, para colocar una entidad en un mapa sin abrir el software de ArcGIS Pro, las herramientas Crear capa de entidades y Guardar en archivo de capa se utilizan en el script para convertir la entidad deseada de una geodatabase en un archivo de capa.
El siguiente flujo de trabajo describe cómo agregar entidades a un mapa existente utilizando un script de Python.
import arcpy
arcpy.env.workspace = r"<folder_location>\<geodatabase.gdb>" project_path = r"<folder_location>\<project_name.aprx>" aprx = arcpy.mp.ArcGISProject(project_path)
layer = r"<folder_location>\<geodatabase.gdb>\<file_name>"
layers_out = "<output_file_name>"
output_location = r"<folder_location>\{}.lyrx".format(layers_out) #Same as project path
arcpy.management.MakeFeatureLayer(in_layer, "<output_file_name>")
arcpy.management.SaveToLayerFile("<output_file_name>" , layers_out)
insertLyr = arcpy.mp.LayerFile(output_location)
m = aprx.listMaps("*")[0]
refLyr = m.listLayers("file_name")[0]
m.insertLayer(refLyr, insertLyr, "BEFORE")
aprx.save()
del aprx
El siguiente bloque de código demuestra el script completo.
import arcpy
arcpy.env.workspace = r"C:\USER\Desktop\Test\test.gdb"
project_path = r"C:\USER\Desktop\Test\Test_Project.aprx"
aprx = arcpy.mp.ArcGISProject(project_path)
layer = r"C:\USER\Desktop\Test\test.gdb\Layer_Test" #The feature name in the geodatabase
layers_out = "Layer_Test" #The file name for the output layer
output_location = r"C:\USER\Desktop\Test\{}.lyrx".format(layers_out)
arcpy.management.MakeFeatureLayer(in_layer, "Layer_Test")
arcpy.management.SaveToLayerFile("Layer_Test" , layers_out)
insertLyr = arcpy.mp.LayerFile(output_location)
m = aprx.listMaps("*")[0]
refLyr = m.listLayers("Existing_Layer")[0]
m.insertLayer(refLyr, insertLyr, "BEFORE") #Insert the new layer onto the map before the specified existing layer
aprx.save()
del aprx #Close the project and delete the lock file
Id. de artículo: 000028522
Obtener ayuda de expertos en ArcGIS
Empieza a chatear ahora