Select to view content in your preferred language

Place a Layer from a Geodatabase onto a Map (unopened) - Python code

1646
11
Jump to solution
09-19-2022 09:32 PM
Anthony_R_McKay
Regular Contributor

A Python project - A river flow analysis - I've generated my vector polyline object for the rivers (four classifications) - how do I move this into a map?

This fc is saved in the gdb - a map already exists.

I want to use Python to add some symbology, but the code I'm using needs the layer to be in a map.

What I need to know is the Python code to send a layer to an existing map from the gdb without opening ArcGIS Pro and doing it manually.

I'd also like to know how to change the line thickness symbology with Python eventually.

Any help would be appreciated.

 

0 Kudos
1 Solution

Accepted Solutions
Anthony_R_McKay
Regular Contributor

Solved - but I had to create a lyrx Layer File and move the layer to the map from there - but the result is what I wanted - thanks to Dan for pointing me in the right direction.

Capture2.JPG

View solution in original post

0 Kudos
11 Replies
DanPatterson
MVP Esteemed Contributor

Did you check out the methods in

Map—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
Anthony_R_McKay
Regular Contributor

Thanks Dan,

I glanced through this earlier today, but I didn't spot:

"addLayer (add_layer_or_layerfile, {add_position})"

"Provides the ability to add a Layer or LayerFile to a map within a project (.aprx) using basic placement options."

I'll give this a try now.

Cheers,

Anthony

0 Kudos
Anthony_R_McKay
Regular Contributor

Not doing at all well with this - I'm either getting the syntax wrong or the file type is incompatible. The examples given are less than helpful.

Here is an illustration of what I want to do = in ArcGIS Pro it would be "right click>add to current map"

Capture.JPG

0 Kudos
DanPatterson
MVP Esteemed Contributor

If you know the source data, then

Make Feature Layer (Data Management)—ArcGIS Pro | Documentation

followed by

Copy Features (Data Management)—ArcGIS Pro | Documentation

if you want it to be permanent


... sort of retired...
0 Kudos
Anthony_R_McKay
Regular Contributor

Thank you Dan,

That was painless - now I've got a 'Stream.lyr' fc, which should be more acceptable for Map methods.

I'll attempt to move this into the map now. (fingers crossed)

0 Kudos
Anthony_R_McKay
Regular Contributor

I feel I'm close, but I can't the script to accept the 'Stream_lyr' value - both full path and workspace get the same error.

This is with ArcGIS closed. The variables are accepted when I run the script with the 'insert layer' commented out.

I'm probably making a rookie mistake  - but I'm out of ideas.

Capture.JPG

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

I can't tell from the script and the above as to whether you created a "layer" on disk as a *.lyrx file which would be in a folder or you want to make one from a featureclass in the geodatabase.


... sort of retired...
0 Kudos
Anthony_R_McKay
Regular Contributor

'Stream_lyr' is in the original gdb folder where the 'Stream' shapefile was.

I've just realised that the name 'Stream_lyr' is just the name I gave the script to output.

Checked - it's just a copy of my original file with a deceptive name - back to square one. 😞

Thanks anyway, for your help Dan.

So I still need to go from this to this - using Python script.

Capture1.JPGCapture2.JPG

0 Kudos
Anthony_R_McKay
Regular Contributor

It's the inserted layer name that is killing me:

Full paths, indexes, simple strings, nothing works. ValueErrors at every turn

"layerfilename = r"C:\Users\OEM\Documents\Tony\Massey\Paper 2 Labs\Lab7_Rasters\Wellington\Wellington.gdb\Stream"

import arcpy
map_object = arcpy.mp.ArcGISProject(project_path)
insertLyr = arcpy.mp.LayerFile(layerfilename)
m = map_object.listMaps('*')[0]
refLyr = m.listLayers("Catchment_Bnd")[0]
m.insertLayer(refLyr, insertLyr, "BEFORE")
map_object.save()
del map_object"

 

0 Kudos