python.md

88 lines | 3.58 kB Blame History Raw Download

layout: default title: Python 3 nav_order: 1 grand_parent: GIS Services parent: Query Examples

Python 3

In this example, we're going to send a pair of coordinates to the House Districts service{:target="_blank"} to determine what house district the location is located in. We can also get the name of the elected representative associated with that district.

Example

Prerequisites:

  • Python 3{:target="_blank"}
  • pip{:target="_blank"} (included by default starting at Python 3.4)

Install the Requests package{:target="_blank"} with pip{:target="_blank"}. Requests{:target="_blank"} is an HTTP library that we will use to make requests to our GIS services. You could also use something like the urllib package.

In you command prompt, install the package with:

pip install requests

Next, create a file and name it get_house_district.py and paste the following:

import requests, json
 
input_coordinates = {
    'lat': 44.921019,
    'lng': -123.315793
}
 
house_districts_service_url = 'https://navigator.state.or.us/arcgis/rest/services/Framework/Admin_Bounds_WM/MapServer/4/query'
 
payload = {
    'inSR': 4326,
    'geometry': '{lng},{lat}'.format(lng=input_coordinates['lng'],lat=input_coordinates['lat']),
    'geometryType': 'esriGeometryPoint',
    'spatialRel': 'esriSpatialRelIntersects',
    'outSR': 4326,
    'outFields': '*',
    'returnGeometry': 'false',
    'f': 'json'
}  
 
r = requests.get(house_districts_service_url, params=payload)
 
match = json.loads(r.text)['features'][0]['attributes']
 
print('Matched District:', match['DISTRICT']) # 23
print("District Party", match['Party']) # Republican
print("Representative:", match['Name']) # Mike Nearman

This python code accomplished the following:

  1. Defines input_coordinates. This is the location we want to use to determine the intersecting house district.
  2. Defines the URL (house_districts_service_url) and parameters (payload) we want to build our query (more on this below).
  3. Sends the request to the GIS service using the Requests library.
  4. Parses the request and prints the matched output.

Probably the most difficult part of this process is understanding what parameters we need to pass to the request. The ArcGIS REST API{:target="_blank"} documents what parameters you can use in the request, but this might not be helpful to developers not familiar with geospatial concepts. Here is a quick guide to understanding our inputs:

  • inSR - The input spatial reference or our coordinates. In this case, we are providing geographic coordinates in degrees using EPSG:4326{:target="_blank"}.

  • geometry - The input coordinates for our point.

  • geometryType - The geometry type, in our case a point.

  • spatialRel - The spatial relationship. In our case, we are looking for the house district that intersects with our point.

  • outSR - The spatial reference we want our result to be in.

  • ourFields - The fields we want returned. In our case, we want all fields (*).

  • returnGeometry - Whether or not we want the actual geometry of the intersecting house district.

  • f - The type of response we want returned.