什麼是Mapnik
Mapnik是一款Open source的地圖繪製的程式庫,它可以畫出非常漂亮的地圖,也可以做出像Google Map那樣的地圖,例如這個範例,不過它還算是在開發中,文件還有點不太齊全,因為我想做一套自己的VisualTracert來顯示tracert的router分部在世界的哪裡,那套VisualTracert是商業軟體,要看地圖是要錢的,因此我想弄一個Open source的版本,不過因為還很新,有的應用目前幾乎都是網頁地圖,GUI的例子很少,我花了一點時間,研究如何將地圖畫到wxPython上
程式其實不難,只是mapnik文件都還沒建好,要用dir函數和看原始碼去了解到底有什麼功能可以用,以下就是使用wxPython來畫mapnik地圖的程式
""" This is a simple wxPython application demonstrates how to integrate mapnik, it do nothing but draw the map from the World Poplulation XML example: http://trac.mapnik.org/wiki/XMLGettingStarted Victor Lin. (bornstub@gmail.com) Blog http://blog.ez2learn.com """ import wx import mapnik class Frame(wx.Frame): def __init__(self, *args, **kwargs): wx.Frame.__init__(self, size=(800, 500) ,*args, **kwargs) self.Bind(wx.EVT_PAINT, self.onPaint) self.mapfile = "population.xml" self.width = 800 self.height = 500 self.createMap() self.drawBmp() def createMap(self): """Create mapnik object """ self.map = mapnik.Map(self.width, self.height) mapnik.load_map(self.map, self.mapfile) bbox = mapnik.Envelope(mapnik.Coord(-180.0, -75.0), mapnik.Coord(180.0, 90.0)) self.map.zoom_to_box(bbox) def drawBmp(self): """Draw map to Bitmap object """ # create a Image32 object image = mapnik.Image(self.width, self.height) # render map to Image32 object mapnik.render(self.map, image) # load raw data from Image32 to bitmap self.bmp = wx.BitmapFromBufferRGBA(self.width, self.height, image.tostring()) def onPaint(self, event): dc = wx.PaintDC(self) memoryDC = wx.MemoryDC(self.bmp) # draw map to dc dc.Blit(0, 0, self.width, self.height, memoryDC, 0, 0) if __name__ == '__main__': app = wx.App() frame = Frame(None, title="wxPython Mapnik Demo By Victor Lin") frame.Show() app.MainLoop()


