IntegrateWithWxPython: demo.py

File demo.py, 1.6 kB (added by victorlin, 20 months ago)
Line 
1"""
2This is a simple wxPython application demonstrates how to
3integrate mapnik, it do nothing but draw the map from the World Poplulation XML
4example:
5http://trac.mapnik.org/wiki/XMLGettingStarted
6
7Victor Lin. (bornstub@gmail.com)
8Blog http://blog.ez2learn.com
9
10"""
11
12import wx
13import mapnik
14
15class Frame(wx.Frame):
16    def __init__(self, *args, **kwargs):
17        wx.Frame.__init__(self, size=(800, 500) ,*args, **kwargs)
18        self.Bind(wx.EVT_PAINT, self.onPaint)
19
20        self.mapfile = "population.xml"
21        self.width = 800
22        self.height = 500
23        self.createMap()
24        self.drawBmp()
25
26    def createMap(self):
27        """Create mapnik object
28
29        """
30        self.map = mapnik.Map(self.width, self.height)
31        mapnik.load_map(self.map, self.mapfile)
32        bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0))
33        self.map.zoom_to_box(bbox)
34
35    def drawBmp(self):
36        """Draw map to Bitmap object
37
38        """
39        # create a Image32 object
40        image = mapnik.Image(self.width, self.height)
41        # render map to Image32 object
42        mapnik.render(self.map, image)
43        # load raw data from Image32 to bitmap
44        self.bmp = wx.BitmapFromBufferRGBA(self.width, self.height, image.tostring())
45
46    def onPaint(self, event):
47        dc = wx.PaintDC(self)
48        memoryDC = wx.MemoryDC(self.bmp)
49        # draw map to dc
50        dc.Blit(0, 0, self.width, self.height, memoryDC, 0, 0)
51
52if __name__ == '__main__':
53    app = wx.App()
54    frame = Frame(None, title="wxPython Mapnik Demo By Victor Lin")
55    frame.Show()
56    app.MainLoop()