Hello, I am wondering how I can get the layer object from a shapefile. In ArcMap, it is pretty easy. I can use:
route_temp = arcpy.GetParameterAsText(0)
route_temp_lyr = arcpy.mapping.Layer(route_temp)
working_f = os.path.split(route_temp_lyr.dataSource)[0]
But how I can do it in ArcGIS Pro? I want to get the layer object from my input parameter.
Solved! Go to Solution.
Why not use GetParameter
instead of GetParameterAsText
, using the former passes you a reference to the layer object so you don't have to create another reference.
'mapping' is now 'mp' in Pro
Thanks! But the question is there is no specific function about it in arcpy.mp. In the picture is the list of the functions but it seems that there is no corresponding function to get it.
In ArcMap, the logic is very clear. First get the parameters, and then arcpy.mapping.Layer to get the layer, then I can use the function lyr.dataSource to get the data source of this layer.
However, I did not figure it out the steps about it with arcpy.mp. In short, how I can use arcpy.mp to get the layer of an input, where the input is string format from the result of arcpy.GetParameterAsText function?
What does the input represent that is getting passed? A specific layer in a specific map in an open ArcGIS Pro session?
Yes, it is a specific layer in a specific map in an open ArcGIS Pro session. I just drag it from table of content as an input.
The scripts is as blow.
# ---------------------------------------------------------------------------
# NAV Combined tool
# Version 1.1
# ---------------------------------------------------------------------------
import arcpy
from arcpy import env
import os
import PopUp
from subprocess import Popen
# Users input
route_temp = arcpy.GetParameterAsText(0) # Testcode_route_temp
arcpy.env.overwriteOutput = True
aprx = arcpy.mp.ArcGISProject ("CURRENT")
aprxMap = aprx.listMaps("Map")[0]
# Define variables
route_temp_display = "route_temp_display.lyrx"
arcpy.MakeFeatureLayer_management(route_temp,"route_temp_lyr")
arcpy.SaveToLayerFile_management("route_temp_lyr", route_temp_display, "ABSOLUTE")
route_temp_lyr = arcpy.mp.LayerFile(route_temp_display)
working_f = os.path.split(route_temp_lyr.dataSource)[0]
arcpy.AddMessage(working_f)
test_f = os.path.split(working_f)[0]
testcode = os.path.split(test_f)[1]
codeshort = testcode.split("_")[0] + "_" + testcode.split("_")[3]
rt_shp = working_f + '\\' + testcode + '_route_temp.shp'
cl_f = test_f + r'\NAV\Clusters'
dr_f = test_f + r'\NAV\Drive_route'
streets = working_f + '\\' + testcode + '_Streets_NAV.shp'
fc5clusters = working_f + '\\' + testcode + '_FC5Clusters.shp'
stragglers = working_f + '\\' + testcode + '_stragglers.shp'
driveroute = dr_f + '\\' + testcode + '_driveroute.shp'
driveroute_display = dr_f + '\\' + testcode + '_driveroute.lyr'
zlevels = working_f + r'\Working.gdb\Zlevel_fd' + '\\' + testcode + '_Auto_Zlevels'
zlevels_rj = working_f + '\\' + testcode + '_Zlevels_route_join.shp'
mob = working_f + '\\' + testcode + '_mob.shp'
steets_df = test_f + r'\DATA'+ '\\' + testcode + '_Streets.shp'
navb = dr_f + '\\' + testcode + '_NAVBuffer.shp'
scrap_gdb = test_f + r'\DATA\scrap.gdb'
And what I want is to get the data source of this input layer? I did not figure it out how to get it.
Did you try actually selecting the layer within the toolbox rather than dragging? And you might try moving line 15 above line 11 just in case the "CURRENT" needs to be set before the parameter is accessed
Thanks! I think this is also an option. So far I have tried GetParameter
and I get it works. I will try this option too.
Why not use GetParameter
instead of GetParameterAsText
, using the former passes you a reference to the layer object so you don't have to create another reference.
Thanks! That works!