Extent of in_memory layer/adding an in_memory layer to map

1385
2
Jump to solution
01-05-2021 03:28 PM
by Anonymous User
Not applicable

Hello, 

I'm trying to get the extent of an in_memory layer from a standalone script. 

However I'm having some issues running into this. 

For some reason I can't add the in_memory layer to the map current map. See could below. the # are just different ways I've tried. 

I keep getting an error saying 

s2 = map.listLayers('parcels_lyr')[0]
IndexError: list index out of range

I'm pretty sure that it can't find the layer. When I list the layers it is not in there. 

The goal is to export a map with the extent of a 2 mile buffer (in_memory layer) around the original boundary. So  it will show the original boundary with some room around all sides. But I don't want to show the buffered boundary, it's just to get the extent. 

 

 

arcpy.analysis.Buffer(s,r"in_memory\buffer2","2 MILE")
arcpy.MakeFeatureLayer_management(r"in_memory\buffer2", "parcels_lyr")
for lyr in map.listLayers():
    arcpy.AddMessage(lyr.name)
s2 = map.listLayers('parcels_lyr')[0]
#map.addDataFromPath(r"in_memory\buffer2")
#map.insertLayer(s, r"in_memory\buffer2", "BEFORE")
#lyt.camera.setExtent(lyt.getLayerExtent(s2, False, True))

 

 ArcPro 2.5 python 3.x

Thanks!

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
by Anonymous User
Not applicable

Hi @Anonymous User ,

It's documented here that when using the in_memory workspace with geoprocessing models, that the output needs to be written to disk to be displayed as a layer in the map - and while the documentation isn't explicit about this also applying to Python scripts, I suspect the same limitation would also apply. 

If however, our main interest in the in_memory buffer output is getting its extent  so we can use that to set the view extent for our map export  - I'd suggest we use a slightly different approach so that we can get the extent directly from the in_memory feature class, without having to create a layer  in the map. One such approach we could use for this purpose would be to use the arcpy.da.Describe function

arcpy.analysis.Buffer(s,r"in_memory\buffer2","2 MILE")
buff_extent = arcpy.da.Describe(r"in_memory\buffer2")['extent']

# Use the buff_extent variable to set camera extent, then export the map ...

 

Hope this helps!

 

View solution in original post

2 Replies
by Anonymous User
Not applicable

Hi @Anonymous User ,

It's documented here that when using the in_memory workspace with geoprocessing models, that the output needs to be written to disk to be displayed as a layer in the map - and while the documentation isn't explicit about this also applying to Python scripts, I suspect the same limitation would also apply. 

If however, our main interest in the in_memory buffer output is getting its extent  so we can use that to set the view extent for our map export  - I'd suggest we use a slightly different approach so that we can get the extent directly from the in_memory feature class, without having to create a layer  in the map. One such approach we could use for this purpose would be to use the arcpy.da.Describe function

arcpy.analysis.Buffer(s,r"in_memory\buffer2","2 MILE")
buff_extent = arcpy.da.Describe(r"in_memory\buffer2")['extent']

# Use the buff_extent variable to set camera extent, then export the map ...

 

Hope this helps!

 

by Anonymous User
Not applicable

Perfect! Just had to change the CS to get it in the correct units! 

Thanks!