Adding MapService Raster Layer from ArcGIS REST Services directory to a ArcMap MXD using ArcPy

743
4
12-17-2021 06:12 AM
GiuseppeGentili
New Contributor III

Adding MapService raster layer using ArcPy from ArcGIS REST Services directory to a ArcMap MXD

I have map services published in to a specific folder on to an ArcGIS Server site. Each map service publishes raster layers.
Tipically the url of the rest directory of a raster layer is:
https://<my host>/server/rest/services/<folder>/<service name>/MapServer/<id>

Unsing ArcPy I would like to add  the raster layer to a MXD using its rest directory url.
I have tried the following code embedded in the "execute" section of a python arctoolbox:

import arcpy.mapping
IMXD = arcpy.mapping.MapDocument("CURRENT")
DF_layer = arcpy.mapping.ListDataFrames(IMXD, "Layers")[0]
url_ras_layer = r"https://<my host>/server/rest/services/<folder>/<service name>/MapServer/<id>"
arcpy.mapping.AddLayer(DF, arcpy.mapping.Layer(url_ras_layer))

but I got the error message:
ValueError: Object: CreateObject Layer invalid data source

Furthermore I guess that an account must provided in order to establish the right access to the datastore.

Hoping that is possible to perform the add operation via ArcPy, how can I do it ?

0 Kudos
4 Replies
TonyContreras_Frisco_TX
Occasional Contributor III

It looks like this is difficult , if not impossible to do in ArcMap.

Adding data using Python from ArcGIS Online/ArcGIS REST Services directory to a ArcMap MXD 

ArcGIS Pro is better suited for this type of operation.

addDataFromPath 

TanuHoque
Esri Regular Contributor

It is not supported in ArcMap.

ArcGIS Pro 2.7 or above allows you to do that. Here is a sample code.

 

p = arcpy.mp.ArcGISProject("CURRENT")
m = p.listMaps()[0]
m.addDataFromPath("https://rmgsc.cr.usgs.gov/arcgis/rest/services/contUS/MapServer/0")

 

 

please note, it still gets added as an "ArcGIS Map Service" layer with one sublayer points to the raster layer off the map service.

0 Kudos
GiuseppeGentili
New Contributor III

Well, many thanks for your clarification and sample. My hope was to find a solution also for the ArcMap environment ... 

 

0 Kudos
TanuHoque
Esri Regular Contributor

since the discussion is about ArcMap, I'd like to point out to this post

https://support.esri.com/en/arcmap-esri-plan

0 Kudos