How do I add feature layers to map through python?

5089
6
Jump to solution
07-17-2019 08:29 PM
AndrewClark2
New Contributor III

Hi, 

I am working in ArcGIS Pro 2.4. I am trying to create map layouts automatically through a Python Tool, which is located in a toolbox. I have been able to create this in Jupyter but the people that will be running the tool do not have access to the software. I have the following code at the moment:

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps("Map")[0]
lyt = aprx.listLayouts("Child Care Need Index")[0]

lyrfile = arcpy.mp.LayerFile(path + "/LayerFiles/Neighbourhoods.lyrx")
m.addDataFromPath(lyrfile)

It runs perfectly if I copy and paste it into the python window, but as soon as I try to run it through the toolbox I get the following error message: 

Traceback (most recent call last):
File "C:\users\*****\appdata\local\temp\arcgisprotemp11624\mapping.py", line 32, in <module>
m.addDataFromPath(lyrfile)
File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\_mp.py", line 1531, in addDataFromPath
return convertArcObjectToPythonObject(self._arc_object.addDataFromPath(*gp_fixargs((data_path,), True)))
RuntimeError
Failed to execute (Script1).

Any ideas on what I am doing wrong?

Thanks!

Andrew

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

That leads me to believe that it is the parameters of the toolbox.  If you created a conventional toolbox,

What is a script tool?—Geoprocessing and Python | ArcGIS Desktop 

then the problem may be with the toolbox itself or how the parameters (or lack of them) are defined for the toolbox.

If you are using a python toolbox (*.pyt) then there is a load more you have to handle.

Although somewhat dated, the principles described here are largely the same

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 

To make it easier for the user, you can set Defaults and provide Value lists for user selection.

View solution in original post

0 Kudos
6 Replies
DanPatterson_Retired
MVP Esteemed Contributor

what is 'path'?

path + "/LayerFiles/Neighbourhoods.lyrx")

0 Kudos
AndrewClark2
New Contributor III

Just the location of my layer file that I define earlier. 

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

Andrew, so can we assume that you imported arcpy?  You say the people don't have the software.... what software are they missing? Pro?  Jupyter?

0 Kudos
AndrewClark2
New Contributor III

Yes I imported arcpy. It doesn't matter what they are missing... sorry that was useless details regarding my issue. I am just trying to make my tool as "idiot proof" as possible, which requires to create a standalone toolbox that uses a syntax to create preset maps from existing data.

This exact code works when I use Jupyter, but when I try to create a python script tool for a toolbox I am creating in ArcGIS PRO the same code no longer works. I can even run it in the python window in pro and it works fine. I just don't understand why it crashes when using a toolbox vs the other two methods of running code.

DanPatterson_Retired
MVP Esteemed Contributor

That leads me to believe that it is the parameters of the toolbox.  If you created a conventional toolbox,

What is a script tool?—Geoprocessing and Python | ArcGIS Desktop 

then the problem may be with the toolbox itself or how the parameters (or lack of them) are defined for the toolbox.

If you are using a python toolbox (*.pyt) then there is a load more you have to handle.

Although somewhat dated, the principles described here are largely the same

/blogs/dan_patterson/2016/05/19/toolbox-creation-in-arcgis-pro 

To make it easier for the user, you can set Defaults and provide Value lists for user selection.

0 Kudos
AndrewClark2
New Contributor III

Thanks Dan!!! I got it working. Here is the full syntax and parameters: 

import arcpy, os
arcpy.CheckOutExtension("network")

arcpy.env.overwriteOutput = True

path = arcpy.GetParameterAsText(0)
gdb = arcpy.GetParameterAsText(1)
nds = arcpy.GetParameterAsText(3)
nd_layer_name = "Routing_ND"
input_facilities = arcpy.GetParameterAsText(4)
Scratch = arcpy.GetParameterAsText(2)
SA_ChildCare = Scratch + "/SA_ChildCare"
SA_CCLoc_Final = Scratch + "/SA_ChildCare_Final"
people = arcpy.GetParameterAsText(5)
people_pt = gdb + "/Boundary/people_pt"
SA_Population = Scratch + "/SA_Population"
SA_PopLoc_Final = Scratch + "/SA_Population_Final"
CC_Pop_Inter = Scratch + "/cc_pop_i"
ChildCare_Summary = Scratch + "/cc_sum"
CC_Pop_Sum = Scratch + "/cc_pop_sum"
E2SFCA_temp = Scratch + "/E2SFCA_temp"
DA_Summary = Scratch + "/DA_sum"
E2SFCA = gdb + "/FinalAnalysis/E2SFCA"
Z_Summary = Scratch + "/Z_Summary"

aprx = arcpy.mp.ArcGISProject("CURRENT")
m = aprx.listMaps(arcpy.GetParameter(5))[0]
lyt = aprx.listLayouts(arcpy.GetParameter(6))[0]

lyrfile = arcpy.GetParameter(7)
m.addLayer(lyrfile)

Here is the parameters: