April 18, 2021

Introduction to ArcGIS API for Python

2 min read

 

Introduction to Jupyter Notebooks for Spatial Analysis

In this tutorial, we are going to be using the Jupyter Notebooks to analyse spatial data

Installing ArcGIS API for Python

Following the steps below, you will be able to install the API on your machine and get to work with this Rich Python Library for Spatial Data Science

Download Anaconda

  • Navigate to Anaconda Website and get to downlaod the Anaconda application which is going to download all the python libraries and install Jupyter Notebooks which are much needed for this tutorial
  • Run the installation file to install it on your machine

Run Commands

  • click on the Start Menu,
  • find Anaconda folder,
  • click on it to expand it
  • Right Click the Jupyter NoteBook icon and Open file location
  • Right click on the Command Prompt and Run as Administrator

The command line tool will open and input the following code:

  • installing the ArcGIS Python Libraries onto your machine

conda install -c esri arcgis

  • Type y to accept the packages listed to install on your machine
  • that’s it. Python API is installed

Open Jupyter NoteBooks

Now we need to start working and we just need to run one more command in the command prompt.
Type and run,

jupyter notebook

  • the environment opens
  • navigate to a Folder in which you want to work in
  • create a Python 3 Notebook by clicking on New which is on the tool bar
  • a new tab opens and you are ready to begin

CONGRATULATIONS ON YOUR FIRST STEP

Congratulations GIF


Creating Maps & Adding Layers

Importing the Python Libraries into our project.

  • call the ArcGIS library
  • import the GIS capabilities
  • declare a variable and assign the GIS Methods to it
In [5]:
from arcgis.gis import GIS
gis = GIS()

We just imported the ArcGIS API for Python by calling it from the arcgis library for the Python language.

Let’s create a simple map using a named location

  • create a variable named map or any other name you might give it
  • call the variable in order to display an out

If you do not call the variable after declaring it, then no output is displayed

In [6]:
map = gis.map("Africa")
map

>> Your Map Depending On Your Chosen Location Will Appear Here <<

Styling The Map

We can edit attributes like Centering and Zooming among others which we will touch on in future tutorials

In [7]:
map.center = [-19.0154, 29.1549]
map.zoom = 7

Get the JSON feedback for all available Basemaps to use within the map display

In [8]:
map.basemaps
Out[8]:
['dark-gray',
 'dark-gray-vector',
 'gray',
 'gray-vector',
 'hybrid',
 'national-geographic',
 'oceans',
 'osm',
 'satellite',
 'streets',
 'streets-navigation-vector',
 'streets-night-vector',
 'streets-relief-vector',
 'streets-vector',
 'terrain',
 'topo',
 'topo-vector']

Change the basemap to the original map.

Take note that the map variable is still the one in use and it will just change the map displayed above

In [9]:
map.basemap = 'dark-gray-vector'

Searching and Retrieving OpenData

  • create a variable to store results
  • define the search terms
  • define the item types you want to search
  • if you don’t want to flood your screen with results, define the maximum items to display
  • call the variable to display the results
In [11]:
display_content = gis.content.search("Zimbabwe COVID-19", item_type="Feature Layer", max_items=10)
display_content
Out[11]:
[<Item title:"CASES IN ZIMBABWE" type:Feature Layer Collection owner:mohcccovid19>,
 <Item title:"SexDistrubution" type:Table Layer owner:surveyor_jr>,
 <Item title:"Vaccine Distribution Program" type:Feature Layer Collection owner:surveyor_jr>,
 <Item title:"CaseFatalityRateByAgeGroup" type:Table Layer owner:surveyor_jr>,
 <Item title:"CasesAndDeathsPerAgeGroup" type:Table Layer owner:surveyor_jr>,
 <Item title:"CASES IN ZIMBABWE" type:Feature Layer Collection owner:yona@esri_southernafrica>,
 <Item title:"POSITIVE CASES IN ZIMBABWE" type:Feature Layer Collection owner:yona@esri_southernafrica>,
 <Item title:"case_distribution_by_district_WFL1" type:Feature Layer Collection owner:surveyor_jr>]

display the results in a pleasing way than just text format

Using the same variable declared above.

In [12]:
from IPython.display import display 
for item in display_content:
    display(item)


CASES IN ZIMBABWE

Feature Layer Collection by mohcccovid19

Last Modified: April 14, 2020

0 comments, 23 views



SexDistrubution

Table Layer by surveyor_jr

Last Modified: December 07, 2020

0 comments, 4 views



Vaccine Distribution Program

A collection of the Coronavirus Vaccination roll-out and progress in ZimbabweFeature Layer Collection by surveyor_jr

Last Modified: February 18, 2021

0 comments, 345 views



CaseFatalityRateByAgeGroup

Table Layer by surveyor_jr

Last Modified: December 07, 2020

0 comments, 3 views



CasesAndDeathsPerAgeGroup

Table Layer by surveyor_jr

Last Modified: December 07, 2020

0 comments, 2 views



CASES IN ZIMBABWE

Feature Layer Collection by yona@esri_southernafrica

Last Modified: December 18, 2020

0 comments, 946 views



POSITIVE CASES IN ZIMBABWE

Feature Layer Collection by yona@esri_southernafrica

Last Modified: April 01, 2020

0 comments, 332 views



case_distribution_by_district_WFL1

Feature Layer Collection by surveyor_jr

Last Modified: January 31, 2021

0 comments, 154 views

Adding Content to Map

In [13]:
poi = display_content 
map.add_layer(poi)
Token Required
Token Required
(Error Code: 499)
Item.layers is a 'NoneType' object: nothing to be added to map
Token Required
Token Required
(Error Code: 499)
Token Required
Token Required
(Error Code: 499)
Item.layers is a 'NoneType' object: nothing to be added to map
Token Required
Token Required
(Error Code: 499)

Some Layers not added to the map, because they do not belong to us and for that we need access tokens which we can get in the ArcGIS Developers Dashboard meaning, if you haven’t already you need to create an account for that

In [ ]:
 
You can view the full (31-minute tutorial) video on YouTube 

error: Content is protected !!