pycon_lightning_1 slides

A Python API for your GeoSpatial needs

[https://developers.arcgis.com/python]()

Atma Mani amani@esri.com

About 80% of all data has a geospatial component

Where do you go to analyse such data?

ArcGIS

<img src=https://pbs.twimg.com/media/C4xV_EsVUAAAxPD.jpg style=”height:228px;”>

ArcGIS provides powerful tools for mapping and spatial reasoning so you can explore data and share location-based insights.

ArcGIS API for Python

ArcGIS API for Python is a new, object-oriented API from ArcGIS that is easy to learn, intuitive and plays well with open source Python libraries. I am sure you will fall in love with the beautiful, interactive map widget for Jupyter notebooks.

To display a map, just type:

In [5]:
from arcgis.gis import *

map1 = gis.map("Palm Springs, CA")
map1

Now lets take a look at some of the common functionalities that is possible with the API:

Geocode

Geocode - turn address / place names to co-ordinates. The ArcGIS World Geocoding service that powers the Geocoding module of the API is versatile. You can specify single line address, multi-line, POI names and go town with category filtering and envelope definition. We will keep things a bit simple here

In [5]:
from arcgis.geocoding import geocode
geocode_results = geocode("Oregon convention center")
pycon = geocode_results[1]
In [6]:
print(pycon['location'])
print(pycon['attributes']['Place_addr'])
{'y': 45.528279719000466, 'x': -122.6617093219997}
777 NE Martin Luther King Jr Blvd, Portland, Oregon
In [7]:
map2 = gis.map(pycon['address'], zoomlevel=15)
map2.draw(pycon['location'])
map2

Now let us see how to search for restaurants around the convention center

In [8]:
map3 = gis.map(pycon['address'], zoomlevel=14)
map3.draw(pycon['location'])
map3
In [9]:
restaurants = geocode('restaurant', search_extent = pycon['extent'], max_locations=10)
for r in restaurants:
    content_string = "Phone: {} Address: {}".format(r['attributes']['Phone'], r['attributes']['Place_addr'])
    popup = {'title':r['attributes']['PlaceName'] + " | " + r['attributes']['Type'],
            'content':content_string}
    symbol_design = {'type':"esriSMS", 'style':'esriSMSDiamond', 'color':[ 50, 50, 255, 150 ]}
    map3.draw(r['location'], popup, symbol=symbol_design)

You dont have to stop with listing the restaurants. If you like one, you can find the directions to it as well.

In [10]:
ice_cream = restaurants[6]
from arcgis.features.use_proximity import plan_routes
import time
In [11]:
pr = plan_routes(stops_layer=ice_cream['location'],route_count=1, max_stops_per_route=1, 
            route_start_time=time.time(), start_layer=pycon['location'])
{"code" : 0, "messageCode":"GPEXT_016","message": "Invalid layer object.", "params":{}}
{"code" : 0, "messageCode":"GPEXT_016","message": "Invalid layer object.", "params":{}}
{"messageCode": "AO_100063", "message": "PlanRoutes failed."}
{"messageCode": "AO_100063", "message": "PlanRoutes failed."}
Failed to execute (PlanRoutes).
Failed to execute (PlanRoutes).
Failed.
Failed.
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-11-a3b2dfb8ae75> in <module>()
      1 pr = plan_routes(stops_layer=ice_cream['location'],route_count=1, max_stops_per_route=1, 
----> 2             route_start_time=time.time(), start_layer=pycon['location'])

/Users/atma6951/anaconda/envs/geosaurus_gold_py35/lib/python3.5/site-packages/arcgis/features/use_proximity.py in plan_routes(stops_layer, route_count, max_stops_per_route, route_start_time, start_layer, start_layer_route_id_field, return_to_start, end_layer, end_layer_route_id_field, travel_mode, stop_service_time, max_route_time, output_name, context, gis)
    327         max_route_time,
    328         output_name,
--> 329         context)

/Users/atma6951/anaconda/envs/geosaurus_gold_py35/lib/python3.5/site-packages/arcgis/_impl/tools.py in plan_routes(self, stops_layer, route_count, max_stops_per_route, route_start_time, start_layer, start_layer_route_id_field, return_to_start, end_layer, end_layer_route_id_field, travel_mode, stop_service_time, max_route_time, output_name, context)
   1966         task_url, job_info = super()._analysis_job(task, params)
   1967 
-> 1968         job_info = super()._analysis_job_status(task_url, job_info)
   1969         job_values = super()._analysis_job_results(task_url, job_info)
   1970         #print(job_values)

/Users/atma6951/anaconda/envs/geosaurus_gold_py35/lib/python3.5/site-packages/arcgis/_impl/tools.py in _analysis_job_status(self, task_url, job_info)
    183 
    184                     if job_response.get("jobStatus") == "esriJobFailed":
--> 185                         raise Exception("Job failed.")
    186                     elif job_response.get("jobStatus") == "esriJobCancelled":
    187                         raise Exception("Job cancelled.")

Exception: Job failed.
In [ ]: