KEMBAR78
PYAPI Workbook v2.0.1 | PDF | Arc Gis | Esri
0% found this document useful (0 votes)
59 views251 pages

PYAPI Workbook v2.0.1

Python API

Uploaded by

Waleed GIS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views251 pages

PYAPI Workbook v2.0.1

Python API

Uploaded by

Waleed GIS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 251

Public

An Introduction to Administering your


Portal using the ArcGIS API for Python
Introducing the ArcGIS API for Python

Workbook
V2.0.1 UK
An Introduction to Administering your
Portal using the for ArcGIS API for Python

Page ii © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Copyright
Copyright © 1992-2010 Environmental Systems Research Institute, Inc. and ESRI (UK) Ltd
All rights reserved. Printed in Aylesbury, Bucks, England.
The information contained in this document is subject to change without notice and is the exclusive property of ESRI (UK)
Ltd. and Environmental Systems Research Institute, Inc., and any respective copyright owners. This work is protected under
United Kingdom and United States copyright law and other international copyright treaties and conventions and except with
the prior written consent of ESRI (UK) Ltd, shall be used solely for the purpose of undergoing training provided by or with
the express written permission of ESRI (UK) Ltd. To the extent permitted by law, no part of this work may be reproduced or
transmitted in any form or by any means, electronic or mechanical, including but not limited to photocopying and recording,
or by any information storage or retrieval system, except as expressly permitted in writing by ESRI (UK) Ltd. If any lawful
copies are made, all such copies, whether in whole or in part, shall include the appropriate ESRI (UK) Ltd and
Environmental Systems Research Institute, Inc. copyright notice. All requests should be sent to Attention: Contracts
Manager, ESRI (UK) Ltd., Millennium House, 65 Walton Street, Aylesbury, Bucks HP21 7QG, England.

@esri.com, 3D Analyst, ADF, AML, ARC/INFO, ArcAtlas, ArcCAD, ArcCatalog, ArcCOGO, ArcData, ArcDoc, ArcEdit,
ArcEditor, ArcEurope, ArcExplorer, ArcExpress, ArcFM, ArcGIS, ArcGrid, ArcIMS, ArcInfo Librarian, ArcInfo,
ArcInfo—Professional GIS, ArcInfo—The World's GIS, ArcLogistics, ArcMap, ArcNetwork, ArcNews, ArcObjects,
ArcOpen, ArcPad, ArcPlot, ArcPress, ArcQuest, ArcReader, ArcScan, ArcScene, ArcSchool, ArcSDE, ArcSdl, ArcStorm,
ArcSurvey, ArcTIN, ArcToolbox, ArcTools, ArcUSA, ArcUser, ArcView, ArcVoyager, ArcWatch, ArcWeb, ArcWorld,
Atlas GIS, AtlasWare, Avenue, BusinessMAP, Database Integrator, DBI Kit, ESRI, ESRI—Team GIS, ESRI—The GIS
People, FormEdit, Geographic Design System, Geography Matters, Geography Network, GIS by ESRI, GIS Day, GIS for
Everyone, GISData Server, InsiteMAP, MapBeans, MapCafé, MapObjects, ModelBuilder, MOLE, NetEngine, PC
ARC/INFO, PC ARCPLOT, PC ARCSHELL, PC DATA CONVERSION, PC STARTER KIT, PC TABLES, PC
ARCEDIT, PC NETWORK, PC OVERLAY, Rent-a-Tech, RouteMAP, SDE, SML, Spatial Database Engine, StreetEditor,
StreetMap, TABLES, the ARC/INFO logo, the ArcAtlas logo, the ArcCAD logo, the ArcCAD WorkBench logo, the
ArcCOGO logo, the ArcData logo, the ArcData Online logo, the ArcEdit logo, the ArcEurope logo, the ArcExplorer logo,
the ArcExpress logo, the ArcFM logo, the ArcFM Viewer logo, the ArcGIS logo, the ArcGrid logo, the ArcIMS logo, the
ArcInfo logo, the ArcLogistics Route logo, the ArcNetwork logo, the ArcPad logo, the ArcPlot logo, the ArcPress for
ArcView logo, the ArcPress logo, the ArcScan logo, the ArcScene logo, the ArcSDE CAD Client logo, the ArcSDE logo,
the ArcStorm logo, the ArcTIN logo, the ArcTools logo, the ArcUSA logo, the ArcView 3D Analyst logo, the ArcView
Business Analyst logo, the ArcView Data Publisher logo, the ArcView GIS logo, the ArcView Image Analysis logo, the
ArcView Internet Map Server logo, the ArcView logo, the ArcView Network Analyst logo, the ArcView Spatial Analyst
logo, the ArcView StreetMap 2000 logo, the ArcView StreetMap logo, the ArcView Tracking Analyst logo, the ArcWorld
logo, the Atlas GIS logo, the Avenue logo, the BusinessMAP logo, the Data Automation Kit logo, the Digital Chart of the
World logo, the ESRI Data logo, the ESRI globe logo, the ESRI Press logo, the Geography Network logo, the MapCafé
logo, the MapObjects Internet Map Server logo, the MapObjects logo, the MOLE logo, the NetEngine logo, the PC
ARC/INFO logo, the Production Line Tool Set logo, the RouteMAP IMS logo, the RouteMAP logo, the SDE logo, The
World's Leading Desktop GIS, Water Writes, www.esri.com, www.geographynetwork.com, www.gisday.com, and Your
Personal Geographic Information System are trademarks, registered trademarks, or service marks of ESRI in the United
States, the European Community, or certain other jurisdictions.
Other companies and products mentioned in this document may be trademarks or registered trademarks of their respective
trademark owners.
INFO and PC-INFO are trademarks of Doric Computer Systems International Ltd.
ArcView GIS uses Neuron Data’s Open Interface

Contains Ordnance Survey data © Crown copyright and database right November 2023

© ESRI (UK) Ltd Page iii


An Introduction to Administering your
Portal using the for ArcGIS API for Python

Page iv © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Table of Contents

Copyright ........................................................................................................................ iii


Table of Contents ............................................................................................................. v
Introduction .......................................................................................................... INTRO-1
The ArcGIS GeoSpatial System ................................................................................... INTRO-2
Course goals ............................................................................................................... INTRO-3
Using the course workbook ....................................................................................... INTRO-4
Additional resources .................................................................................................. INTRO-5
Your course credentials .............................................................................................. INTRO-6
1 Introducing the ArcGIS API for Python..................................................................... 1-1
Learning objectives .............................................................................................................1-1
What is the ArcGIS API for Python? ....................................................................................1-2
ArcGIS API for Python communication ...............................................................................1-4
Install the ArcGIS API for Python: ArcGIS Pro .....................................................................1-5
Install the ArcGIS API for Python: No ArcGIS Pro ................................................................1-7
How do I access the ArcGIS API for Python? .......................................................................1-9
Exercise 1: Working with a Notebook .............................................................................. 1-11
Answers to Exercise 1 Questions ..................................................................................... 1-20
2 Introducing Notebooks ........................................................................................... 2-1
Learning objectives .............................................................................................................2-1
Using an Integrated Development Environment ................................................................2-2
ArcGIS Notebook installed with ArcGIS Pro ........................................................................2-3
Portal Notebooks ................................................................................................................2-5
Jupyter Notebook ................................................................................................................2-8
Jupyter Notebook user interface ..................................................................................... 2-11
Types of cell ...................................................................................................................... 2-12
Executing cells .................................................................................................................. 2-14
Markdown options for your notebook ............................................................................ 2-16
Writing code in the Notebook.......................................................................................... 2-19
Exercise 2: Working with the Jupyter Notebook ............................................................. 2-21

© ESRI (UK) Ltd Page v


An Introduction to Administering your
Portal using the for ArcGIS API for Python

Answers to Exercise 2 Questions ..................................................................................... 2-34


Section 2 Lesson review ................................................................................................... 2-35
3 Understanding the SDK ........................................................................................... 3-1
Learning objectives .............................................................................................................3-1
Section keywords ................................................................................................................3-2
An overview of the ArcGIS API for Python ..........................................................................3-3
Activity: Investigate the architecture of the API .................................................................3-5
What is the gis module? ...................................................................................................3-7
The GIS() class .................................................................................................................3-9
Using the GIS class to understand the API Reference .................................................... 3-11
The map widget................................................................................................................ 3-15
Activity: Accessing the MapView object via the map() method .................................... 3-17
Answers to section activities ............................................................................................ 3-18
Exercise 3: Understanding the SDK through the map widget .......................................... 3-20
Answers to Exercise 3 Questions ..................................................................................... 3-31
Exercise 3 Solution ........................................................................................................... 3-33
4 Searching for portal items ....................................................................................... 4-1
Learning objectives .............................................................................................................4-1
The GIS Resource Manager classes .....................................................................................4-2
Using the search() method............................................................................................4-4
The search() method’s query argument .....................................................................4-5
Searching for content ..........................................................................................................4-7
Investigating your search results ........................................................................................4-9
Searches based upon dates .............................................................................................. 4-10
Activity: Complete the following code… .......................................................................... 4-12
Answers to section activities ............................................................................................ 4-13
Exercise 4: Investigating the contents of your portal ...................................................... 4-14
Answers to Exercise 4 Questions ..................................................................................... 4-25
Exercise 4 Solution ........................................................................................................... 4-27
5 Administering your portal ....................................................................................... 5-1
Learning objectives .............................................................................................................5-1
Administration capabilities .................................................................................................5-2

Page vi © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing portal members ..................................................................................................5-4


Creating and managing groups ...........................................................................................5-7
Managing content in your portal ........................................................................................5-9
Managing portal Items ..................................................................................................... 5-10
Managing GIS servers in your Enterprise ......................................................................... 5-11
Administering your services ............................................................................................. 5-14
Administration samples ................................................................................................... 5-15
Visualising your analysis using Matplotlib ....................................................................... 5-16
Exercise 5: Identifying portal usage of users.................................................................... 5-18
Answers to Exercise 5 Questions ..................................................................................... 5-33
Exercise 5 solution............................................................................................................ 5-35
6 Publishing and creating content .............................................................................. 6-1
Learning objectives .............................................................................................................6-1
Adding data to your organisation .......................................................................................6-2
Publishing to your portal through ArcPy .............................................................................6-5
Activity: Adding Items and publishing content ...................................................................6-8
Adding Items to your portal ............................................................................................. 6-10
Create a Hosted Feature Service ...................................................................................... 6-11
Sharing your content to others ........................................................................................ 6-12
The zipfile module ..................................................................................................... 6-14
Extracting your data from your portal ............................................................................. 6-16
Working with Attachments .............................................................................................. 6-18
Answers to section activity .............................................................................................. 6-20
Exercise 6: Adding content to your portal ....................................................................... 6-22
Answers to Exercise 6 Questions ..................................................................................... 6-35
Exercise 6 solution............................................................................................................ 6-37
7 Exploratory Data Analysis ....................................................................................... 7-1
Learning objectives .............................................................................................................7-1
Working with the Pandas module .......................................................................................7-2
Creating a Pandas DataFrame .............................................................................................7-3
Pandas DataFrame members ..............................................................................................7-5
Correcting issues in the DataFrame ....................................................................................7-8

© ESRI (UK) Ltd Page vii


An Introduction to Administering your
Portal using the for ArcGIS API for Python

Activity: Identify possible issues in a spreadsheet ........................................................... 7-10


Introducing the Esri Spatial Data frame ........................................................................... 7-11
Managing a Spatially Enabled DataFrame ....................................................................... 7-14
Creating a Spatially Enabled DataFrame from a Hosted Feature Layer ........................... 7-16
Members found on Accessor classes ............................................................................... 7-17
Displaying your SeDF ........................................................................................................ 7-19
Answers to section activity .............................................................................................. 7-22
Exercise 7: Cleansing an Excel spreadsheet ..................................................................... 7-23
Answers to Exercise 7 Questions ..................................................................................... 7-39
Exercise 7 Solution ........................................................................................................... 7-41

Page viii © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

Introduction

Welcome to the course!

Over the next two days you will learn how to get the most out of the ArcGIS API for Python.

The ArcGIS API for Python is a lightweight library for analysing spatial data, managing your
Web GIS, and performing spatial data science. In this course you will learn how to navigate
the API’s software developer kit (SDK) to help streamline your administrative tasks which
will help you save time and improve efficiency by automating administration and
management of your Web GIS.

First of all let’s define what an API is.

An API (Application Programming Interface) is a collection of interfaces, objects and


methods that constructs requests, usually in the form of URLs, and sends then to a remote
server. The remote server performs a task and sends the response back to your client device
and the API converts the response back into something useful.

© ESRI (UK) Ltd Page INTRO-1


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

The ArcGIS GeoSpatial System


ArcGIS is not a single product – it is a range of products and tools all interacting with each
other.

There are many ArcGIS Geospatial System diagrams available. This one shows the
importance of the Portal with in the platform.

You may be aware that there are two types of Portal that you have access to.

The first type of portal is the cloud-based portal which is commonly known as ArcGIS Online.

The second type of portal is the on-premise portal which lives behind a secure firewall and is
known as ArcGIS Enterprise which has the Portal for ArcGIS extension enabled.

Both portals are essentially content management systems controlling who has access to
content (maps, apps, layers and applications).

Access is granted via an organisational account. This account allows you to gain access to
your content through any device or desktop which is connected to the web.

Using the organisational account allows the user to perform management tasks based upon
the provided role and user type associated with the account.

Page INTRO-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

Course goals
By the end of this course, you will be able to:

• Understand the structure of the ArcGIS API for Python.


• Investigate various Integrated Development Environments (IDEs) which consume the
API.
• Learn how to navigate the documentation for the API.
• Perform portal-based administration tasks using the API.
o Find and access content
o Manage users
o Upload and publish content
o Download content, including attachments
• Investigate supporting modules, such as zipfile, matplotlib, collections
and pandas.
• Investigate the Spatially Enabled DataFrame.
… plus much much more.

© ESRI (UK) Ltd Page INTRO-3


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

Using the course workbook


The course workbook is an integral part of your learning experience. During class, you will
use the workbook to complete activities and exercises that reinforce specific tasks and skills.
After class, the workbook is your personal reference to review activities or work through
exercises again to emphasise what you've learned.

Essential elements include:

• Lessons: learning objectives at the beginning of each lesson to help you find the
information you're looking for
• Guided activities: interactive activities to reinforce key topics
• Exercises: step-by-step instructions for accomplishing essential tasks and building
skills
• Review: questions and answers that reinforce key concepts

Icons used in the book:

Page INTRO-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

Additional resources

Refer to the following resources to learn more:

ArcGIS Python Libraries


https://www.esriuk.com/en-gb/arcgis/products/arcgis-python-libraries/overview

ArcGIS API for Python


https://developers.arcgis.com/python/

Sample Notebooks
https://developers.arcgis.com/python/sample-notebooks/

Markdown Guide
https://www.markdownguide.org/

ArcGIS Documentation
https://doc.arcgis.com/en/

You might also find this link a useful resource….

Jupyter Notebook
https://jupyter.org/

© ESRI (UK) Ltd Page INTRO-5


An Introduction to Administering your
Portal using the ArcGIS API for Python v2.0

Your course credentials


To access the functionality associated with the ArcGIS API for Python you will need to have a
relevant organisational account.

You will use the named user accounts associated with the Esri UK online training portal
which will grant access to the API through ArcGIS Pro.

Your instructor will provide you with a temporary account to use during class.

Record the following information:

Username: _______________________________________________________________
Password: _______________________________________________________________

Page INTRO-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

1 Introducing the ArcGIS API for


Python

The ArcGIS API for Python is an Application Programming Interface (API) for managing your
portal. It is the preferred programming language of choice within ArcGIS and it continues to
evolve.

It is used widely within ArcGIS Pro, ArcGIS Enterprise and ArcGIS Online and is accessible
within these products through the addition of ArcGIS Notebooks, based upon the popular
Jupyter Notebook developer environment.

But how does the API communicate with your portal from within its hosting application?
How is it installed? and how can access to the API be obtained?

This section will cover these things.

Learning objectives
After completing this lesson, you will:

• Understand what modules are installed with the ArcGIS API for Python.
• Understand how the API communicates with your portal.
• Installation options for the API.
• How to access the API.

© ESRI (UK) Ltd Page 1-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

What is the ArcGIS API for Python?


The ArcGIS API for Python is a Pythonic library which allows power users, administrators and
developers to perform GIS visualisation, analysis and administration tasks for your GIS. It
includes objects and tools to access your GIS’ content and services.

Using the API you can take advantage of the many capabilities of the ArcGIS Geospatial
Platform to create and display web maps, query and administer your content while taking
advantage of the many analytical capabilities, such as performing analysis and geocoding.

The GIS represents either your on-premise portal or your online portal. Your GIS can be
accessed through the arcgis site package and this is the ArcGIS API for Python.

The ArcGIS API for Python also requires the SciPy ecosystem of modules (NumPy, Pandas,
SymPy and IPython) which are used extensively within the scientific and engineering
communities.

Page 1-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

It also integrates well with the Jupyter Notebook Integrated Development Environment
(IDE), which provides the ability to write and share code, along with providing excellent
visualisations and descriptive comments.

© ESRI (UK) Ltd Page 1-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

ArcGIS API for Python communication


Using an IDE you can write a Python script that takes advantage of the different API objects,
attributes and methods to define requests that can be sent to a published service hosted by
your GIS. Once the GIS has processed the request and performed an action the response is
sent back to the API for display.

The API communicates directly with your GIS server (ArcGIS Online or ArcGIS Enterprise)
using the REST architectural style; for example you can get access to your GIS by sending a
request to the GIS which includes properties identifying the address of your server (the
URL), username and password.

These requests to the server and the responses are all defined using the Python scripting
language.

Page 1-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Install the ArcGIS API for Python: ArcGIS Pro


The API is distributed as a conda package called arcgis.

Conda is a Python package manager that helps you install, manage and update packages and
their dependencies. The ArcGIS API for Python is managed via conda as are its
dependencies, such as the SciPy stack of modules.

The API can be installed in a number of ways. Complete instructions on how to install the
API can be found on the following page at the developers.arcgis.com website:

https://developers.arcgis.com/python/guide/install-and-set-up/

By far the simplest way to access the API is to install ArcGIS Pro.

The API and Conda is shipped with ArcGIS Pro 2.1 and all later releases. All you have to do is
install ArcGIS Pro.

The 2x release of ArcGIS Pro contains the Python Package Manager in the backstage area of
ArcGIS Pro, in which the arcgis package and its supporting dependencies can be viewed.

© ESRI (UK) Ltd Page 1-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

This is not the case at the 3x release of ArcGIS Pro. The arcpy and arcgis site packages
are not displayed in the backstage area, even though they are installed.

You can view the current release of arcpy and arcgis by viewing the installed packages
in the PyCharm developer environment:

At the ArcGIS Pro 3.1 release the release of the API is 2.1.0.2 and this is also the latest
release of the API as of September 2023.

Jupyter notebook is also installed and can be accessed through the Python Command
Prompt.

Page 1-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Install the ArcGIS API for Python: No ArcGIS


Pro
If you fancy a challenge then the API can be installed on a vanilla machine which does not
contain ArcGIS Pro.

There are a number of ways to install the ArcGIS API for Python. A few ways are mentioned
below but for a complete list (with steps on how to install the ArcGIS API for Python, you are
recommended to read the documentation for full instructions:

https://developers.arcgis.com/python/guide/install-and-set-up/

Install via Anaconda Distribution


An overview for installing the API is highlighted below:

Get Conda with Anaconda Optional: Install deep


Install the arcgis package
for Python Distribution learning dependencies

Step 1: Get Conda with Anaconda

Anaconda installs Python, Conda (for package management) and many additional useful
Python packages. Anaconda is a distribution and includes Conda. The ArcGIS API for Python
requires Python 3.5 or higher and so the latest release of Anaconda contains the 3.11
release of Python, plus additional useful packages.

2: Install the arcgis package

The API is installed via the Anaconda terminal application using the command:

conda install -c esri argis

3: Optional: install deep learning dependencies

If you wish to perform deep learning using the API or ArcGIS Pro then additional deep
learning dependencies will need to be installed which will support the API’s learn module.

© ESRI (UK) Ltd Page 1-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

These dependencies include PyTorch and TensorFlow, which are used for language
processing and object detection.

Install for use offline


The API can be installed, and used, offline…. But you will need to be online in order to get
access to the relevant packages and installers.

You will need to download:

• The latest version of Anaconda for Python 3x for your operating system.

• The appropriate version of the API for your operating system from the Esri channel
on anaconda.org.

Anaconda should then be installed and configured for use offline, and then the API can be
installed.

Install as a Docker image


A Docker container image is a lightweight standalone, executable package of software that
includes everything needed to run an application. Images become containers at runtime.

For more information on Docker, containers and images then please go to the following link:

https://www.docker.com/resources/what-container

The API is shipped as an image which you can download and power up whenever you want
to use it. The container containing the API runs in an isolated environment without making
any changes to your underlying file system.

Google Colaboratory
A new way to access the ArcGIS API for Python is via Google Colaboratory. It is basically a
Google-hosted Jupyter notebook service that allows a user to access notebooks from
anywhere by storing them in Google Drive.

Page 1-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

How do I access the ArcGIS API for Python?


The ArcGIS API for Python can be accessed through a number of different ways:

• Integrated Development Environments

• ArcGIS Pro Notebook

• ArcGIS Portal Notebooks

The ArcGIS Notebooks are all based upon the Jupyter Notebook The Jupyter Notebook is the
preferred IDE for data scientists for its ability to provide markup, its ability to run code
within cells and its rich data display function:

© ESRI (UK) Ltd Page 1-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

ArcGIS Notebooks are discussed in more detail in the next section.

It is within these different adaptations of Jupyter Notebook and IDEs that you can write
Python code which replicates and realises your workflows.

As well as accessing the ArcGIS API for Python through these different developer
environments, you can also access the additional Python packages installed by the API.

Page 1-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 1: Working with a Notebook

Estimated Time: 15 Minutes


In this quick introductory exercise you will investigate the online ArcGIS Notebook hosted in
ArcGIS Online. This is a great starting place to try out an implementation of a Jupyter
Notebook if you have never used one before and you want to become familiar with it before
you use it in anger.

You will carry out the following:

• Launch the online ArcGIS Notebook.


• Create a new Notebook document.
• Write some very basic Python code in your Notebook.
• Investigate the online samples.

Step 1: Access the online ArcGIS Notebook


As you will see in the course there are many ways in which you can get access to notebooks
– the preferred environment to write your ArcGIS API for Python scripts.

Let’s get started…

❑ Open the Chrome browser and type in:

https://www.arcgis.com

For all web-based applications in this course it is strongly recommended to


use Chrome as the de-facto browser of choice due to the releases of
browser installed on this image.

❑ Sign-in to the online portal using the credentials you were supplied with a few
moments ago.

Your online portal contains several tabs which allow you to access different parts of your
portal.

The Notebook tab provides you with access to create a new ArcGIS Notebook.

❑ Click the Notebook tab.

© ESRI (UK) Ltd Page 1-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

The Notebooks page is displayed. This is where any notebooks you create can be managed,
where sample notebooks can be accessed and where new notebooks can be created.

Currently the page is empty:

You will first create a new ArcGIS Notebook.

❑ Click the New Notebook button to display the options that are available for the type
of notebook you wish to create:

Page 1-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Notice that two of the options consume credits. The Advanced option allows you to use the
ArcPy site package in an online environment and consumes 3 credits an hour. The Advanced
with GPU support notebook engine is best suited for deep learning processes and will cost
30 credits and hour.

❑ Choose the Standard notebook option

Please choose the above runtime, otherwise an advanced runtime will


consume credits!

A new Notebook item is created and will display the following:

The online implementation of the ArcGIS Notebook allows you to perform Python based
processes and workflows within your own portal. It is where you will write your Python code
to perform simple administrative tasks as well as more complicated data analytics. It allows

© ESRI (UK) Ltd Page 1-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

you to easily search for and add content into your Notebook which is hosted in your portal
or within The Living Atlas of the World.

The default Notebook contains code cells which help you to access your GIS (portal)…

…and a code cell, underneath, for you to write your own code.

The notebook allows you to execute code contained within your cells. This is handy if you
have a cell which contains code which will use credits (for example performing enrichment
of a layer) and you only want to run that cell once to save on credit consumption. You can
run other cells which subsequently takes advantage of the data created in that previous cell
without having to run the code in it again.

Step 2: Write some basic code


Let’s write some basic Python code.

❑ In the Notebook locate the cell underneath the message “Now you are ready to
start!”.

❑ Place you cursor in the empty cell and write the following line of Python code:

print(“Hello world!!”)

In any programming language you are contractually obliged to write


“Hello World….!” As your first line of code….

Page 1-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Notice that the cell which you have focus in, for example as you are typing, is green and that
you have code colourisation, just as you would get in any other Python developer
environment.

❑ Click inside the cell and press the Run button.

This will run the code in the cell, display any output underneath the cell and create a new
cell.

As you can see it is quite straight forward to write your code in cells and execute cells
containing lines of code.

❑ Finally, save your Notebook (call it MyFirstNotebook) and fill in the metadata for
your new notebook.

Question 1: What properties must you need to fill in?

___________________________________________________________________________

___________________________________________________________________________

This will create a new Notebook item in your Content area of your portal.

❑ Click the burgers (yep – that’s what they’re called…) next to the name of your
notebook and click the Content menu item.

© ESRI (UK) Ltd Page 1-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

This will display the contents (items) in your portal which you have created. At the moment
you only have your notebook as the single item.

You can open this up and use it again in future or make it available to others either
internally within your organisation or externally by altering the item’s sharing properties.

Step 3: Investigate the online samples


The online Notebook provides you with a number of samples which you can investigate to
see the sorts of things you might want to do within the API. You can access the samples
from within the Notebook tab.

❑ Click the Notebook tab again to return to the notebook section of your portal.

You will notice your notebook is available here.

The notebook samples can be accessed from the Esri sample notebook button:

❑ Press the Esri samples notebooks button.

Notice that there are about 20 samples and they are organised into categories, such as
‘Administration’ and ‘Data Science and Analysis’.

Categories can be displayed underneath the Filters area.

❑ On the left hand side in Filters expand Categories.

Page 1-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Click the Content Management category.

❑ Locate the sample called “Content Management: Check for broken URLs”.

❑ Click on the sample’s notebook icon to display the metadata page for the sample
notebook and click Open Notebook to display the code.

If the notebook fails to open then carry out the following:

1: Close the browser tab with the notebook error message.


2: Click the three dots ( … ) on the right hand side of the notebook entry.
3: Click Preview.

❑ Spend a moment casting your eye over the code.

© ESRI (UK) Ltd Page 1-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

It can look quite daunting…. But if you spend a couple of minutes reading the sample then
the comments (markup) provide a description of the sample.

❑ Close all of the open tabs on your browser which relate to ArcGIS Online.
One of the aims of this course is to make you feel comfortable reading and investigating the
samples by understanding the different aspects of the API.

Step 4: Open ArcGIS Pro


In this very quick step you will just start ArcGIS Pro just to make sur that you can access
ArcGIS Pro in readiness for future exercises.

❑ Start ArcGIS Pro.

On the ArcGIS Sign-in dialog notice that the Sign me in automatically check box is ticked.

This indicates that when you start ArcGIS Pro again then you will not have to login using
your ArcGIS organisation accounts.

❑ Fill in the username and password and press the Sign in button.

You will see the ArcGIS Pro Home screen from which you can create new projects or open
existing ones

❑ Close ArcGIS Pro down


In this quick exercise you have quickly explored an online ArcGIS Notebook. This notebook is
now hosted as an item in your portal. You wrote a single line of code in a cell and executed
the contents of the cell.

Page 1-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

You also started to investigate some of the sample notebooks that are available to you in
the ArcGIS Notebooks. These samples can appear daunting at first site but as you become
more familiar with the Notebook and the API you will soon start to gain confidence in
understanding what the notebooks do and start to write your own code within the different
types of Notebook.

You will start to investigate the other types of Notebook, in its many different guises, in the
next section.

Exercise End

© ESRI (UK) Ltd Page 1-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 1 Questions

Question 1: What properties must you need to fill in?

Answer: You must fill in the Title and Tags properties. The Folder option is already filled in
with a default value. All other properties are optional.

Page 1-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

2 Introducing Notebooks

There are many tools which you can use to write your Python code. One of the most popular
tools at the moment is to use Jupyter Notebook.

Jupyter notebooks are a free, open-source web tool and is an example of a computational
notebook. They can be used to combine code, create rich output, explanatory text and
multimedia resources in a single document. As well as this, improvements in the underlying
web software, combined with increasing use of Python and data science in general, and the
ease in which remote, cloud-based data can be accessed has seen a huge increase in the use
of Jupyter Notebooks.

The Jupyter Notebook doesn’t just work with Python and this is reflected in its name which
is inspired, according to its co-founder Fernando Perez, by the programming languages Julia
(Ju), Python (Pyt) and R.

This section will introduce you to the different types of Notebook that are available to write
your API code.

Learning objectives
After completing this lesson, you will:

• Introduce the different types of notebook.


• Write some Python code in a Jupyter Notebook
• Examine the components of the Notebook

© ESRI (UK) Ltd Page 2-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

Using an Integrated Development


Environment
IDEs are the recommended way for writing your Python code. This is because they provide
the ability to debug code, provide comments, code completion and intellisence – all the
traditional things you need to write production scripts.

PyCharm is the recommended IDE for writing geoprocessing-based Python scripts for ArcGIS
Pro as it allows you to use virtual environments (as defined by Conda). Notice that the
Project Interpreter is using the default Python 3.9 environment called arcgispro-py3. The
arcgis package is one of the available packages within this default environment which
means that you can use the ArcGIS API for Python (and supported modules and packages) in
conjunction with arcpy.

The Jupyter Notebook App is the recommended IDE for working with the ArcGIS API for
Python. This is because you can run Python code interactively, visualise script output (such
as charts, maps and tables), display advanced comments and mark-up, and share scripts to
other IDEs.

If ArcGIS Pro is installed, or the API is installed through Anaconda, then Jupyter Notebook
will be installed as well.

As well as the traditional Jupyter Notebook app, Esri has introduced variations of the app
which are embedded into many Esri products, and are collectively known as ArcGIS
Notebooks.

Page 2-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

ArcGIS Notebook installed with ArcGIS Pro


ArcGIS Notebooks were made available in ArcGIS Pro 2.5 and they are an adaption of
traditional Jupyter Notebook. You can create, edit and save ArcGIS Notebooks as part of
your ArcGIS Pro projects. Using ArcGIS Notebooks you can perform analysis and immediately
view results in a geographic context, document and automate your workflows.

All Python functionality is available including the core standard library, ArcPy and the ArcGIS
API for Python, as well as useful and dependent packages such as NumPy and pandas.

Existing Notebooks can be accessed within the project through the Catalog pane.

A Notebook can be viewed within ArcGIS Pro by opening the notebook document (ipynb)
file. This will display the notebook document in its own view tab.

If the Notebook needs to be part of the ArcGIS Pro project then it can be added to the
project via the Catalog pane.

© ESRI (UK) Ltd Page 2-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

The project then references the notebook.

A new entry called Notebooks will then be added to the Catalog pane.

You have the ability to create a new Notebook within ArcGIS Pro via the Insert tab > New
Notebook and you should then specify the location and the name of the notebook
document.

The location of a notebook can be identified by hovering over the notebook in the
Notebooks tab.

Notebooks can be displayed via the Notebook folder on the Catalog pane by locating the
Notebook and choosing Open Notebook via its context menu.

ArcGIS Pro Notebooks will be used in future exercises.

Page 2-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Portal Notebooks
ArcGIS Notebooks are also available within your Portal (ArcGIS Online and ArcGIS
Enterprise).

Initiating an ArcGIS Online Notebook


ArcGIS Notebooks in ArcGIS Online have been available since the July 2020 update. The
Notebook interface has been optimised for working within web GIS but familiar functionality
is available, such as browsing for data and performing analysis. The online ArcGIS Notebooks
provide traditional Python libraries, as well as the ArcGIS API for Python and ArcPy!

If you have permissions to create Notebooks then you will see a new Notebook option in the
top ribbon navigation of ArcGIS Online.

For non-administrators to create notebooks then a new set of privileges will need to be
granted as part of a custom role:

• Basic privilege (General Privileges) for Create and Edit Notebooks.

• Advanced privilege (Premium Content) to use ArcPy or GPU enabled notebooks called
Advanced Notebooks.

Creating and editing notebooks using the General privileges, will not consume credits (apart
from running the online geoprocessing tools), as it takes advantage of standard open source
libraries or functionality that are already built into ArcGIS Online.

© ESRI (UK) Ltd Page 2-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

Running a notebook using the Advanced runtime (which will run ArcPy) will consume 3
credits an hour per notebook.

If you are running a notebook using the Advanced with GPU runtime (to run deep learning
models) then this will cost 30 credits per hour, per notebook.

When creating a notebook in ArcGIS Online then code will be written for you to help you get
started:

Initiating an ArcGIS Enterprise Notebook


How do we get access to a notebook inside of ArcGIS Enterprise?

Let’s first of all define what an ArcGIS Enterprise installation is comprised of.

An ArcGIS Enterprise system is made up of 4 components:

• Web Adaptor
• Portal for ArcGIS
• GIS Server
• Data Store
These four components are known as the ‘base deployment’.

Page 2-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

If you want to be able to have access to and create your own Notebooks then you need to
perform the following tasks:

1: Install the Notebook Server software on a suitable machine.

The machine then needs to be licensed to run the Notebook Server software. This creates
an ArcGIS Server site, licensed with the ArcGIS Notebook Server role, which can provide
notebook capabilities to an ArcGIS Enterprise deployment.

2: Add the Notebook Server site to Portal for ArcGIS.

This is done by logging into Portal for ArcGIS and adding the notebook server as a Server
site. In doing so you are federating the ArcGIS Notebook Server site with your Enterprise
portal. Other federated ArcGIS Server sites then provide functionality to the notebook, for
example a federated ArcGIS Image Server site provides image analysis capabilities to an
ArcGIS notebook site.

Performing the above tasks as an administrator will add the Notebook tab to your portal.
You now need to allow users to create, edit and run notebooks in the portal.

3: Create a custom role for notebook authors.

A custom role needs to be created and assigned to those users who will perform tasks with
notebooks in the portal. The custom role will have privileges assigned to it which determine
what the users assigned with the role can do. The Create and edit notebooks privilege must
be assigned to the custom role at the very least in order for users to……… create and edit
notebooks. Additional privileges are available which help users perform additional tasks
such as Advanced notebooks which allow users to utilise notebooks using the Advanced
notebook runtime.

For detailed information on ArcGIS Notebooks in ArcGIS Enterprise then please go to the
following URL:

https://enterprise.arcgis.com/en/notebook/

© ESRI (UK) Ltd Page 2-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

Jupyter Notebook
The Jupyter Notebook App is an open source web application which is running on your local
desktop. It allows you to create, edit and run your Python code in notebook documents. It is
installed when the ArcGIS API for Python is installed with ArcGIS Pro, or through the Conda
package management system.

The Jupyter notebook combines two components:

1: A web application: Running on your own machine which provides you with the ability
to create and author notebook documents. This is called Notebook dashboard.

2: Notebook documents: A representation of all content visible in the web application, I


including your code (inputs and outputs), explanatory text and images.

The notebook can be started by typing jupyter notebook at the prompt for the Python
command prompt; or via the Start menu.

This will launch the Jupyter Notebook.

In doing so some information about the notebook will be displayed in the command
window, including the URL of the web application which is hosting the dashboard by default
http://127.0.0.1:8888.

The picture below displays the Notebook Dashboard and this allows you to access local
notebook documents and Python scripts:

Page 2-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The landing page of the Jupyter Notebook web application, the dashboard, shows the
notebooks currently available in the notebook directory. This directory is displayed as this is
the directory in which the notebook server was started, namely:
C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3

This is ArcGIS Pro’s default, active Python environment.

Creating a new (or opening an existing) notebook


New notebooks are created from the dashboard by the New > Python 3 item.

The new notebook will be created in the same directory which is currently being viewed and
will be reflected in the notebook list in the dashboard.

The contents of existing notebooks can be displayed by clicking on the notebook (ipynb) file.
In the folder list.

Either action will launch a new tab on the browser and will start a kernel.

© ESRI (UK) Ltd Page 2-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

The Python kernel


A kernel is a process that executes code and communicates the results back to the
notebook.

The Jupyter Notebook app has a kernel for Python code, but there are other kernels
available for other programming languages.

The kernel remains active even if the browser window is closed. Reopening the same
notebook from the dashboard will reconnect the web application to the same kernel.

Kernel information is printed to the command prompt terminal, including the unique ID of
the kernel.

Periods of notebook inactivity will close the associated kernel down, and you may lose your
work as a result if you have not saved the notebook.

Page 2-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Jupyter Notebook user interface


The structure of a new notebook is quite simple.

You will see the Untitled notebook name, a menu bar, a simple toolbar and an empty cell in
which you can write your code.

The default name of the notebook is set to Untitled. This can be changed by clicking on the
name and typing a new name in the dialog box.

The menu bar contains options that help you manage notebooks.

The toolbar contains many quick-access options which will help you write, run and format
your code.

The code cell is where you will write your code. There are different types of cells.

© ESRI (UK) Ltd Page 2-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

Types of cell
Jupyter notebook support three types of cell. Code cells and Markdown cells are more
important than the Raw cells.

Why should you use these different types of cells?

• Markdown provides rich text for annotating and commenting your code.

• Cells provide a mechanism for logical breaks within your process.

• Your code is easier to understand if it is broken up.

• You can test each process independently without having to re-run the entire script.

Code cells
A code cell allows you to edit and write your code. It contains full syntax intellisence and tab
completion.

Page 2-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

When the code cell is executed, the contained code is sent to the kernel, executed and
returned back to the notebook as the cell’s output. The output can be text, HTML tables,
matplotlib figures and maps. This is known as Jupyter Notebook’s rich display capability.

IPython is an intrinsic part of Jupyter Notebook and allows for the


rich display of content within the notebook.

Markdown cells
Markdown allows you to document your notebook with text and images using rich text. In
iPython this is accomplished by marking up text with the Markdown language, which allows
text to be emphasised using italics, make your text bold etc.

An overview of some common markdown options are covered in a moment.

Raw cells
Raw cells provide a place in which you can write output directly. They are used if you wish to
convert a notebook to another format (such as HTML) and those cells will be converted in a
way specific to HTML.

© ESRI (UK) Ltd Page 2-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

Executing cells
Cells do not execute automatically – you will need to execute the cells yourself. You have
choice as to which cells you wish to execute.

Once you have written your code in the cell you should press <SHIFT> and <ENTER> to
execute the code in the cell, or you can execute the code in the cell through the toolbar or
menu bar. Successful execution will also create a new cell for you.

A common issue is that you might forget to run code in a previous cell, but that code is
required in order for subsequent code to be executed successfully.

If you have written code in multiple cells and want to run all the code then from the Cell
menu choose Run All.

Page 2-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

There are additional options for running cells on the Cell menu.

New cells can be added via the Insert menu.

Checkpoints
Every time you create and save a notebook a corresponding checkpoint file is created. This is
stored in a folder called \ipynb_checkpoints and it is in the same folder as the parent
notebook file.

Every time you perform a manual save then the checkpoint file is updated with your
amendments.

You can revert back to the saved checkpoint via:

File menu > Revert to Checkpoint > <your previous checkpoint>

Downloading your code


Finally, once you have written your code you can save the notebook, via the File menu, in a
number of different formats, such as .py, .ipynb, .html, .pdf and many other formats.

These files will be “downloaded” from your active Python environment to your Downloads
folder.

© ESRI (UK) Ltd Page 2-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

Markdown options for your notebook


As mentioned earlier in this section, the Jupyter Notebook provides many options for
creating rich text display. Markdown is a lightweight and popular Markup language. It is
often converted into corresponding HTML for easy sharing between different clients.

Headings
Headings start with a ‘#’ for the largest heading and end with 6 ‘#’s for minor headings:

Blockquotes
Blockquotes can be thought of as general comments. When viewed they are indented and
are obtained by using the Markdown symbol ‘>’.

In the Markdown cell:

Displays…

Page 2-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Displaying commented code


Code can be displayed within a Markdown cell using back ticks at the beginning and end of
the code.

Notice that the code is highlighted when the markup cell is executed.

Making your text bold and italicized


It is recommended to use HTML tags to achieve bold text and text with italics:

Notice the line break <br> tag, which adds a new line into the markup.

Creating Lists
List can be created using a couple of techniques:

Ordered (numbered) lists can be created using the <ol> tag and then <li> embedded inside
of the <ol> tag:

© ESRI (UK) Ltd Page 2-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

Unordered (bullet) lists can be created pretty much in the same way as ordered lists; the
only difference is that the <ul> tag is used instead of <ol>.

There are many additional formatting tags that can be used which aren’t mentioned here,
such as creating tables and inserting images. For a more extensive list you are
recommended to have a look at the following blog article:

https://www.datacamp.com/community/tutorials/markdown-in-jupyter-notebook

Page 2-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Writing code in the Notebook


Writing code in a Jupyter Notebook is pretty much the same as writing code in other IDEs.
You have access to Python’s standard library (functions, statements and packages to
import), as well as any additional packages available through Anaconda or via pip. The major
difference is that you can split and order your code into manageable chunks through cells.

Code colourisation
Just like in other IDEs you have code colourisation to help you write your code:

So, operators are purple, strings are brown, statements are bold green while built-in
functions are green.

Code completion
Auto-completion is available although it doesn’t occur naturally, as is the case with other
IDEs. Instead you have to specify the name of the object, followed by a full-stop, and then
the <TAB> key.

Up / down keys can be used to travel up and down the list.

© ESRI (UK) Ltd Page 2-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

Enabling intellisence
By default intellisence is not enabled in the Notebook. To enable it then the following code
should be written at the top of the Notebook:

%config IPCompleter.greedy=True

And then within the brackets of the function enter <Shift> and <Tab> to display the floating
help.

Obtaining code documentation


Code documentation is obtained with a “?” – you are literally asking a question. You can
obtain documentation for packages, functions and methods; for example:

Page 2-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 2: Working with the Jupyter


Notebook

Estimated Time: 40 Minutes


In this exercise you will spend a few moments investigating the Jupyter Notebook as it
important to understand and become confident in using this developer environment as it is
recognised as the standard environment for data science and web based scripting.

You will carry out the following:

• Start Jupyter Notebook.


• Examine the Jupyter Notebook environment.
• Write some standard Python code.
• Obtain code help
• Load and run an existing Jupyter Notebook.

Step 1: Open Jupyter Notebook and create a notebook


One thing you will need to understand is how to start Jupyter Notebook and its default
location for where Notebooks are created. You will also need to understand how to navigate
to a specific directory and start Jupyter Notebook to obtain notebooks contained inside of
sub-directories.

First of all you will start Jupyter Notebook, create a new notebook and identify where the
notebook has been created.

❑ In the Windows search box type “Python command prompt”.

This will display the Python command prompt. It is very similar to a DOS command prompt
except that it allows you to run Python code within the active Python environment.

Question 1: What is the name of the default Python environment?

___________________________________________________________________________

___________________________________________________________________________

© ESRI (UK) Ltd Page 2-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 2: Where is the default Python environment located?

___________________________________________________________________________

This Python environment was created when ArcGIS Pro was installed, and this is where
Python is installed.

❑ Open up File (Windows) Explorer and browse to the directory location which is the
answer to question 2.

Notice the directories which are contained here. These are the standard Python 3x
installation folders and associated files.

You shouldn’t create any scripts or notebooks in this directory as it contains the Python
system files and folders, and there may be locks placed upon the files and folders in this
location. Instead you should start Jupyter Notebook in a more accessible location.

You will launch the Jupyter Notebook in a location from which you can explore the course
data and access any existing notebooks to create new ones.

❑ In the Python command line window type cd \

This will send the command prompt to the root of the C:\ drive.

❑ Now cd into C:\EsriTraining\PYAPI

❑ At the Python command prompt type:

jupyter notebook

❑ …. And press the <return> key.

After a moment or two the command window creates the notebook server and runs
additional Jupyter related information, including the location at which notebook server is
running at.

If prompted then choose the Google Chrome browser to host the


Jupyter Notebook.

Page 2-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The command line information also indicates how to shut down your local web server which
is running this instance of Jupyter Notebook by using the <CTRL> and C keys.

A new tab in your browser opens to show the notebook dashboard. The dashboard displays
the contents of the \PYAPI folder.

You can access the contents of any of these sub directories within the dashboard, and
create new notebooks in here too.

❑ Click the Notebooks folder to display the contents of the directory.

Inside is an existing notebook called CreateMap.ipynb. You will investigate this later on in
the exercise.

You will now create a new notebook.

© ESRI (UK) Ltd Page 2-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ From the New menu choose Python 3 (ipykernel).

You can see that the notebook has a simple user interface with a single cell that is ready to
accept your code. Your notebook is currently called Untitled.

When a notebook is created for the first time a kernel is started. The kernel executes code
and returns the results back to the notebook. An entry relating to the kernel can be seen in
the Python command window.

❑ Click Untitled in the title area and rename the notebook as “HelloWorld”.

A new file called HelloWorld.ipynb is created in the Home folder of the notebook dashboard.

Page 2-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ In the cell type the following code:

print(“Hello World…”)

❑ Press <Shift> and <Enter> to run the code in the active cell.

The kernel executes the code and returns the results back to the notebook for display.
Notice, also, that a new cell is created:

You can ‘download’ (export) your notebook into the Downloads folder on your machine.
This can be useful if you want to export your notebook to a different format.

❑ From the File menu choose Download as > Python (.py).

The script is then downloaded to your machine’s Downloads folder.

Finally you will close Jupyter Notebook down.

❑ Close all of the open browser tabs.

Although it appears that the Jupyter Notebook has been closed, in reality it hasn’t. It is still
running in the background, including the kernel. To close the notebook down properly you
will need to do this in the terminal window.

❑ Locate the Python command window and press <Ctrl> and C to shutdown the
Notebook session.

Once the session has been terminated you will be taken back to the command prompt.

In this first step you have investigated the notebook server and the dashboard. You also
created a basic script, renamed it and downloaded it to a more accessible location. You
finally learned how to close the notebook server.

© ESRI (UK) Ltd Page 2-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

Step 2: Working with Python Dictionaries


In this quick step you will work with a Python dictionary. The reason behind this is to refresh
your knowledge of Python dictionaries as they are used extensively in the API and
supporting packages, such as matplotlib. Techniques used to process dictionaries are the
same as processing JSON objects which are used extensively in the API too.

❑ In the Python command window make sure the current directory is


C:\EsriTraining\PYAPI

❑ Start Jupyter notebook, click the Notebooks directory to access its contents and
create a new notebook called ProcessDictionary.

Remember to choose the Python 3 (ipykernel) kernel.

❑ In the empty cell create a variable called trees and assign to it an empty dictionary.

trees = {}

As you know, a Python dictionary is composed of key : value pairs. Your dictionary will be
composed of the name (key) and the genus (value); for example, Hazel : Corylus.

❑ Add the following items in the table (see the next page) to the trees dictionary, for
example to add Hazel (key) and Corylus (value) to the dictionary you would type:

trees[“Hazel”] = “Corylus”

Here is the table…..

Key Value

Hazel Corylus

Elm Ulmus

Oak Quercus

Beech Fagus

Ash Fraxinus

Page 2-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Print out the dictionary.

❑ Press <Shift> and <Enter> to run the cell (and create a new cell).

Your notebook should look like the following:

Let’s examine how you can obtain intellisence.

❑ In the new cell, type trees, and then a full stop followed by a press of the <TAB>
key.

This will display a drop-down list of valid members for the trees dictionary object:

❑ Create a variable called treeItems and assign to it the trees dictionary with the
items() method:

treeItems = trees.items()

© ESRI (UK) Ltd Page 2-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

You can obtain documentation (the docstring) for the method by placing the mouse cursor
between the brackets and pressing <SHIFT> and <TAB>.

❑ Obtain the docstring for the items() method.

Basic floating help is displayed, which might help you….

You will now write a basic for loop to process the dictionary’s items. You will store the key
and value pair in two variables which will be part of the for loop.

❑ Write the following line of code:

for key, value in treeItems:

❑ Print the data contained in the key and value inline variables.

❑ Run the cell.

Your output might look like the following:

In this quick step you have had a brief recap of working with Python dictionaries, which are
used frequently within the ArcGIS API for Python.

The Jupyter Notebook allows you to write traditional Python code and provide you with the
ability to obtain basic intellisence and documentation.

Page 2-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Step 3: Creating Markup for your notebook


In this quick step you will learn how to create some basic Markup (rich comments) for your
notebook. You will provide a main heading with some descriptive text. You will then create a
simple unordered list for the values which will be populated inside of the dictionary.

You will first of all create your main heading and some additional descriptive text.

❑ Click in the top cell (the cell in which the dictionary is created) and from the Insert
menu choose Insert Cell Above.

This will add a new Code cell above the existing cell.

❑ Change the cell type from Code to Markdown.

You will first of all add a main heading to the notebook.

❑ Add a hash sign # to the markdown cell and then type after it “Working with a
Python Dictionary”, so:

Underneath it you will then add a blockquote which allows you to provide slightly indented
free text.

❑ Underneath the main heading add a > and then type the following “In this simple
notebook you will explore how to create a dictionary, populate it and process it
using a for loop. The output will be written to the end of each cell.”

The markup in the Markdown cell should look like the following:

© ESRI (UK) Ltd Page 2-29


An Introduction to Administering your
Portal using the ArcGIS API for Python

To see the markup in action you need to run the cell.

❑ Make sure the mouse cursor is in the Markdown cell and press the Run button.

The output should look like the following:

The markup language has been interpreted and the cell has now been formatted.

❑ Double-click the Markdown cell to edit the cell.

❑ Add a line break tag <br> after the text “populate it”.

This will split the line onto two lines which is more pleasing on the eye.

❑ Add the following markup underneath the blockquote to create an unordered list
explaining the key : value pairs which are added to the dictionary.

❑ Run the cell.

Page 2-30 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The output should look like the following:

❑ Save the notebook.

In this quick step you have refreshed your memory on how to work with Python dictionaries
and how to create some descriptive notation which aids the understanding of your script.

Step 4: Load and run a notebook


In this final step you will run a basic script which has been written for you. It is the ArcGIS
API for Python equivalent of “Hello World..”.

This is one of many pre-written notebooks which you will use in the course.

❑ In the dashboard tab make sure you are in the Notebooks folder and that the
CreateMap.ipynb file is present.

Inside is a notebook called CreateMap.ipynb.

❑ Click the CreateMap notebook to display its contents in a new tab.

It contains some markup and four lines of code:

© ESRI (UK) Ltd Page 2-31


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 3: What do you think the code does?

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

You will now run the code in the cell.

❑ Place the mouse cursor in the cell containing the code and click the Run button.

As the code in the cell executes notice the * next to the cell. This indicates that the kernel is
executing the code:

Once execution has been completed, the kernel then displays the result back to the
notebook in the form of a map widget object.

The map contained inside of the map widget is displayed on the next page.

This map is fully interactive in that you can pan, zoom and rotate the map!

Page 2-32 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

In the next section you will start to investigate the code in more detail and gain a full
understanding of how to build a map and add extra detail to it by exploring the
documentation.

❑ Close all of the browser tabs containing the Jupyter Notebook and close the Python
command prompt window.

So in this exercise you have investigated the Jupyter Notebook IDE. You have explored how
to write some basic Python code and run it; create markup and obtain help. Finally, you
have learned how to launch Jupyter Notebook in a directory which can the access pre-
written notebooks to run some API code.

Exercise End

© ESRI (UK) Ltd Page 2-33


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 2 Questions

Question 1: What is the name of the default Python environment?

Answer: It is called arcgispro-py3

Question 2: Where is the default Python environment located?

Answer: C:\Program Files\ArcGIS\Pro\bin\Python\envs\arcgispro-py3

Question 3: What do you think the code does?

Answer: The cell containing the code does a couple of things:

The GIS class is imported from the arcgis package, gism dule; a new instance of the GIS is
created and from that a map, centred on Aylesbury is displayed.

Page 2-34 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Section 2 Lesson review

Question 1: What are the two components of the notebook?

The Notebook Server and the Dashboard

Question 2: What is a kernel?

The kernel executes code and provides the results back to the notebook for display

Question 3: What types of cell are available in the notebook?

Code, Markdown, Raw NBConvert and the (deprecated) Heading.

Question 4: How do you fully close the Notebook Server down?

Close all tabs down in your browser and press CTRL and C in the Python command window
to close the kernel down.

Question 5: How do you launch a Jupyter Notebook in a directory other than the home
directory?

In the Python command window you should use the cd command to navigate to the desired
folder.

© ESRI (UK) Ltd Page 2-35


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 2-36 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

3 Understanding the SDK

The key to learning how to write code is to understand how to read the software developer
kit (SDK). The SDK for the ArcGIS API for Python is comprehensive in that it provides:

• A full API reference.

• The API reference is supplemented with sample notebooks.

• A guide which describes how to use the API.

This section will show you how to take full advantage of the SDK by learning how to navigate
the API reference, while introducing the sample notebooks and the guide.

Learning objectives
After completing this lesson, you will:

• Understand the structure of the ArcGIS API for Python.


• Navigate the API reference:
o Identify classes, class members and types.
• Write code based upon the API reference contents.

© ESRI (UK) Ltd Page 3-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

Section keywords

Keyword Description

Module A module is essentially a python (.py) file. It contains classes,


and functions that you might want to include in your
application.

Class A class is used to create objects. An object is known as an


instance of the class. The class defines attributes and
methods which all objects created by the class have access
to. Attributes and methods are collectively known as
members.

Attribute An attribute is basically a data variable. It is used to describe


characteristics of a class.
Generally speaking, a Python attribute is also known as a
property.

Method A method is something an object knows how to do when


asked to do so.
Methods are basically functions which belong to a class.

Page 3-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

An overview of the ArcGIS API for Python


The ArcGIS API for Python (the API) is a Pythonic representation of your GIS, in that the API
adheres to Python’s best practices and design. Remember; the GIS refers to either your
online or on-premise portal.

As we have seen, the API is installed through ArcGIS Pro, or via conda or by pip.

The API is supplied as a Python package called arcgis. The package contains a number of
different modules which in turn contain classes, functions and types. These allow you to
perform administration, spatial analysis, run deep learning models and data cleaning across
the ArcGIS platform.

The modules provide access to and organise functionality and focuses on one part of your
GIS.

As you can see, there is a certain amount of colour-coding which is there to help you
identify functionally similar modules. Generally speaking:

• Green modules provide access to geographic data and spatial capabilities, for
example geoprocessing functions and helper objects (more about helper objects
later). The env module provides the ability to share variables between different
modules, for example the default geocoder and environment settings that are
common to all geoprocessing tools.

© ESRI (UK) Ltd Page 3-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

• Blue modules provide additional functionality such as geocoding, geo-enrichment of


lifestyle data, geometry processing functions and additional geoprocessing support.

• Purple modules work with either feature data, feature layers and collections of
feature layers in your portal; providing extended support for the Pandas DataFrame;
and for storing continuous data stored in the form of raster data and imagery layers.

• Orange modules generally speaking allow you to visualise and share your data and
results of your work. The widgets module allows you to work with the MapView
notebook widget.

• The red module contains very basic functionality for managing Notebooks.

• The gray Learn module provides access to many models for machine learning within
the API. The module is not installed automatically and needs to be installed
separately.

The most important module is the gis module.

It represents and provides access to your GIS. We will look at this in a few moments. But
first……..

Page 3-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Activity: Investigate the architecture of the


API
In this quick activity you will open up the ArcGIS API for Python documentation and answer
the following questions.

Perform the following actions:

1: Open up a browser and type in:

https://developers.arcgis.com/python/guide/overview-of-the-arcgis-api-for-python/

This page provides a nice pictorial representation of the contents of the ArcGIS
API for Python. It is not accessible directly through the Guide so it is worth
bookmarking this page for your future reference.

2: Spend a few moments reading the page documentation.

3: Answer the following questions:

Question 1: The arcgis.gis module provides functionality to manage GIS users, groups
and content. True or False?

___________________________________________________________________________

___________________________________________________________________________

Question 2: Which module allows you to work with feature data, feature layers and
collections of feature layers?

___________________________________________________________________________

___________________________________________________________________________

Question 3: What does the arcgis.raster module allow you to do?

___________________________________________________________________________

___________________________________________________________________________

___________________________________________________________________________

© ESRI (UK) Ltd Page 3-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 4: What are the three types of geocoding that the arcgis.geocoding module
performs?

___________________________________________________________________________

___________________________________________________________________________

Question 5: Which module would you investigate if you wanted to process sensor data and
real-time data feeds?

___________________________________________________________________________

___________________________________________________________________________

Page 3-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

What is the gis module?


The gis module provides a representation of your GIS – your portal. Your GIS is either one
hosted with ArcGIS Online or ArcGIS Enterprise.

It is an important module as it provides functionality to manage and administer GIS users,


groups and content. It is considered the most important module as it provides access to
your GIS and is therefore an entry point for you to write code.

The gis module is composed of a number of classes which help you manage your GIS.
These include GIS, Item, User, Group, Role.

There are also a number of sub-modules, such as admin, server, agonb, sharing and tasks.
Each of these sub modules contain their own classes which help you administer your GIS.

The gis module can be represented as below:

There are also a number of Helper classes, so-called Resource Managers which help you
manage your users, groups and content in general.

© ESRI (UK) Ltd Page 3-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

We will explore the pure administration side of the gis module more in a subsequent
section.

To access the gis module you might type the following code:

import arcgis

myGIS = arcgis.gis

But it is rare you will see the above code.

Instead you will normally see the following code:

from arcgis.gis import GIS

Can you work out what the code is doing?

It is basically saying….

“from the arcgis package, and from the gis module, give me
access to my GIS through the GIS object.”

GIS is a class within the gis module and it is your entry point into your code and into
either your online or on-premise portal.

Page 3-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The GIS() class


The GIS class represents your GIS – whether it is ArcGIS Online or ArcGIS Enterprise. It is
your entry point into the API.

The corresponding GIS object is used to manage users, groups and datastores and for
accessing your GIS content.

Accessing your portal


To access your GIS you need to provide your credentials, just as you would to get access to
your online or on-premise portal.

To access your organisation in ArcGIS Online you would write the following code:

myGIS = GIS(“https://www.arcgis.com”,

Org_uname, Org_pword)

If no username and password are supplied then a connection to ArcGIS Online is made as an
anonymous user. This will allow you to access content that has been shared with the
EVERYONE option.

When connecting to an ArcGIS Enterprise portal your URL becomes:

http://machinename.domain.com/webadapter

When accessing an on-premise portal you have a number of authentication options:

Anonymous Access: If using Integrated Windows Authentication (IWA)

Built-in user account: Similar to how accounts are managed in ArcGIS Online, through a built
in Identity Store.

Use an Enterprise Identity Store: Use web-tier \ Portal-tier authentication using LDAP
(Lightweight Directory Access Protocol), or Portal-tier authentication using Active Directory.

Using PKI for Enterprise web-tier authentication: PKI stands for Public Key Infrastructure.
There are two methods for connecting to a PKI protected ArcGIS Enterprise; a file is used
either as a client-side certificate, or as a formatted certificate file and password.

Through ArcGIS Pro: This might be a good option to consider if you have an advanced
authentication scheme such as Kerberos or Smart Card.

© ESRI (UK) Ltd Page 3-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

Examples of how to authenticate correctly can be found at the following link:

https://developers.arcgis.com/python/guide/working-with-different-authentication-
schemes/

But how do we know what the arguments are for working with the GIS class? Are they
mandatory or are they optional? The API Reference contains all of this information.

Page 3-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Using the GIS class to understand the API


Reference
The key to writing your code is to gain an understanding of how to interpret the API
reference.

The API Reference for the ArcGIS API for Python can be found here:

https://developers.arcgis.com/python/api-reference/

Documentation for each individual module can be found in the reference.

The reference displays documentation for the arcgis.gis module by default and it is
from here that you can locate the documentation for the GIS class, and all of the other
classes.

Clicking the link for the GIS class will display its documentation within the API Reference.

© ESRI (UK) Ltd Page 3-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

Class description
The API reference provides a description of what the class is and what it does, along with
information as to how to create an object from that class.

The keyword class indicates that GIS is indeed a class. Its location within the API (arcgis
package \ gis module) is also presented.

The class constructor arguments

The constructor (the brackets) specifies what arguments the GIS class can receive which
helps instantiate the GIS object; in this case the authentication options mentioned earlier in
the section. For the GIS class the arguments are all optional and specify default values, or
what will happen if no optional arguments are supplied.

The constructor also supports optional arguments (**kwargs), such as proxy_host.

Generally, the documentation provides a description for each argument and indicates if the
argument is optional or not, and its type i.e. string, boolean, integer:

Page 3-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Code snippets
The documentation for the GIS also includes some code samples which should be studied:

Instance properties and methods


The GIS class supports a number of properties. These are attributes / characteristics which
describe the GIS object and can be defined by the word property followed by the name
of the property, for example groups.

More often than not the return value for the property will be specified in the
documentation, but in this example it is not.

Notice that the groups property provides a hyperlink to another part of the API Reference
from which you can access the GroupManager helper class which is found in the
arcgis.gis module.

Hovering over the GroupManager hyperlink provides a floating message indicating the
location of the GroupManager class.

© ESRI (UK) Ltd Page 3-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

Methods can be identified by having a pair of brackets after their name, for example
update_properties(). Once again, arguments can either be optional or required, with
the method documentation describing the arguments and the method’s return value.

If a hyperlink exists then you can hover over the hyperlink to determine where the object
created by the method resides within the API.

Page 3-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The map widget


The map widget can be used to display content within your portal, analysis you might
perform and content from outside of your portal.

The map widget is instantiated by calling the map() method, which is found on the GIS
object.

The map() method takes a number of arguments which, for example, allow you to specify a
location to centre the map on and a zoom scale based upon the basemap levels of detail
scale settings.

The map() method returns a reference to a MapView object. The corresponding MapView
class is found in the arcgis.widgets module.

By exploring the MapView class documentation, the properties and methods of the
MapView can be examined and applied to the map. For example, add / remove a layer,
change the basemap, draw on it, rotate the basemap and save it as an item in the GIS.

© ESRI (UK) Ltd Page 3-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

Code snippet
This code snippet demonstrates how to work with some of the members on the MapView
object, such as basemap, and the save() method.

The above snippet sets a new basemap for the map widget and then the save() method
creates a brand new web map item in the online portal.

Notice that a dictionary of item properties is supplied to the save() method, as this is
used to provide the required metadata for your new portal item.

Page 3-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Activity: Accessing the MapView object via


the map() method
In this activity you will identify how the map() method can access the MapView class. You
will do this by performing the following instructions:

• In your browser go to the following page:

https://developers.arcgis.com/python/api-reference/

• Locate the GIS class and click on it to access its documentation.

• Scroll down to find the map() method on the GIS class and answer the following
questions:

Question 1: How many arguments are optional for the map() method and what are they?

___________________________________________________________________________

___________________________________________________________________________

Question 2: Is the map widget only supported within Jupyter Notebook? (Yes / No)

___________________________________________________________________________

Question 3: What hyperlink should you click on to access the documentation for the map
widget?

___________________________________________________________________________

Question 4: Which module does the hyperlink take you to?

___________________________________________________________________________

Question 5: On the MapView class help, what does the snap_to_zoom() method do?

___________________________________________________________________________

___________________________________________________________________________

© ESRI (UK) Ltd Page 3-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to section activities

Investigate the architecture of the API


Question 1: The arcgis.gis module provides functionality to manage GIS users, groups
and content. True or False?

Answer: TRUE

Question 2: Which module allows you to work with feature data, feature layers and
collections of feature layers?

Answer: the arcgis.features module allows you to accomplish the above tasks.

Question 3: What does the arcgis.raster module allow you to do?

Answer: The arcgis.raster module contains classes and raster analysis functions for
working with raster data and imagery layers.

Question 4: What are the three types of geocoding that the arcgis.geocoding module
performs?

Answer: The arcgis.geocoding module allows you to perform single address


geocoding, batch geocoding and reverse geocoding.

Question 5: Which module would you investigate if you wanted to process sensor data and
real-time data feeds?

Answer: The arcgis.realtime module provides types and functions for processing
sensor data and real-time data feeds.

Page 3-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Accessing the MapView object via the map() method


Question 1: How many arguments are optional for the map() method and what are they?

Answer: The map() method takes 4 optional arguments and they are location, zoomlevel,
mode and geocoder.

Question 2: Is the map widget only supported within Jupyter Notebook? (Yes / No)

Answer: Yes – only the Jupyter Notebook (and Jupyter Lab).

Question 3: What hyperlink should you click to access the documentation for the map
widget?

Answer: You should click the map_widget hyperlink.

Question 4: Which module does the hyperlink take you to?

Answer: Clicking on the hyperlink will take you to the arcgis.widgets module.

Question 5: On the MapView class help, what does the snap_to_zoom() method do?

Answer: It allows the map to display at the next level of detail when zooming in or out
when set to True, or for continuous zooming when set to False.

© ESRI (UK) Ltd Page 3-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 3: Understanding the SDK through


the map widget

Estimated Time: 20 Minutes


In this exercise you are going to get some practice in navigating the ArcGIS API for Python
documentation and experience some of the common classes and class members.

In this exercise you will carry out the following:

• Start Jupyter Notebook and connect to your portal.


• Investigate the map() method on the GIS class.
• Write some code to display a map.

Step 1: Access your portal


You will use the Jupyter Notebook developer environment. You will learn how to start your
notebook in a particular directory so you can then access the directory’s content from
within the Jupyter Notebook. You will also connect to the Esri UK Learning Services online
portal, via the ArcGIS API for Python, using the credentials you were supplied with earlier.

First of all you will start Jupyter Notebook and navigate to the exercise working directory.

❑ In the Windows search bar type “Python command prompt” and press <Return>.

Page 3-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

This will display the Python command prompt.

❑ Type the following (highlighted in bold) at the command prompt:

..\..\arcgispro-py3>cd \

This will take to the root directory of your C:\ drive. This is a good place to be as you can
now start Jupyter Notebook and navigate other folders while within the Jupyter Notebook
environment.

❑ Once again, navigate into the C:\EsriTraining\PYAPI folder using the cd command:

cd C:\EsriTraining\PYAPI

The Python command prompt should look the same as the picture below:

❑ Start Jupyter Notebook by typing in jupyter notebook at the command prompt:

The Notebook Dashboard will display in a new browser tab after a few moments,

❑ Within the Notebook dashboard click the InvestigatePortal folder.

Inside is a single text file called ThisFolderIsEmpty.txt. It is in this folder that you will create
your notebook.

❑ Create a new notebook by clicking on the New button > Python 3 (ipykernel).

This will open up your new notebook in a new tab on your browser.

Let’s rename the Notebook.

© ESRI (UK) Ltd Page 3-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 1: What is the current name of the Notebook?

___________________________________________________________________________

___________________________________________________________________________

❑ From the File menu choose Rename and change the name to InvestigatePortal and
click the Rename button.

You are now ready to write some code and connect to your portal.

Before you do that you will first of all add some markdown notes to your notebook to
indicate what you are going to do.

❑ Change the cell format from Code to Markdown.

❑ Enter the following:

## Step 1: Connect to your Portal

<b> You will need to provide a password</b>

❑ And run the Markdown cell

This will display the following in the cell. You have created some basic comments.

Now you will investigate some of the documentation.

❑ Open the API Reference for the ArcGIS API for Python by accessing the following help
page:

https://developers.arcgis.com/python/api-reference/

Page 3-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ In the arcgis.gis module click on the GIS class.

❑ Spend a few moments reading the documentation for the GIS class…

…..and answer the following questions:

Question 2: Are there any required arguments?

___________________________________________________________________________

___________________________________________________________________________

Question 3: Are there any optional arguments?

___________________________________________________________________________

___________________________________________________________________________

Question 4: What happens if no url, username or password is supplied?

___________________________________________________________________________

___________________________________________________________________________

In order to access your GIS (i.e. your portal) you will perform some imports to get access to
the API.

❑ In the newly created cell underneath the markup import the API and access the
GIS class by typing the following:

from arcgis.gis import GIS

© ESRI (UK) Ltd Page 3-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Now create an instance of the GIS class called gis and pass in the arcgis online url
and your username into the GIS() class constructor:

Your code should look like the following:

from arcgis.gis import GIS

gis = GIS(“https://www.arcgis.com”,
“<<add your_ArcGIS_username here>>”)

❑ Underneath, add a print() function and provide a message to indicate that you
have successfully connected to your portal.

❑ Run your code.

Notice that when the code in the cell is executing a blue (*) asterisk is displayed next to the
cell.

You will need to enter a password for the username you supplied.

Hopefully a message will be displayed indicating that a connection to the portal was made
successfully.

Notice that the username is “Ed_Training” – yours will begin with


‘em_esriuk_st…’

Once you have successfully logged into the online portal a new cell is created on successful
execution of your code.

The final thing you will do is create a quick map of where you live.

❑ In the documentation for the GIS class look for the map() method.

Notice that all of the arguments for the method are optional.

Page 3-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 5: What argument might allow you to specify a location for your map?

___________________________________________________________________________

Question 6: If supplying a location what data type should the location be supplied as?

___________________________________________________________________________

Question 7: What does the zoom level indicate? What is the data type that should be
supplied for the zoom level?

___________________________________________________________________________

___________________________________________________________________________

A zoom level of 1 will display the whole of the default basemap as defined by
your portal administrator. The Learning Service default basemap is the
Topographic basemap.

❑ In the newly created cell write a line of code which will create a map widget at the
location of your choice.

❑ To display the map widget containing the map just type the name of the variable
storing the map.

❑ Run your code.

❑ Investigate choosing some different zoom levels.

You can change the zoom level and re-run the cell without have to run
any previous cells.

The map below displays the Cambridge area on the Topographic basemap.

© ESRI (UK) Ltd Page 3-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

Creating a map is always a good test to see whether or not you have made a connection to
your portal.

The code to create the map and display it, may look similar to the code displayed below:

In this step you have connected to your online portal through the GIS class and referenced
your portal in a variable called gis. You have also used a method on the GIS class called
map() to create a map using a specified location and zoom level as arguments to the
method.

❑ Save your Notebook.

Step 2: Investigating the map widget


In this quick step you will have a quick look at some of the attributes (properties) and
methods which you can apply against a map view widget.

The documentation for the map() method states that the method “Creates a map widget
centred at the declared location with the specified zoom level…. See MapView for more
information”.

Notice that the word MapView is hyperlinked.

Page 3-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Click on the hyperlink.

Question 8: Which class is displayed?

___________________________________________________________________________

Question 9: Which module contains the MapView class?

___________________________________________________________________________

❑ In the new cell, underneath the displayed map, write some basic markup as you did
for step 1. Include the following text:

## Step 2: Investigate the Map Widget

<b>Explore members on the MapView class</b>

❑ Run the cell to display the markup.

❑ In the new cell, create a new variable called myMap.

❑ To the myMap variable use the map() method on the gis to create a map. Supply
the following arguments to the method:

Location: “Keswick, UK”

Zoomlevel: 12

❑ To display the map, underneath the last line of code you wrote type:

myMap

… and run the code cell.

A 2D map of Keswick will be displayed…..

© ESRI (UK) Ltd Page 3-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

The variable myMap is now referencing the MapView class in the arcgis.widgets module.

❑ If you haven’t done so already then locate the MapView class in the widgets module
by clicking the widgets hyperlink or by navigating to the class in the documentation.

You will now investigate the basemaps that are available to you for this map view widget.

Question 10: Which property will display the basemaps for the map view?

___________________________________________________________________________

❑ Comment out the line of code which displays the map view widget and underneath
it, create a variable called viewBasemaps.

❑ Assign to the viewBasemaps variable the code myMap.basemaps.

❑ Print the contents of the viewBasemaps variable.

This will display the basemaps that are available for you to display in the map view.

Page 3-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

By looking at the basemaps property on the MapView class you will see that it returns a
Python list of basemap names.

❑ Assign the basemap of your choice to the basemap property on the myMap object
and display the map by writing the following code:

myMap.basemap = ‘topo-vector’

myMap

❑ Run the code cell.

In the example below the ‘topo-vector’ basemap is being displayed:

Notice the 2D / 3D icon on the map view.

This will toggle your map view into either a 2D view or a 3D view. Full navigation properties
are available in both views and it is a good way to explore your area of interest.

❑ Click the 2D / 3D toggle button to place the map view mode into 3D.

❑ Explore some of the navigation controls for panning and tilting the 3D view.

© ESRI (UK) Ltd Page 3-29


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Once you have explored the 3D view switch back to the 2D mode and save your
notebook.

Your code for this step should look similar to the following:

❑ Keep the Jupyter Notebook open for the next exercise.

In this exercise you have navigated from the GIS class in the arcgis.gis module to the
MapView class in the arcgis.widgets module and explored a couple of the properties on the
MapView class, and in doing so you have started to investigate the API Reference.

Exercise end

Page 3-30 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 3 Questions


Question 1: What is the current name of the Notebook?

Answer: The current name of the Notebook is called Untitled.ipynb.

Question 2: Are there any required arguments?

Answer: There are no required arguments.

Question 3: Are there any optional arguments?

Answer: There are a number of optional arguments (9 ) and a number of additional


keyword arguments (**kwargs).

Question 4: What happens if no url, username or password is supplied?

Answer: If no url or credentials are supplied then anonymous access to ArcGIS Online is
obtained.

Question 5: What argument might allow you to specify a location for your map?

Answer: The location argument!

Question 6: If supplying a location what data type should the location be supplied as?

Answer: the location should be supplied as a string.

Question 7: What does the zoom level indicate? What is the data type that should be
supplied for the zoom level?

Answer: The zoom level specifies the map scale based upon levels of detail and should be
supplied as a number (integer).

© ESRI (UK) Ltd Page 3-31


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 8: Which class is displayed?

Answer: The MapView class is displayed.

Question 9: Which module contains the MapView class?

Answer: The MapView class is contained in the arcgis.widgets module.

Question 10: Which property will display the basemaps for the map view?

Answer: basemaps

Page 3-32 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 3 Solution

© ESRI (UK) Ltd Page 3-33


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 3-34 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

4 Searching for portal items

In the previous section you saw that the GIS class is a great starting point into the API. It
represents your portal and from this you can then start to access content within your portal,
whatever that content might be.

This section will show you how can start to get access to your content. This is an important
pre-cursor to any administration of your portal. This is achieved by taking advantage of a
number of resource manager classes (available via the GIS class) which provide access to the
different types of items within your portal.

Learning objectives
After completing this lesson, you will:

• Understand how to access the GIS resource manager classes.


• Understand the different types of items that can be attained from these managers.
• Present the items using the Jupyter notebook’s rich-display functionality.

© ESRI (UK) Ltd Page 4-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

The GIS Resource Manager classes


The GIS class contains a number of properties which provide access to a number of GIS
resources. Types of GIS resources are users, groups, content and datastores. The properties
which provide access to these GIS resources are:

GIS.content

GIS.datastore

GIS.groups

GIS.users

Each of these properties provides access to so-called resource manager classes, namely
UserManager, PortalDataStore, GroupManager and ContentManager. These resource
manager classes provide the ability to manage users, create and manage roles, manage
groups and to add, access, modify and delete GIS content.

Think of the resource manager classes as abstract classes in that they are not instantiable.
Instead, access is granted to the desired resource object by working with the relevant
attribute.

Looking at the documentation for each of these properties provides access to the respective
class, for example, the users property provides access to the corresponding resource
manager for GIS users – namely UserManager.

There are similar methods on the returned helper (resource manager) object such as
get(), search() and create() to manage the resource in question.

Page 4-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The UserManager resource manager contains a property called roles. This property
provides access to an instance of the resource manager helper object called RoleManager.

Code snippet
In the code snippet below a new folder is created called “MyNewFolder” in the online
portal. To accomplish this the content property, found on the GIS object, is used to gain
access to an instance of the ContentManager class, from which the create_folder()
method can then be used.

If the folder was successfully created then a JSON object can be displayed and interrogated.

© ESRI (UK) Ltd Page 4-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

Using the search() method


Each resource manager implements the search() method so you can search for content,
groups and users.

The search() method found on the ContentManager class has the following syntax:

The search() method on the UserManager is very similar to the syntax for the
ContentManager class, and is as follows:

The GroupManager class’ search() method again has similarities to the other
search() methods:

Each variation of the search() method has a number of arguments, whose descriptions
are available within the API documentation, for example, sort_order, max_fields,
outside_org and query.

Page 4-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The search() method’s query argument


The key to searching your portal is to understand how to use the query argument.

The query argument is a string which defines which items you wish to search for. It is not
obvious how this string is constructed.

A query is made up of terms and operators.

A term can be a single word, for example (“London”), or a phrase (multiple words
surrounded by double quotes, such as “London City”). Operators include AND, OR, NOT.

When searching for items or groups within your portal the query can be based upon named
fields or by using default fields. When using default fields then the name of the actual field
does not need to be specified – just the search term. Default fields vary depending upon the
type of content you are searching for in your portal.

A description of those fields which are default and those which need to be named are found
in the following help page:

https://developers.arcgis.com/rest/users-groups-and-items/search-reference.htm

The default fields for items are:

title
tags
snippet
description
type
typekeywords

The following are some of the non-default fields which can be searched upon for an item:

id
owner
created
modified
title
type
typekeywords
description
tags
snippet
accessinformation
access

© ESRI (UK) Ltd Page 4-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

Any field can be searched for by typing the name of the field followed by a colon, and then
the search item, so:

query=”owner: Ed_Training”

Searching of groups will have slightly different default fields and general search fields.

The type filter for item is case sensitive and must be surrounded by double
quotation marks for exact item type matching.

For example, use type:"Web Map" if you are only interested in finding web
map items; type:"web map" will return both web map and Web Mapping
Application if both exist.

Searching for dates within the portal

Date ranges can be searched by specifying the lower and upper bounds of the dates. The
dates are specified as milliseconds (more about working with dates later in this section).

Range queries can be inclusive or exclusive of the upper and lower bounds. Inclusive range
queries are denoted by brackets ([]). Exclusive range queries are denoted by braces ({}).

For example, to find all items created between December 1, 2009, and December 9, 2009,
the search expression is as follows:

created: [1259692864000 TO 1260384065000]

Page 4-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Searching for content


So how do you search for content within your portal? Let’s look the example below:

The content property accesses an instance of the ContentManager helper class. The
search() method on the helper class is then used to query content in the portal. The
query identifies all Web Maps owned by Ed_Training. A maximum of 100 items (Web Maps)
are returned.

Valid values for the item_type argument can be found in the Type column for the various
tables which can be found on the following web page:

https://developers.arcgis.com/rest/users-groups-and-items/items-and-item-types.htm

The query returns a list (myContent) of API Item objects.

Each item can be accessed based upon its index position within the list or processed using a
for loop.

An item object is a Python dictionary object which has a number of useful keys.

More about items in the next section.

© ESRI (UK) Ltd Page 4-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

Wildcard searches can be carried out by using the “*” symbol; for example:

Page 4-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Investigating your search results


If you are working with Jupyter Notebooks then you can display the resulting items within
your list as they are processed. The display of the ‘rich representation’ of your items is
because Jupyter Notebook takes advantage of the inbuilt IPython (Interactive Python)
support – in fact Jupyter Notebook used to be known as IPython Notebook.

Importing the IPython module will provide the ability to display the rich representation view
of your portal items:

© ESRI (UK) Ltd Page 4-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

Searches based upon dates


Special mention should be made as to how dates are stored and queried within your portal.

In Python, dates are based upon “the beginning of time” (AKA the epoch) which is taken as
being 1st January 12:00am 1970. Time and dates are measured in seconds from this so-
called epoch. There are a number of modules that support working with dates and time such
as the time and calendar modules and the popular datetime module.

The time module works with unix time stamps and is expressed as a floating point number
which can make calculations cumbersome. The datetime module supports many of the
functions which the time module supports and also provides a more object orientated
approach.

If you wish to query items based upon dates then you will need to import the datetime
module; often the module name is simplified by using the as keyword so:

import datetime as dt

print(dt.now()) # >> 2020-08-19 17:58:31.19179

If you wish to create a date as a basis for searching then the datetime module supports
the datetime() class, which has 3 mandatory arguments: year, month and day and five
optional arguments (hour, minute, second, microsecond, tzone) whose default values are
0.; so:

date = dt.datetime(2020, 8, 19)

The date you have created needs to be based upon the number of seconds that have
elapsed since the epoch and this is achieved by working with the timestamp() function.

But, your portal stores times and dates not as seconds but as milliseconds. This means that
if you want to perform a search based upon a date then the date object will need to be
converted to seconds via the timestamp() method, and then multiplied by 1000 to
convert into milliseconds:

ts = date.timestamp()

portalDate = int(ts * 1000)

print(portalDate) # >> 1597791600000

Page 4-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

These representations of portal dates can be used to return items either by using list
comprehension or by using a range search based upon the created field:

Notice that the format() method is used to place the dates used in the range into the
query string.

© ESRI (UK) Ltd Page 4-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

Activity: Complete the following code…


In this activity you are going to fill in the missing pieces of code by navigating the API
reference.

So, open a browser and type on the following:

https://developers.arcgis.com/python/api-reference/

The reference contains the documentation to help you write your code.

As you complete this activity you should be working with the GIS(), ContentManager() and
Item() classes. Use the documentation to navigate to the relevant classes and take
advantage of the hyperlinks that the help provides.

Page 4-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to section activities

Complete the following code…

© ESRI (UK) Ltd Page 4-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 4: Investigating the contents of your


portal

Estimated Time: 30 Minutes


In this exercise you are going to gain some experience in accessing and finding content
within your portal. In doing this you will start to get some practice in navigating the ArcGIS
API for Python documentation and experience some of the common classes and class
members.

In this exercise you will carry out the following:

• Access the GIS class and investigate resource manager classes.


• Search for content using query options.
• Optional Step: Obtain metadata information relating to a particular hosted feature
layer.
• Challenge step: Present portal dates in a human-readable form.

Step 1: Access your portal


Once again you will use the Jupyter Notebook to write your python code.

Hopefully the Jupyter Notebook is still open and available from the last exercise.

❑ If Jupyter Notebook is open then click on the Dashboard tab and navigate to the
\PYAPI\Searching folder to display its contents.

If Jupyter Notebook is not open then carry out the following:

• In the Python Command Prompt cd to the root of C:\

• cd into the C:\EsriTraining\PYAPI folder and then start Jupyter Notebook.

• Navigate to the following folder: \PYAPI\Searching folder within the dashboard.

• Display its contents.

So… you have access to the contents of the Searching folder.

Page 4-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Inside is a single text file called ThisFolderIsEmpty.txt. It is in this folder that you will create
your notebook.

❑ Create a new notebook by clicking the New button > Python 3 (ipykernel).

This will open up your new notebook in a new tab on your browser.

❑ Rename notebook as SearchContent.

You are now ready to write some code and connect to your portal.

Before you do that you will first of all add some markdown notes to your Notebook to
indicate what you are going to do.

❑ Change the cell format from Code to Markdown.

❑ Enter the following:

## Step 1: Connect to your Portal

<b> You will need to provide a password</b>

❑ And run the Markdown cell

© ESRI (UK) Ltd Page 4-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

This will display a markdown cell with comments in and a new cell is created underneath it.

You will now make a connection to your portal, just as you did in the previous exercise.

❑ Write the following code to access the gis module, in the API, and use the GIS class.

from arcgis.gis import GIS

❑ Now create an instance of the GIS class called gis and pass in the ArcGIS Online url
and your username into the GIS() class constructor:

Your code should look like the following:

from arcgis.gis import GIS

gis = GIS(“https://www.arcgis.com”,
“<<your_ArcGIS_username>>”)

Remember to specify your ArcGIS username provided at the


beginning of the course. It begins with em_esriuk_st…

❑ Underneath, add a print() function and provide a message to indicate that you
have successfully connected to your portal.

❑ Run your code.

You will need to enter a password for the username you supplied.

Page 4-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Hopefully a message will be displayed indicating that a connection to the portal was made
successfully.

A new cell has been created underneath the successfully executed code cell.

You are now ready to investigate the contents of your portal!

Step 2: Search for content in your portal


In this step you will investigate the process of searching for and processing content within
your portal.

Your portal is made up of different types of content, such as web maps, web scenes, feature
layers, web mapping applications, etc. The API provides excellent searching facilities to
isolate the type of content you are interested in. In this step you will find information about
feature layers that are within your portal.

❑ In the newly created cell add some mark-up so that the final result looks like
following when the cell is run as markdown:

The GIS class contains a number of properties which provide access to so-called resource
manager objects, such as UserManager, GroupManager and ContentManager. Think of
these objects as helper objects; they are there to provide functionality for managing their
corresponding objects such as adding, or searching for content

❑ Within the new cell create a new variable called contManager and assign
gis.content to it.

© ESRI (UK) Ltd Page 4-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

The property content is found on the GIS class and provides access to the corresponding
ContentManager object.

You will use the search() method on the ContentManager class to find all items of type
“Feature Layer” in your portal.

❑ Locate the documentation for the search() method on the ContentManager class

On the GIS class locate the content property and click on the hyperlink
associated with the ContentManager.

Question 1: Which argument(s) are required for the search() method?

___________________________________________________________________________

The query argument will, by default, search in a number of content metadata fields, such
as title, tags, and type. There are others. These search parameters should be supplied as a
string.

If an empty query string is supplied, then there is no restriction on the types of content
which will be returned.

The item_type argument defines what type of content the search() method will
return. The values for this argument can be found in the associated documentation. You will
search for ‘Feature Layer’ item types.

Question 2: What is the default maximum number of items returned by the search()
method?

___________________________________________________________________________

Your portal contains more than 10 feature layers. You will set the max_items argument to
a value of 500.

❑ Write the following code to return all those items in the portal which are of type
‘Feature Layer’.

❑ Run the code in the cell.

Page 4-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 3: What data type is the featLayers variable? (HINT: Use the type() builtin
function)

___________________________________________________________________________

❑ Use the print() function to count the number of feature layers that are currently
within the portal. HINT : Use the len() function.

❑ Run the code in the cell again.

Question 4: How many feature layers are in the portal?

___________________________________________________________________________

The number of feature layers is based upon all feature layers in the portal.

Let’s try and amend the query argument to find the number of feature layers owned by
emorris@esriuk.com_EsriUK_Training.

❑ Amend the query argument with the following:

owner: emorris@esriuk.com_EsriUK_Training

The search() method should look like the following:

Question 5: How many feature layers are owned by


emorris@esriuk.com_EsriUK_Training?

___________________________________________________________________________

There are many feature layers owned by emorris@esriuk.com_EsriUK_Training,


over 20 of them.

© ESRI (UK) Ltd Page 4-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Create a for loop to print out the basic metadata information for each feature layer
item in the list.

The output will look very similar to the following:

Remember that the items in the list are only those which the owner has shared to the
organisation or to all users who have access to the ArcGIS Online portal.

Step 3: Display the results as rich output


In the previous step you displayed all of the feature layers owned by a user (which have
been shared appropriately). One of the advantages of using a Jupyter Notebook is that you
can display the feature layer list items via their rich representation. In order to do this you
will need to import the IPython module.

❑ In the newly created cell underneath your existing code, add some mark-up so that
the final result looks like following when the cell is run as markdown:

❑ Write the following line of code in the new cell underneath the Step 3 comments:

❑ Recreate the for loop you wrote in step 2 underneath the import statement you
just wrote.

❑ Replace the print() function in the for loop with display().

Remember to pass in the variable which currently holds the item you are processing in the
list within the brackets of the display() function.

Page 4-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The finished code for this step should look very similar to the following:

❑ Run the cell.

The rich representation output will look similar to the following:

In this step you have learned how to perform a search by applying a number of arguments
which accesses feature layers owned by a particular portal member. You have also displayed
a subset of those feature layers using the rich representation functionality available within
Jupyter Notebook.

Optional Step: Identify Item metadata


In this optional step you will investigate an existing feature layer item in the Esri UK Training
portal and answer some questions about it, before writing some simple code to back up the
answers you have found. In doing so you will see that the item is based upon a Python
dictionary object.

❑ Open a new tab on your browser and type in:

https://www.arcgis.com/

© ESRI (UK) Ltd Page 4-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Log in to the portal using the credentials you used to connect to the portal while
using the ArcGIS API for Python.

❑ Using your ArcGIS portal skill search for the “York_Historic_Buildings” layer to
display its metadata.

❑ Answer the following questions:

Question 6: When was the dataset created?

___________________________________________________________________________

Question 7: What is the unique item ID? (Hint: Look at the URL)

___________________________________________________________________________

Question 8: Who is the owner?

__________________________________________________________________________

You will now write some code to confirm your findings.

❑ In the newly created cell add some mark-up to indicate that this is an optional step
to obtain some item information stored within the portal.

❑ Create a variable called yorksList and write some code to perform a search to
find the York_Historic_Buildings item. You should construct a line of code which
performs the following:

• Call the search() method on the gis.content property.

• For the query parameter search for the “York_Historic_Buildings” item using
title as the search tag.

• Set the item_type parameter to be “Feature Layer”.

Question 9: What data type is the yorksList variable storing? (Hint: use the type()
built-in function.)

Page 4-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Print the list out.

You will see there is one item in the list.

❑ Create a variable called yorks and use the zero-index notation to obtain the item
from the list and store it in the yorks variable.

The variable yorks stores an Item object. By looking in the documentation for the Item
object you can see that it is based upon a Python dictionary. The Item class documentation
is found in the arcgis.gis module.

As you may know, a dictionary object has a keys() method.

❑ Write the following line of code to display the keys() that are found on the yorks
dictionary (item) object.

print(yorks.keys())

You can use these keys on the yorks dictionary object to obtain the associated values of the
feature layer.

❑ Write some code to obtain the values which are associated with the id, owner and
created keys.

Your code, and output, should be very similar to the code displayed below:

© ESRI (UK) Ltd Page 4-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Run the code in the code cell.

How does the output compare to the answers to the questions?

One striking difference is the value returned for the created date. This number refers to the
date stored as milliseconds i.e. the number of milliseconds that have elapsed since the 1 st
January 1970, 12:00am – the beginning of the Python epoch.

If you would like a challenge (and you have time) then perhaps you might like to create a
human-readable date based upon the milliseconds value referenced by the ‘created’ key.

Challenge step: Convert milliseconds into a human-


readable format
In this challenge step you will write some code to convert the value stored in the ‘created’
key into a recognisable date.

In order to do this you should carry out the following:

• Create a new cell.

• import datetime as dt

• Obtain the value associated with the ‘created’ key and divide by 1000.

• Pass the result of the above line into the fromtimestamp() method on the
dt.datetime class to create a readable date.

• Print the result out and compare to the answer to question 6.

The output should be the following:

If you have made it this far then great work.

Exercise end

Page 4-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 4 Questions

Question 1: Which argument(s) are required for the search() method?

Answer: query argument is the only required argument.

Question 2: What is the default maximum number of items returned by the search()
method?

Answer: The default maximum number of items returned is 10 items.

Question 3: What data type is the featLayers variable?

Answer: A Python list is returned by the search() method.

Question 4: How many feature layers are in the portal?

Answer: There are over 50 feature layers in the portal!

Question 5: How many feature layers are owned by


emorris@esriuk.com_EsriUK_Training?

Answer: There are over 20 feature layers owned by


emorris@esriuk.com_EsriUK_Training.

Question 6: When was the dataset created?

Answer: Jun 13th 2019

Question 7: What is the unique item ID? (Hint: Look at the URL)

Answer: b15076e69a514364a94d809688d4c081

© ESRI (UK) Ltd Page 4-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 8: Who is the owner?

Answer: emorris@esriuk.com_Esriuk_Training

Question 9: What data type is the yorksList variable storing? Hint: use the type()
built-in function.

Answer: the yorks variable stores a Python list.

Page 4-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 4 Solution

© ESRI (UK) Ltd Page 4-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 4-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

5 Administering your portal

The ability to administer your portal is one of the most important things you will do with the
ArcGIS API for Python. Administration covers all sorts of things, from creating new users and
groups, assigning users to groups, managing content (for example identifying which user’s
content accounts for the most space), to cloning your existing portal.

This section will investigate some of the classes and their members that allow administrative
tasks to be performed with just a few lines of code. You will also investigate some of the
samples that are available and how to visualise your results.

Learning objectives
After completing this lesson, you will:

• Consider how online users and groups are created and managed.
• Introducing how your content is managed within your portal.
• An overview of the gis.admin and gis.server submodules to support Portal for
ArcGIS.
• Investigate how Portal for ArcGIS enterprise servers are managed.
• Basic service administration within your enterprise
• Identify administration samples.
• Investigate visualisation through Matplotlib.

© ESRI (UK) Ltd Page 5-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

Administration capabilities
The ArcGIS API for Python can programmatically manage every aspect of your portal by
working with the gis module and two of its sub-modules: gis.admin and gis.server.

The gis module

The gis module provides access to the base classes which allow for the access and creation
of corresponding items, for example the UserManager base class contains the create()
method which will create a new user.

Items in your portal can be managed by properties and methods on the relevant class, for
example the User class represents a user in your portal and can be administered by the
properties and methods on the associated class.

The gis.admin sub-module

The gis.admin submodule provides the ability to perform many of the supporting
administration tasks not covered by the traditional admin-item classes or the gis module.
These tasks can be summarised below:

• Credit management (ArcGIS Online portal).

• Obtain usage reports.

• Investigate log files.

• Manage categories and collaborations.

• Obtain Enterprise Users and Groups.

The submodule contains many classes which perform these administration tasks; many of
which relate to the ArcGIS Enterprise portal, for example:

• PortalAdminManager class which is the root resource class for administering your
on-premise portal).

• Logs class to investigate log files created on a machine hosting Portal for ArcGIS.

• Federation class, which provides information about the ArcGIS Server registered
with your Portal.

Page 5-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The gis.server sub-module


If you are an ArcGIS Enterprise user then the API can be used to administer the underlying
ArcGIS Servers that drive ArcGIS Enterprise. The gis.server submodule provides a number of
classes with the following abilities, amongst other things:

• Access GIS Servers.

• Access services.

• Access the Services Directory.

• Query log files.

• Obtain information about the Data Store.

• Information about Enterprise administrative users and server roles.

© ESRI (UK) Ltd Page 5-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing portal members


People who wish to access your portal and its content will need to be supplied with an
account. This account is known as an organisational account and has relevant privileges
applied to it which determines what content the account has access to, what the account is
able to do within the portal (publish, administer, edit, etc) and what apps are accessible to
that account.

These accounts are associated with a user, also known as a member of the portal, and they
are managed by portal administrators. The API provides classes which allow you to manage
members of your portal.

The GIS class contains the users property which provides access to the UserManager
helper class; remember that this class is not created directly. This class contains methods
which allow for the management of users contained within your portal, such as create(),
get(), search(), invite(), to name but a few.

The user_groups() method returns back the groups a user belongs to; more about that
in a moment.

A useful property is me. This will get information about the user who has signed into the
portal and is currently running the notebook. It returns a reference to the User class.

Code snippet: Who are you…?

Page 5-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The get() method allows you to access information about a specific user through a User
object:

Code snippet: Get a specific user via the UserManager.get() method.

If you wish to identify members which belong to your portal or you want to get a subset of
members then use the search() method. Note that there a number of arguments which
the search() method can implement. The result is a Python list of User objects.

Code snippet: Use the UserManager.search() method to find those users beginning
with ‘ed’.

Many of the methods and the me property provide access to a User object.

The User object is a dictionary object; it has many keys such as firstName, lastLogin,
availableCredits. A full list of keys is obtained by inspecting the User class definition
in the API reference.

© ESRI (UK) Ltd Page 5-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

Code Snippet: Examining keys for the user object

A user can be deleted, the ability for the user to login into the portal can be enabled or
disabled; access to My Esri or GeoNet can be enabled through the esri_access property,
thumbnail information can be obtained as well as a list of folders the user has created. The
groups the user belongs to and the items owned by the user can also be obtained. More
about that in a moment…

Code Snippet: Get a User and identify groups the user belongs to

Page 5-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Creating and managing groups


Groups are used to organise and to provide access to items. A group has an owner who has
the ability to manage group members (i.e. invite members to join a group or remove them
from existing groups) and therefor grant access to the items stored in the group.

Additional information about groups can be found here:

https://doc.arcgis.com/en/arcgis-online/share-maps/groups.htm

In the API, access to an organisation’s groups for the logged in user can be accessed by the
groups property on the GIS class. The groups property provides access to the
GroupManager helper class.

The GroupManager class has 4 methods; create(), create_from_dict(), get()


and search().

The search() method on the GroupManager class is used to gain access to groups within
an organisation. The query parameter can be used to isolate groups of interest. It contains
a number of default fields, such as title, description and owner in which the query
automatically searches within the group metadata.

A list of group objects is returned from the search() method.

Code Snippet: Identify groups owned by Ed_Training

© ESRI (UK) Ltd Page 5-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

Returns:

The GroupManager provides access to a Group object, for example, via the search()
method or the get() method.

The Group class provides users with the ability to add users to the group, obtain a list of
items shared with the group, delete the group, obtain membership information about the
group, invite users into a group and remove users from the group, amongst other things.

Code Snippet: Create a new group with required title and tags, add a couple of users and
then update with an image.

Page 5-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing content in your portal


Your portal is essentially a content management system; it is used to share content to
members of an organisation, or publicly to anyone with an internet connection. Hosted
content is known as an item and your portal supports many different types of item, such as
a feature layer, Notebook, Web map, Web mapping application, web scene, StoryMap,
Scene layer, etc. These items can be managed manually or through the API.

So how do we get access to this content within your portal? Remember that the content
property on the GIS class provides access to the ContentManager helper class. This is the
gateway to your portal’s content.

The ContentManager class contains methods for folder management, obtaining a specific
item via the get() method, searching for items based upon supplied arguments via the
search() method, and methods for sharing or un-sharing items and deleting items.

Individual items contained within a portal can be obtained by using the get() method or
through the search() method – remember that the search method returns a Python list
of item objects.

An item is a “unit of content” in your portal. Each item has a unique item identifier and a
URL that allows access to the item if access permissions have been granted on that item.

© ESRI (UK) Ltd Page 5-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing portal Items


Your portal items can be managed through the Item class.

Methods exist for managing the item such as update(), move() and delete(). The
dry_run() method provides information about the item’s ability to be deleted while the
delete_items() method allows for the deletion of a collection of items.

Additional methods also exist for publishing data based upon an item resource via the
publish() method, copying items and changing the access permissions of the item via
the share() method. Methods also exist for downloading and exporting the item – more
about that in the following section.

Code Snippet: Share a feature layer to a group

Page 5-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing GIS servers in your Enterprise


The API has a number of native classes that are found in the gis.server submodule for
managing the GIS Servers that power your ArcGIS Enterprise, for example:

ServiceManager Service LogManager SystemManager

• create_folder() • delete() • clean() • clear_cache()


• create_service() • edit() • edit() • configuration_store
• exists() • start() • query() • directories()
• folders • stop() • jobs
• list • refresh() • server_properties

SiteManager MachineManager User ReportManager

• create() • get() • add_role() • create()


• delete() • list() • delete() • edit()
• import_site() • register() • user() • list()
• join() • rename() • quick_report()
• upgrade()

It is important to realise that to use the gis.server module the account used to connect to
the gis must have administrator privileges and a connection must be made to an ArcGIS
Enterprise portal.

To use these classes you must first work with the admin property on the gis objects. This is
set at runtime and is based upon what portal the administrator user connects to.

For an ArcGIS Enterprise portal the admin property will return an instance of the
PortalAdminManager class. This class is the root resource for administering your on-
premise portal, from which all resources and portal items can be obtained.

© ESRI (UK) Ltd Page 5-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

The PortalAdminManager class contains a property called servers which returns a


reference to a ServerManager object which is found in the gis.server submodule.

gis.admin will return an instance of PortalAdminManager if


accessing an ArcGIS Enterprise portal, or AGOLManager if
accessing an ArcGIS Online portal.

The ServerManager class contains a number of members which help perform validation of
the servers in the enterprise portal, to obtain a list of all servers in the on-premise portal or
to obtain a specific server machine.

Once a particular server object has been obtained then it is now possible to get access to
manage datastores, access to content, query the log records, and ultimately start, stop,
publish and delete services.

Page 5-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Code Snippet: Access an Enterprise Server and associated service


The following code snippet indicates how to obtain a specific server and check to see if a
particular service exists:

from arcgis.gis import GIS

import arcgis.gis.admin

gis = GIS("<portal URL>", "<portal admin username>",

"<portal admin password>")

serverManager = gis.admin.servers # Returns ServerManager object


# on gis.server module

# Validate your federated servers with your portal; True all OK,
# False if there is an issue with one or more federated servers

print (“Servers are valid?: “ + serverManager.validate())

myServers = serverManager.list() # Get a list of all servers

myServer = myServers[0] # Get a Server machine object

The relationship between the classes can be presented as follows:

© ESRI (UK) Ltd Page 5-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

Administering your services


In order to carry out those types of administrative service-based operation a list of all of the
services running on a particular server needs to be obtained. This is achieved by the
services property, as this returns a reference to the ServiceManager object, which in
turn contains the list() method.

By processing the list of services individual service properties can be obtained.

In the code snippet below a list of all the services found on a particular server machine is
obtained. A for loop can then process each service in the list to perform an administrative
action. The properties property on the Service object returns a dictionary whose keys
are exposed as properties, for example serviceName.

serviceList = myServer.services.list() # A list of services

for service in serviceList:

if service.properties.serviceName == “SampleWorldCities”:

service.stop()

Once a particular service has been found then it can be stopped, started or its status can be
obtained (i.e. has it started? Is it stopped?)

The workflow in obtaining these objects is displayed below:

Page 5-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Administration samples
The ArcGIS API for Python has many samples that can be used directly or as a basis on which
to replicate your workflows.

These so-called Sample Notebooks can be found at the following location:

https://developers.arcgis.com/python/samples/

These have already been downloaded for you as part of your course data.

Additional samples are available if you are accessing ArcGIS Notebooks through ArcGIS
Enterprise or ArcGIS Online.

Information about the available samples can be found here:

https://enterprise.arcgis.com/en/notebook/latest/use/windows/about-sample-notebooks.htm

© ESRI (UK) Ltd Page 5-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

Visualising your analysis using Matplotlib


Matplotlib is one of a number of additional modules that is used to supplement your
analysis when using the ArcGIS API for Python. Other modules include pandas or zip.

Matplotlib is the preferred 2D plotting library for Python. It is used to provide graphing and
charting capabilities when a map is not required to present your analysis as it can be used
within Jupyter Notebook as it supports IPython which underpins the Notebook’s rich display
capabilities.

The matplotlib website contains an example for each different chart type, full
documentation and a number of tutorials.

https://matplotlib.org/index.html

A wide variety of chart and graph types are supported, for example:

• Types of bar chart

• Polar charts

• Scatter plots

• Pie charts

• Boxplots

Properties for each chart type can be controlled, such as title, chart symbology, legend
information.

Each chart will have unique properties which can be further manipulated.

Page 5-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Many of the visualisation techniques used by matplotlib rely on using sequence objects such
as tuples and lists as well as Python dictionaries.

Matplotlib is composed of a number of submodules, such as matplotlib.pyplot, for


creating and displaying simple plots. The submodules contain classes which can be used, for
example, to control fonts (font_manager), colours and colourmaps (colors) and
positioning ticks (ticker).

The pie chart, on the previous page, was created with the following code:
%matplotlib inline

import matplotlib.pyplot as plt

# Pie chart, where the slices will be ordered #


# and plotted counter-clockwise:

labels = 'Hosted Feature Layers', 'Web Maps', 'Web Scenes', \ '"Other"


content'

sizes = [15, 30, 45, 10]

explode = (0, 0.1, 0, 0) # only "explode" the 2nd slice (i.e. 'Web Maps')

fig1, ax1 = plt.subplots()

ax1.pie(sizes, explode=explode,

labels=labels, autopct='%1.1f%%', shadow=True, startangle=90)

ax1.axis('equal') # Equal aspect ratio ensures

#that pie is drawn as a circle.

plt.show()

© ESRI (UK) Ltd Page 5-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 5: Identifying portal usage of users

Estimated Time: 40 Minutes


The biggest use of the API is to perform administration tasks which would otherwise be
quite tricky or tedious to do, if done manually.

There are many tasks which fall under the category of “administration”, for example credit
budgeting, creating users and assigning relevant privileges to those users, re-assigning
content to a new user if a user leaves the organisation, re-categorising content…. You will
have your own administrative requirements and thoughts.

This exercise will concentrate on identifying those users who have not used the portal lately
and to ultimately identify those users who are inactive (i.e. those uses who have never
logged into the portal…). This will help you to reassign those dormant accounts to more
deserving users.

In this exercise you will:

• Access the portal and add some useful Python site packages which your notebook
will use.

• Create a function to calculate the number of days which have elapsed since a user
last logged into the portal.

• Identify those users who have accessed the portal today.

• An optional step to create a bar chart which will display on average the number of
days it has taken for accounts associated with each trainer to login to the portal.

• A challenge step to identify those users who are ‘inactive’.

Page 5-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Step 1: Login to your portal


In this first step you will set up your Jupyter Notebook environment. You will navigate to the
correct directory which contains a Notebook containing comments under which you will
write your code. You will also log into your portal and import some modules which will be
used in the Notebook.

❑ If the Jupyter Notebook is closed then launch the application in the


C:\EsriTraining\PYAPI folder.

❑ In the Jupyter Notebook dashboard open the Admin folder.

Inside you will find a notebook called UsageStats.ipynb.

❑ Open up the UsageStats notebook and spend a moment reading the comments.

You will see some markdown comments specifying what you are going to do in the exercise
and empty code cells into which you will write the code for each step.

There is also a pre-written function which relates to the optional step you might want to
have a go at later on.

❑ Locate the code cell under the comment Step 1: Log into your portal.

© ESRI (UK) Ltd Page 5-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ In the cell write the following code to import the ArcGIS API to access the GIS class:

from arcgis.gis import GIS

You are also going to work with the datetime module as you are going to calculate how
many days have elapsed since a particular point in time.

❑ Write some code to import datetime as dt.

The above line of code imports the datetime module and references it through an alias
called dt.

❑ Also, import the pprint and collections modules.

❑ Create a variable called last_logged_in and assign to it an empty dictionary.

Think of this variable as a global variable in that you can use throughout your script,
including any functions which you might create.

❑ Create a variable called gis.

❑ Write a line of code which logs you into your GIS using your credentials and store the
resulting portal reference in the gis variable.

❑ Finally write a line of code that will confirm that you have logged into your portal
successfully.

Your code for step 1 should look like the following:

❑ Run the code in the cell.

Once your code has successfully run, and you have logged into the portal, you are now
ready to carry out the main body of your task.

Page 5-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Step 2: Identify the number of days since last login


In this step you will discover when the last time a user logged in to the portal was. In order
to do this you will use the functionality in the imported datetime module to identify the
amount of time that has elapsed (in seconds) between today’s current time and date, and
the time and date when a user last logged in.

You will write this code in a Python function.

So why contain this code in a Python function?

You are going to obtain all users that are within the portal and then call the function to
calculate how many days have elapsed since the last login for each user. Placing that code in
a function makes it fairly simple to manage the code for each user and will keep the code
that processes each user compact.

❑ Locate the cell underneath the markdown comment “Step 2: Create a function to
calculate…….”.

❑ Using the def keyword create a Python function called


num_days_since_last_login and in the function’s parentheses add a
parameter called user.

The parameter user will be used to accept the current portal user whose number of days
since the last login occurred needs to be calculated within the function.

In the previous section we discussed how dates are stored as milliseconds in the portal, and
that the User object has a number of properties, such as availableCredits,
fullName, etc.

❑ In the API Reference locate the arcgis.gis module, and find the User class.

The User class is based upon a Python dictionary.

Question 1: What key on the User object will return the date in milliseconds of the last login
for that user?

___________________________________________________________________________

❑ In the function create a variable called ts_of_last_login.

© ESRI (UK) Ltd Page 5-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Assign to the variable, code which will obtain the last login for the user, divide that
by 1000 and cast the resulting number as an integer using the int() built-in
function.

The datetime module has a class called datetime which allows you to create dates and
manipulate timestamaps. Remember that a timestamp represents the number of seconds
since the Unix epoch i.e. January 1, 1970 1200am. The fromtimestamp() method
converts a time stamp stored in the portal, to a usable date.

❑ Create a variable called last_login and assign to it the following code:

dt.datetime.fromtimestamp(ts_of_last_login)

dt is an alias for the datetime module.

❑ Create a variable called now.

❑ Use the now() method on the dt.datetime class to identify the current date and
time and store that in the now variable.

Question 2: What object does the now() method on the datetime class return?

___________________________________________________________________________

Both the objects contained inside of the now and last_login variables are datetime
objects. To work out the difference between the two you can take last_login away
from now. Use days property on the result to display the difference between the two as
the number of days.

❑ Create a variable called days and write some code based upon the above
description which will calculate the number of elapsed days between the user’s last
login and the current date. Remember to use the days property.

❑ Finally pass the contents of the variable days back to the calling code by the
return keyword.

Page 5-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Your code to calculate the number of days since the user last logged in should look like the
following:

❑ Run the code cell containing the function.

The function should be placed before the code which calls the function otherwise the
function will not be recognised when it is called.

Step 3: Search your portal for all users


Now that you have written a function which will calculate the number of days between now
and when the user last logged in, let’s get all users in the portal and pass the user into the
num_days_since_last_login() function to identify the last time each user logged
into the portal.

To do this you will do the following:

• Construct a for loop to process each user in the portal.

• Use the search() method on the UserManager resource manager helper class to
find all the users in the portal and override the default max_users argument.

• Store the current user in a variable called user.

To understand how to obtain all the users in the portal you will investigate the
documentation.

❑ Locate the GIS class in the arcgis.gis module.

Question 3: Which property on the GIS class will provide you with access to the resource
manager for GIS users?

___________________________________________________________________________

© ESRI (UK) Ltd Page 5-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Click the UserManager hyperlink in the description for the users property.

Notice that you are taken to the documentation for the UserManager object.

Question 4: Which method allows you to search all portal users?

___________________________________________________________________________

Question 5: What is the default value for the max_users argument?

___________________________________________________________________________

This default value of 100 is not enough as there are more than 100 users in the Esri UK
Training Portal.

Question 6: What Python object does the search() method return?

___________________________________________________________________________

❑ Locate the cell underneath the Markdown comments:

Step 3: Calculate number of days since a user…..

❑ Based upon your investigations write a line of code underneath which will provide
access to all portal users (set a value of 1000 for the maximum number of users):

gis.users._________(__________)

❑ Using the code you have written as a guide on the line above, create a loop which
will obtain a list of the portal users and store the current user being processed in an
inline variable called user.

Your code should look like the following below:

Page 5-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Within the loop create a variable called num_days and call the
num_days_since_last_login() function, passing in the current user.

❑ Still within the loop, create a variable called user_name and use the
user[‘username’] notation to obtain the current user name of the user being
processed.

The user_name variable stores a string which will be useful in reporting the number of
days since the last portal login for that user.

The code you have written should look like the following:

Do you remember the last_logged_in dictionary object you created at the beginning
of step 1? You will now populate it in which the dictionary’s key will be the unique user
name and the value will be the number of days since last login (i.e. the value returned by
the function).

❑ Write the following code to populate the last_logged_in dictionary

last_logged_in[user_name] = num_days

The population of the dictionary must be inside the loop otherwise the
number of days since the users last login will not be recorded.

Finally, you will display the contents of the dictionary to identify the users and the number
of days that have elapsed since their last login.

❑ Outside of the loop use the pprint module and the pprint() method to display
the contents of the last_logged_in dictionary.

Your code for this step should look like the following:

© ESRI (UK) Ltd Page 5-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Spend a couple of moments understanding what you have just written.

❑ Run the code in the cell.

The pprint() method displays the key : value pairs which is the user name and the
number of days elapsed since that user last logged into the portal:

You can scroll through this to pick out some useful information, but it is fairly
unconstructed. In the next step you will process the dictionary to identify those users who
have logged in today.

Step 4: Identify those users who have logged into the


portal today
In this last mandatory step, you are going to isolate the users who have logged into the
portal today.

You will do this by manipulating the last_logged_in dictionary through the


collections module.

If you spend a moment studying the last_logged_in dictionary you will see that the
keys are the users and are displayed pretty much in alphabetical order, and the values are
the number of days since last logged in. As you might expect the number of days since a user
last logged in will be different for the portal users, there is no pattern in how often the users
log in to the portal.

To help us answer the question posed in the title of the step we need to group all those keys
(users) which have a value of 0. The collections module will help us answer this question.

The collections module was imported in step 1 of the exercise.

For more information about the collections module please have a look at the
following page:

https://stackabuse.com/introduction-to-pythons-collections-module/

Page 5-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The collections module has been developed to provide additional support to existing types
of Python collections such as lists, dictionaries and tuples. You are going to use the
defaultdict() method to help extract all the keys which have a value of 0.

❑ Locate the empty cell underneath the markdown cell “Step 4: Identify those users
who have logged into…”.

❑ Write the following line of code:

key_list = collections.defaultdict(list)

The defaultdict() method creates a Python dictionary first and foremost but the
method’s argument specifies the data type of the value of the dictionary. The above line of
code specifies that the dictionary’s value will be a Python list.

❑ Write the following lines of code to access the key and values (items)for the
last_logged_in dictionary and then populate the key_list dictionary with
the number of days since the last login as the dictionary’s key, and the user which
shares that number of days as the dictionary’s value:

for user, days in last_logged_in.items():

key_list[days].append(user)

❑ Outside the loop, use the pprint() method on the pprint module to print out
the populated key_list dictionary.

❑ Run the code in the cell.

The output should look similar to that displayed below:

© ESRI (UK) Ltd Page 5-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

As you can see the dictionary is populated with key and value pairs as you might expect.

Question 7: What does the key_list key represent?

___________________________________________________________________________

Question 8: What does the key_list value represent.

___________________________________________________________________________

❑ Underneath the pprint() method, create a variable called logged_in_today


and assign to it the value for key_list[0].

Question 9: What data type is the logged_in_today variable?

___________________________________________________________________________

❑ Construct a for loop which will create the following formatted output.

❑ Run your script!

The number of users and the names of the users will more than likely
be different.

You hopefully have identified those members of the Esri UK Training organisation who have
logged in today. Is your account one of them? It should be…..

Page 5-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Optional Step: Create a bar chart of user account activity


In this optional step you will create a histogram using the Matplotlib module which will
provide a visual indication as to which trainers’ accounts are the most and least active. It
should be quite obvious which Esri UK trainer is utilising ArcGIS Online most and…… who
isn’t…..

Before you start to write some code to create the bar chart you will quickly inspect a
function which collates all of the accounts governed by each Esri UK Trainer and count the
number of days since the last login for those accounts and creates a summed total number
of days for each trainer.

❑ Locate the function called total_days_per_trainer().

❑ Spend a few moments understanding what it does and answer the following
questions:
Question 10: What is passed into the function?

___________________________________________________________________________

Question 11: What Python data type does the function return?

___________________________________________________________________________

Question 12: What does the key in the returned dictionary represent?

___________________________________________________________________________

Question 13: What does the value in the returned dictionary represent?

___________________________________________________________________________

The function returns a Python dictionary. The keys represent the user names managed by
the Esri UK Trainers and the values are the average number of days it takes for the accounts
managed by each trainer to login to the online portal. This gives an indication as to which
trainers’ accounts are the most active and which trainers’ account are the least active.

Let’s create the histogram.

❑ First of all run the code cell containing the function.

© ESRI (UK) Ltd Page 5-29


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Locate the empty cell underneath the function.

❑ Call the total_days_per_trainer() function passing in the


last_logged_in variable and store the result in a variable called
user_counts.
The user_counts variable is a dictionary.

❑ Write the following two lines of code:


import matplotlib.pyplot as plt

%matplotlib inline

The matplotlib.pyplot class is a plotting library containing basic functions for creating
charts, such as pie charts, bar charts (including horizontal bar charts), box plots, scatter plots
etc.

The line underneath the import statement allows for output generated by any plotting
commands associated with maptplotlib to be displayed in the Jupyter Notebook underneath
the cell which created the plot.

❑ Write the following line of code to create a figure object:


fig = plt.figure()

A ‘figure’ keeps track of how the chart is displayed and presented on the plotting canvas. It
includes things such as axes, legend, title, grid and the types of plot which display the data
on the chart.

❑ On the fig object call the add_axes() method and pass in the following list [0,
0, 1, 1] and store in a variable called ax.
The list represents the bottom left, bottom right, width and height of the axes. It will place a
figure that is exactly as large as the canvas itself.

The X-axis will be the trainer names and the Y-axis will be the average counts per trainer.

❑ Write some Python code to extract the names (keys) of the trainers and average
time since last login (values) from the user_counts dictionary. Store the results in
variables called trainers and ave_time_since, respectively.
Look at the answers for questions 12 and 13 in order to obtain the information.

Page 5-30 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Write the following code to create the bar chart:


ax.bar(trainers,ave_time_since)

❑ Finally on the plt alias call the show() method to display the finished plot in the
Notebook, when you run the cell.

❑ Run the cell containing the pre-written total_days_per_trainer() function,


if you haven’t done so already.

❑ Run the cell containing the code you have just written.
A bar chart will be created and displayed which should look similar to the one displayed
below:

The reason a particular trainer’s accounts do not access ArcGIS Online might be because
that particular trainer does not teach courses with ArcGIS Online…….. possibly.

Matplotlib is a powerful Python library for creating charts and plots for inclusion within
reports. It is definitely worth spending some time to become familiar with this module (it is
part of Python’s ‘standard library’) if you are going to perform administrative / reporting
tasks as it is easier to gain an understanding of performance within your portal in picture
form when tables of figures may hide underlying patterns.

© ESRI (UK) Ltd Page 5-31


An Introduction to Administering your
Portal using the ArcGIS API for Python

Challenge Step: Identify ‘inactive’ users


In this challenge step you will try and identify those accounts which have never accessed the
portal!

In step 4 you created a variable called key_list. This contains the users (as values of the
key_list dictionary) and the days since that user last logged into the Esri UK Training portal
(as keys of the dictionary).

Finding those users who have never logged into the portal is actually quite straight forwards
as they will be associated with the largest key in the key_list dictionary.

Write code which carries out the following (and create any necessary variables to store any
objects you might want to create:

1: Get all of the keys from the key_list.

2: Use the max() builtin function to find the largest number in the returned keys. This is
the number of days since the user(s) last logged in, as you know.

3: Obtain the value (list of users) from the key_list based upon the largest key.

4: Print out the list of inactive users.

Question 14: Which users have never logged into the Esri UK Training portal?

___________________________________________________________________________

Question 15: What is the rough date that the key represents?

___________________________________________________________________________

To answer the above question, you will need to take the value returned back by the max()
function and divide it by 365. That will give you the number of years

What do you think is significant about the years value you have worked out?

Well…. It relates roughly to 1st Jan 1970 (not taking into account leap years…) which is the
length of time which has elapsed since the beginning of Unix time.

Exercise End

Page 5-32 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 5 Questions


Question 1: What property on the User object will return the date in milliseconds of the last
login for that user?

Answer: lastLogin is the name of the key (property) which will return the date in
milliseconds of the user’s last login date.

Question 2: What object does the now() method on the datetime class return?

Answer: the now() method returns a datetime object.

Question 3: Which property on the GIS class will provide you with access to the resource
manager for GIS users?

Answer: The users property will provide access to the UserManager helper class.

Question 4: Which method allows you to search all portal users?

Answer: The search() method will allow you to search for users in your portal.

Question 5: What is the default value for the max_users argument?

Answer: The default value for max_users is 100.

Question 6: What Python object does the search() method return?

Answer: The search() method returns a Python list.

Question 7: What does the key_list key represent?

Answer: The key represents the number of days since the last login.

© ESRI (UK) Ltd Page 5-33


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 8: What does the key_list value represent.

Answer: The value represents the portal users associated with the key.

Question 9: What data type is the logged_in_today variable?

Answer: The variable holds a Python list object.

Question 10: What is passed into the function?

Answer: A dictionary object is passed into the function.

Question 11: What Python data type does the function return?

Answer: The function returns a dictionary.

Question 12: What does the key in the returned dictionary represent?

Answer: The key in the dictionary represents the Esri UK Trainer who manages the
associated accounts

Question 13: What does the value in the returned dictionary represent?

Answer: The values are the average amount of days it takes for the accounts to login to
the portal which are managed by the Esri UK Trainer.

Question 14: Which users have never logged into the Esri UK Training portal?

Answer: You might find that at least 3 users have never logged into the portal
(cw_esriuk_st13, 14, and 15) and they are owned by Colin.

Question 15: What is the rough date that the key represents?

Answer: The date relates to approximately 50 years ago! Which refers to the 1 st January
1970 – the beginning of Python time!

Page 5-34 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 5 solution

© ESRI (UK) Ltd Page 5-35


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 5-36 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

© ESRI (UK) Ltd Page 5-37


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 5-38 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

6 Publishing and creating content

Your portal is basically a content management system. It provides a central location to


store, manage and share spatial (and non-spatial) data in the form of files and web layers.
The files and layers that you add to your organisation provide the base for your
visualisation, analysis and editing workflows.

So how is this content uploaded into the portal? You can use tools within ArcGIS Pro, or
tools within your portal, or through a mixture of the ArcPy site package or the ArcGIS API for
Python.

Learning objectives
After completing this lesson, you will:

• Understand manual publishing options within ArcGIS Pro.


• Investigate how to automate a workflow for publishing content using the ArcPy site
package.
• Investigate the classes and members in the API which upload, publishing and share
your content.
• Introduce the zipfile module to support your publishing workflows.

© ESRI (UK) Ltd Page 8-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

Adding data to your organisation


In your portal you work with geographic data through layers (also known as web layers).
They are collections of geographic data that can be used to create maps and scenes.

Layers are based on a variety of data sources. Some sources are native to the portal, such as
hosted services, while others are file based, such as CSV files, or open standards such as
KML or OGC web sources.

In order to add data to your portal you need an account which allows you to create content.

Once the data has been uploaded you can then publish the item as a hosted layer if the
account you’re are using has publisher-based privileges and the item allows it.

Items are added in one of four ways:

1: Adding New items via the My Content area.

2: Published via ArcGIS Pro.

3: Using the ArcGIS API for Python and or the ArcPy site packages.

Adding New Items through My Content


Items can be added through the My Content > New Item. Using this technique allows data to
be uploaded from a local machine by selecting Your Device.

Page 8-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

File geodatabases and shapefiles must be added to your portal as a zip file. They can then be
published as a hosted feature layer:

The “zipped up” shapefile is added as a Shapefile item and the hosted feature layer is then
published from it.

The New Item button also allows other items to be added, for example KML, documents
and OGC-based web services.

If you have data (CSV files, GeoJSON, Excel spreadsheets, zipped up content) stored in a
cloud drive (Dropbox / MS One Drive) then these repositories can be used as sources for
content items.

Sharing content with ArcGIS Pro


ArcGIS Pro provides numerous options for sharing your work with others via your portal..

© ESRI (UK) Ltd Page 8-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

The Share tab provides the ability to share a number of dataset types in different ways:

Maps or selected layers can be shared as web layers, which can then be used for
visualisation, analysis and editing. Scenes and standalone tables can also be shared.

Packages (map, geoprocessing, deep learning, etc) can also be shared to your portal as a
compressed package file and they are a great way of sharing data, preparing data for apps
and for disseminating workflows to other audiences.

Sharing to your portal is achieved by a geoprocessing tool. As always mandatory arguments


must be filled in on the dialog (tags, share as a package or to the portal, share as a feature
layer, tile package or vector tile package).

Before the dataset is shared it is analysed. This process checks for performance issues and
data errors. Data errors must be fixed before the dataset is shared (and published to your
portal).

To share content in ArcGIS Pro you must have a publisher-based privileges.

Page 8-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Publishing to your portal through ArcPy


ArcPy is a Python site package supplied by Esri. It allows for the automation and extension
of ArcGIS Pro functionality. It is used predominantly for data processing, especially when
repeatable workflows / batch processing workflows are required.

At the 3.1 release of ArcGIS Pro, ArcPy provides access to over 1700 geoprocessing tools
along with a number of helper functions and classes.

The ArcPy site package complements the ArcGIS API for Python and can be used in both
desktop and web GIS workflows.

The workflow for publishing a layer in a project to your portal is displayed below:

Access a layer Create a Configure Stage the


Upload the
in a map in Sharing Draft and export Service
SD file and Validate
the ArcGIS Definition the SDDraft Definition
Publish
Pro project (SDDraft) file file. (SD).

The above workflow is accomplished by using a combination of ArcGIS Pro geoprocessing


tools in the Publishing toolset and ArcPy functions.

1: Obtain a layer from the ArcGIS Pro project


The first step is to obtain the layer you wish to publish in a particular map in an ArcGIS Pro
project.

The ArcGISProject class in the mapping (mp) module is used to obtain a reference to an
existing map project.

The project can contain many maps, which in turn can contain many layers. The
listMaps() method provides a Python list of map objects. The listLayers() method
on the Map class provides a Python list of Layer objects.

The particular layer you wish to publish can be obtained from this list.

© ESRI (UK) Ltd Page 8-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

import arcpy

project = arcpy.mp.ArcGISProject(r”C:\MyProjects\Liverpool.aprx”)

map = project.listMaps()[0]

layer = map.listLayers(“Grounds”)[0]

2: Create a Sharing Draft


A Sharing Draft is a configurable set of properties for a web layer.

It specifies:

• if the layer is going to be published to an on-line or on-premise portal, or a federated


server.

• The service type (feature, tile or map image service type).

• The name of the service.

The service type determines the type of draft sharing object.

featSD = map.getWebLayerSharing Draft(“HOSTING_SERVER”,

“FEATURE”,

“Football_Grounds”,

[layer])

The above line of code returns a Feature Sharing Draft object. Other sharing draft objects
are created if the service_type argument is TILE or MAP_IMAGE.

3: Configure and Export the Sharing Draft


Properties of the Sharing Draft object should be set as these determine the initial behaviour
of the web layer; for example supplying a description and tags, and over-writing an existing
feature layer.

featSD.tags = “Liverpool, Everton, Football”

featSD.summary = “Football grounds in Liverpool”

featSD.description = “Locations of Everton FC and Liverpool FC”

featSD.serviceName = “Premiership Grounds in Liverpool”

Page 8-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Once configured, the Share Draft object can then be shared to a Service Definition Draft
(.sddraft) file using the exportToSDDraft() function on the object.

featSD.exportToSDDraft(r”C:\MyData\Grounds.sddraft”)

The SDDraft file is then passed as input into the Stage Service geoprocessing tool.

Additional types of SDDraft files can be created by using the CreateGeocodeSDDraft(),


CreateGPSDDraft(), or CreateImageSDDraft() functions in the arcpy.sharing module when
publishing to your on-premise portal.

4: Stage the Service Definition


The Stage Service tool converts a Service Definition Draft file into a Service Definition that
can be used to upload and publish a web layer. The tool analyses the sddraft file to
determine performance issues or any errors that will stop the publishing process.

arcpy.StageService_Server(r”C:\MyData\Grounds.sddraft”,
r”C:\MyData\Grounds.sd”)

If there are no errors then the service definition file can be uploaded and published to the
portal.

5: Publish the Service Definition


The Upload Service Definition tool uploads and shares a web layer, a web tool (if you have
access to an on-premise portal) or a an ArcGIS Server service.

arcpy.UploadServiceDefinition(r“C:\MyData\Grounds.sd”,
“My Hosted Services”)

6: Validate your workflow


Once the layer has been published it is a good idea to check that the hosted feature layer
has been published correctly. Validate that the metadata has been populated, for example
the summary, description and tags, and the features in the hosted feature layer display
correctly in the Visualisation tab of the layer’s metadata.

© ESRI (UK) Ltd Page 8-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

Activity: Adding Items and publishing content


So, you can publish content to your portal using ArcGIS Pro, and how the same workflow
can be achieved through the ArcPy API.

But what about if you do not have access to ArcGIS Pro? The ArcGIS API for Python provides
you with the ability to upload and publish new content through the add() method on the
ContentManager helper class and the publish() method on the Item class.

Answer the following questions:

In the arcgis.gis module locate the ContentManager class and find the add()
method:

Question 1: What file types can be added to your portal?

___________________________________________________________________________

___________________________________________________________________________

Question 2: The add() method takes a number of arguments. Which argument is


mandatory? What datatype does this required argument expect?

___________________________________________________________________________

___________________________________________________________________________

Question 3: What keys can be added to the item_properties dictionary?

___________________________________________________________________________

___________________________________________________________________________

Question 4: What keys are recommended when working with the item_properties
dictionary?

___________________________________________________________________________

___________________________________________________________________________

Page 8-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 5: What optional argument establishes a path (or URL) to your data that you wish
to add?

___________________________________________________________________________

___________________________________________________________________________

In the arcgis.gis module locate the Item class and answer the following questions:

Question 6: What method allows for the publishing of a hosted service?

___________________________________________________________________________

___________________________________________________________________________

Question 7: What are the types of services that a publisher can create?

___________________________________________________________________________

___________________________________________________________________________

Question 8: What object does the publish() method return?

___________________________________________________________________________

___________________________________________________________________________

Question 9: Why might you use the optional file_type argument?

___________________________________________________________________________

___________________________________________________________________________

Question 10: When publishing a CSV file, why should you specify the optional
address_fields dictionary?

___________________________________________________________________________

___________________________________________________________________________

© ESRI (UK) Ltd Page 8-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

Adding Items to your portal


Items can be added manually to your portal using the Add Item button; the add() method
on the ContentManager class is the equivalent piece of functionality through the API.

The newly added Item object can be inspected directly in your portal or through the rich
representation display of the Notebook.

Content which can be added to your portal can be in the form of zipped files, OGC compliant
web services, CSV and SD files, references to applications and packages.

The add() method allows you to specify item properties (an optional argument in the form
of a Python dictionary which helps define metadata for the new item), a pathway to the
source data, a thumbnail, a folder in the portal in which the item is to be created in, to
name but a few arguments.

Code snippet:

By default, the item owner will be the logged in user who uploaded the item.

Page 8-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Create a Hosted Feature Service


Once a source item has been added to the portal the a hosted service can be created from it
via the publish() method on the Item class.

The user requires publishing credentials defined by a user type or a custom role.

A number of different service types can be created through the publish() method:

• Hosted feature services

• Tiled maps services

• Vector tile service

• Scene services.

The type of service is defined by the item source, for example, if vector tile packages are
added to the portal then a vector tile layer will be created once the package has been
published.

Creating a hosted feature service from a CSV file requires the optional address_fields
argument to be populated. Consider the following CSV file:

The file contains a LOCATION field which is used to position the data points, as there is no
X,Y columns. The address_items argument is a dictionary object in which standardised
field names are used as the dictionary key and the field containing the address information
is the dictionary value so:

csv_lyr = csv_item.publish(None, {"Address":"LOCATION"})

© ESRI (UK) Ltd Page 8-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

Sharing your content to others


Once you have added or published an item it is customary to make it available to others,
either internally within your organisation, or externally to others outside of your
organisation. By default, only the item owner initially has access to the item.

There are a number of levels of sharing:

Everyone The item becomes ‘pubic’. It is shared with ‘everyone’ including


anonymous users.

Organisation The item is shared just to members within the organisation.

Those from outside your organisation can not access your item.

Groups Sharing to a group restricts access to the item to members of the group.

Members of the group can be from the same organisation or different


organisations.

Private Unshared; default access state.

Only the owner and others who have privileges to view content owned
by other members can view private items.

Page 8-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The methods to allow items to be shared are found on the Item class:

The share() method shares the item to groups of users, the organisation or to the wider
public.

The shared_with() method determines to whom a particular item has been shared
with.

© ESRI (UK) Ltd Page 8-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

The zipfile module


The zipfile module is a useful module which will help supplement your workflows, especially
when adding or extracting items to and from your portal.

The zip file is an archive file format which supports lossless compression. Its advantages are:

• It’s a great way to keep related files together, such as the many files we see in a
shapefile and file geodatabase.

• To make large files smaller.

• To improve transfer speeds over standard internet connections.

The zipfile module contains the ZipFile class. This is used to create a Zipfile object.

The Python with statement is used to manage an archive. The location and name of the
archive is specified along with the operation that is to be performed on the archive, for
example “r” for reading an existing archive, “w” to create a new archive and “a” to add
items into an existing archive.

Code Snippet:

The ZipFile class contains the following methods which will be useful for managing an
archive:

• extractall()
• extract()
ZipFile • printdir()
• read()
• write()

The extractall() method will extract all the contents of the archive into the current
working directory while the extract() method will extract a particular file into a
specified location.

Page 8-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The printdir() method displays the contents of the archive.

The write() method will add a particular file into an archive.

As you can imagine the ZipFile module may be useful in your workflows for packaging files
into an archive for uploading the shapefile or file geodatabase into your portal, or for
extracting files once the archive has been downloaded from your portal.

© ESRI (UK) Ltd Page 8-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

Extracting your data from your portal


The ArcGIS API for Python provides the ability to extract your data from your portal.

Your workflow may require you to download content from your portal. For example, you
might be a data manager who needs to collate the results of surveys into a single file on
your local machine using ArcGIS Pro. The API can export and download these files into a
single file location and then post-process using ArcGIS Pro.

The Item class contains methods for exporting your data from one format to another and
then downloading the item to your host machine.

Code Snippet:

Page 8-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

This creates a new item in the portal.

The new shapefile item can then be obtained and downloaded to your local machine:

Not only can the source data be downloaded but supplementary information such as an
associated thumbnail and metadata.

The functionality is available within your portal through the Download and Export buttons.

© ESRI (UK) Ltd Page 8-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

Working with Attachments


Feature layers have a useful characteristic in that they support the storage of attachments.
This is a useful operation when performing field data collection as it allows for a given
feature, the storage of photographs or associated documentation, such as PDF files.

Downloading and accessing attachments has always been a problem if using the “out of the
box” functionality of the portal. The ArcGIS API for Python provides a workflow for accessing
attachments.

The FeatureLayer class is found in the features module. It has an attachments attribute
which provides access to a helper class called AttachmentManager which is in the
features.managers submodule. This manages the manipulation of attachments.

So how do you get access to the attachments?

The get_list() method on the AttachmentManager will return a Python list of


dictionaries containing information about the attachments. The parent OID key provides
access to the feature to which the attachment belongs to (remember that a feature can
have more than one attachment). It is therefore possible to loop through all of the features
in a feature layer and call this method to access the attachments in the layer.

The download() method allows individual attachments to be downloaded. It has three


optional arguments: oid, attachment_id and save_path. If no optional arguments
are provided, then all attachments are downloaded to the user’s temporary directory.

The oid argument can either be a single OID value (supplied as a string) or if a list of OIDs is
provided then all of the attachments for those object IDs will be downloaded.

The attachment_id indicates which attachment(s) for a given feature are to be


downloaded.

Page 8-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Code snippet:

Consider downloading the following attachment:

Will create on disk:

© ESRI (UK) Ltd Page 8-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to section activity


Answer the following questions:
In the arcgis.gis module locate the ContentManager class find the
add() method:

Question 1: What file type can be added to your portal?

Answer: The following files types can be added to your portal:

Sd (Service Definition) file; zipped up file geodatabases and shapefiles, CSV, layer package,
geoprocessing package and map package.

Question 2: The add() method takes a number of arguments. Which argument is


mandatory? What datatype does this required argument expect?

Answer: item_properties is the required argument and its datatype is a Python


dictionary.

Question 3: What keys can be added to the item_properties dictionary?

Answer: The item_properties dictionary supports the following keys: type,


dataUrl, filename, title, description, tags, etc….

Question 4: What keys are recommended when working with the item_properties
dictionary?

Answer: Although optional, it is recommended that you include the following keys:
title, type, typekeywords, tags, snippet and description.

Question 5: What optional argument establishes a path (or URL) to your data that you wish
to add?

Answer: The data argument allows you to specify a pathway to your data.

Page 8-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

In the arcgis.gis module locate the Item class and answer the following
questions:

Question 6: What method allows for the publishing of a hosted service?

Answer: The publish() method.

Question 7: What are the types of services that a “publisher” can create?

Answer: A publisher can create feature, tiled map, vector tile and scene services.

Question 8: What object does the publish() method return?

Answer: An Item object is returned.

Question 9: Why might you use the optional file_type argument?

Answer: Some formats are not automatically detected so adding a file type confirms the
format that is to be published.

Question 10: When publishing a CSV file, why should you specify the optional address fields
dictionary?

Answer: The addition of the optional address fields dictionary allows the CSV file to be
spatially enabled

© ESRI (UK) Ltd Page 8-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 6: Adding content to your portal

Estimated Time: 40 Minutes


In this exercise you are going to create a script which will be used as a proof of concept for
adding content to your portal and subsequently publishing it as a hosted feature service.

Ultimately you would like to create an automated workflow for adding content to your
portal when new datasets arrive in a directory from various departments in your company
and publish datasets to a portal group called Contractors, so external contractors can view
the datasets.

You will carry out the following:

• Understand the data you wish to publish


• Create a zip archive for your file geodatabase
• Publish the file geodatabase datasets.
• Validate your output
• An optional step to share to the Contractors group.

In the exercise you will add zipped files to the portal. You must add your
initials to the name of the file geodatabase to provide a unique name to
the uploaded item.

You can not have a duplicate item of the same name in your portal as
this will cause an error!

Step 1: Inspect your data


The first thing you need to do is to inspect your data. The reasoning behind this is that you
want to make sure that the final published feature layers bare some resemblance to the
data in an existing map in ArcGIS Pro. Viewing your data also allows you to inspect any
attributes and understand the process of creating a new layer based upon XY coordinates.

❑ Open ArcGIS Pro and log in using your organisation credentials.

❑ Open the AylesburyProject.aprx. It is found at the following location:

C:\EsriTraining\PYAPI\Publishing\AylesburyProject

Page 8-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The project will open and display the AylesburyArea map:

As you can see it is made up of 3 feature layers and a couple of layers which make up the
background basemap.

You can see that the 3 feature layers are a combination of point, polyline and polygon
feature types. But what formats are their data sources?

❑ Click in the List By Data Source button on the Contents pane.

Question 1: What is the data source for both the CarParks and MajorRoads feature layers?

___________________________________________________________________________

© ESRI (UK) Ltd Page 8-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

Both feature layers have data sources which are made up of multiple files (shapefiles and
file geodatabases). This information is necessary as you will need to carry out some
additional processing in order to add your datasets to your portal.

❑ Save the ArcGIS Pro project and close ArcGIS Pro down.

You will carry out the next step in Jupyter Notebook. This is because you will
write some code to create the zip archives. If you are viewing a feature class in
the file geodatabase in ArcGIS Pro while you try and create the zip file of the
geodatabase, within ArcGIS Pro, then the process will fail as a lock is applied
against files relating to the open feature class.

Step 2: Create a zip archive for the file geodatabase


In this step you are going to start Jupyter Notebook and work with an existing notebook.
You are going to create a zip file archive for the file geodatabase.

❑ Open the Python Command Prompt window and change directory (cd) into the
following directory:

C:\EsriTraining\PYAPI\Publishing\Notebooks

❑ Launch Jupyter Notebook.

Python command line > Jupyter notebook

You will find a notebook called CreateArchive.

❑ Open the CreateArchive.ipynb.

There are a number of cells which contain some pre-written code.

❑ Spend a few moments reading the code in the cells.

The first cell is importing a number of useful modules and creating some variables specifying
directory locations for the datasets. Notice that the zipfile module is imported along with
the glob and os modules.

The glob module is useful for accessing files in a folder by specifying a wildcard and is useful
for finding all of the files whose name is the same but whose file extension is different, so
this would be handy for finding those files which make up a shapefile.

❑ Run the code in this cell.

Page 8-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The cell underneath contains a function.

Question 2: What is the name of the function?

___________________________________________________________________________

___________________________________________________________________________

Question 3: What two arguments does the function expect?

___________________________________________________________________________

___________________________________________________________________________

The function is called zipFileGDB() and it is used to zip up a file geodatabase. Remember
that a file geodatabase is a directory and it contains many binary files.

❑ Run the code in the cell which contains the function.

This means that the function can now be called in future code.

❑ Locate the cell which contains the following comment:

# Call the zipFileGDB() function to archive the file geodatbase

This cell contains code which identifies the source file geodatabase to be zipped and the
output name of the archive. The os.path.join() function is used to concatenate file
names to directories which are going to be passed into the function.

❑ Locate the comment: # TODO: Call the function...

❑ Underneath the comment write a line of code which calls the zipFileGDB()
function. Pass in the outputZIP variable as the first argument and inputGDB as
the second argument.

❑ Add a print() function indicating that the geodatabase has been successfully
packaged up.

Before you run the code in the cell you will just change the name of the zip file which will be
created.

❑ Locate the variable outputGdbZIPname and alter the name of the file to include

© ESRI (UK) Ltd Page 8-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

your initials at the end of the name of the file geodatabase, for example:

outputGdbZIPname = r”Transport_<Your_Initials_Here>.gdb.zip”

This will help the uploading process as a portal can not have duplicate
names of the same item type.

❑ Run the code cell to call the function.

A new zip file is created in the C:\EsriTraining\PYAPI\Publishing\Zip folder.

You are now ready to add the archive to your portal.

Step 3: Add the archive as a portal item


You will perform the remainder of the exercise in ArcGIS Pro as you are adding items and
publishing new feature services in the portal.

❑ Open ArcGIS Pro and load the AylesburyProject.

You are going to add a notebook into the project which contains some pre-written code.

❑ Locate the Insert tab and choose New Notebook > Add Notebook.

❑ Browse to the following location:

C:\EsriTraining\PYAPI\Publishing\Notebooks

❑ And add the PublishItems.ipynb notebook.

Notice that the Catalog pane has a new node called Notebooks and the newly added
PublishItems notebook is referenced here.

❑ Open the newly added notebook as a Notebook view.

Page 8-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

After a few moments the notebook will open and you will see that there is existing mark-up
and comments, which is provided for you as a guide as to where you will write your code for
the rest of this exercise.

❑ In the first cell is a comment # Create a reference to the GIS. Add the following lines
of code:

from arcgis.gis import GIS


gis = GIS(“pro”)

Notice that you are using the pro authentication option. This creates a reference to the
active portal associated with the running ArcGIS Pro application without having to pass your
credentials into the script.

❑ Run the code cell.

On successful login to the active portal your organisational account username will be
displayed.

You can now use the gis variable within the rest of your notebook.

Before you can publish the zip file archive you created in the previous step you need to add
it to your portal. You write some code to achieve this.

❑ Open the API reference at:

https://developers.arcgis.com/python/api-reference/

© ESRI (UK) Ltd Page 8-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Locate the ContentManager class in the arcgis.gis module and then find the add()
method.

❑ Spend a couple of moments reading the help for the method.

You will see that the item_properties argument is a required dictionary. The dictionary
contains information about the item you wish to upload and add to the portal. This is
normally the information that you would enter on the “Add an item from your computer”
dialog box.

You will add the file geodatabase archive to the portal.

❑ Locate the cell which contains the following comment:

# TODO: Add code for uploading the file geodatabase zip file

❑ Create a dictionary called dTransport and assign to it the following key : value
pairs:

KEY VALUE

“type” “File Geodatabase”

“description” “Transport infrastructure”

“title” “AylesburyTransport_<Your-initials>”

“tags” “PYAPI, Aylesbury, Roads, Car Parks”

Hopefully you have added your initials to the AylesburyTransport title key :
value pair.

❑ Create a variable called transportData and assign to it the name and location of
the zipped up geodatabase.

Remember to include your initials in the name of the zip file BEFORE the
.gdb.zip part of the file to maintain a unique naming convention.

Page 8-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Create a variable called transportItem and assign to it the following code:

gis.content.add(item_properties=dTransport,
data=transportData)

❑ …and run your code.

As long as there are no errors then the archive will have been uploaded into your portal!

❑ Check that the item is present within your portal, for example:

You are now ready to publish your newly added item as hosted features service.

Step 4: Publish the item as a feature service


In the previous step you added a new item to your portal by using the ContentManager :
add() method.

You will now use the Item : publish() method to create hosted feature service from your
uploaded portal item.

❑ In the documentation locate the publish() method on the Item class.

Notice that all of the arguments for the method are all optional, including the
publish_parameters argument. This means that the metadata associated with the
source item will be used when publishing as a hosted feature service.

❑ Locate the area of the notebook which relates to step 4.

You will write your code underneath the comments:

# TODO: Publish your newly added item as feature service

© ESRI (UK) Ltd Page 8-29


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Create a variable called transFeatLayer and call the publish() method on


the transportItem variable which you created in the previous step.

❑ Run your code cell.

On successful completion of running the code cell you will see the message indicating that
the hosted feature layer has been created successfully.

In the final step you will inspect your newly created feature layer.

Step 5: Validate your publishing workflow


The final part of the workflow is to validate the results of the workflow. This should be a
mandatory step in anything that you do.

Let’s check that what you have done has worked!

❑ Open a browser and log into ArcGIS Online using the credentials you were provided
with in the course.

❑ Click on the Content tab.

You should see three items in your Content area of the portal: the File geodatabase, the
Feature Layer (Hosted) and the notebook you created in your first exercise:

Page 8-30 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ On the Content tab click the AylesburyTransport feature layer to display the
metadata for the service.

Question 4: How many layers are contained inside of the service?

___________________________________________________________________________

___________________________________________________________________________

There should be two layers in the AylesburyTransport hosted feature layer.

❑ Inspect each layer in turn in the Visualization tab to make sure the layers have been
published correctly. You might want to check that:

• The layers display when you zoom in / out and pan around the map.

• The pop-up attribute dialog box appear when you click on a feature.

❑ Click the Data tab and inspect the attribute table for each layer.

❑ Compare the attributes in each layer in your portal to the attributes of each layer in
ArcGIS Pro.

Hopefully the attributes are the same.!

These are the sorts of things you should have check when you are enacting a workflow. Can
you think of anything else?

Notice that the symbology is not the same as it was in ArcGIS Pro. This is because you have
published the layers via a notebook. If the layers were published from within ArcGIS Pro
using the Share As Web Layer dialog then the symbology would have been preserved in the
publishing process.

❑ Close ArcGIS Pro down and do not save any changes if you are asked to do so.

© ESRI (UK) Ltd Page 8-31


An Introduction to Administering your
Portal using the ArcGIS API for Python

If time permits then perhaps you might wish to investigate the optional step of changing the
sharing options for your newly created hosted feature layer.

Otherwise…..

In this exercise you have experienced the publishing workflow, from viewing the original
source data, to using the zip and glob modules to help you accomplish particular tasks to
then using the ArcGIS API for Python to add content and publish items. Please remember
that many types of data can be published, and the process of publishing is similar to that
described and performed in this exercise.

It is strongly recommended that you investigate the Sample Notebooks and the API
Reference for additional code samples in order to publish other types of file.

Optional step: Share the items to the Contractors portal


group
As you probably know, when you create or share an item to your portal then, by default,
you are the only person who can access the newly created item (as well as your portal
administrator).

In this optional step you will write some code to share your newly created hosted feature
layer to the existing portal Contractors group. Only members of this group will be able to
see the contents of this group.

You will use the share() method on the Item class to share the content to the
Contractors group.

❑ Locate the share() method on the Item class in the API reference, and answer the
following questions:

Question 5: If you wish to share content to a group, what must you supply to the groups
argument?

___________________________________________________________________________

___________________________________________________________________________

Question 6: How can you identify the ID for a group?

___________________________________________________________________________

___________________________________________________________________________

Page 8-32 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ In the PublishItems notebook, which is open in ArcGIS Pro, locate the code cell
which has the comment # TODO: Share your items…….

Notice that there is a line of code which already references the Contractors group ID.

Question 7: What is the name of the variable which references the Contractors group ID?

___________________________________________________________________________

❑ Write a line of code which will share the transport feature layer to the Contractors
group, using the answers to the above questions as your guide.

❑ Run the code cell which shares your content to the Contractors group.

On successfully running the code cell you should see a message indicating that the feature
layer was shared to the Contractors group.

Within the ArcGIS Online portal click the Content tab and choose All my content in the
Folders panel:

Question 8: The hosted feature layer is shared to which two levels?

___________________________________________________________________________

___________________________________________________________________________

Let’s make sure your hosted feature layer is available in the Contractors group.

❑ In the portal click the Groups tab and locate the Contractors group.

© ESRI (UK) Ltd Page 8-33


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Click the Contractors group name

Notice that under the Recently added content part of the page you should find your
Aylesbury_Transport feature layer which has been shared to the group.

You have successfully shared your hosted Aylesbury Transport feature layer to the
Contractors group so other group members can now access your item.

Exercise End

Page 8-34 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 6 Questions

Question 1: What is the data source for both the CarParks and MajorRoads feature layers?

Answer: The data source for both layers is a file geodatabase feature class.

Question 2: What is the name of the function?

Answer: the function is called zipFileGDB.

Question 3: What two arguments does the function expect?

Answer: The output zip file name and the source file geodatabase to be zipped up, passed
in as a directory.

Question 4: How many layers are contained inside of the service?

Answer: There are two layers within the AylesburyTransport service; CarParks and
MajorRoads.

Question 5: If you wish to share content to a group, what must you supply to the groups
argument?

Answer: A Python list of groups IDS as strings, or a list of arcgis.gis.Group objects, or a


comma separated list of group IDs.

Question 6: How can you identify the ID for a group?

Answer: You find the ID of the group as you would do any other item, by looking at its URL
and taking note of the alphanumeric characters which follow the “id” part of the URL.

Question 7: What is the name of the variable which references the Contractors group ID?

Answer: It is called groupID.

© ESRI (UK) Ltd Page 8-35


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 8: The hosted feature layer is shared to which two levels?

Answer: The hosted feature layer is shared to Owner (you) and the Contractors group.

Page 8-36 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 6 solution
CreateArchive.ipynb

© ESRI (UK) Ltd Page 8-37


An Introduction to Administering your
Portal using the ArcGIS API for Python

PublishItems.ipynb

Page 8-38 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

7 Exploratory Data Analysis

One of the things you will need to do is to validate and possibly correct your data, especially
if you are going to be working with table-based data, such as CSV files. To help you perform
these types of operations you can use the Pandas module and its DataFrame object or its
close cousin, the Spatially Enabled Dataframe which is found inside of the ArcGIS API for
Python.

The Matplotlib site package compliment both DataFrames and is a great way to further
visualise your data.

Key to working with the Pandas module is an understanding of the DataFrame as this
contains a number of methods for inspecting and amending your data. Esri’s Spatial
DataFrame is based upon the Pandas DataFrame and it manipulates, manages and
translates data into new information.

This section will introduce you to both types of DataFrame object and the types of
operations that they support.

Learning objectives
After completing this lesson, you will be able to:

• Investigate the pandas module and the DataFrame object.


• Correcting your data using pandas.
• Introduce the Esri Spatial Dataframe.
• Visualising the Spatial DataFrame.

Notes

© ESRI (UK) Ltd Page 8-1


An Introduction to Administering your
Portal using the ArcGIS API for Python

Working with the Pandas module


Pandas stands for “Python Data Analysis Library”. It allows you to clean, transform and
analyse your data.

At the headline level it allows you to take data contained inside of a CSV file, an SQL table ,
or pretty much any other type of table, and create a Python object called a DataFrame. The
DataFrame is made up of rows and columns – it is essentially a table.

The other major component of the Pandas module is a Series object and represents a single
column in the DataFrame. It can be defined as a “one-dimensional labelled array”.

The following is an overview of just a few of the operations which can be performed, and
you are encouraged to further explore this module.

Page 8-2 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Creating a Pandas DataFrame


One of the main things you will want to do is to inspect a dataset for QA/QC operations.

A Pandas DataFrame object can also be created from a Python dictionary. Consider the
following code:

There are a number of reader functions available in the Pandas module which allows you to
access CSV files, JSON objects, html documents and Excel spreadsheets to name but a few.

Each read function has a number of arguments and keyword arguments which specify how
the Pandas DataFrame is created,

The code below uses the Pandas read_excel() function which returns a DataFrame
object:

© ESRI (UK) Ltd Page 8-3


An Introduction to Administering your
Portal using the ArcGIS API for Python

The output is displayed below:

Notice, that by default the first five and the last five records are displayed, and that an
additional column has been added which is an auto-index column which starts at 0.

The readers have a complimentary write object, for example:

Resources: for more information on the Pandas reader and writer


functions you are recommended to look at the following location:

https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html

Page 8-4 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Pandas DataFrame members


The DataFrame class has a number of members which help you investigate and change
values within your dataset. The picture below shows just a few of the many methods that
are available to the object:

• shape
• dropna()
• fillna()
• head()
• iloc()
DataFrame • info()
• isnull()
• loc()
• rename()
• tail()

Methods for inspecting the DataFrame


The head() method will return the first 5 rows by default in the dataframe, which is handy
if you are working with a large dataset and you want a quick view of the first few rows of
the dataset. A different number of rows can be obtained by specifying a number in the
argument list.

© ESRI (UK) Ltd Page 8-5


An Introduction to Administering your
Portal using the ArcGIS API for Python

To see the last 5 records from the dataframe then the tail() method can be used; again
a number can be specified in the argument list.

The info() method is another exploratory tool which provides information about a
number of properties for your dataset, such as how many rows there are, the number of
columns, the datatype of each column, the number of NULL (NaN) values in the dataframe
and the memory footprint of the dataframe.

The shape attribute returns a tuple of two values: the number of rows and the number of
columns:

(1000, 11)

Values within the dataframe can be obtained by using the iloc and loc indexers.

For example, the indexer iloc[2] will get the third row in the dataframe object as it is an
integer based indexer. It will return a Series object back if a single row is fetched:

If a list of rows, by index position, is specified then a DataFrame object is returned back:

The loc[] is label based; for example loc[“UK”] will obtain the row associated with the
“UK” label value. Data can be filtered if the loc[] is used.

Page 8-6 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Pandas DataFrames can be sliced (just as strings and other sequence objects can be sliced).
Slicing allows you to specify a start position and a finish position for rows; so:

df[0:15]

The above code will obtain the first 15 rows in a dataset.

© ESRI (UK) Ltd Page 8-7


An Introduction to Administering your
Portal using the ArcGIS API for Python

Correcting issues in the DataFrame


Pandas provides you with the ability to remove null values within the dataframe using the
dropna() method, or to replace the null values in a dataframe using the fillna()
method.

The dropna() method will delete any row (or column) containing a null value and return a
new dataframe or the edits can be persisted in the original dataframe. It contains optional
arguments which can alter the how records (or columns) are dealt with:

A number which indicates how many nulls should be present


thresh
before the row is deleted.

How should the row be deleted?


how “all”: for a row to be deleted all values should be null.
“any”: a row will be deleted if any value is null.

A boolean:
inplace True will affect the original dataframe.
False will create a new dataframe object.

An example of the DataFrame fillna() method is displayed below:

Page 8-8 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

One final thing…. You may have noticed that the column names have some issues; they have
spaces in them, one or two column names are on separate rows. The rename() method
will correct issues with the names of the columns.

In the example below, the suspect columns are replaced by passing in a dictionary of
original column name and the renamed column:

Pandas is a powerful module for exploring your datasets. Having a rudimentary knowledge
of this module is a must for any aspiring data scientist

© ESRI (UK) Ltd Page 8-9


An Introduction to Administering your
Portal using the ArcGIS API for Python

Activity: Identify possible issues in a


spreadsheet
In this quick activity you will identify some possible issues with the spreadsheet.

You have been asked to create a new hosted feature service of GP Surgery locations for the
Covid-19 vaccination. The information about the surgeries is found in a spreadsheet and you
want to make sure that the data is in a suitable condition from which the hosted feature
layer can then be created.

Look at the following spreadsheet:

Can you identify five issues with it?

Write you findings below:

1: _____________________________________________________________________

2: _____________________________________________________________________

3: _____________________________________________________________________

4: _____________________________________________________________________

5: _____________________________________________________________________

Page 8-10 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Introducing the Esri Spatial Data frame


The Spatially Enabled Data Frame (SeDF) is conceptually very similar to the Pandas
DataFrame object in that it allows data to be manipulated. It differs from the Pandas
DataFrame in that it allows spatial data to be processed; it has good memory management
and can efficiently process large datasets.

The SeDF is actually built using Pandas site package.

The SeDF reads from many different data sources, such as:

• Pandas DataFrames,

• Feature classes

• GeoJSON

• Feature layers

• Shapefiles

The functionality of the SeDF is based upon the geometry engine available to the SeDF
object at creation. The SeDF will try to use ArcPy, if that is not available then the Shapely
engine will be used, and finally, if no engine is available then base geometry objects will be
created. This needs to be taken into account when using the to_featureclass()
method.

How is the Spatially Enabled DataFrame built upon the Pandas module?
Esri has injected two accessors (namespaces) into the Pandas object when it is imported.
The accessors are called spatial and geom.

The spatial accessor provides access to spatial operations on the DataFrame while the
geom accessor provides functions which work on the spatial Series object.

A new datatype called geometry is created as a column type.

This means all the good data processing operations are available through Pandas plus the
additional spatial operations through the SeDF.

© ESRI (UK) Ltd Page 8-11


An Introduction to Administering your
Portal using the ArcGIS API for Python

The following code provides access to the SeDF by injecting / registering the two Geo
namespaces with Pandas:

import pandas as pd

from arcgis.features import GeoAccessor, GeoSeriesAccessor

GeoAccessor is the DataFrame accessor which performs dataset manipulation operations,


while GeoSeriesAccessor provides spatial capabilities to the designated shape field.

Once the accessors have been injected into Pandas, spatial properties and functions can
then be applied against the DataFrame object, for example consider the following code:

df[‘SHAPE’].geom.area

So area is a property on a geometry object which is accessed via the geom accessor. It
returns back a series of areas.

The spatial accessor provides the DataFrame with access to a number of spatial
functions, for example from_xy().

Even though a SeDF has been created it should be noted that its type is still a Pandas
DataFrame. But notice that the SeDF has an additional SHAPE column which allows for the
performing of spatial operations. In essence the DataFrame is now geo-enabled!

Page 8-12 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

A word of warning….
Be aware that within the documentation at the 1.8.2 release of the API that the API
reference makes note of a class in the arcgis.features module called
SpatialDataFrame.

This has now been deprecated since the 1.5x release of the API.

You should use the arcgis.features.GeoAccessor class and the


arcgis.features.GeoSeriesAccessor class instead!

Annoyingly there is a sample in the ArcGIS Tutorials which still uses the SpatialDataFrame
class.

© ESRI (UK) Ltd Page 8-13


An Introduction to Administering your
Portal using the ArcGIS API for Python

Managing a Spatially Enabled DataFrame


There are many methods for creating the SeDF from a wide number of sources, such as
Pandas DataFrame, feature classes and feature layers. The SeDF can be exported as a
geodatabase feature class, shapefile or hosted feature layer in the portal. These are
discussed in a moment.

The SeDF can be obtained from a number of different sources. It can also be exported to a
number of different file formats too, including hosted feature layers within your portal.

There are a number of methods which create a SeDF or export to other formats:

• from_df()
• from_featureclass()
• from geodataframe()
GeoAccessor • from_layer()
• from_xy()
• to_featureclass()
• to_featurelayer()

The from_df() method will take a Pandas DataFrame and create a SeDF if an address
column is present. Credits will be consumed to perform geocoding against the address
column. A Geopandas DataFrame can also be converted into a SEDF through the
from_geodataframe() method.

The from_featureclass() method allows you to access local geospatial datasets. The
types of datasets that can be accessed depends upon what Python modules and site
packages are installed.

Page 8-14 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

If the ArcPy site package is installed, due to the API being available through the installation
of ArcGIS Pro, then the from_featureclass() method can read geodatabase feature
classes, shapefiles and OGC services, and ArcGIS Online web services, to name but a few.

If ArcPy is not installed then the SEDF from_featureclass() method


only supports consuming a shapefile, but only if the Pyshp site package is
installed as this allows shapefiles be read in the Python environment.

Also, if ArcPy is not installed and you wish to read a file geodatabase feature class,
then the fiona Python package must be installed.

The method supports a number of optional keyword arguments including a where clause.

The from_layer() method will read feature layers from online and on-premise portals
into a SEDF.

A Pandas DataFrame can be converted into a SeDF if it contains X and Y columns via the
from_xy() method. A spatial reference should be applied to confirm the spatial reference
of the newly created SeDF.

The GeoAccessor class provides a number of methods for exporting the SeDF to a number of
dataset types, for example to a feature set (to_featureset() ), or to a portal hosted
feature layer through the to_featurelayer() method.

The SeDF provides the to_featureclass()method which allows partial or complete


datasets to be exported.

The ArcPy site package is required if exporting to a file geodatabase feature class.

The argument for the to_featureclass() method requires a pathway and dataset
name if exporting to a shapefile or file geodatabase feature class; or just the name and
optional folder if creating a hosted feature layer from the SEDF.

Remember that the Pandas DataFrame contains many methods to


export to other file formats.

© ESRI (UK) Ltd Page 8-15


An Introduction to Administering your
Portal using the ArcGIS API for Python

Creating a Spatially Enabled DataFrame from


a Hosted Feature Layer
As well as using the above methods on the SeDF object, a subset of records can be obtained
in a Spatial Enabled DataFrame by using the query() method on a feature layer (which
returns a FeatureSet object) and then call the sdf property to return a SeDF object.

Once the SeDF object has been created then the traditional Pandas operations can be
applied, such as head(), tail(), iLoc(), etc.

Page 8-16 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Members found on Accessor classes


The spatial accessor provides access to many useful properties and methods which are
managed by the GeoAccessor class which represents the SeDF, some of which are displayed
below:

• bbox
• centroid
• full_extent
GeoAccessor • geometry_type
• overlay()
• plot()
• sanitize_column_names()

The GeoAccessor class contains many properties which return metadata about the SeDF,
such as the minimum bounding box (bbox) and the SeDF’s centroid (centroid).

The overlay() method allows for a limited number of spatial operations to be performed
using two SeDF objects. The default operation is union but other operations are erase,
identity and intersection. A new SeDF is returned.

A useful method for cleaning column names is the sanitize_column_names()


method as it removes special characters, renames duplicate columns to include an integer
suffix and those columns which do not have a name will be renamed as “noname”.

The geom accessor provides access to members of the GeoSeriesAccessor class.

• as_arcpy
• as_shapely
• buffer()
• centroid()
GeoSeriesAccessor • clip()
• contains()
• intersects()
• project_as()
• within()

© ESRI (UK) Ltd Page 8-17


An Introduction to Administering your
Portal using the ArcGIS API for Python

The class contains many methods which allow for spatial analysis to be performed on the
SHAPE field, such as buffer(), clip(), contains(). There are methods which will
convert a geometry for use with other geometry engines such as ArcPy and Shapely. The
project_as() method re-projects the geometries stored in the series from one
coordinate system to another, while applying an optional transformation.

Page 8-18 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Displaying your SeDF


The class also provides a useful method to inspect your SeDF and the results of any overlay
operations which might be applied to it.

The plot() method is based upon the Matplotlib library plot() function and performs
similar operations.

To access spatial members on the SeDF the spatial accessor still needs to be applied
because the SeDF is still a Pandas DataFrame.

The code snippet below shows how the plot() method is used to display the SeDF on the
map. The default renderer and symbology for point geometries is used:

A map widget object should be supplied on to which the SeDF’s features are plotted, while a
number of different renderers / colour palettes can be applied to symbolise the features.
Renderer types are:

• Single symbol

• Unique values

• Class breaks

• Heatmap

© ESRI (UK) Ltd Page 8-19


An Introduction to Administering your
Portal using the ArcGIS API for Python

The documentation for the plot() provides a description for the different symbol types.

An example of plotting the data within a SEDF using a single symbol while creating red
squares of a certain size is presented below:

Some keyword arguments to make a note of:

renderer_type: specifies the type of renderer to use. Options are s (Simple), u


(Unique), c (Class Breaks) and h (Heatmap).

symbol_type: The type of symbol the user is creating. Options are simple, picture,
text and carto.

symbol_style: The symbology that is used for the geometry.

In the SDK this is incorrectly labelled as symbol_type.

Different keyword arguments are available depending upon if you are rendering Simple,
Unique, class breaks or heatmap renderers.

Page 8-20 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

The most consistent way to apply colours to the SeDF is to use color ramps. These are
matplotlib colour ramps, for example, “Reds_r” refers to the reversed Reds colour ramp,
and cstep refers to a particular colour in the color ramp.

If you are plotting your data through a SEDF then remember that
all you are doing is investigating the data so keep the symbology
simple. Do not over complicate things or it will not work!

© ESRI (UK) Ltd Page 8-21


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to section activity


In this quick activity you will identify some possible issues with the spreadsheet.

Look at the following spreadsheet:

Can you identify five issues with it?

Write you findings below:

1: Column heading spans multiple lines.


2: Row 4 has an incomplete postcode and row 9 should be populated with the correct
postcode.
3: Columns f6 and f7 are composed of NULL values.

4: Columns f8 and f9 are duplicates of the X column and the Y column.

5: The column headings are not very descriptive. It might be a good idea to rename them.

Page 8-22 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 7: Cleansing an Excel spreadsheet

Estimated Time: 40 Minutes

In the section’s activity you inspected an Excel spreadsheet and you identified some issues
with it. For example, there are a number of columns which are composed entirely of NULL
values, some of the column names are not particularly descriptive and one or two of the
fields are duplicates of other existing columns.

In this exercise you will carry out the following:

• Create a Pandas DataFrame from a spreadsheet.

• Delete those fields which contain NULL values.

• Delete those fields which contain duplicate data.

• Rename the attribute fields to provide something more descriptive.

• Plot your amended DataFrame by creating a Spatially enabled DataFrame.

• Create a new GPs surgery hosted feature layer in your portal.

You will use an existing Jupyter Notebook which contains some markup to provide you with
the necessary guidance.

Step 1: Access your Notebook


As always you will write your code in a Notebook.

But which one? A Jupyter Notebook or a variation of the ArcGIS Notebook?

The deciding factor is that because the data you are going to work with is in an Excel
spreadsheet, you will use the standard Jupyter Notebook. If you were to perform the
following actions in ArcGIS Online then the spreadsheet and the exercise’s Notebook would
have to be added as portal items.

To save a small amount of time you will use the Jupyter Notebook.

❑ Start Jupyter Notebook, if it is not been started already. And navigate to the
C:\EsriTraining\PYAPI folder.

© ESRI (UK) Ltd Page 8-23


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Locate the DataCleansing folder and open it up.

Inside is an Excel spreadsheet called EalingGPs.xlsx and a Notebook called


DataCleansing.ipynb.

❑ Open the DataCleansing notebook to display its contents.

As you might expect it contains some comments presented as markdown and a number of
empty code cells.

❑ Locate the cell underneath the Step 1: Accessing your Notebook markdown.

❑ Write the following code to access your portal:

Let’s now add references to the Pandas module.

❑ Underneath the print() function write some code which will import the
pandas module and reference it as an alias called pd.

Page 8-24 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Now you will add a reference to two classes in the arcgis.features module which will allow
you to create a Spatially Enabled DataFrame (SeDF) and a series.

❑ Underneath the line of code which imports the Pandas module write the following
code:

Your code for the first step should look like the following:

❑ Run the code in the cell.

In this first step, you have successfully made a connection to your portal, imported the
Pandas module and the two Accessor classes have been referenced as well.

Step 2: Create and inspect a Pandas DataFrame


Before you publish the spreadsheet as a hosted feature layer, you should inspect its
contents and correct any schema issues that might be found inside it. The best way to
inspect spreadsheet’s content is as a Pandas DataFrame.

❑ Locate the cell underneath the markdown which says: “Step 2: Create the Pandas
DataFrame”

In the previous step you created a reference to the imported Pandas module.

Question 1: What is the name of the Pandas alias?

___________________________________________________________________________

The Pandas module contains a number of methods which allow you to create DataFrames
from a number of formats. You will use the read_excel() method to create a Pandas
DataFrame from the spreadsheet.

© ESRI (UK) Ltd Page 8-25


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Create a variable called df and assign the following code to it:

pd.read_excel()

❑ Pass the EalingsGPs.xlsx spreadsheet into the brackets of the method.

The read_excel() method reads the Excel spreadsheet into a Pandas DataFrame.

❑ Under the function type df.

This will display some of the rows in the Excel spreadsheet.

Your code should look like the following:

❑ Run the code cell.

Notice that the first and last 5 rows of the spreadsheet are displayed. Notice that the issues
you identified in the section activity are still present in the DataFrame:

The Pandas DataFrame is a good vehicle to correct these issues. Before you do that you will
obtain some information about the DataFrame.

First of all, let’s identify how many rows and columns are in the DataFrame.

❑ Underneath the cell which created the DataFrame, add a new cell.

❑ Write a line of code to use the df.shape attribute to return the number of rows
and columns.

Page 8-26 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 2: How many rows and columns does the DataFrame contain?

___________________________________________________________________________

Now that you have an idea of the number of rows and columns the DataFrame is composed
of, you will now get some information about the DataFrame columns and the amount of
memory the DataFrame occupies.

❑ Use the info() method on the dataframe object to display DataFrame


information.

❑ Answer the following questions:

Question 3: What is the data type (Dtype) for the GP Practice Code field?

___________________________________________________________________________

Question 4 What is the data type for the x and y fields?

___________________________________________________________________________

Question 5: For the f6 and f7 columns, how many records have non-null values?

___________________________________________________________________________

There is little point in keeping these columns and so you will remove them from the
DataFrame in the next step.

Question 6: How many float64 columns are there?

___________________________________________________________________________

Question 7: How much memory does the DataFrame occupy?

___________________________________________________________________________

The DataFrame is three times smaller than the actual spreadsheet as it contains just the
data and basic column definition.

© ESRI (UK) Ltd Page 8-27


An Introduction to Administering your
Portal using the ArcGIS API for Python

The info() method would have displayed something very similar to the following:

Finally, you will display just the first three records of the DataFrame.

❑ Write the following code:

df.head(3)

Notice that the rows are zero-indexed – the first row is referenced as index position 0.

Now that you have gained an understanding of the contents of the DataFrame you can now
correct the issues found in the spreadsheet through the methods found on the DataFrame
object.

Step 3: Correct your DataFrame issues


You will now fix your DataFrame issues.

You will first of all remove the columns composed entirely of null values.

Page 8-28 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 8: Which DataFrame method will delete columns if they contain null values?

___________________________________________________________________________

The dropna() method will delete any rows or columns containing null values. You can
specify whether it is rows or columns which are deleted, and the threshold for the number
of null values which determines when the deletion occurs. You can also specify if the
deletion occurs within the source DataFarme or if a new DataFrame is created.

In this step you will perform all operation son the same DataFrame. This will remove any
issues with DataFrames being referenced.

You will delete columns where all records are null.

❑ Locate the markdown which displays “Step 3: Correct your DataFrame issues”.

❑ In the cell underneath the markdown, write the following code:

❑ Run the cell.

Notice that both the f6 and f7 columns have been removed as they were composed entirely
of null values. This confirms your answer to question 5.

There are a number of columns which are not really required, for example EXCEL_ID.

You will now delete the column.

❑ Underneath the previous cell which you just ran, create a new cell.

❑ Write the following code to delete the EXCEL_ID column:

❑ Run the code cell.

© ESRI (UK) Ltd Page 8-29


An Introduction to Administering your
Portal using the ArcGIS API for Python

Notice that the EXCEL_ID column has now been removed.

There are some additional columns which should also be removed; namely f4, f5, f8 and f9
as they contain data which is duplicated in other columns or data that is just not useful.

❑ Create a new cell under the last line of code you just executed.

❑ Use the same notation as above to delete the f4, f5, f8, f9 and objectId columns.

Note the case of the objectId field. There is a capital “I”.

Notice that the columns have been removed:

The final thing you will do in this step is to rename some of the columns to something more
useful.

❑ Create a new cell underneath the code which you just executed.

You will use the rename() method to perform the renaming of the columns.

The rename() method has an argument called columns which requires a Python
dictionary; the key of the dictionary is the existing field to be renamed, and the value is the
new column name.

Page 8-30 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Write the following code:

❑ Run the code in the cell.

Your final view of the DataFrame should look like the one displayed below:

Step 4: Plot the DataFrame


The final thing you will do before you create the hosted feature layer is to create a quick
plot on a map widget to display the GP surgeries. Luckily X, Y information is available as part
of the spreadsheet / DataFrame you have been working with.

You will symbolise the surgeries as blue circles, with a size of 6.

First of all you will create a reference to a map widget.

❑ Locate the cell underneath the markdown which displays “Step 4: Plot the
DataFrame”.

You will first of all write a line of code which will convert the Pandas DataFrame to a
Spatially enabled DataFrame (SeDF). You will use the spatial accessor property on the
DataFrame object followed by the from_xy() method. This is found on the GeoAccessor
class, which the spatial property provides access to.

© ESRI (UK) Ltd Page 8-31


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Write the following code:

The x_column argument references the X column of the DataFrame and the y_column
references the Y column in the DataFrame.

Question 9: What does the sr argument refer to?

___________________________________________________________________________

Question 10: What spatial reference does 4326 represent?

___________________________________________________________________________

❑ Run the code cell.

Notice that a new SHAPE field has been added:

It contains information about the geometry type and the spatial reference (WKID).

The DataFrame is now spatially enabled, and is referenced in the variable called sed.

❑ Underneath the cell you just ran, create a new cell.

❑ Write a line of code which creates a map focusing on Ealing, London with a
zoomlevel of 12.

Remember that this was one of the first things you created when
you first started to access the API.

Page 8-32 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Display the map using the map variable and run the code cell.

You will see a map centred on Ealing in West London.

Now that you have spatially enabled the DataFrame, you will use the plot() method on
the spatial accessor property to display the data in the SeDF on the map.

❑ Write the following code underneath the map() method.

sed.spatial.plot()

You will use the information in the following table to correctly populate the plot()
method:

Keyword argument Value

map_widget map

renderer_type ‘s’

symbol_type ‘simple’

symbol_style ‘o’

colors ‘Blues_r’

cstep 50

outline_color ‘Blues_r

marker_size 6

❑ Use the table to fill in the missing keyword arguments for the plot() method.

© ESRI (UK) Ltd Page 8-33


An Introduction to Administering your
Portal using the ArcGIS API for Python

Your code should look like the following:

❑ Run the code in the cell.

You should see a map which looks like the following!

You can pan around the map, zoom in / out and click on the symbol to display information
associated with each feature:

Page 8-34 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Now you have inspected your DataFrame on a map by spatially enabling it, you are now
ready to publish it to your portal.

Step 5: Create a Hosted Feature Layer from the SeDF


It is now time to publish your DataFrame of surgeries in Ealing as a hosted feature layer in
your portal.

❑ Locate the markdown “Step 5: Create a Hosted Feature Layer from the SeDF”.

The GeoAccessor class has a number of methods which you can use to create new datasets,
for example - to_featureclass() will export a SeDF to a geodatabase feature class.

You will use the to_featurelayer() method to create a new hosted feature layer.

❑ Locate the GeoAccessor class : to_featurelayer() method in the API


documentation.

Question 11: What argument does the method require?

___________________________________________________________________________

There is only one required argument (title) – the other arguments are optional.

Question 12: What does the to_featurelayer() method return?

___________________________________________________________________________

❑ In the cell underneath the markdown create a new variable called fl and assign to it
the following:

sed.spatial.to_featurelayer()

© ESRI (UK) Ltd Page 8-35


An Introduction to Administering your
Portal using the ArcGIS API for Python

❑ Use the following table as a guide to add the necessary arguments into the
to_featurelayer() method:

Argument Value

title ‘EalingsGPs_<YourInitials>’

gis gis

tags ‘Ealing, GPs, Surgeries’

❑ On the next line type fl as this will display properties of the feature layer using the
Notebook’s rich display capabilities.

Your code should look like the following (Your initials for the title argument will be
different…):

❑ Run the code in the cell.

The result will be the rich display of the newly created feature layer properties in the
Notebook:

You will quickly investigate the newly created feature layer in your portal.

❑ Click on the title of the hosted feature layer in the rich representation display.

In the above picture it is called ‘EalingGPs_EPJ’.

Page 8-36 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

This will display the feature layer item’s metadata page.

❑ Click on the Data tab to display the attribute data.

Notice that the field headings are as expected; the unrequired columns have been deleted,
while other columns have been renamed.

❑ Click on the Visualization tab to view the actual GP surgery points.

Notice that the default symbology has been provided for the surgery locations.

❑ Click the Overview tab to take you back to the item’s metadata.

❑ Spend a few moments investigating the metadata….

Are the title and tags populated correctly?

Is there anything you might like to change?

Perhaps you might like to change the name of the actual layer; possibly add a description
and a summary? That might be something to do for another time…..

© ESRI (UK) Ltd Page 8-37


An Introduction to Administering your
Portal using the ArcGIS API for Python

In this exercise you have taken an Excel spreadsheet, cleaned it through methods found on
the Pandas DataFrame and then inspected the data using the SeDF’s plot() method.
Finally. you created a new hosted feature layer and validated your work!

Exercise End

Page 8-38 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Answers to Exercise 7 Questions

Question 1: What is the name of the Pandas alias?

Answer: The alias is called pd.

Question 2: How many rows and columns does the DataFrame contain?

Answer: 79 rows and 13 columns.

Question 3: What is the data type (Dtype) for the GP Practice Code field?

Answer: The data type is ‘object’.

Question 4 What is the data type for the x and y fields?

Answer: The data type for the x and y fields is float 64.

Question 5: For the f6 and f7 columns, how many records have non-null values?

Answer: Both fields contain zero non-null records, i.e the field values are composed
entirely of NULL values.

Question 6: How many float64 columns are there?

Answer: The are 8 columns of the data type float64

Question 7: How much memory does the DataFrame occupy?

Answer: The DataFrame occupies just over 8KB in memory.

Question 8: Which DataFrame method will delete columns if they contain null values?

Answer: The method is dropna().

© ESRI (UK) Ltd Page 8-39


An Introduction to Administering your
Portal using the ArcGIS API for Python

Question 9: What does the sr argument refer to?

Answer: The sr argument refers to the Spatial Reference of the SeDF.

Question 10: What spatial reference does 4326 represent?

Answer: The 4326 code (WKID) refers to WGS 1984.

Question 11: What argument does the method require?

Answer: The title argument is mandatory.

Question 12: What does the to_featurelayer() method return?

Answer: A Feature Layer object that resides in your portal.

Page 8-40 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

Exercise 7 Solution
DataCleansing.ipynb

© ESRI (UK) Ltd Page 8-41


An Introduction to Administering your
Portal using the ArcGIS API for Python

Page 8-42 © ESRI (UK) Ltd


An Introduction to Administering your
Portal using the ArcGIS API for Python

© ESRI (UK) Ltd Page 8-43

You might also like