Book Image

Mastering Geospatial Analysis with Python

By : Silas Toms, Paul Crickard, Eric van Rees
Book Image

Mastering Geospatial Analysis with Python

By: Silas Toms, Paul Crickard, Eric van Rees

Overview of this book

Python comes with a host of open source libraries and tools that help you work on professional geoprocessing tasks without investing in expensive tools. This book will introduce Python developers, both new and experienced, to a variety of new code libraries that have been developed to perform geospatial analysis, statistical analysis, and data management. This book will use examples and code snippets that will help explain how Python 3 differs from Python 2, and how these new code libraries can be used to solve age-old problems in geospatial analysis. You will begin by understanding what geoprocessing is and explore the tools and libraries that Python 3 offers. You will then learn to use Python code libraries to read and write geospatial data. You will then learn to perform geospatial queries within databases and learn PyQGIS to automate analysis within the QGIS mapping suite. Moving forward, you will explore the newly released ArcGIS API for Python and ArcGIS Online to perform geospatial analysis and create ArcGIS Online web maps. Further, you will deep dive into Python Geospatial web frameworks and learn to create a geospatial REST API.
Table of Contents (23 chapters)
Title Page
Copyright and Credits
Packt Upsell
Contributors
Preface
7
Geoprocessing with Geodatabases
Index

Managing Python packages


After installing Anaconda, it's time to discuss how to manage different Python packages. Anaconda offers several options to do this—Anaconda Navigator, Anaconda Cloud, and the conda package manager.

Managing packages with Anaconda Navigator

After installing Anaconda, you will notice a working folder with various applications inside of it. One of these is Anaconda Navigator, which provides a Graphical User Interface (GUI). You can compare it to Windows File Explorer, that is, an environment to manage projects, packages, and environments. The term environment refers to a collection of packages and a Python install. Notice that this is similar to how you would use virtualenv, but this time using a GUI instead of a command prompt to create one (virtualenv is covered in more detail later in this chapter).

After opening Anaconda Navigator, click the Environments tab on the left of the screen and Anaconda Navigator will provide an overview of existing environments and the packages it contains. There's one pre-defined environment available, a so-called root environment that provides you with 150+ pre-installed Python packages. New environments can be made by clicking the Create button on the bottom of the screen. This will automatically install five default Python packages, including pip, which means you're free to use that too for package management. What's interesting about Anaconda Navigator is that, with every new environment, you can choose a preferred Python version and install from a list of 1000+ packages that are available locally if you installed the default Anaconda version and not Miniconda. This list is available by selecting the option Not Installed from the drop-down menu next to the Channels button. You can easily search and select the packages of your choice by using the Search Packages field and hitting Enter. Mark the packages and install them for the environment of your choice. After installation, the package will be listed by name in the environment. If you click the green box with a checkmark next to the package name, you can choose to mark a package for an upgrade, removal, or specific version installation.

After installing the packages, you can start working with an environment by opening up a terminal, Jupyter Notebook, or another Anaconda application with one mouse click on the arrow button inside of the environment of your choice. If you wish to use an IDE instead of one of the options that Anaconda Navigator offers you, be sure to redirect your IDE to the right python.exe file that is used by Anaconda. This file can usually be found at the following path, which is the default installation path of Anaconda:

C:\Users\<UserName>\Anaconda3\python.exe.

Online searching for packages using Anaconda Cloud

If you are searching for a Python package that is not found in the local list of available Python packages, you can use Anaconda Cloud. This application is also part of Anaconda3 and you can use the Anaconda Cloud application for sharing packages, Notebooks, and environments with others. After clicking on the Anaconda Cloud desktop icon, an internet page will open where you can sign up to become a registered user. Anaconda Cloud is similar to GitHub, as it lets you create a private online repository for your own work. These repositories are called channels.

If you create a user account, you can use Anaconda Cloud from inside Anaconda Navigator. After creating a user account for Anaconda Cloud, open Anaconda Navigator and use your login details to sign into Anaconda Cloud in the upper-right corner of the screen where it says Sign in to Anaconda Cloud. Now, you can upload your own packages and files to a private package repository and search for existing files or packages. 

Managing Python packages with conda

Apart from using Anaconda Navigator and Cloud for package management, you can use conda, a binary package manager, as a command-line tool to manage your package installations. conda quickly installs, runs, and updates packages and their dependencies. conda easily creates, saves, loads, and switches between environments on your local computer. The best ways to install conda are through installing either Anaconda or Miniconda. A third option is a separate installation through Python Package Index (PyPI), but may not be up-to-date so this option is not recommended.

Installing packages with conda is straightforward, as it resembles the syntax of pip. However, it is good to know that conda cannot install packages directly from a Git server. This means that the latest version of many packages under development cannot be downloaded with conda. Also, conda doesn't cover all the packages available on PyPI as pip does itself, which is why you always have access to pip when creating a new environment with Anaconda Navigator (more on pip as we proceed further).

You can verify if conda is installed by typing the following command in a terminal:

>> conda -version

If installed, conda will display the number of the version that you have installed. Installing the package of your choice can be done with the following command in a terminal:

>> conda install <package-name>

Updating an already installed package to its latest available version can be done as follows:

>> conda update <package-name>

You can also install a particular version of a package by pointing out the version number:

>> conda install <package-name>=1.2.0

You can update all the available packages simply by using the --all argument:

>> conda update --all

You can uninstall packages too:

>> conda remove <package-name>

Extensive conda documentation is available at: https://conda.io/docs/index.html.

Managing Python packages using pip

As stated earlier, Anaconda users always have pip available in every new environment, as well as the root folder—it comes pre-installed with every version of Anaconda, including Miniconda. As pip is a Python package manager used to install and manage software packages written in Python, it runs in the command line, as opposed to Anaconda Navigator and Cloud. If you decide not to use Anaconda or anything similar to it, and use a default Python installation from python.org, you can either use easy_install or pip as a package manager. As pip is seen as an improvement over easy_install and the preferred Python package manager for Python 3, we will only discuss pip here. It is recommended to use either pip, conda, Anaconda Navigator, or Cloud for Python package management in the upcoming chapters.

Optionally, as you install Anaconda, three environment variables will be added to your list of user variables. This enables you to access commands such as pip from any system location if you open a terminal. To check if pip is installed on your system, open a terminal and enter:

>> pip

If you don't receive any error message, it means pip is installed correctly and you can use pip to install any package of your choice from the PyPI by using:

>> pip install <package-name>

For Anaconda users, the pip command file should be stored at the following path:

C:\Users\<User Name>\Anaconda3\Scripts\pip.exe.

If pip is not available on your system, you can install pip by following the instructions given at: https://pip.pypa.io/en/latest/installing.

Upgrading and uninstalling the package with pip

Whereas Anaconda Cloud automatically displays a version number of a certain installed package, users choosing to use a default Python installation can use pip to display it through the following command:

>> import pandas
>> pandas.__version__ # output will be a version number, for example: u'0.18.1'

Upgrading a package, for example when there's a new version you'd like to use, can be done as follows:

>> pip install -U pandas==0.21.0

Upgrading it to the latest available version can be done as follows:

>> pip install -U pandas

Uninstalling a package can be done with the following command:

>> pip uninstall <package name>