MapInfo Pro

MapInfo Monday: Creating Uber H3 Hexagon layers in MapInfo Pro

  • 1.  MapInfo Monday: Creating Uber H3 Hexagon layers in MapInfo Pro

    Posted 08-23-2021 08:05
    Edited by Peter Møller 08-23-2021 08:47
      |   view attached
    Happy #MapInfoMonday,

    Today I'll dive a bit into the World of Hexagons and Python in MapInfo Pro. I'll look at how you can create hexagons in MapInfo Pro using the H3 Library published by Uber.

    The first thing you probably ask yourself is, what is ​this "H3" thing that Uber has developed? Basically, it's a set of hexagons covering the entire World at several levels. Well, not quite it's more a library of methods that make it possible to work with such a hexagon grid. It doesn't supply this grid but it gives you the option to do this yourself. That is if you know how to and have the skills to take advantage of it, as you'd need to build an application to really harness the power.

    You can find more information about this on this website: H3: Uber's Hexagonal Hierarchical Spatial Index.

    Two years ago, I wrote about the H3 in the All Things Location community. A few days ago, @Jim Coleman asked a question about creating these H3 grids in MapInfo Pro, or even in an external application. That made me revisit the H3 project. When I looked at it two years ago trying to take advantage of it inside MapInfo Pro, I ran into the issue of it only being compiled for 32-bit. But since then we have added Python support to MapInfo Pro and I was thinking that might be a way to go.

    I reached out to @Bob Fortin and @Anshul Goel on the MapInfo Pro Engineering team. Soon after we had a working tool that allows a user to create H3 hexagons around polygons in a MapInfo table. I have attached this tool as a .py file that can be run from the Run Application dialog.

    Note that the tool is using the H3 Python library that you either have to install yourself, or you can let the add-in install it. For that to work, you will have to run MapInfo Pro as an administrator to ensure you have the needed elevated permissions.

    How it works

    In MapInfo Pro open the table to create H3 Hexagons around. Please note that the table has to be using Longitude /Latitude WGS84 as its coordinate system. If that's not the case, save a copy of the table and change the coordinate system during this process.

    Now hit Cltr+U to select and run the Python script. You can also access Run Program from the Options dropdown in the Tools Window.

    The first time you run a Python script in your MapInfo Pro session it might take a bit to start. That's because MapInfo Pro needs to load the Python engine in the background before the script can be executed. Next time, this will be faster.

    In the first dialog shown, you will have to select the table you want to have covered with H3 grids. Notice the Python script will run through all the records in the table and use the H3 method Polyfill to calculate which hexagons to create. The Polyfill method only returns the hexagons whose centroid is within the polygon provided. This may also mean that there can be areas where your input polygons aren't completely covered by H3 grids.

    As I mentioned earlier, the Input Table dropdown only lists the tables that can be used, ie. tables that are using Longitude/Latitude WGS84.

    The next step is to configure the H3 hexagons to create. You need to specify the name of the output file and the H3 level. The level goes from 1 to 15.

    Be cautious of the number of hexagons you may end up creating. In my example, I created hexagons for France at levels 5 and 6. Level 5 resulted in around 2,300 hexagons. Level 6 resulted in almost 16,000 hexagons. Remember each time you go one level up, you end up with around 7 times more hexagons. If you try to create hexagons at level 10 for France, you will up with around 38,000,000 hexagons.

    Below you can see a map representing H3 hexagon levels 5 and 6 around Paris.

    Other ways to create hexagons

    I have also earlier discussed hexagons creation with MapInfo Pro. In fact, I created a small tool, HexagonTool, that could create hexagons using any coordinate system.

    In the article mentioned above, you can also find links to other tools that can help you generate hexagons in MapInfo Pro.

    Now, the benefit of these other tools compared to the H3 hexagons is mostly the way they look on a map that isn't based on Longitude/Latitude. The H3 grid comes with a number of other benefits especially the fact that it's hierarchical which means it does allow you to aggregate values upwards as each H3 grid cell is mostly covered by only one cell in the level above.

    If you use any of the other tools, there is no link between a grid cell at one level/size to a grid cell of a level above/larger size.

    Thanks, Anshul and Bob for creating the Python script.

    Are you already using Uber H3 hexagons in your organization?

    Peter Horsbøll Møller
    Principal Presales Consultant | Distinguished Engineer
    Precisely | Trust in Data