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

Python virtual environments


The recommended approach to using Python, in general, is a project-based one. This means that each project uses a separate Python version, along with the packages required and their mutual dependencies. This approach gives you the flexibility to switch between different Python versions and installed package versions. Not following this approach would mean that, every time you update a package or install a new one, its dependencies will be updated too, resulting in a different setup. This may cause problems, for example, code that won't run correctly because of changes under the hood, or packages that do not communicate correctly with each other. While this book focuses on Python 3, there won't be any need to switch to a different Python version, but maybe you can imagine using different versions of the same packages for different projects.

Before Anaconda, this project-based approach would require using virtualenv, a tool for creating isolated Python environments. This approach has gotten a lot easier with Anaconda, which offers the same approach but in a more simplified way. Both options are covered in detail as we proceed further.

Virtual environments using Anaconda

As stated before, Anaconda Navigator has a tab called Environments, that when clicked will display an overview of all local environments created by the user on a local file system. You can easily create, import, clone, or remove environments, specify the preferred Python version, and install packages by version number inside such an environment. Any new environment will automatically install a number of Python packages, such as pip. From there, you are free to install more packages. These environments are the exact same virtual environments that you would create by using the virtualenv tool. You can start working with them by opening a terminal or by running Python, which opens a terminal and runs python.exe

Anaconda stores all environments in a separate root folder, keeping all your virtual environments in one place. Note that each environment in Anaconda Navigator is treated as a virtual environment, even the root environment.

Managing environments with conda 

Both Anaconda and Miniconda offer the conda package manager, which can also be used to manage virtual environments. Open a terminal and use the following command to list all available environments on your system:

>> conda info -e

Use the following command for creating a virtual environment based on Python version 2.7:

>> conda create -n python3packt python=2.7

Activate the environment next as follows:

>> activate python3packt

Multiple additional packages can now be installed with a single command:

>> conda install -n python3packt <package-name1> <package-name2>

This command calls conda directly. 

Deactivate the environment you've been working in as follows:

>> deactivate

More on managing environments with conda can be found at: https://conda.io/docs/user-guide/tasks/manage-environments.html

Virtual environments using virtualenv

If you don't want to use Anaconda, virtualenv needs to be installed first. Use the following command to install it locally:

>> pip install virtualenv

Next, a virtual environment can be created by assigning with the virtualenv command followed by the name of the new environment, for example:

>> virtualenv python3packt

Navigate to the directory with the same name:

>> cd python3packt

 Next, activate the virtual environment with the activate command:

>> activate

Your virtual environment is now ready for use. Use pip install to install packages exclusively to this environment and use them in your code. Use the deactivate command to stop the virtual environment from working:

>> deactivate

If you have multiple Python versions installed, use the argument -p together with the desired Python version or path to the python.exe file of your choice, for example:

>> -p python2.7

You can also do it as follows:

>> -p c:\python34\python.exe

This step follows creation of the virtual environment and precedes installation of the required packages. For more information on virtualenv, see: http://virtualenv.readthedocs.io/en/stable