Python Geospatial Development


There is a newer version of this book available - Python Geospatial Development - Second Edition
Python Geospatial Development
eBook: $29.99
Formats: PDF, PacktLib, ePub and Mobi formats
$25.49
save 15%!
Print + free eBook + free PacktLib access to the book: $79.98    Print cover: $49.99
$49.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • Build applications for GIS development using Python
  • Analyze and visualize Geo-Spatial data
  • Comprehensive coverage of key GIS concepts
  • Recommended best practices for storing spatial data in a database
  • Draw maps, place data points onto a map, and interact with maps
  • A practical tutorial with plenty of step-by-step instructions to help you develop a mapping application from scratch

Book Details

Language : English
Paperback : 508 pages [ 235mm x 191mm ]
Release Date : December 2010
ISBN : 1849511543
ISBN 13 : 9781849511544
Author(s) : Erik Westra
Topics and Technologies : All Books, Application Development, Open Source, Python

Table of Contents

Preface
Chapter 1: Geo-Spatial Development Using Python
Chapter 2: GIS
Chapter 3: Python Libraries for Geo-Spatial Development
Chapter 4: Sources of Geo-Spatial Data
Chapter 5: Working with Geo-Spatial Data in Python
Chapter 6: GIS in the Database
Chapter 7: Working with Spatial Data
Chapter 8: Using Python and Mapnik to Generate Maps
Chapter 9: Web Frameworks for Python Geo-Spatial Development
Chapter 10: Putting it All Together: A Complete Mapping Application
Chapter 11: ShapeEditor: Implementing List View, Import, and Export
Chapter 12: ShapeEditor: Selecting and Editing Features
Index
    • Chapter 2: GIS
      • Core GIS concepts
        • Location
        • Distance
        • Units
        • Projections
          • Cylindrical projections
          • Conic projections
          • Azimuthal projections
          • The nature of map projections
        • Coordinate systems
        • Datums
        • Shapes
      • GIS data formats
      • Working with GIS data manually
      • Summary
      • Chapter 3: Python Libraries for Geo-Spatial Development
        • Reading and writing geo-spatial data
          • GDAL/OGR
            • GDAL design
            • GDAL example code
            • OGR design
            • OGR example code
          • Documentation
          • Availability
        • Dealing with projections
          • pyproj
          • Design
            • Proj
            • Geod
          • Example code
          • Documentation
          • Availability
        • Analyzing and manipulating geo-spatial data
          • Shapely
          • Design
          • Example code
          • Documentation
          • Availability
        • Visualizing geo-spatial data
          • Mapnik
          • Design
          • Example code
          • Documentation
          • Availability
        • Summary
        • Chapter 4: Sources of Geo-Spatial Data
          • Sources of geo-spatial data in vector format
            • OpenStreetMap
              • Data format
              • Obtaining and using OpenStreetMap data
            • TIGER
              • Data format
              • Obtaining and using TIGER data
            • Digital Chart of the World
              • Data format
              • Available layers
              • Obtaining and using DCW data
            • GSHHS
              • Data format
              • Obtaining the GSHHS database
            • World Borders Dataset
              • Data format
              • Obtaining the World Borders Dataset
          • Sources of geo-spatial data in raster format
            • Landsat
              • Data format
              • Obtaining Landsat imagery
            • GLOBE
              • Data format
              • Obtaining and using GLOBE data
            • National Elevation Dataset
              • Data format
              • Obtaining and using NED data
          • Sources of other types of geo-spatial data
            • GEOnet Names Server
              • Data format
              • Obtaining and using GEOnet Names Server data
            • GNIS
              • Data format
              • Obtaining and using GNIS data
          • Summary
          • Chapter 5: Working with Geo-Spatial Data in Python
            • Prerequisites
            • Reading and writing geo-spatial data
              • Task: Calculate the bounding box for each country in the world
              • Task: Save the country bounding boxes into a Shapefile
              • Task: Analyze height data using a digital elevation map
            • Changing datums and projections
              • Task: Change projections to combine Shapefiles using geographic and UTM coordinates
              • Task: Change datums to allow older and newer TIGER data to be combined
            • Representing and storing geo-spatial data
              • Task: Calculate the border between Thailand and Myanmar
              • Task: Save geometries into a text file
            • Working with Shapely geometries
              • Task: Identify parks in or near urban areas
            • Converting and standardizing units of geometry and distance
              • Task: Calculate the length of the Thai-Myanmar border
              • Task: Find a point 132.7 kilometers west of Soshone, California
            • Exercises
            • Summary
            • Chapter 6: GIS in the Database
              • Spatially-enabled databases
              • Spatial indexes
              • Open source spatially-enabled databases
                • MySQL
                • PostGIS
                  • Installing and configuring PostGIS
                  • Using PostGIS
                  • Documentation
                  • Advanced PostGIS features
                • SpatiaLite
                  • Installing SpatiaLite
                  • Installing pysqlite
                  • Accessing SpatiaLite from Python
                  • Documentation
                  • Using SpatiaLite
                  • SpatiaLite capabilities
              • Commercial spatially-enabled databases
                • Oracle
                • MS SQL Server
              • Recommended best practices
                • Use the database to keep track of spatial references
                • Use the appropriate spatial reference for your data
                  • Option 1: Use a database that supports geographies
                  • Option 2: Transform features as required
                  • Option 3: Transform features from the outset
                  • When to use unprojected coordinates
                • Avoid on-the-fly transformations within a query
                • Don't create geometries within a query
                • Use spatial indexes appropriately
                • Know the limits of your database's query optimizer
                  • MySQL
                  • PostGIS
                  • SpatiaLite
              • Working with geo-spatial databases using Python
                • Prerequisites
                • Working with MySQL
                • Working with PostGIS
                • Working with SpatiaLite
                • Speed comparisons
              • Summary
              • Chapter 7: Working with Spatial Data
                • About DISTAL
                • Designing and building the database
                • Downloading the data
                  • World Borders Dataset
                  • GSHHS
                  • Geonames
                  • GEOnet Names Server
                • Importing the data
                  • World Borders Dataset
                  • GSHHS
                  • US placename data
                  • Worldwide placename data
                • Implementing the DISTAL application
                  • The "Select Country" script
                  • The "Select Area" script
                    • Calculating the bounding box
                    • Calculating the map's dimensions
                    • Setting up the datasource
                    • Rendering the map image
                  • The "Show Results" script
                    • Identifying the clicked-on point
                    • Identifying features by distance
                    • Displaying the results
                • Application review and improvements
                  • Usability
                  • Quality
                    • Placename issues
                    • Lat/Long coordinate problems
                  • Performance
                    • Finding the problem
                    • Improving performance
                    • Calculating the tiled shorelines
                    • Using the tiled shorelines
                    • Analyzing the performance improvement
                    • Further performance improvements
                  • Scalability
                • Summary
                • Chapter 8: Using Python and Mapnik to Generate Maps
                  • Introducing Mapnik
                  • Creating an example map
                  • Mapnik in depth
                    • Data sources
                      • Shapefile
                      • PostGIS
                      • GDAL
                      • OGR
                      • SQLite
                      • OSM
                      • PointDatasource
                    • Rules, filters, and styles
                      • Filters
                      • Scale denominators
                      • "Else" rules
                    • Symbolizers
                      • Drawing lines
                      • Drawing polygons
                      • Drawing labels
                      • Drawing points
                      • Drawing raster images
                      • Using colors
                    • Maps and layers
                      • Map attributes and methods
                      • Layer attributes and methods
                    • Map rendering
                  • MapGenerator revisited
                    • The MapGenerator's interface
                    • Creating the main map layer
                    • Displaying points on the map
                    • Rendering the map
                    • What the map generator teaches us
                  • Map definition files
                  • Summary
                  • Chapter 9: Web Frameworks for Python Geo-Spatial Development
                    • Web application concepts
                      • Web application architecture
                        • A bare-bones approach
                        • Web application stacks
                        • Web application frameworks
                        • Web services
                      • Map rendering
                      • Tile caching
                      • Web servers
                      • User interface libraries
                      • The "slippy map" stack
                      • The geo-spatial web application stack
                    • Protocols
                      • The Web Map Service (WMS) protocol
                        • WMS-C
                      • The Web Feature Service (WFS) protocol
                      • The TMS (Tile Map Service) protocol
                    • Tools
                      • Tile caching
                        • TileCache
                        • mod_tile
                        • TileLite
                      • User interface libraries
                        • OpenLayers
                        • Mapiator
                      • Web application frameworks
                        • GeoDjango
                        • MapFish
                        • TurboGears
                    • Summary
                    • Chapter 10: Putting it All Together: A Complete Mapping Application
                      • About the ShapeEditor
                      • Designing the application
                        • Importing a Shapefile
                        • Selecting a feature
                        • Editing a feature
                        • Exporting a Shapefile
                      • Prerequisites
                      • The structure of a Django application
                        • Models
                        • Views
                        • Templates
                      • Setting up the database
                      • Setting up the GeoDjango project
                      • Setting up the ShapeEditor application
                      • Defining the data models
                        • Shapefile
                        • Attribute
                        • Feature
                        • AttributeValue
                        • The models.py file
                      • Playing with the admin system
                      • Summary
                      • Chapter 11: ShapeEditor: Implementing List View, Import, and Export
                        • Implementing the "List Shapefiles" view
                        • Importing Shapefiles
                          • The "import shapefile" form
                          • Extracting the uploaded Shapefile
                          • Importing the Shapefile's contents
                            • Open the Shapefile
                            • Add the Shapefile object to the database
                            • Define the Shapefile's attributes
                            • Store the Shapefile's features
                            • Store the Shapefile's attributes
                          • Cleaning up
                        • Exporting Shapefiles
                          • Define the OGR Shapefile
                          • Saving the features into the Shapefile
                          • Saving the attributes into the Shapefile
                          • Compressing the Shapefile
                          • Deleting temporary files
                          • Returning the ZIP archive to the user
                        • Summary
                        • Chapter 12: ShapeEditor: Selecting and Editing Features
                          • Selecting a feature to edit
                            • Implementing the Tile Map Server
                              • Setting up the base map
                              • Tile rendering
                            • Using OpenLayers to display the map
                            • Intercepting mouse clicks
                            • Implementing the "find feature" view
                          • Editing features
                          • Adding features
                          • Deleting features
                          • Deleting Shapefiles
                          • Using ShapeEditor
                          • Further improvements and enhancements
                          • Summary

                          Erik Westra

                          Erik Westra has been a professional software developer for over 25 years, and has worked almost exclusively in Python for the past decade. Erik's early interest in graphical user-interface design led to the development of one of the most advanced urgent courier dispatch systems used by messenger and courier companies worldwide. In recent years, Erik has been involved in the design and implementation of systems matching seekers and providers of goods and services across a range of geographical areas. This work has included the creation of real-time geocoders and map-based views of constantly changing data. Erik is based in New Zealand, and works for companies worldwide

                          Sorry, we don't have any reviews for this title yet.

                          Code Downloads

                          Download the code and support files for this book.


                          Submit Errata

                          Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                          Errata

                          - 17 submitted: last submission 12 Dec 2013

                          Errata type: code | Page number: 43

                          Error in code formatting: if results['north'] and if results['south'] should be inside the for loop. Additionally, at the end of this sample, geometry = ... should be geometry = feature.GetGeometryRef().GetGeometryRef(6).GetGeometryRef(0) to get to the actual object

                          Errata type: code | Page number: 40

                          Code formatting error: the last print statement should be inside the last for loop instead of outside of it. Otherwise it just outputs the last Feature's name and number (Texas) instead of each Feature's name and number.

                          Errata type: Others | Page number: 183 | Errata date: 08 Sep 2011

                          a) The problem is with the data of levels 1 and 2, but the exception should be caught and a more user-friendly message should be printed: psycopg2.InternalError: geometry contains non-closed rings HINT: "...000000005,-180.0 70.962072000000006))" <-- parse error at position 334 within geometry A quick solution is to skip levels 1 and 2: for level in [3, 4]: b) When a) is applied, no results will be found for LONDON which is in level 1. Thus another exception is generated because shoreline is None: Traceback (most recent call last): File "PostGIS.py", line 65, in f.write(shoreline) TypeError: argument 1 must be string or read-only character buffer, not None A quick solution is to test whether shoreline is None before you try writing it: if shoreline: f.write(shoreline) c) The file uk-shoreline.wkt will be empty since London is in level 1 but we skipped the first 2 levels. Thus we need to update the point to have valid values found in either level 3 or 4. I used Quantum GIS to open the level 3 shapefile and get the coordinates of a random point: PLACE = 'POINT(-82.164 45.779)' cursor.execute("SELECT id,AsText(geom) FROM gshhs " + "WHERE (level=%s) AND " + "(ST_Contains(geom, GeomFromText(%s, 4326)))", (3, PLACE))

                           

                          Errata type: Others | Page number: 26 | Errata date: 16th May, 2011

                          The great circle distance is the length of a semicircle going between two 

                          points on the surface of the earth, where the semicircle is centered around the middle 

                          of the earth:



                          Should be


                           

                          The great circle distance is the length of the arc going between two points 

                          on the surface of the earth, where the arc is centered around the middle 

                          of the earth:


                           

                           

                          Errata type: Others | Page number: 148 | Errata date: 16th May, 2011

                          And, because very geometry is reduced to a simple

                          bounding box, R-Trees can support any type of geometry, not just polygons

                          Should be:

                          And, because every geometry is reduced to a simple

                          bounding box, R-Trees can support any type of geometry, not just polygons

                           


                           

                           

                           

                           

                           

                           

                           

                           

                           

                          Errata Type:Code Page Nos:197, 202

                          4326, 'POLYGON', 2) should be 4326, 'GEOMETRY', 2) since 'outline' column of 'countries' table should accommodate both POLYGON and MULTIPOLYGON from TM_WORLD_BORDERS-0.3.shp. And, accordingly, on page 202, last 3rd line: "VALUES (%s, ST_PolygonFromText(%s, " + should be "VALUES (%s, ST_GeomFromText(%s, " + On page 202, last 4th line: Indentation is required (i.e., cursor.execute command should be inside for loop).

                          Errata Type:Code Page No:249

                          In the DISTAL tiling process, the intersection creates multipolygons of the shorelines and the table created in the DB is for polygons. The table creation code should be (for PostGIS): cursor.execute("""SELECT AddGeometryColumn('tiled_shorelines', 'outline', 4326, 'MULTIPOLYGON', 2)""") The code to save the results in the DB should also be changed by using ST_Multi to make sure that in the event that a Polygon is created, it is converted to a MultiPolygon to fit with the geometry constraints, the SRID should also be added. (For PostGIS) cursor.execute("INSERT INTO tiled_shorelines " + "(intLat, intLong, outline) " + "VALUES (%s, %s, ST_Multi(ST_GeomFromText(%s, 4326)))"

                          Errata Type:Code Page Nos: 250,251

                          int(round(startLat)) int(round(startLong)) should be int(startLat) int(startLong) (as stated by Author on page 242)

                          Errata Type:Code Page Nos: 247, 248

                          Upper bound of range for iLong should be maxLong instead of maxLong+1 (appears twice, on page 247 and 248) Upper bound of range for iLat should be maxLat instead of maxLat+1 (appears once, on page 248)

                          Errata Type:Code Page Nos: 106

                          replace dstLayer = dstFile.CreateLayer ("layer", spatialReference), with dstLayer = dstFile.CreateLayer( "point_out", spatialReference, osgeo.ogr.wkbPolygon)

                          Errata Type:Code Page Nos: 250, 251

                          int(round(startLat)) int(round(startLong)) , should be int(startLat) int(startLong) , (as stated by Author on page 242)

                          Errata Type:Code Page Nos: 183

                          a) The problem is with the data of levels 1 and 2, but the exception should be caught and a more user-friendly message should be printed: psycopg2.InternalError: geometry contains non-closed rings HINT: "...000000005,-180.0 70.962072000000006))" <-- parse error at position 334 within geometry A quick solution is to skip levels 1 and 2: for level in [3, 4]: b) When a) is applied, no results will be found for LONDON which is in level 1. Thus another exception is generated because shoreline is None: Traceback (most recent call last): File "PostGIS.py", line 65, in f.write(shoreline) TypeError: argument 1 must be string or read-only character buffer, not None A quick solution is to test whether shoreline is None before you try writing it: if shoreline: f.write(shoreline) c) The file uk-shoreline.wkt will be empty since London is in level 1 but we skipped the first 2 levels. Thus we need to update the point to have valid values found in either level 3 or 4. I used Quantum GIS to open the level 3 shapefile and get the coordinates of a random point: PLACE = 'POINT(-82.164 45.779)' cursor.execute("SELECT id,AsText(geom) FROM gshhs " + "WHERE (level=%s) AND " + "(ST_Contains(geom, GeomFromText(%s, 4326)))", (3, PLACE))

                          Errata Type:Code Page Nos: 244, 245

                          On page 244, after midLong = (minLong + maxLong) / 2.0 insert cirLong = 2 * geod.inv(0,midLat,+180,midLat)[2] to calculate horizontal circumference. Then, after availDistance = geod.inv(maxLong, midLat, +180, midLat)[2] insert if maxLong<0: availDistance = cirLong - availDistance Likewise, on page 245, after availDistance = geod.inv(minLong,midLat,-180 midLat)[2] insert if minLong>0: availDistance = cirLong - availDistance

                          Errata Type:Code        Page Nos: 245

                          8th line: availDistance = geod.inv(maxLong, midLat, -180, should be availDistance = geod.inv(minLong, midLat, -180, (i.e., maxLong should be minLong)

                          Errata Type:Typo nbsp;  Page Nos: 438

                          The mapnik data sources should use host information when the database server is running on a remote host. Otherwise the code runs properly only when postgres is installed locally. Example: datasource = mapnik.PostGIS(user=dbSettings['USER'], password=dbSettings['PASSWORD'], dbname=dbSettings['NAME'], table=query, srid=4326, geometry_field=geometryField, geometry_table='"shapeEditor_feature"', host=dbSettings['HOST'])

                          Errata Type:Typo Page Nos: 26

                          Last para, second sentence. The great circle distance is the length of the arc going between two points ... where the arc is centred ... (not semicircle)?

                          Errata Type:Typo Page Nos:148

                          In the block of text below the second picture at line 5: ..."because very geometry" should read ..."because every geometry"...

                          Errata type : Code | Page no 179

                          In the section "Working with MySQL", in order to create a database you need to add the following lines right after line 3 cursor = connection.cursor():

                          cursor.execute("DROP DATABASE IF EXISTS myDatabase")

                          cursor.execute("CREATE DATABASE myDatabase")

                           

                          Also, in the CREATE TABLE line, a comma (,) is missing after PRIMARY KEY (id). It should also specify engine MyISAM:

                          cursor.execute("""CREATE TABLE gshhs (

                                                  id INTEGER AUTO_INCREMENT,

                                                  level INTEGER,

                                                  geom POLYGON NOT NULL,

                                                  PRIMARY KEY (id),

                                                 INDEX (level),

                                                 SPATIAL INDEX (geom)) ENGINE=MyISAM

                                          """)

                          Sample chapters

                          You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                          Frequently bought together

                          Python Geospatial Development +    Oracle APEX 4.0 Cookbook =
                          50% Off
                          the second eBook
                          Price for both: $45.60

                          Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                          What you will learn from this book

                          • Develop applications for GIS development using the Python programming language
                          • Get to grips with the process of accessing, manipulating, and displaying geo-spatial data
                          • Understand some of the major data formats you are likely to encounter when working with geo-spatial data
                          • Analyze and manipulate geo-spatial data directly within your Python programs
                          • Use powerful Python-based tools for converting geo-spatial data into good-looking maps
                          • Learn to read and write to geo-spatial data in both vector and raster format
                          • Represent, transfer, and store geo-spatial data using the Well-Known Text (WKT) format
                          • Work efficiently with geo-spatial databases using Python
                          • Solve complex, real-world geo-spatial problems in your applications using the three spatial databases MySQL, PostGIS, and SpatialLite
                          • Explore some of the frameworks available for creating web-based geo-spatial applications
                          • Get in touch with major applications and recent trends in the field of Geo-Spatial development

                          In Detail

                          Open Source GIS (Geographic Information System) is a growing area with the explosion of applications such as Google Maps, Google Earth, and GPS. The GIS market is growing rapidly and as a Python developer you will find yourself either wanting grounding in GIS or needing to get up to speed to do your job. In today's location-aware world, all commercial Python developers can benefit from an understanding of GIS development gained using this book.

                          Working with geo-spatial data can get complicated because you are dealing with mathematical models of the Earth's surface. Since Python is a powerful programming language with high-level toolkits, it is well suited to GIS development. will familiarize you with the Python tools required for geo-spatial development such as Mapnik, which is used for mapping in Python. It introduces GIS at the basic level with a clear, detailed walkthrough of the key GIS concepts such as location, distance, units, projections, datums, and GIS data formats. We then examine a number of Python libraries and combine these with geo-spatial data to accomplish a variety of tasks. The book provides an in-depth look at the concept of storing spatial data in a database and how you can use spatial databases as tools to solve a variety of geo-spatial problems.

                          It goes into the details of generating maps using the Mapnik map-rendering toolkit, and helps you to build a sophisticated web-based geo-spatial map-editing application using GeoDjango, Mapnik, and PostGIS. By the end of the book, you will be able to integrate spatial features into your applications and build a complete mapping application from scratch.

                          A hands-on tutorial about accessing, manipulating, and displaying Geo-Spatial data efficiently using a range of Python tools for GIS development

                          Approach

                          This is a tutorial style book that will teach usage of Python tools for GIS using simple practical examples and then show you how to build a complete mapping application from scratch. The book assumes basic knowledge of Python. No knowledge of Open Source GIS is required.

                          Who this book is for

                          This book is useful for Python developers who want to get up to speed with Open Source GIS in order to build GIS applications or integrate Geo-Spatial features into their applications.

                          Code Download and Errata
                          Packt Anytime, Anywhere
                          Register Books
                          Print Upgrades
                          eBook Downloads
                          Video Support
                          Contact Us
                          Awards Voting Nominations Previous Winners
                          Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                          Resources
                          Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software