Each spatial feature type should provide a classmethod named mapnik_style which returns a mapnik Style object for that model. The Style object can contain rules for classification as well as symbolizers for controling the appearance of points, lines, polygons and labels:
class Mpa(PolygonFeature):
....
@classmethod
def mapnik_style(self):
polygon_style = mapnik.Style()
ps = mapnik.PolygonSymbolizer(mapnik.Color('white'))
ps.fill_opacity = 0.5
ls = mapnik.LineSymbolizer(mapnik.Color('#555555'),0.75)
ls.stroke_opacity = 0.5
r = mapnik.Rule()
r.symbols.append(ps)
r.symbols.append(ls)
polygon_style.rules.append(r)
return polygon_style
The madrona.features app contains a generic link view that leverages staticmap to create PNG Download links for all Features.
Madrona ships with several example datasets. These default datasets are in ESRI Shapefile format and stored in [[MEDIA_ROOT]]/staticmap/data/.
In addition there are django fixtures (ie initial data to populate the database)
These models contain polygon fields and are represented as postgis geometry fields in the database. Since mapnik can also render data from a postgis data source, these are added to the default map.
The staticmap application uses Mapnik to render map images from spatial data. Specifically, we use the mapnik XML files to configure the spatial data sources and their styling. If you are unfamiliar with Mapnik, we suggest going over the XML Configuration Tutorial first.
The default mapnik XML config file (<MEDIA_ROOT>/staticmap/socal.xml) is a good starting point. You will need to add two main XML elements in order to set up any additional data for the staticmap
This element defines the path/connection to the data source, the spatial reference system of the input data and the name of the style to use:
<Layer name="world" srs="+proj=latlong +datum=WGS84">
<StyleName>My Style</StyleName>
<Datasource>
<Parameter name="type">shape</Parameter>
<Parameter name="file">/path/to/your/world_borders</Parameter>
</Datasource>
</Layer>
This element defines “rules” which determine the colors, symbology, classification and filtering of the data. In the simplest case, you will simply define a single style for all features in the layer:
<Style name="My Style">
<Rule>
<PolygonSymbolizer fill="steelblue" />
<LineSymbolizer stroke="rgb(5%,5%,5%)" stroke-width="1" />
</Rule>
</Style>
As an alternative to editing xml text files, you can try Quantumnik which allows you to use Quantum GIS to style the layers in a familiar GIS graphic interface and export the map as a mapnik XML.
You may not always know the full path to a local datasource and using relative paths is problematic since deployment details will change the current working directory. To avoid hardcoding paths, you can set use variable substitution to leverage the MEDIA_ROOT setting:
<Parameter name="file">[[MEDIA_ROOT]]/world_borders</Parameter>
staticmap will load the mapfile text and replace [[MEDIA_ROOT]] with settings.MEDIA_ROOT before mapnik loads the mapfile.
The spatial reference system (SRS) of each layer should be explicitly defined using a proj4 string. Its important to note that this is the SRS of the original data source - it is not necessarily the SRS of the output map. If the SRS defined in the Map element differs from the Layer SRS, mapnik will reproject each Layer to the common SRS of the Map.
If you create a new mapnik xml map, you’ll need to register it with your django project. First, login to the django admin site and navigate to Home › Staticmap › Map configs › Add map config. Here you will define the short n name of your new map, the initial map dimensions and the path to the xml file.
To access your new map, hit http://your.domain.com/staticmap/yourmapname
The staticmap can be accessed by url template tags:
<img align="top" src="{% url 'staticmap-show' 'default' %}">
The staticmap view also takes several parameters to configure the map
- uids : a comma-seperated list of feature uids. If the feature uid is a feature collection, staticmap will attempt to draw all features contained in that collection.
- width and height : Dimensions of the output image (in pixels)
- bbox : a comma-seperated list of four floating point values representing the geographic extent of the output map (ie minx,miny,maxx,maxy)
- autozoom : set to ‘True’ if you need the map to automatically set the geographic extent according to the selected features.
- show_extent : set to ‘True’ if you need to show a box indicating the extent of selected features. Useful for overview maps.
You could incorporate these into a django template as follows:
<img align="top" src="{% url 'staticmap-show' 'default' %}?uids={{ feature.uid }}&width=154&height=200&show_extent=True">