Spatial Libraries in Python 3: A Working Set-up for Windows
Get started with open-source spatial analysis in Python can be tricky. For one, there’s many possible libraries from which to choose, of varying and overlapping functionality but not enough cross-compatibility. Piecemeal and sparse documentation also remains a challenge, and most top blog posts for common questions on Google I’ve found to be using older code conventions.
This two-part article aims to get you started in an geodata-stack that is modern and works. Part One (below) will walk you through installation on Windows. Part Two more or less follows the example of Andrew Gadius’ blog on the same topic to give a real world example of downloading shapefile ZIPs from the web, but updates the code to more modern standards and dependencies that work with Python 3.
Getting installations to work in Windows can be a minefield, but just for fun let’s do it anyway using Windows 8 Pro, and I will assume you have Anaconda for Python 3 installed. For reference, any code you see that looks like
> this should be entered into your local Anaconda command prompt. (Find this pressing Windows key-R then typing Anaconda command prompt.)
Preparing your environment
To get your installation working properly, the best thing to do is to start fresh in a new environment. Per conda docs, exiting your environment before entering a new one is considered best practice for Windows. (Here, “snakes” is my environment name)
> deactivate snakes
Now we’ll create a new environment running Python 3.5, which we will call geo, and install some basic data analysis libraries at the same time.
> conda create -n geo python-3.5 numpy pandas scipy
Activate the new environment and install geopandas. This will automatically load the necessary dependencies. Do not attempt to install the dependencies separately. If you already did, delete the environment and start fresh to prevent headaches and bugs later on.
> activate geo
> conda install -c conda-forge geopandas
I do a lot of data science work, so I like to make sure jupyter notebook also gets installed at this time.
> conda install jupyter
For handling the HTTP requests, Urllib2 in Python 2.x has been replaced by Urllib in Python 3.x. These built-in libraries both have requests modules, however, the requests library has emerged as a modern standard (see discussion on Stack Overflow), so you will also want to install this.
> conda install requests
Optional: We won’t use this in this tutorial, but I also recommend that at this point you install the feather-format library. When downloading large amounts of data from the internet such as shapefiles, it makes sense to only do so once, keeping the data locally for future use. You could save to Python pickle, but feather is a faster, more cross-compatible filetype.
> conda install feather-format -c conda-forge
Assuming everything installed without any errors, and you’re able to launch a notebook by typing
> jupyter notebook you’re ready to download and start working with the data in Part Two.
Lauren Oldja is a data scientist in Brooklyn, NY.