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

Chapter 7. Using Python and Mapnik to Generate Maps

Because geospatial data is almost impossible to understand until it is displayed, the use of maps to visually represent spatial data is an extremely important topic. In this chapter, we will look at Mapnik, a powerful Python library for transforming geospatial data into great-looking maps. In particular, we will look at:

  • The underlying concepts Mapnik uses to generate maps
  • An example program using Mapnik to generate a straightforward map
  • Various data sources that you can use to add geospatial data to your map
  • How to use rules, filters, and styles to control the map-generation process
  • The "symbolizers" you can use to add points, lines, polygons, textual labels, and raster images to your maps
  • How maps and map layers work together to create a map
  • Ways of rendering a map into an image file

Introducing Mapnik

We first looked at Mapnik in Chapter 3, Python Libraries for Geospatial Development. If you haven't already done so, please go back to the Mapnik section of that chapter and follow the instructions for installing it onto your computer.

Mapnik is a complex library with many different parts, and it is easy to get confused by the various names and concepts. Let's start our exploration of Mapnik by looking at a simple map:

Introducing Mapnik

One thing that may not be immediately obvious is that the various elements within the map are layered, like this:

Introducing Mapnik

To generate this map, you have to tell Mapnik to initially draw the background, then the polygons, and finally the labels. This ensures that the polygons sit on top of the background and the labels appear in front of both the polygons and the background.

Note

Strictly speaking, the background isn't a layer. It's simply a color or image that Mapnik draws onto the map before it starts drawing the first layer.

Mapnik allows you to control the order in which the map elements are drawn through the use of Layer objects. A simple map may consist of just one layer, but most maps have multiple layers. The layers are drawn in a strict back-to-front order, so the first layer you define will appear behind all the others. In the example map we just looked at, the Polygons layer would be defined first, followed by the Labels layer. This ensures that the labels appear in front of the polygons. This layering approach is called the painter's algorithm because of its similarity to placing layers of paint onto an artist's canvas.

Each Layer has its own data source, which tells Mapnik where to load the data from. A data source can refer to a shapefile, a spatial database, a raster image file, or any number of other geospatial data sources. In most cases, setting up a Layer's data source is very easy.

Within each Layer, the visual display of the geospatial data is controlled through something called a symbolizer. While there are many different types of symbolizers available within Mapnik, three symbolizers are of interest to us here:

  • A PolygonSymbolizer is used to draw filled polygons:
    Introducing Mapnik
  • A LineSymbolizer is used to draw the outline of polygons as well as to draw LineStrings and other linear features:
    Introducing Mapnik
  • A TextSymbolizer is used to draw labels and other text onto a map:
    Introducing Mapnik

In many cases, these three symbolizers will be enough to draw an entire map. Indeed, almost all of the preceding map was produced using just one PolygonSymbolizer, one LineSymbolizer, and one TextSymbolizer, as shown here:

Introducing Mapnik

Within each layer, the symbolizers are processed using the same painter's algorithm described earlier. In the case of the "Polygon" layer, the LineSymbolizer would be drawn on top of the PolygonSymbolizer.

Notice that the symbolizers aren't associated directly with a layer. Rather, there is an indirect association of symbolizers with a layer through the use of styles and rules. We'll look at styles in a minute, but for now, let's take a closer look at the concept of a Mapnik rule.

A rule allows a set of symbolizers to be applied only when a given condition is met. For example, the map at the start of this chapter displayed Angola in a different color. This was done by defining two rules within the "Polygons" layer, like this:

Introducing Mapnik

The first rule has a filter that only applies to features that have a NAME attribute equal to the value "Angola". For features that match this filter condition, the rule's PolygonSymbolizer will be used to draw the feature in dark red.

The second rule has a similar filter, this time checking for features that don't have a NAME attribute equal to "Angola". These features are drawn using the second rule's PolygonSymbolizer, which draws the features in dark green.

Obviously, rules are a very powerful tool for selectively changing the way features are displayed on a map. We'll be looking at rules in more detail in the Rules, filters, and styles section of this chapter.

When you define your symbolizers, you place them into rules. The rules themselves are grouped into styles, which can be used to organize and keep track of your various rules. Each map layer has a list of the styles that apply to that particular layer.

While the relationship between layers, styles, rules, filters, and symbolizers may seem complicated, it also provides much of Mapnik's power and flexibility. It is important that you understand how these various classes work together:

Introducing Mapnik

As you can see, the style definitions are stored directly within the map, while the various layers use style references to identify which of the defined styles each layer will use. This works in much the same way as a stylesheet in a word processing document, where you define styles and use them again and again. Note that the same style can be used in multiple layers.

Note

Mapnik provides an alternative way of defining your various styles, though the use of an XML-format map definition file. We will not be using map definition files in this book, however, as the XML-format files are rather hard to read. It is also easier to create your map definitions directly using Python code.