I want to batch insert WMS layer to my arcmap MXD project with python/arcpy script.
Some issues has been unanserwed, here my "state of art":
All of these solution propose to use
- ArcMap to Add WMS layer manually
- do "Save as Layer File"
-
reuse the .lyr to add to a new TOC by arcpy with this code
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] imagery_lyr = arcpy.mapping.Layer(r"path\to\.lyr") arcpy.mapping.AddLayer(df, imagery_lyr)
The lastest idea work properly with "static" WMS services.
When you have a "dynamic" changing WMS services, you need to maintain manually all the pre-generated lyr.
Does somebody know how to load WMS layer into ArcMap MXD with arcpy?
Does somebody know how to create .lyr file from WMS with arcpy?
Best Answer
This is not possible in pure arcpy.
It can be done in python, but requires the use of arcobjects. One way to access arcobjects in python is to use the
comtypes
package.Firstly install
comtypes
and get set up with the arcobjects snippets module. See this question for links to the snippets module and for more information on accessing arcobjects from python.Below is some code, derived from the ArcGIS documentation and the ArcMap CSW Client to generate a layer file of the WMS. You need to write the layer file to disk to be able to use ArcPy to add it to an MXD, but you can delete the file once it's been added to the map.