-
Wigal, Jacob (CIV) authoredWigal, Jacob (CIV) authored
Installing Python Packages for Geospatial Data Analysis
The purpose of this page is to walkthrough installing Python and Python packages that are needed to perform various GIS-tasks.
How do I begin?
Install Anaconda which is an open source distribution of the Python and R programming languages for large-scale data processing, predictive analytics, and scientific computing, that aims to simplify package management and deployment, and virtual environments. In short, it makes life easier when working with Python.
Install Anaconda on Windows
Download Anaconda installer (Latest Python version, 64 bit) for Windows.
Install Anaconda to your computer by double clicking the installer and install it into a directory you want (needs admin rights). Install it to all users and use default settings.
Test that the Anaconda package manager called conda
works by opening the newly downloaded Anaconda Prompt and running the command conda --version
.
Install GIS related packages with conda (and pip) by running the following commands in your terminal / command prompt /anaconda prompt.
As you can see below, many of the GIS packages are available from specific channel
from conda called conda-forge
.
We recommend that you first install only geopandas, and add the other packages once you need them. Installing geopandas will automatically install several fundamental GIS packages (such as Shapely and fiona) on your computer, so you will have a nice setup already using one installation command!
conda install geopandas -c conda-forge
Install Anaconda on Mac / Linux
Download Anaconda installer (Latest Python version, 64 bit) for Mac or Linux.
Test that the Anaconda package manager called conda
works by opening Terminal on your Mac or Linux machinne and running the command conda --version
.
Add Anaconda to system path
# Add Anaconda installation permanently to PATH variable
nano ~/.bashrc
# Add following line at the end of the file and save (EDIT ACCORDING YOUR INSTALLATION PATH)
export PATH=$PATH:/PATH_TO_ANACONDA/anaconda3/bin:/PATH_TO_ANACONDA/anaconda3/lib/python3.7/site-packages
Install Jupyter Notebook
Per their own documentation , "Jupyter Notebook is an open-source web application that allows you to create and share documents that contain live code, equations, visualizations and narrative text. Uses include: data cleaning and transformation, numerical simulation, statistical modeling, data visualization, machine learning, and much more."
We use this a lot, and will use this in other tutorials so you will want to install this. You can install Jupyter Notebook using conda:
conda install -c conda-forge notebook
After installation is completed, you can start a Jupyter Notebook instance by running this command:
jupyter notebook
Jupyter Notebook should open up in a browser window. Jupyter Notebooks save with the file extension .ipynb, short for IPython Notebook. (Note: to open one of these files, you will need to first make sure jupyter notebook has been launched from either Anaconda Prompt(Windows) or Terminal(Mac).)
Install Python Packages
Install GIS related packages with conda (and pip) by running in command prompt following commands (in the same order as they are listed): You can start by installing geopandas, and add other packages once you start needing them. (Note: installing geopandas will automatically install several dependencies, or other python packages (such as Shapely and fiona) on your computer, so you will have a nice setup already using one installation command!)
Install packages
conda install -c conda-forge geopandas
conda install -c conda-forge matplotlib
conda install -c conda-forge geojson
conda install -c conda-forge folium
conda install -c conda-forge ipyleaflet
conda install -c conda-forge rasterio
You can test that the installations have worked by running following commands in a new Jupyter Notebook (try importing only those packages you installed):
import geopandas as gpd
import pysal
import cartopy
import folium
import dash
import rasterio
If you don't receive any errors, everything should be working!
Creating a new environment using conda
Conda allows us to create different Python environments . Python packages have often many dependencies, and some packages might, for example, only work with an older version of Python.
Run this codes in order to get started with a GIS Python environment (tested to work on Windows 10 in November 2019):
# Create a new environment with python 3.7
conda create -n gis python=3.7
# Activate the environment
conda activate gis
# Install jupyter notebook again, because there are no packages on this new environmennt
conda install -c conda-forge notebook
# Start jupyter notebook
jupyter notebook
# Jupyter notebook opens up in a browser
# Shut down jupyter notebook using CTRL+C once done working
How to find out which conda -command to use when installing a package?
The first thing to try when installing a new module X
is to run in a command prompt (as admin) following command (here we try to install a hypothetical
module called X)
conda install X
or downloading from a specific channel:
conda install X -c conda-forge
In most cases this approach works but sometimes you may need to google the package name + 'anaconda cloud' to find the correct command to run. Here is an example anaconda cloud page with more information on how to install GeoPandas.
Instructions on this page have been adapted from Automating GIS processes -course @ Department of Geosciences & Geography, University of Helsinki