<!DOCTYPE html><htmllang="en-US"><head><metacharset="UTF-8"><metahttp-equiv="X-UA-Compatible"content="IE=Edge"><title>Python 3 - Oregon GEO</title><linkrel="shortcut icon"href="/home/favicon.ico"type="image/x-icon"><linkrel="stylesheet"href="/home/assets/css/just-the-docs-default.css"><scripttype="text/javascript"src="/home/assets/js/vendor/lunr.min.js"></script><scripttype="text/javascript"src="/home/assets/js/just-the-docs.js"></script><metaname="viewport"content="width=device-width, initial-scale=1"><!-- Begin Jekyll SEO tag v2.7.1 --><title>Python 3 | Oregon GEO</title><metaname="generator"content="Jekyll v3.8.7" /><metaproperty="og:title"content="Python 3" /><metaproperty="og:locale"content="en_US" /><metaname="description"content="The Oregon Geospatial Enterprise Office offers GIS Software, Services, and Coordination." /><metaproperty="og:description"content="The Oregon Geospatial Enterprise Office offers GIS Software, Services, and Coordination." /><linkrel="canonical"href="/home/docs/services/python.html" /><metaproperty="og:url"content="/home/docs/services/python.html" /><metaproperty="og:site_name"content="Oregon GEO" /><metaname="twitter:card"content="summary" /><metaproperty="twitter:title"content="Python 3" /><scripttype="application/ld+json"> {"url":"/home/docs/services/python.html","headline":"Python 3","description":"The Oregon Geospatial Enterprise Office offers GIS Software, Services, and Coordination.","@type":"WebPage","@context":"https://schema.org"}</script><!-- End Jekyll SEO tag --></head><body><svgxmlns="http://www.w3.org/2000/svg"style="display: none;"><symbolid="svg-link"viewBox="0 0 24 24"><title>Link</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-link"><pathd="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><pathd="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></symbol><symbolid="svg-search"viewBox="0 0 24 24"><title>Search</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-search"><circlecx="11"cy="11"r="8"></circle><linex1="21"y1="21"x2="16.65"y2="16.65"></line></svg></symbol><symbolid="svg-menu"viewBox="0 0 24 24"><title>Menu</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-menu"><linex1="3"y1="12"x2="21"y2="12"></line><linex1="3"y1="6"x2="21"y2="6"></line><linex1="3"y1="18"x2="21"y2="18"></line></svg></symbol><symbolid="svg-arrow-right"viewBox="0 0 24 24"><title>Expand</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-chevron-right"><polylinepoints="9 18 15 12 9 6"></polyline></svg></symbol><symbolid="svg-doc"viewBox="0 0 24 24"><title>Document</title><svgxmlns="http://www.w3.org/2000/svg"width="24"height="24"viewBox="0 0 24 24"fill="none"stroke="currentColor"stroke-width="2"stroke-linecap="round"stroke-linejoin="round"class="feather feather-file"><pathd="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polylinepoints="13 2 13 9 20 9"></polyline></svg></symbol></svg><divclass="side-bar"><divclass="site-header"><ahref="/home/"class="site-title lh-tight"> Oregon GEO </a><ahref="#"id="menu-button"class="site-button"><svgviewBox="0 0 24 24"class="icon"><usexlink:href="#svg-menu"></use></svg></a></div><navrole="navigation"aria-label="Main"id="site-nav"class="site-nav"><ulclass="nav-list"><liclass="nav-list-item"><ahref="/home/"class="nav-list-link">Home</a></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/arcgis-online.html"class="nav-list-link">ArcGIS Online</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/getting-started.html"class="nav-list-link">Getting Started</a></li><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/contacts.html"class="nav-list-link">Contacts</a></li><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/roles.html"class="nav-list-link">Roles</a></li><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/training.html"class="nav-list-link">Training</a></li><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/hub-premium.html"class="nav-list-link">Hub Premium</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/arcgis-online/tools.html"class="nav-list-link">Tools</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/home/docs/arcgis-online/thumbnail-builder.html"class="nav-list-link">Thumbnail Builder</a></li></ul></li></ul></li><liclass="nav-list-item active"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/services.html"class="nav-list-link">GIS Services</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/home/docs/services/inventory_3857.html"class="nav-list-link">Inventory (EPSG:3857)</a></li><liclass="nav-list-item "><ahref="/home/docs/services/inventory_2992.html"class="nav-list-link">Inventory (EPSG:2992)</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/services/mapping_examples.html"class="nav-list-link">Mapping Examples</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/home/docs/services/arcgis_api_javascript.html"class="nav-list-link">ArcGIS API for JavaScript</a></li></ul></li><liclass="nav-list-item active"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/services/query_examples.html"class="nav-list-link">Query Examples</a><ulclass="nav-list"><liclass="nav-list-item active"><ahref="/home/docs/services/python.html"class="nav-list-link active">Python 3</a></li></ul></li></ul></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/imagery.html"class="nav-list-link">Imagery Services</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/home/docs/imagery/inventory_3857.html"class="nav-list-link">Inventory (EPSG:3857)</a></li><liclass="nav-list-item "><ahref="/home/docs/imagery/inventory_2992.html"class="nav-list-link">Inventory (EPSG:2992)</a></li><liclass="nav-list-item "><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/imagery/examples.html"class="nav-list-link">Examples</a><ulclass="nav-list"><liclass="nav-list-item "><ahref="/home/docs/imagery/tile_service_arcgis.html"class="nav-list-link">(Tile Layer) ArcGIS API for JavaScript</a></li></ul></li></ul></li><liclass="nav-list-item"><ahref="/home/docs/geocoder.html"class="nav-list-link">Geocoder</a></li><liclass="nav-list-item"><ahref="#"class="nav-list-expander"><svgviewBox="0 0 24 24"><usexlink:href="#svg-arrow-right"></use></svg></a><ahref="/home/docs/software.html"class="nav-list-link">Software & Training</a><ulclass="nav-list "><liclass="nav-list-item "><ahref="/home/docs/software/products.html"class="nav-list-link">Esri Products</a></li><liclass="nav-list-item "><ahref="/home/docs/software/training.html"class="nav-list-link">Training</a></li></ul></li><liclass="nav-list-item"><ahref="/home/docs/contact.html"class="nav-list-link">Contact</a></li></ul></nav><footerclass="site-footer"> This site uses <ahref="https://github.com/pmarsceill/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll. </footer></div><divclass="main"id="top"><divid="main-header"class="main-header"><divclass="search"><divclass="search-input-wrap"><inputtype="text"id="search-input"class="search-input"tabindex="0"placeholder="Search Oregon GEO"aria-label="Search Oregon GEO"autocomplete="off"><labelfor="search-input"class="search-label"><svgviewBox="0 0 24 24"class="search-icon"><usexlink:href="#svg-search"></use></svg></label></div><divid="search-results"class="search-results"></div></div><navaria-label="Auxiliary"class="aux-nav"><ulclass="aux-nav-list"><liclass="aux-nav-list-item"><ahref="//www.oregon.gov/GEO/Pages/index.aspx"class="site-button" > Oregon Geospatial Enterprise Office </a></li></ul></nav></div><divid="main-content-wrap"class="main-content-wrap"><navaria-label="Breadcrumb"class="breadcrumb-nav"><olclass="breadcrumb-nav-list"><liclass="breadcrumb-nav-list-item"><ahref="/home/docs/services.html">GIS Services</a></li><liclass="breadcrumb-nav-list-item"><ahref="/home/docs/services/query_examples.html">Query Examples</a></li><liclass="breadcrumb-nav-list-item"><span>Python 3</span></li></ol></nav><divid="main-content"class="main-content"role="main"><h1id="python-3"><ahref="#python-3"class="anchor-heading"aria-labelledby="python-3"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> Python 3 </h1><p>In this example, we’re going to send a pair of coordinates to the <ahref="http://navigator.state.or.us/arcgis/rest/services/Framework/Admin_Bounds_WM/MapServer/4"target="_blank">House Districts service</a> to determine what house district the location is located in. We can also get the name of the elected representative associated with that district.</p><h2id="example"><ahref="#example"class="anchor-heading"aria-labelledby="example"><svgviewBox="0 0 16 16"aria-hidden="true"><usexlink:href="#svg-link"></use></svg></a> Example </h2><p><strong>Prerequisites</strong>:</p><ul><li><ahref="https://www.python.org/downloads/"target="_blank">Python 3</a></li><li><ahref="https://pypi.org/project/pip/"target="_blank">pip</a> (included by default starting at Python 3.4)</li></ul><p>Install the <ahref="http://docs.python-requests.org/en/master/"target="_blank">Requests package</a> with <ahref="https://docs.python.org/3/installing/index.html"target="_blank">pip</a>. <ahref="http://docs.python-requests.org/en/master/"target="_blank">Requests</a> is an HTTP library that we will use to make requests to our GIS services. You could also use something like the <ahref="https://docs.python.org/3/library/urllib.html">urllib package</a>.</p><p>In you command prompt, install the package with:</p><divclass="language-bash highlighter-rouge"><divclass="highlight"><preclass="highlight"><code>pip <spanclass="nb">install </span>requests
</code></pre></div></div><p>Next, create a file and name it <codeclass="highlighter-rouge">get_house_district.py</code> and paste the following:</p><divclass="language-python highlighter-rouge"><divclass="highlight"><preclass="highlight"><code><spanclass="kn">import</span><spanclass="nn">requests</span><spanclass="p">,</span><spanclass="n">json</span><spanclass="n">input_coordinates</span><spanclass="o">=</span><spanclass="p">{</span><spanclass="s">'lat'</span><spanclass="p">:</span><spanclass="mf">44.921019</span><spanclass="p">,</span><spanclass="s">'lng'</span><spanclass="p">:</span><spanclass="o">-</span><spanclass="mf">123.315793</span><spanclass="p">}</span><spanclass="n">house_districts_service_url</span><spanclass="o">=</span><spanclass="s">'https://navigator.state.or.us/arcgis/rest/services/Framework/Admin_Bounds_WM/MapServer/4/query'</span><spanclass="n">payload</span><spanclass="o">=</span><spanclass="p">{</span><spanclass="s">'inSR'</span><spanclass="p">:</span><spanclass="mi">4326</span><spanclass="p">,</span><spanclass="s">'geometry'</span><spanclass="p">:</span><spanclass="s">'{lng},{lat}'</span><spanclass="p">.</span><spanclass="nb">format</span><spanclass="p">(</span><spanclass="n">lng</span><spanclass="o">=</span><spanclass="n">input_coordinates</span><spanclass="p">[</span><spanclass="s">'lng'</span><spanclass="p">],</span><spanclass="n">lat</span><spanclass="o">=</span><spanclass="n">input_coordinates</span><spanclass="p">[</span><spanclass="s">'lat'</span><spanclass="p">]),</span><spanclass="s">'geometryType'</span><spanclass="p">:</span><spanclass="s">'esriGeometryPoint'</span><spanclass="p">,</span><spanclass="s">'spatialRel'</span><spanclass="p">:</span><spanclass="s">'esriSpatialRelIntersects'</span><spanclass="p">,</span><spanclass="s">'outSR'</span><spanclass="p">:</span><spanclass="mi">4326</span><spanclass="p">,</span><spanclass="s">'outFields'</span><spanclass="p">:</span><spanclass="s">'*'</span><spanclass="p">,</span><spanclass="s">'returnGeometry'</span><spanclass="p">:</span><spanclass="s">'false'</span><spanclass="p">,</span><spanclass="s">'f'</span><spanclass="p">:</span><spanclass="s">'json'</span><spanclass="p">}</span><spanclass="n">r</span><spanclass="o">=</span><spanclass="n">requests</span><spanclass="p">.</span><spanclass="n">get</span><spanclass="p">(</span><spanclass="n">house_districts_service_url</span><spanclass="p">,</span><spanclass="n">params</span><spanclass="o">=</span><spanclass="n">payload</span><spanclass="p">)</span><spanclass="n">match</span><spanclass="o">=</span><spanclass="n">json</span><spanclass="p">.</span><spanclass="n">loads</span><spanclass="p">(</span><spanclass="n">r</span><spanclass="p">.</span><spanclass="n">text</span><spanclass="p">)[</span><spanclass="s">'features'</span><spanclass="p">][</span><spanclass="mi">0</span><spanclass="p">][</span><spanclass="s">'attributes'</span><spanclass="p">]</span><spanclass="k">print</span><spanclass="p">(</span><spanclass="s">'Matched District:'</span><spanclass="p">,</span><spanclass="n">match</span><spanclass="p">[</span><spanclass="s">'DISTRICT'</span><spanclass="p">])</span><spanclass="c1"># 23
</span><spanclass="k">print</span><spanclass="p">(</span><spanclass="s">"District Party"</span><spanclass="p">,</span><spanclass="n">match</span><spanclass="p">[</span><spanclass="s">'Party'</span><spanclass="p">])</span><spanclass="c1"># Republican
</span><spanclass="k">print</span><spanclass="p">(</span><spanclass="s">"Representative:"</span><spanclass="p">,</span><spanclass="n">match</span><spanclass="p">[</span><spanclass="s">'Name'</span><spanclass="p">])</span><spanclass="c1"># Mike Nearman
</span></code></pre></div></div><p>This python code accomplished the following:</p><ol><li>Defines <codeclass="highlighter-rouge">input_coordinates</code>. This is the location we want to use to determine the intersecting house district.</li><li>Defines the URL (<codeclass="highlighter-rouge">house_districts_service_url</code>) and parameters (<codeclass="highlighter-rouge">payload</code>) we want to build our query (more on this below).</li><li>Sends the request to the GIS service using the Requests library.</li><li>Parses the request and prints the matched output.</li></ol><p>Probably the most difficult part of this process is understanding what parameters we need to pass to the request. The <ahref="http://navigator.state.or.us/arcgis/sdk/rest/index.html#//02ss0000000r000000"target="_blank">ArcGIS REST API</a> 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:</p><ul><li><p><strong>inSR</strong> - The input spatial reference or our coordinates. In this case, we are providing geographic coordinates in degrees using <ahref="https://epsg.io/4326"target="_blank">EPSG:4326</a>.</p></li><li><p><strong>geometry</strong> - The input coordinates for our point.</p></li><li><p><strong>geometryType</strong> - The geometry type, in our case a point.</p></li><li><p><strong>spatialRel</strong> - The spatial relationship. In our case, we are looking for the house district that <codeclass="highlighter-rouge">intersects</code> with our point.</p></li><li><p><strong>outSR</strong> - The spatial reference we want our result to be in.</p></li><li><p><strong>ourFields</strong> - The fields we want returned. In our case, we want all fields (<codeclass="highlighter-rouge">*</code>).</p></li><li><p><strong>returnGeometry</strong> - Whether or not we want the actual geometry of the intersecting house district.</p></li><li><p><strong>f</strong> - The type of response we want returned.</p></li></ul></div></div><divclass="search-overlay"></div></div></body></html>