-2

I'M using pycharm (python) (and mapnik)on windows 7, I just wanted to test if everything is in place after installation. I used an example from the net here is it , and I have a frame error. Could it be an installation problem ? compiler ?? I'M very new to python. thanks in advance for your time.

"""
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 mapnik
import wx


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")
        frame.Show()
        app.MainLoop()

here is the error message:

 Traceback (most recent call last):
  File "C:/Python27/example.py", line 16, in <module>
    class Frame(wx.Frame):
  File "C:/Python27/example.py", line 56, in Frame
    frame = frame(None, title="wxPython Mapnik Demo")
NameError: name 'frame' is not defined

Process finished with exit code 1
MattDMo
  • 100,794
  • 21
  • 241
  • 231
Ken
  • 1
  • 2
    You're calling a function called `frame`. `frame` is not defined. Were you expecting it to be? (maybe you meant `Frame`?) Python is a case-sensitive language, like most. – Iguananaut Feb 19 '15 at 23:53
  • Which Frame ? there are many on the program – Ken Feb 20 '15 at 19:35
  • You made a class called `Frame` that subclasses `wx.Frame`. Then you try to instantiate it but you've misspelled it. – Iguananaut Feb 20 '15 at 20:23

2 Answers2

0

You have a couple of problems with your code. This line is incorrect:

frame = frame(None, title="wxPython Mapnik Demo")

It should be:

frame = Frame(None, title="wxPython Mapnik Demo")

The reason is that the class name is Frame (not the capital F) and you want to instantiate that class to run the program. Also note that the line if __name__ == '__main__': is incorrectly indented. The following complete example should work:

import mapnik
import wx


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")WAS WRONG
    frame.Show()
    app.MainLoop()
Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
0

I think is getting g better, I GET a different error:

    C:\Python27\python.exe C:/Python27/text.py
Traceback (most recent call last):
  File "C:/Python27/text.py", line 45, in <module>
    frame = Frame(None, title="wxPython Mapnik Demo")
  File "C:/Python27/text.py", line 13, in __init__
    self.createMap()
  File "C:/Python27/text.py", line 21, in createMap
    mapnik.load_map(self.map, self.mapfile)
RuntimeError: Could not load map file: File does not exist       of 'population.xml'

Now what is the population.xml file ??

Ken
  • 1