You are here because you love Python programming and are interested in making your own Geographic Information Systems (GIS) application. You want to create a desktop application, in other words, a user interface, that helps you or others create, process, analyze, and visualize geographic data. This book will be your step-by-step guide toward that goal.
We assume that you are someone who enjoys programming and being creative but are not necessarily a computer science guru, Python expert, or seasoned GIS analyst. To successfully proceed with this book, it is recommended that you have a basic introductory knowledge of Python programming that includes classes, methods, and the Tkinter toolkit, as well as some core GIS concepts. If you are a newcomer to some of these, we will still cover some of the basics, but you will need to have the interest and ability to follow along at a fast pace.
In this introductory chapter, you will cover the following:
Learn some of the benefits of creating a GIS application from scratch
Set up your computer, so you can follow the book instructions.
Become familiar with the roadmap toward creating our application.
The first step in preparing ourselves for this book is in convincing ourselves why we want to make our own GIS application, as well as to be clear about our motives. Spatial analysis and GIS have been popular for decades and there is plenty of GIS software out there, so why go through the trouble of reinventing the wheel? Firstly, we aren't really reinventing the wheel, since Python can be extended with plenty of third-party libraries that take care of most of our geospatial needs (more on that later).
For me, the main motivation stems from the problem that most of today's GIS applications are aimed at highly capable and technical users who are well-versed in GIS or computer science, packed with a dizzying array of buttons and options that will scare off many an analyst. We believe that there is a virtue in trying to create a simpler and more user-friendly software for beginner GIS users or even the broader public, without having to start completely from scratch. This way, we also add more alternatives for users to choose from, as supplements to the current GIS market dominated by a few major giants, notably ArcGIS and QGIS, but also others such as GRASS, uDig, gvSIG, and more.
Another particularly exciting reason to create your own GIS from scratch is to make your own domain-specific special purpose software for any task you can imagine, whether it is a water flow model GIS, an ecological migrations GIS, or even a GIS for kids. Such specialized tasks that would usually require many arduous steps in an ordinary GIS, could be greatly simplified into a single button and accompanied with suitable functionality, design layout, icons, and colors. One such example is the Crime Analytics for Space-Time (CAST) software produced by the GeoDa Center at Arizona State University, seen in the following picture:
Also, by creating your GIS from scratch, it is possible to have greater control of the size and portability of your application. This can enable you to go small—letting your application have faster startup time, and travel the Internet or on a USB-stick easily. Although storage space itself is not as much of an issue these days, from a user's perspective, installing a 200 MB application is still a greater psychological investment with a greater toll in terms of willingness to try it than a mere 30 MB application (all else being equal). This is particularly true in the realm of smartphones and tablets, a very exciting market for special-purpose geospatial apps. While the specific application we make in this book will not be able to run on iOS or Android devices, it will run on Windows 8-based hybrid tablets, and can be rebuilt around a different GUI toolkit in order to support iOS or Android (we will mention some very brief suggestions for this in Chapter 8, Looking Forward).
Finally, the utility and philosophy of free and open source software may be an important motivation for some of you. Many people today, learn to appreciate open source GIS after losing access to subscription-based applications like ArcGIS when they complete their university education or change their workplace. By developing your own open source GIS application and sharing with others, you can contribute back to and become part of the community that once helped you.
In this book, we follow steps on how to make an application that is developed in a Windows environment. This does not mean that the application cannot be developed on Mac OS X or Linux, but those platforms may have slightly different installation instructions and may require compiling of the binary code that is outside the scope of this book. Therefore, we leave that choice up to the reader. In this book, which focuses on Windows, we avoid the problem of compiling it altogether, using precompiled versions where possible (more on this later).
The development process itself will be done using Python 2.7, specifically the 32-bit version, though 64-bit can theoretically be used as well (note that this is the bit version of your Python installation and has nothing to do with the bit version of your operating system). Although there exists many newer versions, version 2.7 is the most widely supported in terms of being able to use third-party packages. It has also been reported that the version 2.7 will continue to be actively developed and promoted until the year 2020. It will still be possible to use after support has ended. If you do not already have version 2.7, install it now, by following these steps:
Go to https://www.python.org/.
Under Downloads click on download the latest 32-bit version of Python 2.7 for Windows, which at the time of this writing is Python 2.7.9.
Download and run the installation program.
For the actual code writing and editing, we will be using the built-in Python Interactive Development Environment (IDLE), but you may of course use any code editor you want. The IDLE lets you write long scripts that can be saved to files and offers an interactive shell window to execute one line at a time. There should be a desktop or start-menu link to Python IDLE after installing Python.
The Python Package Index (PyPI) website currently lists more than 240 packages tagged Topic :: Scientific/Engineering :: GIS. For a less overwhelming overview of the more popular GIS-related Python libraries, check out the catalogue at the Python-GIS-Resources website created by the author:
We will have to define which packages to use and install, and this depends on the type of application we are making. What we want to make in this book is a lightweight, highly portable, extendable, and general-purpose GIS application. For these reasons, we avoid heavy packages like GDAL, NumPy, Matplotlib, SciPy, and Mapnik (weighing in at about 30 MB each or about 150-200 MB if we combine them all together). Instead, we focus on lighter third-party packages specialized for each specific functionality.
Dropping these heavy packages is a bold decision, as they contain a lot of functionality, and are reliable, efficient, and a dependency for many other packages. If you decide that you want to use them in an application where size is not an issue, you may want to begin now by installing the multipurpose NumPy and possibly SciPy, both of which have easy-to-use installers from their official websites. The other heavy packages will be briefly revisited in later chapters.
Specific installation instructions are given for each package in the chapter where they are relevant (see the following table for an overview) so that if you do not want certain functionalities, you can ignore those installations. Due to our focus to make a basic and lightweight application, we will only be installing a small number of packages. However, we will provide suggestions throughout the book about other relevant packages that you may wish to add later on.
Raster data, management, and analysis
Vector management and analysis
Vector data speedup
The typical way to install Python packages is using
pip (included with Python 2.7), which downloads and installs packages directly from the Python Package Index website.
Pip is used in the following way:
Step 2—in the black screen window that pops up, one simply types
pip install packagename. This will only work if
pipis on your system's environment path. If this is not the case, a quick fix is to simply type the full path to the
C:\Python27\Scripts\pipinstead of just
For C or C++ based packages, it is becoming increasingly popular to make them available as precompiled wheel files ending in
.whl, which has caused some confusion on how to install them. Luckily, we can use
pip to install these wheel files as well, by simply downloading the wheel and pointing
pip to its file path.
Since some of our dependencies have multiple purposes and are not unique to just one chapter, we will install these ones now. One of them is the Python Imaging Library (PIL), which we will use for the raster data model and for visualization. Let's go ahead and install PIL for Windows now:
Click on the latest
.exefile link for our 32-bit Python 2.7 environment to download the PIL installer, which is currently
Run the installation file.
Open the IDLE interactive shell and type
import PILto make sure it was installed correctly.
Download the Shapely wheel file that fits our system, looking something like
As described earlier, open a command line window and type
C:\Python27\Scripts\pip install path\to\Shapely‑1.5.7‑cp27‑none‑win32.whlto unpack the precompiled binaries.
Before we begin developing our application, it is important that we create a vision of how we want to structure our application. In Python terms, we will be creating a multilevel package with various subpackages and submodules to take care of different parts of our functionality, independently of any user interface. Only on top of this underlying functionality do we create the visual user interface as a way to access and run that underlying code. This way, we build a solid system, and allow power-users to access all the same functionality via Python scripting for greater automation and efficiency, as exists for ArcGIS and QGIS.
To setup the main Python package behind our application, create a new folder called
pythongis anywhere on your computer. For Python to be able to interpret the folder
pythongis as an importable package, it needs to find a file named
__init__.py in that folder. Perform the following steps:
Open Python IDLE from the Windows start menu.
The first window to pop up is the interactive shell. To open the script editing window click on File and New.
Click on File and then Save As.
In the dialog window that pops up, browse into the
__init__.pyas the filename, and click on Save.
There are two main types of GIS data: vector (coordinate-based geometries such as points, lines, and polygons) and raster (a regularly spaced out grid of data points or cells, similar to an image and its pixels).
For a more detailed introduction to the differences between vector and raster data, and other basic GIS concepts, we refer the reader to the book Learning Geospatial Analysis with Python, by Joel Lawhead. You can find this book at:
Since vector and raster data are so fundamentally different in all regards, we split our package in two, one for vector and one for raster. Using the same method as earlier, we create two new subpackage folders within the
pythongis package; one called
vector and one called
raster (each with the same aforementioned empty
__init__.py file). Thus, the structure of our package will look as follows (note that
: package is not part of the folder name):
To make our new
raster subpackages importable by our top level
pythongis package, we need to add the following relative import statements in
from . import vector from . import raster
Throughout the course of this book, we will build the functionality of these two data types as a set of Python modules in their respective folders. Eventually, we want to end up with a GIS application that has only the most basic of geospatial tools so that we will be able to load, save, manage, visualize, and overlay data, each of which will be covered in the following chapters.
As far as our final product goes, since we focus on clarity and simplicity, we do not put too much effort into making it fast or memory efficient. This comes from an often repeated saying among programmers, an example of which is found in Structured Programming with go to Statements, ACM, Computing Surveys 6 (4):
premature optimization is the root of all evil
|--Donald E. Knuth
This leaves us with software that works best with small files, which in most cases is good enough. Once you have a working application and you feel that you need support for larger or faster files, then it's up to you if you want to put in the extra effort of optimization.
The GIS application you end up with at the end of the book is simple but functional, and is meant to serve as a framework that you can easily build on. To leave you with some ideas to pick up on, we placed various information boxes throughout the book with ways that you can optimize or extend your application. For any of the core topics and features that we were not able to cover earlier in the book, we give a broader discussion of missing functionality and future suggestions in the final chapter.
In this chapter, you learned about why you want to create a GIS application using Python, set up our programming environment, installed some recurring packages, and created your application structure and framework.
In the next chapter, you will take the first step toward making a geospatial application, by creating a simple yet powerful module for loading and saving some common geospatial data formats from scratch.