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

Playing with the admin system

The built-in admin application is enabled by default in new Django projects. Before we can use it, however, we need to register the various database models we want it to support. To do this, edit the admin.py module within the shapeEditor/shared directory, and enter the following into this file:

from django.contrib.gis import admin
from shapeEditor.shared.models import *

admin.site.register(Shapefile, admin.ModelAdmin)
admin.site.register(Feature, admin.GeoModelAdmin)
admin.site.register(Attribute, admin.ModelAdmin)
admin.site.register(AttributeValue, admin.ModelAdmin)

The ModelAdmin class tells Django how to display the model within the admin interface. Notice that we use the GeoModelAdmin class for the Feature class. Because the Feature object includes geometry fields, using the GeoModelAdmin class allows the admin interface to edit these geometry fields using a slippy map. We'll see how this works shortly.

Now that the admin module has been configured, let's try running it. Type the following into your terminal or command-line window:

python manage.py runserver

This will start up the Django server for your project. Open a web browser and navigate to the following URL:

http://127.0.0.1:8000/admin/shared

You should see the Django administration login page:

Playing with the admin system

Enter the username and password for the superuser you created earlier, and you will see the main admin interface for the shapeEditor.shared application:

Playing with the admin system

Let's use this admin interface to create a dummy shapefile. Click on the Add link in the Shapefiles row, and you will be presented with a basic input screen for entering a new shapefile:

Playing with the admin system

Enter some dummy values into the various fields (it doesn't matter what you enter), and click on the Save button to save the new Shapefile object into the database. A list of the shapefiles that are present in the database will be shown. At the moment, there is only the Shapefile record you just created:

Playing with the admin system

As you can see, the new shapefile object has been given a rather unhelpful label: Shapefile object. This is because we haven't yet told Django how to label the shapefile. To fix this, edit the shared.models file and add the following method to the end of the Shapefile class definition:

def __str__(self):
    return self.filename

The __str__ method returns a human-readable summary of the Shapefile object's contents. In this case, we are showing the filename associated with the shapefile. If you then reload the web page, you can see that the shapefile now has a useful label:

Playing with the admin system

Go ahead and add the __str__ method to the other model objects as well:

class Attribute(models.Model):
    ...
    def __str__(self):
        return self.name

class Feature(models.Model):
    ...
    def __str__(self):
        return str(self.id)

class AttributeValue(models.Model):
    ...
    def __str__(self):
        return self.value

While this may seem like busywork, it's actually quite useful for your database objects to be able to describe themselves. If you wanted to, you could further customize the admin interface, for example, by showing the attributes and features associated with the selected shapefile. For now, though, let's take a look at GeoDjango's built-in geometry editors.

Go back to the shared application's administration page (by clicking on the Shared hyperlink near the top of the window), and click on the Add button in the Features row. As with the shapefile, you will be asked to enter the details for a new feature. This time, however, the admin interface will use a slippy map to enter each of the different geometry types supported by the Feature object:

Playing with the admin system

Obviously, having multiple slippy maps like this isn't quite what we want, and if we wanted to, we could set up a custom GeoModelAdmin subclass to avoid this, but that's not important right now. Instead, try selecting the shapefile to associate with this feature by choosing the shapefile you created from the Shapefile menu, and then scroll down to the Geom multipolygon field and try adding a polygon or two to the map. To do this, click on the map to start drawing a new polygon, click repeatedly to add points to the current polygon, or hold down the Shift key and click to finish creating a polygon. The interface can be a bit confusing at first, but it's certainly usable. We'll look at the various options for editing polygons later. For now, just click on the Save button at the very bottom of the page to save your new feature. If you edit it again, you'll see your saved geometry (or geometries) once again on the slippy maps.

This completes our tour of the admin interface. We won't be using this for end users, as we don't want to require users to log in before making changes to the shapefile data. We will, however, be borrowing some code from the admin application so that end users can edit their shapefile's features using a slippy map.