New Tool: MapInfo to QGIS style converter


Hopefully this tool can be of some use to people, as I know it has been very helpful to me since I made it.

As I’m a pretty heavy QGIS user now, and my work place still stores most, if not all, of our data MapInfo TAB format, one  friction point for me using QGIS was having to restyle all the MapInfo layers.  If we only had a handful of layer this wouldn’t be such a pain but we have a lot of tables and it would take me months to go though each one manually and style them.

I thought “there has to be some way I can automate this…” and so the MapInfo To QGIS Style Generator (or mapinfoToQgis.py) was born. Knowing that QGIS uses QML (a XML file format) to store it style information, and that MapInfo was able to export a style string for each object, I compared what QGIS generated for its QML using the same symbol I picked in QGIS as I had in MapInfo.   Almost a 1 to 1 conversion! Once I worked out how to convert MapInfo point size  to QGIS symbol size, and MapInfo colour value to RGB it was just a matter of generating a QML with the correct values.

Long story short, after a bit of clean up and writing a user guide I would like release version 0.1 of the MapInfo To QGIS Style Generator for wider testing.

Here is a quick example of the output.

Step 1: Take One MapInfo table.

Step 2: Run it though mapinfoToQgis.py

python mapinfoToQgis.py WaterFittings.Tab WatterFittings.qml -c FittingType --UseMapInfo

Step 3: Load QML file in QGIS

Result from running mapinfoToQGIS.py

Step 4: Get a beer?

If you are using MapInfo Font symbols or normal MapInfo 3.0 everything should come across almost exactly. mapinfoToQgis.py will use the same fonts in QGIS as you did in MapInfo and select to the correct symbol size. Although if you are using custom MapInfo 3.0 symbols you will get the default QGIS black square symbol,you can just change it to something better after loading the QML.

Currently the program only support converting symbols but I plan on adding line and region support sometime in the future.

The program can be found at https://github.com/NathanW2/MapInfo-to-QGIS-style-generator and more detailed instructions and download link can be found at https://github.com/NathanW2/MapInfo-to-QGIS-style-generator/wiki/Using-MapInfo-to-QGIS-style-generator.

Like I said at the start, hopefully other people will find this tool handy as I know I have.  If you do find it handy let me know, I would love to hear peoples feedback.  Also if you find any bugs let me know in the comments or log a issue on https://github.com/NathanW2/MapInfo-to-QGIS-style-generator/issues

Enjoy :)

Browsers: QGIS vs MapInfo 11


Warning: This post contains small rants! You have been warned.

PBBI have recently released MapInfo 11, the new version has brought one change that I think deserves some attention – even if for the wrong reasons.

MapInfo’s browser window was in need of a very good make over; it didn’t follow normal keyboard conventions, eg holding shift to select rather than ctrl; couldn’t sort via the headers; keyboard navigation was poor; and it looked ugly. PBBI then announced that MapInfo 11 would have a new “improved” browser. I thought “Sweet! About time” and then we got a copy. /sigh

So what’s the problem?

First off it’s slow to resize, this would be due to them using .NET WPF for the new browser (I have never seen a good fast .NET WPF datagrid).

Then we have sorting, which is meant to be the cool new feature. This is not the normal just click on the header to sort the column, no because that would be too easy, you have to right click in the browser, click sort and select the options which then opens a new browser window. um what?

Yes this is a pretty handy feature but no it shouldn’t be the only way to sort, you should have a click on the header kind of sort. This seems to be what people wanted.

Next. No visible scroll progress. When you move the little scroll box on the side the browser waits until you have finished to show you the data. I guess the old browser did this too so why change it!

And finally shift click to select a block of rows doesn’t work, I mean come on this is not a hard thing to do.

Surly you can dock it? Nope!

In the end we have a browser that is pretty much the same as the old one but slower……oh and has alternating row colors.

Overall reaction: Disappointed

Enter QGIS

Now if you are reading this blog you are well aware that I am a huge fan of QGIS, I don’t really make that a secret. So I guess the overall point of this post is to compare the QGIS attribute table (browser) and the new MapInfo 11 one.

Lets run though the same list as MapInfo.

Slow to resize? Nope. Even with a large table open the resize speed doesn’t change.

Header based sorting? Yep. Just click the header and it will sort that column. Multi column sorting is on the to-do list.

Live scrolling (results update as you scroll)? Yep + no lag.

Shift click to select blocks of rows? Sure why not. Or you can hold ctrl to select rows all over the grid.

Docking? Yep and floating so you can put it on a different monitor if you need.

Bonus

The QGIS attribute table has a built-in search/filter box, saves having to run a query and have a new window like in MapInfo if you just want to filter the browser.

The QGIS attribute table (browser)

Extra Bonus
The QGIS browser can even have other UI objects inside the cells. Very bloody handy.

Combo box in browser table.

You can even have a calendar date picker if you want.

Lets review

Feature MapInfo 11 QGIS
Good resize speed. No Yes
Header sorting. Yes [See update] Yes
Multi column sorting. Yes [1] No [2]
Live scrolling. No Yes
Shift click for blocks of rows No Yes
Docking. No[3] Yes

[1] Why does it need to open a new browser window? At least make it an option.
[2] On the to-do list

[3] Yes you can use http://www.pbinsight.com/support/product-downloads/item/windowhelper for this support. It’s a good tool go and download it. I just think it should be built in.

Doesn’t look too good for MapInfo at the moment. QGIS is even accessing the TAB data though ogr. Quick tip: if a free program can access and manage your data faster than you, you are failing.

My work place spends a good deal of money on our annual MapInfo “maintenance” licence, money I would happily send to the QGIS project if I had the choice. Or at least part of it,

Data

Both programs opened a 27000 row .TAB file.

Just for the record I’m not anti-MapInfo. It still has some features that I really like. I just wish they would pick up the game.

Update for 11.03 patch

As promised in my comments this is an update to reflect the new header click sorting in MapInfo Professional 11.03.

The 11.03 patch has added header based sorting, and while the sorting works as expected which is good, there is something a little odd.

What is the typical sort pattern? Left click header, table sorts ascending click again and table sorts descending or vice versa.  Then you normally have any extra sorting stuff in the right-click menu e.g Sort Ascending, Sort Desending, Clear Sort etc.

Go into MapInfo 11.03 left click header, context menu appears 0_o…  I’m not aware of any program that  has ever done that.  A menu on left click is not normal, even crappy ol’ Lotus Notes doesn’t have a menu on left click and Lotus Notes is one really crappy program.

Nuff said.

P.S Don’t talk about your new feature i.e the browser, following normal conventions for browser style windows then do something no one has ever done….and still no shift click block select.

P.P.S I know it may sound like a constant bashing but really something like this should never got passed review. I know UI is hard but come, on a context menu on left click…

The things I would like to see in QGIS. What are yours?


So if you have not already guessed from my increasing post about QGIS. I really like it as a GIS system but with all systems it comes with its shortcomings. (Nothing that can’t be fixed of course)

Here is my little wishlist of a few things I would love to see in QGIS.

Multipule map canvases. (ArcGIS, MapInfo)

I was thinking more like ArcGIS data frames vs something like what MapInfo has (multiple windows).  I think the multi window system can add confusion for people new to GIS, plus I hate having to window manage.

SQL like interface. (MapInfo)

This is something that I really like from MapInfo.  It adds a lot of power to the application being able to spatially join two tables that don’t share a command link column and get back a new layer.  Adding this to QGIS could be a pretty big task, although a very rewarding one IMO.  

Now there is some credit in saying “well you can just import your data into PostGIS and use that”, however that is not always a option and I think having a layer above that can query any open layer would be very very powerful.

These are just two of the main things that I would like to see, I’ll update the post if I can think of anymore.

Of course being open source I can write the features myself but C++ is still a bit over my head at the moment.

I would love to see what other peoples thoughts, or mini wishlists, are.  So if you are willing, drop a comment and let me know.

3D DXF from MapInfo Tab or ESRI Shape (or anything) using OGR


Note:The following post requires gdal 1.8

A lot of times we need to send/use 3D dxf files, we used to use FME however FME is not free or cheap. So I went looking for a free solution.

If you have gdal 1.8 it’s just one simple command line run using, what is becoming my favorite GIS tool, ogr2ogr.

All you have to do is run:

ogr2ogr -f "DXF" {outFile} {inFile} -zfield {ColumnWithZValue}

So in my case I ran:

ogr2ogr -f "DXF" C:\Temp\contourswarwick.dxf C:\Temp\Contours.TAB -zfield Height

I haven’t fully tested it but {outfile} can be any file ogr supports.

and the output:

Top view of contours

Top view of contour layer

Contours side view

The side view of the above image.

In the words of, the not so great, Charlie Sheen. WINNING!

Happy mapping :D

Using Python and MapInfo with Callbacks


The other day I posted an entry about using MapInfo with Python and Qt (see http://woostuff.wordpress.com/2011/03/05/mapinfo-map-control-into-qt-python-form/), 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
             pythoncom.CreateGuid()
             
  • 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
    win32com.server.register.UseCommandLine(Callback)
    main()

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:

mapinfo.SetCallback(callback)

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)
    app.setAttribute(Qt.AA_NativeWindows,True)
    wnd = QMainWindow()
    wnd.resize(400, 400)
    widget = QWidget()
    wnd.setCentralWidget(widget)
    wnd.show()

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

    app.exec_()

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
    win32com.server.register.UseCommandLine(Callback)
    main()

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.

MapInfo map control into Qt Python form


Tonight for a bit of fun, or shits and jiggles as we say here, I thought I would try and embed a MapInfo map control into a Qt python widget (although I should be studying, but it’s Saturday night) .

Turns out it is pretty easy!

pls send me teh codez? OK here you go.

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from win32com.client import Dispatch
import sys

app = QApplication(sys.argv)
app.setAttribute(Qt.AA_NativeWindows,True)
wnd = QMainWindow()
wnd.resize(400, 400)
widget = QWidget()
wnd.setCentralWidget(widget)
wnd.show()

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

app.exec_()

The above code will load MapInfo and open the property layer into the Qt Widget control, with the result below.

MapInfo map in python Qt based form

So this means you don’t “always” have to write your MapInfo based apps in C# or C++; of course I already knew this as anything that can use OLE and provide a native window handle to MapInfo will work, I just never tried it.

Generating contour lines in QGIS


One of the cool things I love about QGIS is finding stuff that you didn’t know it could do, well not just itself but plugins that you didn’t know about.

Today my discovery was in how to generate contour lines from a point layer.

  1. First install the contour plugin for qgis via the plugin installer.  Just search for “contour”
  2. Once installed open a vector point layer in QGIS.  Make sure the point layer has a field that you can use for elevation.

    One I prepared earlier

  3. Then select from the menu: Plugins->Contour->Contour
  4. Fill in the information

    Details form (The above setting will generate 0.5m contours)

  5. Press OK
  6. Results

    Results from plugin

  7. Profit??

The resulting contours will have a field that contains the label and z value for each contour line, you can then just label or color them how you wish.

Note:  There is a bug with QGIS memory layers where the fields don’t  show up in dropdown or attribute browsers, a simple fix is just to make the layer editable and then non editable then the fields will be there.

The contour layer is a QGIS memory layer so remember to save it to disk eg a shapefile before you close you will loose your new fancy contour layer.

Happy mapping :)

Picture: MapInfo to QGIS Styling


Just a quick one.

This afternoon I was having a go at getting our work MapInfo layers to look the same in QGIS as they do in MapInfo.  I wanted to do this; 1) So I can print maps from both systems for people and they are a consistent style 2) Would rather use QGIS for every day map work.  3) To see what they compare like.    I think it came out pretty good, apart from few symbols that I didn’t get time to finish.

MapInfo vs QGIS

Note that MapInfo has anti-aliasing and transparency  turned off, as it makes MapInfo very slow.

EDIT: To be fair here is both with the best settings turned on.  They are pretty much the same, which is a good thing.


MapInfo .Net Wrapper – The End…


Well it was a good project while it lasted, but I’m sorry to say that I will not be continuing work on it. For now anyway. Very sorry to those who were waiting for me to release v2.

Over the last couple of months, the amount of time I have spent on the project has decreased dramatically due to full-time work, part-time uni, other projects and family life.

My reasons for ending the project are: (1) I don’t have to the time to spend on it. My current work has me doing less development and more surveying and map work. (2) full-time work and part-time uni leaves very little time for anything else. (3) Having recently rediscovered QGIS, I feel my programming efforts are better spent on that project and helping develop and promote it more especially in Australia .  (4) Being a lone developer is hard, especially on a large library type project.  I was not able to keep the code coming, examples clean and up to date, and docs written.

I will however be continuing my other MapInfo based applications which can be found on: http://code.google.com/p/nathansmapinfoprojects/ I have no intentions of ending those.  I know a lot [few] of people use them so I am quite happy to continue support and features.

So, again, very sorry that it has ended but everything must come to an end at some stage.   The code is up on http://code.google.com/p/mapinfodotnetwrapper/ if anyone else would like to continue with it I am quite happy to give you full write access to the repository.

MapInfo and QGIS – An overview


Disclaimer: Like all my blog posts, this post is a reflection of my opinions and my opinions only. They do not reflect the opinions of my employer.

Recently I have got more into the open source movement, with my coding and my choice of software.  I tend to try to pick open source software for a couple of reasons, one of the main things is being free but apart from that I try to pick software that I can use to improve my development skills.  Being able to view the code is a big plus for me, being able to change and improve the software myself is an even bigger plus.

My most recent pick has been rediscovering QGIS (Quantum GIS), a free and open source GIS system written [mostly] in C++ using the Qt framework, and extensible by both Python and C++. I first discovered QGIS a couple of years ago, I was impressed but not impressed enough to really get into it. Maybe it was that I was so used to MapInfo I didn’t give it a proper look; or I didn’t really have the data in the right format, as I mainly used MapInfo, and if it couldn’t open and edit MapInfo TAB files I didn’t want to use it; or just lack of time; or whatever the reason. I used it for a day thought “Yep cool” and went back to using MapInfo.

Recently, however, they released QGIS 1.5 [and now QGIS 1.6] which changed the way I looked at the software.

The first thing I noticed was it was a lot faster in regards to map rendering then it used to be, not as fast as MapInfo but still very usable and efforts are being made to improve it even more so.

Styling of the layers has also improved a lot since I first tried it, it now has stack styling for points, lines and regions. Stack styles is something that was introduced in MapInfo 10.5, but unlike MapInfo, QGIS allows you to create a stacked style for each feature class rather than a style override like in MapInfo.  So you have a lot more control with styling in QGIS then you do in MapInfo.

QGIS styling dialogs for lines

and styles applied:

Ok not that pretty but it's a demo image.

Styling in QGIS is also only based on the underlying data of the object, think thematic maps in MapInfo.  I kind of like this, rather than just being able to just style an object you need to think about the data that is stored with an object.  This also stops the style and data getting out of sync.  At times I have seen maps made in MapInfo with no information on the object but  just styled to show different classes.

QGIS supports a large number of formats. It can open and write tab files but just not at the same time, no big deal as you can always use shape or a postgis database then just convert back if needed.

QGIS also has an interesting plugin system, while being able to use C++ and Python, the main app has a plugin installer [a plugin itself] which lets you install plugins from “hosted repositories”.  This makes finding new tools with work with very easy.

Plugin installer. Very very handy.

Composers are basically MapInfo layout windows, they allow you to add maps, legends, text labels, dynamic scale bar. attribute tables.  Composers can be exported to svg, pdf or printed just like layouts and you can have as many composers as you need.

A composer window for QGIS

Labeling options in QGIS are pretty much the same as MapInfo, if not more. You have the ability to use columns from a table that defines information about labels eg colour, X and Y etc if needed.

But one big feature that I really like about QGIS is the info tool. Unlike MapInfos info tool, you can define per table how and what information is displayed for each column and there is no stupid Ctrl+C copy bug.  It’s a bit hard to explain so a picture saves a thousand words:

Defining each how each column should be displayed.

and editing or adding a new feature:

When editing or adding features this dialog is shown.

For me as a developer the one thing that I find great with QGIS is it has a very kind and active developer community, though both IRC and email, and the code is relatively well organized .   I was able to pull down the source code for the latest trunk and have a build running in Ubuntu in under a hour [this mainly being due to my lack of Linux experience].   Being able to see something you don’t like, change it and submit it back to the project is a very good feeling.

Some downsides:

While QGIS is very good, and is always improving, it doesn’t always win over MapInfo in everything.  One major thing that MapInfo has going is its SQL, being able to run the same SQL syntax over any kind of table in MapInfo is a great feature.   QGIS, in this regard, tends to leave those things up to the underlying provider.  You are able to run simple [or complex] queries on an open table but being able to do something like “Select Col1, Col2, Col3 From Table Where Col1 = “SomeCondition” Group By Col2″  isn’t really there, although if you are running PostGIS it is quite easy.

Query

QGIS Query builder.

The next big thing MapInfo has, is MAPBASIC.  While QGIS has a built-in Python shell and has a relatively good API, MAPBASIC is very easy language to get up to speed with especially for beginners.

MapInfo also has multiple map windows, this is something that isn’t currently implemented in QGIS.  However I did bring it up on the #qgis IRC channel and the developers seemed happy with the idea but just needed time and someone to implement it.

Summary:

I could talk all day about all the cool things that QGIS can do but to save boring you I’ll leave it at this for now, more posts to come in the future I’m sure.

At the moment for my current day-to-day map work, I have gone from using MapInfo to using QGIS almost full time.  Like mentioned above, there are still some things that MapInfo does well and I tend to use MapInfo for those kind of jobs.  Also as my employer uses MapInfo and runs pretty much a full MapInfo system, I still use it for maintaining things like Exponare.  Using and developing for QGIS also means I have fallen behind on my MapInfo .Net tools, I still have a couple on the go and plan to get them out just before Christmas [or maybe a little bit after]

So my overall impressions with QGIS, if you couldn’t tell already, are: very very impressed with a very strong feeling it’s only going to get better.

So if you’re interested in trying something new, give QGIS a go.

Some handy links: