python.html

30 lines | 16.942 kB Blame History Raw Download
<!DOCTYPE html> <html lang="en-US"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=Edge"> <title>Python 3 - Oregon GEO</title> <link rel="shortcut icon" href="/home/favicon.ico" type="image/x-icon"> <link rel="stylesheet" href="/home/assets/css/just-the-docs-default.css"> <script type="text/javascript" src="/home/assets/js/vendor/lunr.min.js"></script> <script type="text/javascript" src="/home/assets/js/just-the-docs.js"></script> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- Begin Jekyll SEO tag v2.7.1 --> <title>Python 3 | Oregon GEO</title> <meta name="generator" content="Jekyll v3.8.7" /> <meta property="og:title" content="Python 3" /> <meta property="og:locale" content="en_US" /> <meta name="description" content="The Oregon Geospatial Enterprise Office offers GIS Software, Services, and Coordination." /> <meta property="og:description" content="The Oregon Geospatial Enterprise Office offers GIS Software, Services, and Coordination." /> <link rel="canonical" href="/home/docs/services/python.html" /> <meta property="og:url" content="/home/docs/services/python.html" /> <meta property="og:site_name" content="Oregon GEO" /> <meta name="twitter:card" content="summary" /> <meta property="twitter:title" content="Python 3" /> <script type="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> <svg xmlns="http://www.w3.org/2000/svg" style="display: none;"> <symbol id="svg-link" viewBox="0 0 24 24"> <title>Link</title> <svg xmlns="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"> <path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="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> <symbol id="svg-search" viewBox="0 0 24 24"> <title>Search</title> <svg xmlns="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"> <circle cx="11" cy="11" r="8"></circle><line x1="21" y1="21" x2="16.65" y2="16.65"></line> </svg> </symbol> <symbol id="svg-menu" viewBox="0 0 24 24"> <title>Menu</title> <svg xmlns="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"> <line x1="3" y1="12" x2="21" y2="12"></line><line x1="3" y1="6" x2="21" y2="6"></line><line x1="3" y1="18" x2="21" y2="18"></line> </svg> </symbol> <symbol id="svg-arrow-right" viewBox="0 0 24 24"> <title>Expand</title> <svg xmlns="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"> <polyline points="9 18 15 12 9 6"></polyline> </svg> </symbol> <symbol id="svg-doc" viewBox="0 0 24 24"> <title>Document</title> <svg xmlns="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"> <path d="M13 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V9z"></path><polyline points="13 2 13 9 20 9"></polyline> </svg> </symbol> </svg> <div class="side-bar"> <div class="site-header"> <a href="/home/" class="site-title lh-tight"> Oregon GEO </a> <a href="#" id="menu-button" class="site-button"> <svg viewBox="0 0 24 24" class="icon"><use xlink:href="#svg-menu"></use></svg> </a> </div> <nav role="navigation" aria-label="Main" id="site-nav" class="site-nav"> <ul class="nav-list"><li class="nav-list-item"><a href="/home/" class="nav-list-link">Home</a></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/arcgis-online.html" class="nav-list-link">ArcGIS Online</a><ul class="nav-list "><li class="nav-list-item "><a href="/home/docs/arcgis-online/getting-started.html" class="nav-list-link">Getting Started</a></li><li class="nav-list-item "><a href="/home/docs/arcgis-online/contacts.html" class="nav-list-link">Contacts</a></li><li class="nav-list-item "><a href="/home/docs/arcgis-online/roles.html" class="nav-list-link">Roles</a></li><li class="nav-list-item "><a href="/home/docs/arcgis-online/training.html" class="nav-list-link">Training</a></li><li class="nav-list-item "><a href="/home/docs/arcgis-online/hub-premium.html" class="nav-list-link">Hub Premium</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/arcgis-online/tools.html" class="nav-list-link">Tools</a><ul class="nav-list"><li class="nav-list-item "> <a href="/home/docs/arcgis-online/thumbnail-builder.html" class="nav-list-link">Thumbnail Builder</a> </li></ul></li></ul></li><li class="nav-list-item active"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/services.html" class="nav-list-link">GIS Services</a><ul class="nav-list "><li class="nav-list-item "><a href="/home/docs/services/inventory_3857.html" class="nav-list-link">Inventory (EPSG:3857)</a></li><li class="nav-list-item "><a href="/home/docs/services/inventory_2992.html" class="nav-list-link">Inventory (EPSG:2992)</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/services/mapping_examples.html" class="nav-list-link">Mapping Examples</a><ul class="nav-list"><li class="nav-list-item "> <a href="/home/docs/services/arcgis_api_javascript.html" class="nav-list-link">ArcGIS API for JavaScript</a> </li></ul></li><li class="nav-list-item active"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/services/query_examples.html" class="nav-list-link">Query Examples</a><ul class="nav-list"><li class="nav-list-item active"> <a href="/home/docs/services/python.html" class="nav-list-link active">Python 3</a> </li></ul></li></ul></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/imagery.html" class="nav-list-link">Imagery Services</a><ul class="nav-list "><li class="nav-list-item "><a href="/home/docs/imagery/inventory_3857.html" class="nav-list-link">Inventory (EPSG:3857)</a></li><li class="nav-list-item "><a href="/home/docs/imagery/inventory_2992.html" class="nav-list-link">Inventory (EPSG:2992)</a></li><li class="nav-list-item "><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/imagery/examples.html" class="nav-list-link">Examples</a><ul class="nav-list"><li class="nav-list-item "> <a href="/home/docs/imagery/tile_service_arcgis.html" class="nav-list-link">(Tile Layer) ArcGIS API for JavaScript</a> </li></ul></li></ul></li><li class="nav-list-item"><a href="/home/docs/geocoder.html" class="nav-list-link">Geocoder</a></li><li class="nav-list-item"><a href="#" class="nav-list-expander"><svg viewBox="0 0 24 24"><use xlink:href="#svg-arrow-right"></use></svg></a><a href="/home/docs/software.html" class="nav-list-link">Software & Training</a><ul class="nav-list "><li class="nav-list-item "><a href="/home/docs/software/products.html" class="nav-list-link">Esri Products</a></li><li class="nav-list-item "><a href="/home/docs/software/training.html" class="nav-list-link">Training</a></li></ul></li><li class="nav-list-item"><a href="/home/docs/contact.html" class="nav-list-link">Contact</a></li></ul> </nav> <footer class="site-footer"> This site uses <a href="https://github.com/pmarsceill/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll. </footer> </div> <div class="main" id="top"> <div id="main-header" class="main-header"> <div class="search"> <div class="search-input-wrap"> <input type="text" id="search-input" class="search-input" tabindex="0" placeholder="Search Oregon GEO" aria-label="Search Oregon GEO" autocomplete="off"> <label for="search-input" class="search-label"><svg viewBox="0 0 24 24" class="search-icon"><use xlink:href="#svg-search"></use></svg></label> </div> <div id="search-results" class="search-results"></div> </div> <nav aria-label="Auxiliary" class="aux-nav"> <ul class="aux-nav-list"> <li class="aux-nav-list-item"> <a href="//www.oregon.gov/GEO/Pages/index.aspx" class="site-button" > Oregon Geospatial Enterprise Office </a> </li> </ul> </nav> </div> <div id="main-content-wrap" class="main-content-wrap"> <nav aria-label="Breadcrumb" class="breadcrumb-nav"> <ol class="breadcrumb-nav-list"> <li class="breadcrumb-nav-list-item"><a href="/home/docs/services.html">GIS Services</a></li> <li class="breadcrumb-nav-list-item"><a href="/home/docs/services/query_examples.html">Query Examples</a></li> <li class="breadcrumb-nav-list-item"><span>Python 3</span></li> </ol> </nav> <div id="main-content" class="main-content" role="main"> <h1 id="python-3"> <a href="#python-3" class="anchor-heading" aria-labelledby="python-3"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Python 3 </h1> <p>In this example, we’re going to send a pair of coordinates to the <a href="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> <h2 id="example"> <a href="#example" class="anchor-heading" aria-labelledby="example"><svg viewBox="0 0 16 16" aria-hidden="true"><use xlink:href="#svg-link"></use></svg></a> Example </h2> <p><strong>Prerequisites</strong>:</p> <ul> <li><a href="https://www.python.org/downloads/" target="_blank">Python 3</a></li> <li><a href="https://pypi.org/project/pip/" target="_blank">pip</a> (included by default starting at Python 3.4)</li> </ul> <p>Install the <a href="http://docs.python-requests.org/en/master/" target="_blank">Requests package</a> with <a href="https://docs.python.org/3/installing/index.html" target="_blank">pip</a>. <a href="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 <a href="https://docs.python.org/3/library/urllib.html">urllib package</a>.</p> <p>In you command prompt, install the package with:</p> <div class="language-bash highlighter-rouge"><div class="highlight"><pre class="highlight"><code>pip <span class="nb">install </span>requests
</code></pre></div></div> <p>Next, create a file and name it <code class="highlighter-rouge">get_house_district.py</code> and paste the following:</p> <div class="language-python highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">import</span> <span class="nn">requests</span><span class="p">,</span> <span class="n">json</span>
 
<span class="n">input_coordinates</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">'lat'</span><span class="p">:</span> <span class="mf">44.921019</span><span class="p">,</span>
    <span class="s">'lng'</span><span class="p">:</span> <span class="o">-</span><span class="mf">123.315793</span>
<span class="p">}</span>
 
<span class="n">house_districts_service_url</span> <span class="o">=</span> <span class="s">'https://navigator.state.or.us/arcgis/rest/services/Framework/Admin_Bounds_WM/MapServer/4/query'</span>
 
<span class="n">payload</span> <span class="o">=</span> <span class="p">{</span>
    <span class="s">'inSR'</span><span class="p">:</span> <span class="mi">4326</span><span class="p">,</span>
    <span class="s">'geometry'</span><span class="p">:</span> <span class="s">'{lng},{lat}'</span><span class="p">.</span><span class="nb">format</span><span class="p">(</span><span class="n">lng</span><span class="o">=</span><span class="n">input_coordinates</span><span class="p">[</span><span class="s">'lng'</span><span class="p">],</span><span class="n">lat</span><span class="o">=</span><span class="n">input_coordinates</span><span class="p">[</span><span class="s">'lat'</span><span class="p">]),</span>
    <span class="s">'geometryType'</span><span class="p">:</span> <span class="s">'esriGeometryPoint'</span><span class="p">,</span>
    <span class="s">'spatialRel'</span><span class="p">:</span> <span class="s">'esriSpatialRelIntersects'</span><span class="p">,</span>
    <span class="s">'outSR'</span><span class="p">:</span> <span class="mi">4326</span><span class="p">,</span>
    <span class="s">'outFields'</span><span class="p">:</span> <span class="s">'*'</span><span class="p">,</span>
    <span class="s">'returnGeometry'</span><span class="p">:</span> <span class="s">'false'</span><span class="p">,</span>
    <span class="s">'f'</span><span class="p">:</span> <span class="s">'json'</span>
<span class="p">}</span>  
 
<span class="n">r</span> <span class="o">=</span> <span class="n">requests</span><span class="p">.</span><span class="n">get</span><span class="p">(</span><span class="n">house_districts_service_url</span><span class="p">,</span> <span class="n">params</span><span class="o">=</span><span class="n">payload</span><span class="p">)</span>
 
<span class="n">match</span> <span class="o">=</span> <span class="n">json</span><span class="p">.</span><span class="n">loads</span><span class="p">(</span><span class="n">r</span><span class="p">.</span><span class="n">text</span><span class="p">)[</span><span class="s">'features'</span><span class="p">][</span><span class="mi">0</span><span class="p">][</span><span class="s">'attributes'</span><span class="p">]</span>
 
<span class="k">print</span><span class="p">(</span><span class="s">'Matched District:'</span><span class="p">,</span> <span class="n">match</span><span class="p">[</span><span class="s">'DISTRICT'</span><span class="p">])</span> <span class="c1"># 23
</span><span class="k">print</span><span class="p">(</span><span class="s">"District Party"</span><span class="p">,</span> <span class="n">match</span><span class="p">[</span><span class="s">'Party'</span><span class="p">])</span> <span class="c1"># Republican
</span><span class="k">print</span><span class="p">(</span><span class="s">"Representative:"</span><span class="p">,</span> <span class="n">match</span><span class="p">[</span><span class="s">'Name'</span><span class="p">])</span> <span class="c1"># Mike Nearman
</span></code></pre></div></div> <p>This python code accomplished the following:</p> <ol> <li>Defines <code class="highlighter-rouge">input_coordinates</code>. This is the location we want to use to determine the intersecting house district.</li> <li>Defines the URL (<code class="highlighter-rouge">house_districts_service_url</code>) and parameters (<code class="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 <a href="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 <a href="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 <code class="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 (<code class="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> <div class="search-overlay"></div> </div> </body> </html>