Using Python and MapInfo with Callbacks

The other day I posted an entry about using MapInfo with Python and Qt (see, one big thing that I missed was support for callbacks, which if you want to do anything related to integrated mapping is a must for map tool support.

Turns out it is pretty easy, and today I worked out how.

You will need to create a class in python that looks something like this:

class Callback():
    _public_methods_ = ['SetStatusText']
    _reg_progid_ = "MapInfo.PythonCallback"
    _reg_clsid_ = "{14EF8D30-8B00-4B14-8891-36B8EF6D51FD}"
    def SetStatusText(self,status):
        print status

This will be our callback object that we will need to create for MapInfo.

First I will explain what some of the funny stuff is:

  • _public_methods_ is a Python array of all the methods that you would like to expose to COM eg MapInfo in this case. This attribute is a must for creating a COM object.
  • _reg_progid_ is the name of your COM application or object.  This can be anything you would like.
  • _reg_clsid_ is the GUID, or unique id, for the object or app.  Do not use the one I have, call the following in a Python shell to create your own.
             import pythoncom
  • SetStatusText is the MapInfo callback method that is called when the status bar changes in MapInfo.

In order to use the class as a COM object we have two more steps to complete, one is registering the COM object and the other is creating it.

First, in oder to register the object we call the following code from our main Python method:

if __name__ == "__main__":
    print "Registering COM server..."
    import win32com.server.register

This will register the COM object which will mean it can then be creating for use by MapInfo.

In order to create our callback in Python we call:

callback = win32com.client.Dispatch("MapInfo.PythonCallback")

and set it as our callback object for MapInfo:


So after all that the final code looks like this:

def main():
    from PyQt4.QtCore import *
    from PyQt4.QtGui import *
    from win32com.client import Dispatch
    import sys

    app = QApplication(sys.argv)
    wnd = QMainWindow()
    wnd.resize(400, 400)
    widget = QWidget()

    handle = int(widget.winId())
    mapinfo = Dispatch("MapInfo.Application")
    callback = win32com.client.Dispatch("MapInfo.PythonCallback")
    mapinfo.SetCallback(callback)'Set Next Document Parent %s Style 1' % handle)'Open Table "D:\GIS\MAPS\Property.TAB"')'Map from Property')


class Callback():
    """ Callback class for MapInfo """
    _public_methods_ = ['SetStatusText']
    _reg_progid_ = "MapInfo.PythonCallback"
    _reg_clsid_ = "{14EF8D30-8B00-4B14-8891-36B8EF6D51FD}"
    def SetStatusText(self,status):
        print status

if __name__ == "__main__":
    print "Registering COM server..."
    import win32com.server.register

and the result is a map window and information printed to the console.

Information from MapInfo callback

I think Python could be a good language to prototype MapInfo based app, or even build a whole app itself. If you do end up making something of it let me know I am quite interested with what people could come up with.


3 thoughts on “Using Python and MapInfo with Callbacks

  1. I was looking at this because I’m trying to set up a Python script which could handle events from MediaMonkey. In the MM forums, the consensus is that MM has done something wrong and is failing to provide events to out-of-proc objects. So I was looking deeply into the sources for win32com to see if I could work around (answer: no, or not yet anyway).

    But: I learned win32com.client has a DispatchWithEvents class where you can define a simple (unregistered) Python class that simply has methods named for the callback methods. e.g, using your Callback class but without the _public_methods_, _reg_clsid_, _reg_progid_ members and without the registration. Then you’d call:
    mapinfo = DispatchWithEvents('MapInfo.Application', Callback)
    The mapinfo object is a proxy that both exposes the methods of MapInfo.Application and receives the events; the ‘self’ of the event methods is the mapinfo object.
    There are caveats in the win32com source about this technique which I admit I don’t understand very well; but the InternetExplorer example provided in the docstring works like a charm. (And the same thing with MediaMonkey doesn’t work to receive the events, altho it does work as a controller object.)

    A potential wrinkle, which again I don’t understand well, is that Python may not properly cause event propogation if the script is not repeatedly calling pythoncom.PumpWaitingMessages() in a background thread. I’ve seen a couple of examples implenting this kind of feature, but it’s not clear to me whether it’s required (as the IE example doesn’t do anything like that).

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s