How to copy layer from one APRX map to another?

423
2
11-02-2020 03:13 PM
Arne_Gelfert
Regular Contributor

Trying to get a graphics layer that was created from JSON (using the whole webmap to JSON workflow) from one map in APRX to another map using arcpy.

I've tried this:

myLayer = map1.listLayers("myLayer")[0]
map2.addLayer(myLayer)‍‍‍

map2.listLayers()

The layer now shows up in the layer list. But I can't see it in the map. So I tried this:

layerDef = json.loads(Web_Map_as_JSON)['operationalLayers'][<index of layer>]

map2.addLayer(map1.listLayer("myLayer")[0]
map2.listLayers("myLayer")[0].updateLayerFromJSON(layerDef)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Everything runs without errors. I just don't get the added layer to display. Any suggestions?

0 Kudos
2 Replies
Arne_Gelfert
Regular Contributor

Wanted to add a little more context in the hopes of garnering some feedback.

Using ConvertWebMapToArcGISProject  to create a map. Now as part of the JSON, I'm ending up with a graphics layer. It looks something like this:

{
    "mapOptions": {
        ...
        },
        ...
    },
    "operationalLayers": [
        {
            "id": "defaultBasemap",
            ...
        },
        {
            "id": "<map servce name>",
            ....
        },
        {
            "id": "<mygraphicslayer>",
            "opacity": 1,
            "minScale": 0,
            "maxScale": 0,
            "featureCollection": {
                "layers": [
                    {
                        "layerDefinition": {
                            "name": "pointLayer",
                            "geometryType": "esriGeometryPoint"
                        },
                        "featureSet": {
                            "geometryType": "esriGeometryPoint",
                            "features": [
                                {
                                    "geometry": {
                                        "x": someX,
                                        "y": someY,
                                        "spatialReference": {
                                            "wkid": 102100
                                        }
                                    },
				...
                                    
                                }
                            ]
                        }
                    }
                ]
            }
        }
    ]
}

I can remove <mygraphicsLayer> from map by doing 

mymap.removeLayer(mymap.listLayers("pointLayer")[0] 

But I can't add this "pointLayer" to another map (see my first post).

Is there any way to grab a portion of the JSON above...

json.loads(Web_Map_as_JSON)['operationalLayers'][2]

to define a layer and add it to another map? Such as ... 

json.loads(Web_Map_as_JSON)['operationalLayers'][2]["featureCollection"]["layers"][0]    # doesnt seem to work

... or get some viable layer definition that can be fed into ...

arcpy.JSONToFeatures_conversion()

... and then add those to the other map? 

0 Kudos
Arne_Gelfert
Regular Contributor

Last ditch effort to find someone who knows... 

These are steps I have taken to get me to where I am.

  1. Export JSON from web map
  2. Use Python to turn web map into APRX using a layout with 2 map frames.
    1. The MainMapFrame gets all the layers from JSON.including graphics created in web map.
    2. The other map frame gets layers as declared in layout template.
  3. Now, I'd like to copy one of the graphics layers from MainMapFrame to the other mapframe.
  4. So that I can print it all to PDF.

Nothing has worked so far. I even tried this:

myLayer = map1.listLayers('myLayer')[0]
myLayer.saveACopy(r"...\myLayer.lyrx")
map2.addLayer(arcpy.mp.LayerFile(r"...\mylayer.lyrx"))

Both map frames have same spatial reference and share other common service layers that display without any issues. Must be something about graphics.

0 Kudos