Table of Contents for
Python Geospatial Development - Third Edition

Version ebook / Retour

Cover image for bash Cookbook, 2nd Edition Python Geospatial Development - Third Edition by Erik Westra Published by Packt Publishing, 2016
  1. Cover
  2. Table of Contents
  3. Python Geospatial Development Third Edition
  4. Python Geospatial Development Third Edition
  5. Credits
  6. About the Author
  7. About the Reviewer
  8. www.PacktPub.com
  9. Preface
  10. What you need for this book
  11. Who this book is for
  12. Conventions
  13. Reader feedback
  14. Customer support
  15. 1. Geospatial Development Using Python
  16. Geospatial development
  17. Applications of geospatial development
  18. Recent developments
  19. Summary
  20. 2. GIS
  21. GIS data formats
  22. Working with GIS data manually
  23. Summary
  24. 3. Python Libraries for Geospatial Development
  25. Dealing with projections
  26. Analyzing and manipulating Geospatial data
  27. Visualizing geospatial data
  28. Summary
  29. 4. Sources of Geospatial Data
  30. Sources of geospatial data in raster format
  31. Sources of other types of geospatial data
  32. Choosing your geospatial data source
  33. Summary
  34. 5. Working with Geospatial Data in Python
  35. Working with geospatial data
  36. Changing datums and projections
  37. Performing geospatial calculations
  38. Converting and standardizing units of geometry and distance
  39. Exercises
  40. Summary
  41. 6. Spatial Databases
  42. Spatial indexes
  43. Introducing PostGIS
  44. Setting up a database
  45. Using PostGIS
  46. Recommended best practices
  47. Summary
  48. 7. Using Python and Mapnik to Generate Maps
  49. Creating an example map
  50. Mapnik concepts
  51. Summary
  52. 8. Working with Spatial Data
  53. Designing and building the database
  54. Downloading and importing the data
  55. Implementing the DISTAL application
  56. Using DISTAL
  57. Summary
  58. 9. Improving the DISTAL Application
  59. Dealing with the scale problem
  60. Performance
  61. Summary
  62. 10. Tools for Web-based Geospatial Development
  63. A closer look at three specific tools and techniques
  64. Summary
  65. 11. Putting It All Together – a Complete Mapping System
  66. Designing the ShapeEditor
  67. Prerequisites
  68. Setting up the database
  69. Setting up the ShapeEditor project
  70. Defining the ShapeEditor's applications
  71. Creating the shared application
  72. Defining the data models
  73. Playing with the admin system
  74. Summary
  75. 12. ShapeEditor – Importing and Exporting Shapefiles
  76. Importing shapefiles
  77. Exporting shapefiles
  78. Summary
  79. 13. ShapeEditor – Selecting and Editing Features
  80. Editing features
  81. Adding features
  82. Deleting features
  83. Deleting shapefiles
  84. Using the ShapeEditor
  85. Further improvements and enhancements
  86. Summary
  87. Index

Introducing PostGIS

In this book, we will be working with PostGIS. PostGIS is one of the most popular and powerful geospatial databases and has the bonus of being open source and freely available. PostGIS itself is actually an extension to the PostgreSQL relational database system—to use PostGIS from your Python programs, you first have to install and set up PostgreSQL, then install the PostGIS extension, and then finally install the psycopg2 database adapter for Python. The following illustration shows how all these pieces fit together:

Introducing PostGIS

Tip

Note that PostgreSQL is often referred to as Postgres. We will regularly use this more colloquial name throughout this book.

PostGIS allows you to store and query against various types of spatial data, including points, lines, polygons, and geometry collections. PostGIS provides two different types of spatial fields that can be used to store spatial data:

  • The geometry field holds spatial data that is assumed to be in a projected coordinate system. All calculations and queries for geometry fields assume that the spatial data has been projected onto a flat Cartesian plane. This makes the calculations much simpler, but it will only work reliably if the spatial data is in a projected coordinate system.
  • The geography field holds spatial data that uses geodetic (unprojected) coordinates. Calculations and queries against geography fields assume that the data is in angular units (that is, latitude and longitude values), using sophisticated mathematics to calculate lengths and areas using a spheroid model of the earth.

Because the mathematics involved is much more complicated, not all spatial functions are available for geography fields, and the operations often take a lot longer. However, geography fields are much easier to use if your spatial data uses an unprojected coordinate system such as WGS84.

Let's go ahead an install PostGIS onto your computer and then look at how we can use PostGIS to create and work with a spatial database using Python.

Installing PostgreSQL

PostgreSQL is an extremely powerful open source relational database system. The main web site for Postgres can be found at http://postgresql.org. How you install the Postgres database will depend on which operating system your computer is running:

  • For Linux, follow the instructions on the PostgreSQL download page (http://postgresql.org/download) to install Postgres onto your computer. Choose the appropriate link for your Linux distribution and you will be presented with the corresponding installation instructions.
  • For Mac OS X, you can download an installer for Postgres from the KyngChaos web site (http://www.kyngchaos.com/software/postgres). Make sure you don't download the client-only version, as you'll need the Postgres server. Once it has been downloaded, open the disk image and double click on the PostgreSQL.pkg package file to install Postgres into your computer.
  • For Microsoft Windows, you can download an installer for Postgres from http://enterprisedb.com/products-services-training/pgdownload. Select the appropriate installer for your version of Windows (32 or 64 bit), download the installer file, then simply double click on the installer and follow the instructions.

Once you have installed Postgres, you can check whether it is running by typing psql into a terminal or command-line window and pressing the Return key. All going well, you should see the Postgres command line:

psql (9.4.4)
Type "help" for help.

postgres=# 

Tip

If the psql command-line client can't be found, you may have to add it to your path. For example, on a Mac, you can edit your .bash_profile file and add the following:

export PATH="$PATH:/usr/local/pgsql/bin"

If the psql command complains about user authentication, you may need to identify the user account to use when connecting to Postgres, for example:

% psql -U postgres

Many Postgres installations have a postgres user, which you need to select with the -U command-line option when accessing the database. Alternatively, you may need to use sudo to run psql as root, or open a command prompt as an administrator if you are running Microsoft Windows.

To exit the Postgres command-line client, type \q and press Return.

Installing PostGIS

Our next task is to install the PostGIS spatial extension for Postgres. The main web site for PostGIS can be found at http://postgis.net. Once again, how you install PostGIS depends on which operating system you are running:

To check whether PostGIS has been successfully installed, try typing the following commands into your terminal window:

% createdb test_database
% psql -d test_database -c "CREATE EXTENSION postgis;"
% dropdb test_database

Tip

Don't forget to add the -U postgres option, or use sudo for each of these commands, if you need to run Postgres under a different user account.

The first command creates a new database, the second one enables the PostGIS extension for that database, and the third command deletes the database again. If this sequence of commands runs without any errors, then your PostGIS installation (and Postgres itself) is set up and running correctly.

Installing psycopg2

psycopg2 is the Python database adapter for Postgres. This is the Python library you use to access Postgres from within your Python programs. The main web site for psycopg2 can be found at http://initd.org/psycopg.

As usual, how you install psycopg2 will vary depending on which operating system you are using:

  • For Linux, you will need to install psycopg2 from source. For instructions on how to do this, refer to http://initd.org/psycopg/docs/install.html.
  • For a Mac OS X machine, you can use pip, the Python package manager, to install psycopg2 from the command line:
    pip install psycopg2
    

    Note that you will need to have the Xcode command-line tools installed so that psycopg2 can compile.

  • For MS Windows, you can download a double-clickable installer for psycopg2 from http://www.stickpeople.com/projects/python/win-psycopg.

To check whether your installation worked, start up your Python interpreter and type the following:

>>> import psycopg2
>>>

If psycopg2 was installed correctly, you should see the Python interpreter prompt reappear without any error messages, as shown in this example. If an error message does appear, you may need to follow the troubleshooting instructions on the psycopg2 web site.