pythonrestarcgis

Unable to get shape data from ArcGIS REST API


I'm trying to download hurricane data from ArcGIS. There are two things I can do.

  1. I can go to their web interface at https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/1/query Set a few values ('Where' field I set to 1=1, 'Out Fields' I set to *) Then scroll to the bottom and click the "Query(GET)" button. Some values come up on the screen. All Good, but no shape data.

  2. I can use Python (this is my real goal).
    I have a script to do this. Sorry for the nasty looking code ... the CODE icon in Stack Overflow does not seem to be working at the moment.

    import urllib.request, urllib.error, urllib.parse
    import json
    
    
    
    url_json= 'https://services9.arcgis.com/RHVPKKiFTONKtxq3/ArcGIS/rest/services/Active_Hurricanes_v1/FeatureServer/0/query?where=1%3D1&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&resultType=none&distance=0.0&units=esriSRUnit_Meter&returnGeodetic=false&outFields=*&returnGeometry=false&featureEncoding=esriDefault&multipatchOption=xyFootprint&maxAllowableOffset=&geometryPrecision=&outSR=&datumTransformation=&applyVCSProjection=false&returnIdsOnly=false&returnUniqueIdsOnly=false&returnCountOnly=false&returnExtentOnly=false&returnQueryGeometry=false&returnDistinctValues=false&cacheHint=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&having=&resultOffset=&resultRecordCount=&returnZ=false&returnM=false&returnExceededLimitFeatures=true&quantizationParameters=&sqlFormat=none&f=pjson&token='    
    
    response = urllib.request.urlopen(url_json) 
    
    
    webContent = response.read()  # byte string of the JSON result
    
    
    jsonContent = json.loads(webContent)  # create a JSON object from the byte string
    
    
    for object in jsonContent['features']:
    
    
        rec = {}  # create an empty dictionary for each feature 
    
    
        for k in object['attributes'].keys():
    
    
            rec[k] = object['attributes'][k]
    

To get the url I used in the python program, I just pressed the button on (1) and then used the info in the URL bar. Only difference is I selected JSON instead of html for output. Problem: I can't figure out how to get it to return the blob for the shape info. Does his API permit that? If so, what do I do to in the query window and in the URL to get it?


Solution

  • As you can see at the REST endpoint, the geometry type for the Feature Layer is esriGeometryPoint. The URL in your script uses geometryType=esriGeometryEnvelope, which returns extent. Additionally, the returnGeometry parameter is set to false, so no geometry will be returned regardless of what the geometryType parameter is set to.

    Try setting geometryType to esriGeometryPoint and returnGeometry to true. Geometry should then been returned in the resulting JSON as x/y coordinates correlating to latitude and longitude:

    "geometry" : 
          {
            "x" : -64.899999998509884, 
            "y" : 28.199999999254942
          }
    

    EDIT: the geometryType parameter actually relates to spatial queries, not returning geometry. Only update required to resolve this issue is returnGeometry to true (see doc).