Page:
Mapnik with wxPython
Pages
A perfect testcase
API changes between v2.0 and v2.1
API changes between v3.0 and v3.1
AWS Lambda
About Mapnik
AlsoFilter
Api changes between v2.1 and v2.2
Api changes between v2.2 and v2.3
Api changes between v2.3 and v3.0
ArchInstallation
Aspect Fix Mode
Benchmark Notes
BoundsClipping
BrokenExceptions
BuildingSymbolizer
CSV Plugin
CairoVersions
CentOS_RHEL
ChangeLog
Code sprint
CodingStandards
Compiler Notes
Compositing
Contributing
DebianInstallation
DebugSymbolizer
DebuggingMapnik
DemoGallery
DeveloperTopics
DevelopingPlugins
Development
ElseFilter
ExampleCode
Expression
FasterCompiling
Fasterlabeling
Filter
FontSet
FreeBSDInstallation
GDAL
GEOS
GSOC Ideas
GSOC2010
GSOC2010_Application
GSOC2010_Ideas
GSOC2011
GSOC2011_Ideas
GeoJSON Plugin
GettingStarted
GettingStartedInPython
GettingStartedInXML
GlyphSymbolizer
GroupSymbolizer
Grouped rendering
Gsoc2012 application
Gsoc2012
HaitiStyles
Home
Ideas
Ideas_Compositing
Ideas_FutureMapnik
Image IO
InstallationTroubleshooting
InternationalText
IntroductionToGIS
Kismet
LabelingSupport
Layer
LearningMapnik
Legending
LinePatternSymbolizer
LineSymbolizer
LinuxInstallation
Logging
MacInstallation
MacInstallationSource
MacInstallation_Homebrew
MacPostGIS_Setup
MacPythonUpgradeIssues
ManagingLargeXmlFiles
MapDesign
Mapnik Development
Mapnik Installation
Mapnik with wxPython
Mapnik2
Mapnik2_Changes
MapnikCodeSprint
MapnikCodeSprint_MCS01
MapnikCodeSprint_MCS01_Location
MapnikCodeSprint_MCS01_Results
MapnikCodeSprint_MCS01_Schedule
MapnikCodeSprint_MCS02
MapnikCoreConcepts
MapnikDependencies
MapnikInstallationSvn
MapnikReferences
MapnikReleaseSteps
MapnikReleases
MapnikRenderers
MapnikTutorials
MapnikUtilities
MapnikViewer
Mapnikinstallation
MarkersSymbolizer
MaxScaleDenominator
MemoryDatasource
MetaWriter
MinScaleDenominator
ModServer
Nik2Img
Notes and caveats
OCCI
OGR
OpenSolarisInstallation
OpenSolarisInstallation_32bit
OpenSolarisInstallation_64bit
OpenSolarisInstallation_TroubleShooting
OpenSuse
OpenSuseInstallation
OptimizeRenderingWithPostGIS
OsmPlugin
OutputFormats
PackageBuilding
Paleoserver
Path Expression
PgRaster
PluginArchitecture
PointSymbolizer
PolygonPatternSymbolizer
PolygonSymbolizer
PostGIS
Postgis async
Python Plugin
Python3k
RFC: Raster color interpretation
Raster
RasterColorizer
RasterSymbolizer
Rasterlite
Related projects and utilities
Release0.6.0
Release0.6.1
Release0.7.0
Release0.7.1
Release0.7.2
Release2.0.0
Release2.0.1
Release2.0.2
Release2.1.0
Release2.2.0
Runtime Logging
SQLite
SVG support
Scale factor
ScaleAndPpi
ShapeFile
ShieldSymbolizer
ShieldSymbolizerTests
Style
StyleShare
Svg rendering gochas
SymbologySupport
Text Rendering Overview
TextSymbolizer
Trac to Github wiki migration notes
Troubleshooting
UbuntuInstallation
UbuntuInstallationOld
UsingCustomFonts
UsingScons
WindowsInstallation
World population tutorial
XMLConfigReference
XmlFormatDiscussion
iOS
mapnik.Datasource
mapnik.Layer
mapnik.Map
my choice
No results
1
Mapnik with wxPython
tmcw edited this page 2012-01-24 06:45:51 -08:00
Table of Contents
Mapnik with wxPython
Here's a sample application integrating Mapnik with wxPython
"""
This is a simple wxPython application demonstrates how to
integrate mapnik, it do nothing but draw the map from the World Poplulation XML
example:
https://github.com/mapnik/mapnik/wiki/GettingStartedInXML
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()
The key point of this program is this:
# 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())
We create an Image32 object, and render map on to that image, then load raw data of image into wxPython's Bitmap object. That's it! Now you can use your memory dc to do whatever you like
memoryDC = wx.MemoryDC(self.bmp)
# draw map to dc
dc.Blit(0, 0, self.width, self.height, memoryDC, 0, 0)
Screenshots