Rest Endpoints for Map Images in the Demographics and Lifestyle group in AGOL.

2588
2
11-01-2013 01:57 PM
NunoRicardo
New Contributor
How can I access a Rest End Point for a Map Image available in the Demographics and Lifestyle Group: http://www.arcgis.com/home/group.html?owner=esri&title=Demographics%20and%20Lifestyle? As an example you can use the following Map Image to reproduce the issue: http://www.arcgis.com/home/item.html?id=e092da2d21f144e4b08ac4565204ac9c. That is the Germany Average Household Size Map Image.

When I click on the link under Map Contents: http://demographics1.arcgis.com/arcgis/rest/services/DEU_Demographics_and_Boundaries/MapServer?ts=13..., I get the following error message:
Error: Services Directory has been disabled.
Code: 403

Thank you for your help
0 Kudos
2 Replies
CharlieFrye
Esri Contributor
While it is true that the REST endpoint HTML pages have been disabled for Esri's demographics services, landscape services and others, it is possible to access the information using the ArcGIS APIs, including Python as JSON rather than HTML.  Here is how using Python:

Let's access this information for the USA Median Age service.

The basic process is that you first need to get a token which will be used to authenticate you as a user when making the request for the service information.  Then the service information can be requested.  This example set of code gets the token and uses it to return the service's JSON and specifically get the spatial reference from the service.

# Import python modules
import arcpy
# For Http calls
import httplib, urllib, json

#Globals
refURL = 'ArcGIS'
AGOL_org_User_Name = " "
AGOL_passWord = " "

def getToken(username, password, serverName, serverPort):
    # A function to generate a token given username, password and the adminURL.

    # Token URL is typically http://server[:port]/arcgis/admin/generateToken
    tokenURL = "/sharing/generateToken"
    params = urllib.urlencode({'username': username, 'password': password, 'client': 'referer','referer': refURL, 'expiration':'60','f': 'json'})
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain",'referer': refURL}

    # Connect to URL and post parameters
    if serverPort == "":
        httpConn = httplib.HTTPSConnection(serverName)
    else:
        httpConn = httplib.HTTPConnection(serverName, serverPort)
    httpConn.request("POST", tokenURL, params, headers)

    # Read response
    response = httpConn.getresponse()
    if (response.status != 200):
        httpConn.close()
        FailType = 'Failed getting token; check connection and credentials'
        print "Error while fetching tokens from admin URL. Please check the URL and try again."
        return
    else:
        data = response.read()
        httpConn.close()
        # Check that data returned is not an error object
        if not assertJsonSuccess(data):
            return
        # Extract the token from it
        token = json.loads(data)
        return token['token']

def assertJsonSuccess(data):

    # A function that checks that the input JSON object
    #  is not an error object.

    obj = json.loads(data)
    if 'status' in obj and obj['status'] == "error":
        print "Error: JSON object returns an error. " + str(obj)
        return False
    else:
        return True

def main():
    #  ######   Access for Premium Demographic Content  ######

    # Get a Token
    demogServerName = "demographics1.arcgis.com"
    tokenServerName = "www.arcgis.com"
    serverPort = ""
    tokenString = (getToken(AGOL_org_User_Name,AGOL_passWord,tokenServerName,serverPort)).encode('ascii','ignore')

    # Get the Spatial Reference object from the demographic service from Esri
    demogServerName = "demographics1.arcgis.com"
    headers = {"Content-type": "application/x-www-form-urlencoded", "Accept": "text/plain",'referer': refURL}
    srSvcURL = "/arcgis/rest/services/USA_Demographics_and_Boundaries/MapServer"
    params2 = urllib.urlencode({'token': tokenString, 'f': 'JSON'})
    httpConn = httplib.HTTPSConnection(demogServerName)
    httpConn.request("POST", srSvcURL, params2, headers)
    response = httpConn.getresponse()
    httpReturnCode = response.status
    if (httpReturnCode != 200):
        httpConn.close()
        print "Could not read service information. Code " + str(httpReturnCode)  #403 = Forbidden, #498 = Bad Token
        FailType = "Could not get supply Spatial Reference"
        return
    else:
        data = response.read()
        httpConn.close()
    if not assertJsonSuccess(data):
        return
    dataObj2 = json.loads(data)
    srvJSONSR2 = dataObj2["spatialReference"]
    demandSR = arcpy.SpatialReference(int(srvJSONSR2.get('wkid')))

if __name__ == '__main__':
    main()


The Result, if you print the dataObj2 variable is:

{u'capabilities': u'Data,Map,Query', u'copyrightText': u'Esri, US Census Bureau, Navteq',
 u'currentVersion': 10.2,
 u'description': u'This thematic map contains over 2000 demographic variables from Esri Updated Demographics for 2013, including projections for 2018, and the United States 2010 Census, ready to be symbolized in ten different levels of geography. Two views are available. The default view depicts States at greater than 15m scale, Counties at 1.5m to 15m scale, Zip Codes at 500k to 1.5m scale, Census Tracts at 150k to 500k scale, and Census Block Groups at less than 150k scale. The second view allows the ten separate geography levels to be turned on or off individually, each including the full set of variables. The ten levels of geography represented are State, County, Tract, Block Group, Zip Code, Place, County Subdivision, Congressional District, CBSA and DMA.\n\nFor more information on this map, including our terms of use, visit us online at http://goto.arcgisonline.com/demographics/USA_Demographics_and_Boundaries',
 u'documentInfo': {u'AntialiasingMode': u'Fastest',
                   u'Author': u'Esri',
                   u'Category': u'',
                   u'Comments': u'This thematic map contains over 2000 demographic variables from Esri Updated Demographics for 2013, including projections for 2018, and the United States 2010 Census, ready to be symbolized in ten different levels of geography. Two views are available. The default view depicts States at greater than 15m scale, Counties at 1.5m to 15m scale, Zip Codes at 500k to 1.5m scale, Census Tracts at 150k to 500k scale, and Census Block Groups at less than 150k scale. The second view allows the ten separate geography levels to be turned on or off individually, each including the full set of variables. The ten levels of geography represented are State, County, Tract, Block Group, Zip Code, Place, County Subdivision, Congressional District, CBSA and DMA.\n\nFor more information on this map, including our terms of use, visit us online at http://goto.arcgisonline.com/demographics/USA_Demographics_and_Boundaries',
                   u'Keywords': u'polygon, demographics, census, state, county, tract, block group, Zip Code, postal, CBSA, DMA, place, county subdivision, congressional district, population, income, household, age, race, male, female',
                   u'Subject': u'USA Demographics and Boundaries includes over 2000 demographic variables from the 2010 Census ready to symbolize in ten different geographies.',
                   u'TextAntialiasingMode': u'Force',
                   u'Title': u'USA Demographics and Boundaries'},
 u'exportTilesAllowed': False,
 u'fullExtent': {u'spatialReference': {u'latestWkid': 3857, u'wkid': 102100},
                 u'xmax': -7452828.103399999,
                 u'xmin': -19942589.5526,
                 u'ymax': 11537127.463399999,
                 u'ymin': 2144435.340399999},
 u'initialExtent': {u'spatialReference': {u'latestWkid': 3857,
                                          u'wkid': 102100},
                    u'xmax': -6427885.287170573,
                    u'xmin': -20967532.368829425,
                    u'ymax': 12006762.069549998,
                    u'ymin': 8371850.2991352845},
 u'layers': [{u'defaultVisibility': True,
              u'id': 0,
              u'maxScale': 0,
              u'minScale': 0,
              u'name': u'Demographics and Boundaries',
              u'parentLayerId': -1,
              u'subLayerIds': [1, 9, 10, 11, 12, 16]},
             {u'defaultVisibility': True,
              u'id': 1,
              u'maxScale': 0,
              u'minScale': 0,
              u'name': u'State and County Boundaries',
              u'parentLayerId': 0,
              u'subLayerIds': [2, 3]},
             {u'defaultVisibility': True,
              u'id': 2,
              u'maxScale': 0,
              u'minScale': 1500000,
              u'name': u'County Boundaries',
              u'parentLayerId': 1,
              u'subLayerIds': None},
             {u'defaultVisibility': True,

## Shortened layers element to fit in post

 u'mapName': u'Layers',
 u'maxImageHeight': 4096,
 u'maxImageWidth': 4096,
 u'maxRecordCount': 3500,
 u'maxScale': 0,
 u'minScale': 0,
 u'serviceDescription': u'',
 u'singleFusedMapCache': False,
 u'spatialReference': {u'latestWkid': 3857, u'wkid': 102100},
 u'supportedExtensions': u'KmlServer',
 u'supportedImageFormatTypes': u'PNG32,PNG24,PNG,JPG,DIB,TIFF,EMF,PS,PDF,GIF,SVG,SVGZ,BMP',
 u'supportedQueryFormats': u'JSON, AMF',
 u'supportsDynamicLayers': True,
 u'tables': [],
 u'units': u'esriMeters'}
0 Kudos
NunoRicardo
New Contributor
Thank you Charlie.
0 Kudos