lwu (owner)

Revisions

  • fb85f8 lwu Wed Sep 03 19:29:51 -0700 2008
  • 3a96f5 lwu Wed Sep 03 19:29:31 -0700 2008
  • 212871 lwu Wed Sep 03 19:29:01 -0700 2008
  • 6eef30 Wed Sep 03 19:21:17 -0700 2008
gist: 8702 Download_button fork
public
Public Clone URL: git://gist.github.com/8702.git
README
1
2
3
4
5
This Python code snippet demonstrates basic use of the proposed "PointDatasource"
object exposed by the Mapnik patch attached here: http://trac.mapnik.org/ticket/113
 
To run this code, you will need to grab a copy of Mapnik from trunk, apply this patch,
and also download the style.xml file below.
sea.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python
#
# Leslie Wu
#
# sea.py has code portions adapted from rundemo.py (part of Artem's Mapnik/demo) and cali.cpp
 
try:
    from mapnik import *
except:
    print "Couldn't import mapnik libraries!"
    raise
 
m = Map(800,600,"+proj=latlong +ellps=WGS84")
m.background = Color(220, 226, 240)
 
# Layers are added in stacking order (i.e. bottom layer first)
 
state_lyr = Layer('States')
state_lyr.datasource = Shapefile(file='../data/statesp020')
state_lyr.styles.append('states')
m.layers.append(state_lyr)
 
strict = True
load_map(m, "style.xml", strict) # Load XML
 
ds = state_lyr.datasource
q = Query(state_lyr.envelope(), 1.0)
q.add_property_name('STATE') # Without this call, no properties will be found
 
extent = Envelope()
featureset = ds.features(q) # Zoom into WA by querying feature geometry
 
feat = featureset.next()
filt = Filter("[STATE] = 'Washington'")
 
extent = Envelope()
while (feat):
    if filt.passes(feat): # note 'pass' is a reserved word in Python
        for i in range(feat.num_geometries()):
            geom = feat.get_geometry(i)
            if (extent.width() < 0 and extent.height() < 0):
                extent = geom.envelope()
            extent.expand_to_include(geom.envelope())
 
    feat = featureset.next()
 
print extent
 
# Add map points!
pds = PointDatasource()
pds.add_point(-121.76, 46.85, "name", "Mount Rainier") # via Wikipedia
pds.add_point(-121.49, 46.20, "name", "Mount Adams")
pds.add_point(-121.81, 48.78, "name", "Mount Baker")
# ...
 
mtn_layer = Layer('Mountains')
mtn_layer.datasource = pds
mtn_layer.styles.append('mountains')
mtn_layer.styles.append('mountain_labels')
m.layers.append(mtn_layer)
 
# Set the initial extent of the map.
 
m.zoom_to_box(extent)
m.zoom(1.05)
# Render
 
im = Image(m.width,m.height)
render(m, im)
 
# Save image to files
im.save('sea.png', 'png') # true-colour RGBA
 
style.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE Map>
<!-- Sample Mapnik XML -->
<Map srs="+proj=latlong +datum=WGS84 +k=1.0 +units=m +over +no_defs">
  <Style name="states">
    <Rule>
      <Filter>[STATE] = 'Washington'</Filter>
      <PolygonSymbolizer>
<CssParameter name="fill">#cfdea7</CssParameter>
      </PolygonSymbolizer>
    </Rule>
    <Rule>
      <Filter>[STATE] <> 'Washington'</Filter>
      <LineSymbolizer>
<CssParameter name="fill">#888888</CssParameter>
        <CssParameter name="stroke-opacity">0.25</CssParameter>
        <CssParameter name="stroke-dasharray">10,6</CssParameter>
      </LineSymbolizer>
    </Rule>
  </Style>
 
  <Style name="mountains">
    <Rule>
      <PointSymbolizer file="../viewer/images/info.png" type="png" width="32" height="32" allow_overlap="true"/>
    </Rule>
  </Style>
  <Style name="mountain_labels">
    <Rule>
      <TextSymbolizer name="name" face_name="DejaVu Sans Book" size="12" fill="black" halo_fill="#DFDBE3" allow_overlap="true" dx="64" dy="-8"/>
    </Rule>
  </Style>
  
</Map>