Select to view content in your preferred language

Capture the JSON of a webmap using a GP widget, instead of a Print widget?

1038
1
08-29-2017 10:53 AM
by Anonymous User
Not applicable

I am having issues using the arcpy.mapping.ConvertWebMapToMapDocument function in a GP service. It works great when wired into the Print widget in Web App Builder, which uses the Print Task. However, the same GP service returns an empty JSON string from the webmap if run in the GP widget.

Is there a way to capture the JSON of a webmap (within AGOL's web app builder) just using a GP widget, instead of a Print widget? I am not seeing any verification that this function is limited to the Print widget or the Print Task in the API. It is 'suggested' that one uses the Print Task, but doesn't say one must.

Ideas?

Tags (1)
0 Kudos
1 Reply
by Anonymous User
Not applicable

There is no way to capture it from a GP Service, as that is happening outside of the Web Application environment.  However, you can very easily generate a WebMapAsJSON from within a custom widget and pass it as a parameter in the GP Service.  This is how I have done it in the past (note it does use the PrintTask just to get a helper method):

        var printTask = new PrintTask()
        var template = new PrintTemplate();
        template.preserveScale = false;
        var params = new PrintParameters();
        params.map = this.map;
        params.template = template;
        var mapAsJson = printTask._getPrintDefinition(this.map, params);

        // get laoyout options for legend (excluded layers are specified in config.json)
        var skip = ['defaultBasemap', 'basemap', 'graphicsLayer1', 'map_graphics'].concat(this.excludedLayerIds);
        var legendOptions = {operationalLayers: []}
        mapAsJson.operationalLayers.forEach(function(layer){
          if (skip.indexOf(layer.id) < 0){
            var legendLayer = {id: layer.id}
            if (layer.hasOwnProperty('visibleLayers')){
              legendLayer.subLayerIds = layer.visibleLayers
            }
            legendOptions.operationalLayers.push(legendLayer)
          }
        })
        var layoutOptions = {
          "copyrightText": "",
          "authorText": "",
          "legendOptions": legendOptions
        }
        mapAsJson.layoutOptions = layoutOptions

        // now you can pass the "mapAsJson" object into a GP Service or do whatever with it