python.html

781 lines | 24.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="stylesheet" href="/home/assets/css/just-the-docs.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">
</head>

<body>

  <div class="page-wrap">
    <div class="side-bar">
      <a href="/home" class="site-title fs-6 lh-tight">Oregon GEO</a>
      <span class="fs-3"><button class="js-main-nav-trigger navigation-list-toggle btn btn-outline" type="button" data-text-toggle="Hide">Menu</button></span>
      <div class="navigation main-nav js-main-nav">
        <nav role="navigation" aria-label="Main navigation">
  <ul class="navigation-list">
    
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/404.html" class="navigation-list-link"></a>
            
          </li>
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/" class="navigation-list-link">Home</a>
            
          </li>
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/docs/arcgis-online.html" class="navigation-list-link">ArcGIS Online</a>
            
              
              <ul class="navigation-list-child-list ">
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/arcgis-online/getting-started.html" class="navigation-list-link">Getting Started</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/arcgis-online/contacts.html" class="navigation-list-link">Contacts</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/arcgis-online/roles.html" class="navigation-list-link">Roles</a>
                      
                    </li>
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/arcgis-online/training.html" class="navigation-list-link">Training</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/arcgis-online/tools.html" class="navigation-list-link">Tools</a>
                      
                        
                        <ul class="navigation-list-child-list">
                          
                            
                          
                            
                              <li class="navigation-list-item ">
                                <a href="/home/docs/arcgis-online/thumbnail-builder.html" class="navigation-list-link">Thumbnail Builder</a>
                              </li>
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                        </ul>
                      
                    </li>
                  
                
                  
                
                  
                
              </ul>
            
          </li>
        
      
    
      
        
      
    
      
        
      
    
      
        
          <li class="navigation-list-item active">
            
            <a href="/home/docs/services.html" class="navigation-list-link">GIS Services</a>
            
              
              <ul class="navigation-list-child-list ">
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/services/inventory_3857.html" class="navigation-list-link">Inventory (EPSG:3857)</a>
                      
                    </li>
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/services/inventory_2992.html" class="navigation-list-link">Inventory (EPSG:2992)</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/services/mapping_examples.html" class="navigation-list-link">Mapping Examples</a>
                      
                        
                        <ul class="navigation-list-child-list">
                          
                            
                          
                            
                          
                            
                              <li class="navigation-list-item ">
                                <a href="/home/docs/services/arcgis_api_javascript.html" class="navigation-list-link">ArcGIS API for JavaScript</a>
                              </li>
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                        </ul>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item  active">
                      
                      <a href="/home/docs/services/query_examples.html" class="navigation-list-link">Query Examples</a>
                      
                        
                        <ul class="navigation-list-child-list">
                          
                            
                          
                            
                          
                            
                          
                            
                              <li class="navigation-list-item  active">
                                <a href="/home/docs/services/python.html" class="navigation-list-link active">Python 3</a>
                              </li>
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                            
                          
                        </ul>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
              </ul>
            
          </li>
        
      
    
      
        
      
    
      
        
      
    
      
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/docs/imagery.html" class="navigation-list-link">Imagery Services</a>
            
              
              <ul class="navigation-list-child-list ">
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/imagery/inventory_3857.html" class="navigation-list-link">Inventory (EPSG:3857)</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/imagery/inventory_2992.html" class="navigation-list-link">Inventory (EPSG:2992)</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
              </ul>
            
          </li>
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/docs/geocoder.html" class="navigation-list-link">Geocoder</a>
            
          </li>
        
      
    
      
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/docs/software.html" class="navigation-list-link">Software & Training</a>
            
              
              <ul class="navigation-list-child-list ">
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/software/products.html" class="navigation-list-link">Esri Products</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                    <li class="navigation-list-item ">
                      
                      <a href="/home/docs/software/training.html" class="navigation-list-link">Training</a>
                      
                    </li>
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
                  
                
              </ul>
            
          </li>
        
      
    
      
        
          <li class="navigation-list-item">
            
            <a href="/home/docs/contact.html" class="navigation-list-link">Contact</a>
            
          </li>
        
      
    
  </ul>
</nav>

      </div>
      <footer role="contentinfo" class="site-footer">
        <p class="text-small text-grey-dk-000 mb-0">This site uses <a href="https://github.com/pmarsceill/just-the-docs">Just the Docs</a>, a documentation theme for Jekyll.</p>
      </footer>
    </div>
    <div class="main-content-wrap js-main-content" tabindex="0">
      <div class="page-header">
        <div class="main-content">
          
          <div class="search js-search">
            <div class="search-input-wrap">
              <input type="text" class="js-search-input search-input" tabindex="0" placeholder="Search Oregon GEO" aria-label="Search Oregon GEO" autocomplete="off">
              <svg width="14" height="14" viewBox="0 0 28 28" xmlns="http://www.w3.org/2000/svg" class="search-icon"><title>Search</title><g fill-rule="nonzero"><path d="M17.332 20.735c-5.537 0-10-4.6-10-10.247 0-5.646 4.463-10.247 10-10.247 5.536 0 10 4.601 10 10.247s-4.464 10.247-10 10.247zm0-4c3.3 0 6-2.783 6-6.247 0-3.463-2.7-6.247-6-6.247s-6 2.784-6 6.247c0 3.464 2.7 6.247 6 6.247z"/><path d="M11.672 13.791L.192 25.271 3.02 28.1 14.5 16.62z"/></g></svg>
            </div>
            <div class="js-search-results search-results-wrap"></div>
          </div>
          
          
            <ul class="list-style-none text-small mt-md-1 mb-md-1 pb-4 pb-md-0 js-aux-nav aux-nav">
              
                <li class="d-inline-block my-0"><a href="//www.oregon.gov/GEO/Pages/index.aspx">Oregon Geospatial Enterprise Office</a></li>
              
            </ul>
          
        </div>
      </div>
      <div class="main-content">
        
          
            <nav 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="page-content" role="main">
          <h1 id="python-3">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">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="o">.</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="o">.</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="o">.</span><span class="n">loads</span><span class="p">(</span><span class="n">r</span><span class="o">.</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>
  </div>

</body>
</html>