Using the GIS

The GIS object in the gis module is the most important object when working with the ArcGIS API for Python. The GIS object represents the GIS you are working with, be it ArcGIS Online or an instance of ArcGIS Enterprise. You use the GIS object to consume and publish GIS content and administrators may use it to manage GIS users, groups and datastores. This object becomes your entry point in your Python script when using the API.

To use the GIS object, import GIS from the arcgis.gis module:

In [1]:
from arcgis.gis import GIS

To create the GIS object, we pass in the url and our login credentials as shown below:

In [2]:
gis = GIS("https://www.arcgis.com", "arcgis_python", "P@ssword123")

If connecting to an ArcGIS Enterprise in your premises, your URL becomes http://machinename.domain.com/webadapter. Your GIS can support a number of authentication schemes, refer to this section of the guide to know how to authenticate your scripts and notebooks for various such schemes.

Below, we're connecting to ArcGIS Online (the default GIS used when the url is not provided) as an anonymous user:

In [2]:
gis = GIS()

Adding a '?' mark after an object and querying it brings up help for that object in the notebook:

In [3]:
gis?

The notebook provides intellisense and code-completion. Typing a dot after an object and hitting tab brings up a drop-down with its properties and methods: dropdown showing members of GIS class

Helper objects

The GIS object provides helper objects to manage the GIS resources, i.e. the users, groups, content and datastores. These helper utilities are in the form of helper objects named: users, groups, content and datastore respectively. The helper utility for managing user roles named roles is available as a property on the helper object users.

Each such helper object has similar patterns of usage: there are methods to get(), search() and create() the respective resources.

The prescribed programming pattern is to not create the GIS resources (user, group, item, role, datastore) directly using their constructor, but to access them through their corresponding helper objects described above.

Thus, to access a user, you would use the users property of your gis object which gives you an instance of UserManager class. You would then call the get() method of the UserManager object and pass the user name of the user you are interested in.

In [4]:
user = gis.users.get('john.smith')

Rich IDE experience with Jupyter notebooks

The ArcGIS API for Python is integrated with Jupyter Notebook to make it easy to visualize and interact with GIS resources. The user object has a rich representation that can be queried like this:

In [5]:
user
Out[5]:
John Smith

Bio: None
First Name: John
Last Name: Smith
Username: john.smith
Joined: October 28, 2014

The resources are implemented as Python dictionaries. You can query for the resource properties using the resource['property'] notation:

In [6]:
user['firstName']
Out[6]:
'John'

The properties are also available as properties on the resource object, so you can use the dot notation to access them:

In [7]:
user.lastName
Out[7]:
'Smith'

The resources provide methods to update(), delete() and use the object. The reminder of topics in this module talk in detail about using the various helper objects and resource objects.

Embedded maps in Jupyter notebooks

The GIS object includes a map widget that can be used to visualize the content of your GIS as well as see the results of your analysis. Let's bring up a map of Palm Springs, CA:

In [8]:
map1 = gis.map("Palm Springs, CA")
map1

map of palm springs

We can search for content in our GIS. Let's search for Hiking Trails in the Palm Springs region. We do that by calling gis.content.search() and for each web map or web layers that gets returned, we can display its rich representation within the notebook:

In [9]:
from IPython.display import display

items = gis.content.search('Palm Springs Trails')
for item in items:
    display(item)
Joshua Tree - Lost Palms Oasis Trail
This map tour will take you on a hike through Joshua Tree National Park.Web Mapping Application by JenniferBell_UO
Last Modified: May 19, 2016
0 comments, 684 views
Welcome to Indian Canyons
Story map of the scenic trails in Indian Canyons in Palm Springs, California.Web Mapping Application by kgrahamACBCI
Last Modified: September 29, 2016
0 comments, 590 views
Trails
PS trailsFeature Service by Rupert_Essinger
Last Modified: March 10, 2015
0 comments, 432 views
Skyline Trail
Short trail run that starts from Ramon Rd in Palm Springs and runs down the Skyline Trail.Web Map by Kelly
Last Modified: May 22, 2013
0 comments, 411 views
cactus to clouds trail
Hiking Trail from Palm Springs to San Jacinto PeakWeb Map by cjosephmayfield
Last Modified: April 06, 2012
0 comments, 153 views
Palm Springs Trails
Trails in the Palm Springs area affected by the BLM - Agua Caliente land exchangeFeature Service by paniello_geodesign
Last Modified: March 13, 2015
0 comments, 96 views
Loop7Victor_WFL
Trail going from Palm Canyon and back up VictorFeature Service by kgrahamACBCI
Last Modified: September 28, 2016
0 comments, 90 views
Albert E. Davies Wetland Trail at Lake Newport. Mill Creek Park, Boardman, OH
Lake Newport and the wetland at its southern end are both man-made, dating from the 1920s. Together they cover about 100 acres and in summer attract recreational kayakers, picnickers, and others. It’s rarely crowded, however, and from September to April, it can sometimes be completely deserted. The trail itself is only a few hundred meters long. Winding through reed-beds it dead-ends at a lookout on the lake facing an array of small islands, some of which are accessible only by kayak. The viewing platform is located at 41°03’09.76”N and 80°40’34.05”W. Parking is available a few hundred yards from the trail head; toilets are also provided. Access to the parking lot is from West Newport Drive in Mill Creek Park. The wetlands are a good all-round bird-watching spot. A wide variety of warblers can be encountered during Spring and Fall migration. I’ve seen Palm, Yellow-Rumped, Black-Throated Green, and many others. On the east side of the viewing platform, across a few tens of meters of water, there is a mud-flat. Depending on the water level it varies considerably in size, occasionally disappearing altogether. Although small, it’s often a good place to look for wading birds, particularly in late summer and early fall. Among the ubiquitous Killdeer and Canada Geese, I have seen Least, Spotted, and Solitary Sandpipers and Lesser Yellowlegs on several occasions. Bald Eagles often can be spotted in the trees just across the water. Visit year-round This is my favorite "home patch." It's located between my home in Boardman and YSU, where I teach, so this is the place I bird most often – by far! During the academic year I usually stop in for twenty or thirty minutes early in the morning on the way to work at least every other day. By now I know pretty much every tree, bush, and mud-patch! Web Map by bradshellito
Last Modified: August 11, 2016
0 comments, 84 views
Loop2WestFork_WFL
A loop trail in Indian Canyons starting at the Trading Post parking lot and head down Palm Canyon to West Fork, to West Fork North, up Coffman to Murray Canyon, back down Coffman to Alexander and ending up back at the Trading Post.Feature Service by kgrahamACBCI
Last Modified: September 28, 2016
0 comments, 70 views
Albert E. Davies Wetland Trail at Lake Newport. Mill Creek Park, Boardman, OH
Lake Newport and the wetland at its southern end are both man-made, dating from the 1920s. Together they cover about 100 acres and in summer attract recreational kayakers, picnickers, and others. It’s rarely crowded, however, and from September to April, it can sometimes be completely deserted. The trail itself is only a few hundred meters long. Winding through reed-beds it dead-ends at a lookout on the lake facing an array of small islands, some of which are accessible only by kayak. The viewing platform is located at 41°03’09.76”N and 80°40’34.05”W. Parking is available a few hundred yards from the trail head; toilets are also provided. Access to the parking lot is from West Newport Drive in Mill Creek Park. The wetlands are a good all-round bird-watching spot. A wide variety of warblers can be encountered during Spring and Fall migration. I’ve seen Palm, Yellow-Rumped, Black-Throated Green, and many others. On the east side of the viewing platform, across a few tens of meters of water, there is a mud-flat. Depending on the water level it varies considerably in size, occasionally disappearing altogether. Although small, it’s often a good place to look for wading birds, particularly in late summer and early fall. Among the ubiquitous Killdeer and Canada Geese, I have seen Least, Spotted, and Solitary Sandpipers and Lesser Yellowlegs on several occasions. Bald Eagles often can be spotted in the trees just across the water. Visit year-round This is my favorite "home patch." It's located between my home in Boardman and YSU, where I teach, so this is the place I bird most often – by far! During the academic year I usually stop in for twenty or thirty minutes early in the morning on the way to work at least every other day. By now I know pretty much every tree, bush, and mud-patch! Web Mapping Application by bradshellito
Last Modified: August 11, 2016
0 comments, 62 views

We can then add the returned web layers to our map. To add the last layer returned above, we call the add_layer() method and pass in the layer for Palm Springs Trail:

In [10]:
# Let us filter out the item with title 'Trails' that we want to add
item_to_add = [temp_item for temp_item in items if temp_item.title == "Trails"]
map1.add_layer(item_to_add[0])

The above cell updated the map widget, if you scroll to the top, you can notice a new trail layer being rendered on the map.


Feedback on this topic?