Can I configure pop-ups, add to a new map and save the map from PYTHON in 10.1?

814
6
10-09-2013 04:48 PM
PaulSimpson
New Contributor III
We have 3 employees with varying levels of computer savvy who need to generate several unique maps each day to be shared via one of the web app templates.  I'm trying to automate as much of that process as possible.  I can publish a feature service and share it with its appropriate group using arcpy in 10.1.  I managed to figure out how to add Access and Use Constraints and Credits to the Feature Service Definition (I'm no XML/minidom guru). 

Is there a way to configure the Feature Pop-ups as well, setting the pop-up title to a certain field, and the contents to another?

The biggest problem is having to manually add the feature service to a map and save the map.  That means typing in consistent titles, tags (which are critical for searching in the web app), and summary, and then manually entering the Access and Use Constraints and Credits before sharing the map. 

Can I generate a Service Definition for a whole map which includes the feature service layer?  I could then enter the metadata for the map as I have for the feature layer.  The basemap is not critical for this so I just allow the web app to default and then provide user options to change it.

I hope this makes sense. 

Thanks

Paul

I'm posting the code snippet that deals with the Service Definitions in case it's helpful:
import arcpy
import sys
import traceback
import os
import datetime
import xml.dom.minidom as DOM

    try:

        ##      The following code is adapted from code posted to the ESRI forum by Jeff Moulds, ESRI

        mapDoc = mxd
        sddraft = os.path.join( workspace, 'HostedMS.sddraft')

    ##      Parse out date, city and maxCategory (eps)
        categories = ["Good","Moderate","Unhealthy for Sensitive Groups","Unhealthy","Very Unhealthy","Hazardous","Hazardous"]
        maxCategory = categories[maxGridCode-1]
        (month, day, year, hour, minute) = parseDate(filebase)

##        some date, time, city parsing / string snippet construction

        sddraftSummary = "PM2.5 data from " + city + ", AK, collected on " + date + time
        sddraftTags = ",".join(["PM2.5,Air Quality,Sniffer Map",city,maxCategory])

        service = city + " " + date + " starting " + str(hour) + str(minute)
        serverType = 'MY_HOSTED_SERVICES'
        onlineDirectory = "Sniffer_Maps"        # this doesn't work
            
        tellMe("Running CreateMapSDDraft ...")
        arcpy.mapping.CreateMapSDDraft(mapDoc, sddraft, service, serverType, folder_name=onlineDirectory, summary=sddraftSummary, tags=sddraftTags)

    ###     Returns python Dict of 'errors', 'messages', and 'warnings'
    ###     Need to set data frame extent and projection
        
        con = 'My Hosted Services'

        workspace = os.path.dirname(workspace)
        tellMe("Placing Service Definition files in: " + workspace)

        sd = os.path.join(workspace,service + '.sd')

        # Read the sddraft xml.
        doc = DOM.parse(sddraft)
        # Change service from map service to feature service
        typeNames = doc.getElementsByTagName('TypeName')
        for typeName in typeNames:
            # Get the TypeName we want to disable.
            if typeName.firstChild.data == "MapServer":
                    typeName.firstChild.data = "FeatureServer"

        #Turn off caching
        configProps = doc.getElementsByTagName('ConfigurationProperties')[0]
        propArray = configProps.firstChild
        propSets = propArray.childNodes
        for propSet in propSets:
            keyValues = propSet.childNodes
            for keyValue in keyValues:
                if keyValue.tagName == 'Key':
                    if keyValue.firstChild.data == "isCached":
                        # turn on caching
                        keyValue.nextSibling.firstChild.data = "false"

        disclaimer = 'Pre-defined disclaimer'

        disclaimNode = doc.createTextNode(disclaimer)
        acc = doc.getElementsByTagName("AccessInformation")[0]
        acc.appendChild(disclaimNode)

        credits = "Pre-defined Credits"

        cre = doc.getElementsByTagName("Credits")[0]
        credNode = doc.createTextNode(credits)
        cre.appendChild(credNode)

        tellMe("Writing XML ...")
        outXml = os.path.join(workspace,'HostedMSNew.sddraft') 
        f = open(outXml, 'w')     
        doc.writexml( f )     
        f.close()

        tellMe("Running AnalyzeForSD ...")
        analysis = arcpy.mapping.AnalyzeForSD(outXml)
        if len(analysis['errors'])>0:
            tellMe( str(analysis))
            sys.exit(analysis['errors'])
        
        #arcpy.SignOutFromPortal_server()
##        tellMe("Signing In ...")
##        username = "*****"
##        password = "*****"
##        arcpy.SignInToPortal_server(username,password,"http://www.arcgis.com/")

        tellMe("Running StageService_server ...")
        arcpy.StageService_server(outXml, sd)
        tellMe("Running UploadServiceDefinition_server ...")
        sharegroup = "Sniffer Map Feature Layer Services"
        override = "OVERRIDE_DEFINITION"
        arcpy.UploadServiceDefinition_server(sd, con, in_override=override, in_groups=sharegroup)

    finally:
        tellMe("Cleaning up scratch MXD.")
        arcpy.mapping.RemoveLayer(df, lyrs[0])
        mxd.save()
        del mxd
Tags (2)
0 Kudos
6 Replies
PaulSimpson
New Contributor III
Well, it looks like the pop-ups are a feature of the feature service, not the browser, since configuring them in one browser applies to all.  It should be possible to configure the feature service pop-ups by editing the draft service definition.  Perhaps ESRI can work on enabling such a feature.

I have been unable to find any information otherwise, so it appears that complete web maps can't be generated via PYTHON.  Perhaps ESRI can consider adding the ability to apply the metadata from a layer to the map within which it resides.  Perhaps a drop down arrow next to the layer in the map's "View item details" screen, or perhaps an "Import from layer" button in the map's Edit item details screen.  That way the title, summary, description, all the keys, access and use constraints, and credits can be automatically generated once for the layer, and the user would not have to copy-paste, with the errors inherent with that process, all those into the map.
0 Kudos
by Anonymous User
Not applicable
Hi Paul,

One way would be to define the pop-up in the webmap as shown in the Online Help. So you'd have to generate the json in python, and upload it to the web map item.

Hope that helps,
Andrew
0 Kudos
PaulSimpson
New Contributor III
Thanks Andrew.  It looks promising.  I haven't looked at JSON at all (nor Javascript, nor JAVA, nor REST) so I'll need to get an education.  I have been able to look at the JSON for a working map and see how it should look. 

Do you know where I can find a reference for uploading the JSON to the map?  I found this document for iphones but I haven't been able to find the equivalent to the "Supported Operations: Add Item" button mentioned there.  We are using ArcGIS.com rather than our own server, which is the focus of the iphone document. 

Thanks again.

Paul
0 Kudos
by Anonymous User
Not applicable
Hi Paul,

In python I believe you'll have to use httplib to make request to the ArcGIS Rest API. In the REST API, depending if you are creating the item or updating existing, you will add the json to the 'text' parameter.

REST API - Update Item
REST API - Add Item

Hope that helps!
Andrew
0 Kudos
PaulSimpson
New Contributor III
Thanks Andrew!  It'll be a little while before I have the time to devote to it again, but from what I've read so far it looks like I can get there from here. 

I'll (try real hard to remember to) mark this as answered once I do.

Thanks again!

Paul
0 Kudos
by Anonymous User
Not applicable
We're also working on a little script tool that you might be able to build off of. Once we have it, i'll post a link to it here so you can have a bigger jump start.

Cheers,
Andrew