MapInfo Pro and Python Basics

Products Affected: MapInfo Pro™ 2019

The following is from the MapInfo Pro Digest, 4/6/2020:

What's the story on Python in MapInfo Pro?
MapInfo Pro users have long used the MapBasic scripting language to automate tasks, build custom apps, create domain specific customizations of Pro, Integrate Pro with other tools, etc.
MapBasic is mostly a compiled language, which is a bit of a barrier to non-programmers.
We do have the MapBasic window which allows MapBasic statements and code to execute directly ("Interpreted"), which is very useful. It does have many limitations compared to compiled MapBasic however, such as not supporting control statements, and not being debug-able. Recently we have been improving interpreted MapBasic and even expanding it's use via the new SQL Window. We will continue to do this.

MapBasic has been one of the key's to Pro's success among our users.


So, Why Python ?
This post will explain the reasoning, direction, and what was delivered in 2019.1. Further posts will follow to show how to use Python and how to get started.


What is it
?
Python is also an interpreted scripting and programming langue with wide support and usage across all domains.
It is quick and easy to learn.

From the Python.org website: 
https://www.python.org/

Experienced programmers in any other language can pick up Python very quickly, and beginners find the clean syntax and indentation structure easy to learn.

Python is powerful... and fast;
plays well with others;
runs everywhere;
is friendly & easy to learn;
is Open.

Python is a full featured language used for everything from scripting apps like Pro to creating websites, to data science analysis and visualization. As such there are many available libraries and resources to help you out. Python.org is a good place to start learning more.

Most students coming out of college with any computer course work and even GIS have already been exposed to Python.

Having a more powerful, widely known and used scripting language for Pro that can be debugged will help many existing users and hopefully attract some more. This is in addition to moving MapBasic forward.

Starting in Pro version 17.03 we enabled the ability to create add-ins or apps using Python in Pro. While this was a good first step and we did receive some interest, it was practically too hard to set up except for experienced python developers.


What have we done for MapInfo Pro 2019.1?
Starting with Pro 2019.1 we are working to make Python a first class scripting language in Pro. We also are making it much easier to create add-ins via Python.

First,
Python 3.7 is now installed with Pro. It is an optional component so if you do not need it you can turn it off via a custom installation. (Stop reading now if that is the case...)

Pro installs several well known Python libraries by default such as numpy, scipy, seaborn, pandas, matplotlib, osgeo/gdal, ptvsd, mi_py_debugger. Additional modules can be added and removed as needed.


Running python code in Pro
You can now open and run a python file (*.py) via the "Run Program" dialog. This uses the mapbasic statement "run application "file.py". It can also be called from the MapBasic window or a compiled mbx.

Creating an add-in without compiled MapBasic
You can now create an add-in using only Python. An add-in is different than a script in that it stays loaded and can add ui items such as ribbons, context menus and docking windows. Note that a stock .mbx is generated automatically behind the scenes to accomplish this.

Registering a Python based add-in
You can now register and auto-load a .py file add-in.

Debugging of scripts and add-ins
Both python scripts and add-ins can be debugged. We recommend and explain how to do it with the free Visual Studio Code application.

Python Console
You can use any Python IDE to develop your code, but we are providing a small Python Console via the MapInfo Marketplace to help with interactively editing and running small python scripts. It supports syntax highlighting and intellisense.

Access to Pro's Object Model
In addition to MapBasic statements, MapInfo Pro has an object model exposed to MapBasic and .NET developers that make it possible and easy to customize the ribbon, add context menus, create docking windows, map mini toolbars, etc. It also has many events that you can get notified of when a user or Pro does something like open a workspace or table, make a selection, run a command, etc.

By using a Python.NET implementation we are able to expose the whole object model to Python code very easily and even support intellisense. The ExtensibilityReference, which is a help file that documents the object model is now installed with Pro (in addition to with MapBasic). The Python information from the Release notes was integrated into the ExtensibilityReference.

As we continue to extend the object model it will help MapBasic, .NET, and Python scripts/developers equally.


Opening Native and NativeX tables via Python
Included with the Python installation is Osgeo which contains GDAL -- the well known open source library for reading and writing geospatial data. We have included a new gdal driver called EFAL which is publicly available and can read and write both Native and NativeX tables, which make interacting with other python or 3rd party tools much easier.

Startup.py

If a startup.py file is found executed in the global scope setup. Similar to the way startup.wor is found.

Getting Started With Python
Also available via the MapInfo Marketplace is an app called 'PythonQuickStart' which has some sample code and some reference information on getting started with Python in Pro. Right now it is available but does not have the amount of samples we would like. We will be improving it and updating it in the near future.

A Quick Example Python Script

User-added image

So that is a summary of Why and What we have done with Python in MapInfo Pro so far.

Here is an automation script example:

This is a great example of the kind of automation we are talking about.
Private message me if you would like some help.

I spent about an hour creating this using some sample data that comes with the PythonQuickStart add-in in the MapInfo Marketplace.

It creates a ranged theme on a set of columns and exports them to jpg files based on the column name.
I used the MapBasic window to echo the thematic statement and the save window as statements and then added them to my Python script.

Here is the script:

### Description: Shows how to automate the export of multiple themes to jpg
### Category: Mapping

 
import os
import time
try:
 # open workspace with our map
 modulePath = get_current_module_path()
 table = os.path.abspath(os.path.dirname(modulePath) + r"\..\Data\Demographic_exampleX.TAB")
 do('open table "{}" as demographic'.format(table))
 
 # create list of columns to them on
 themeColumns = [
 'Age_0_4_P',
 'Age_5_9_P',
 'Age_10_14_P',
 'Age_15_19_P',
 'Age_20_24_P',
 'Age_25_29_P',
 'Age_30_34_P',
 'Age_35_39_P',
 'Age_40_44_P',
 'Age_45_49_P',
 'Age_50_54_P',
 'Age_55_59_P',
 'Age_60_64_P',
 'Age_65_69_P',
 'Age_70_74_P',
 'Age_75_79_P',
 'Age_80_84_P',
 'Age_85_89_P'
 'Age_90_94_P',
 'Age_95_99_P',
 'Age_100_yr_over_P',
 'Age_Total'
 ]

 
 for col in themeColumns:
 # open map window and zoom
 do('map from demographic width 4 units "in" height 4 units "in" floating')
 do('set map window frontwindow() zoom entire')

 
 # create the thematic
 do('shade window frontwindow() 1 with {} ranges apply all use color Brush (2,16777168,16777215) 0: 10 Brush (2,16777168,16777215) Pen (1,2,0) ,10: 16 Brush (2,11591824,16777215) Pen (1,2,0) ,16: 21 Brush (2,5296240,16777215) Pen (1,2,0) ,21: 30 Brush (2,2146448,16777215) Pen (1,2,0) ,30: 80 Brush (2,27552,16777215) Pen (1,2,0) default Brush (2,16777215,16777215) Pen (1,2,0) # use 1 round 0.1 inflect off Brush (2,16744640,16777215) at 3 by 1 color 1 # '.format(col))
 
 # export map to jpg using column name
 dir = os.path.join(os.path.dirname(modulePath),'Output')
 if not os.path.exists(dir):
 os.mkdir(dir) 
 file = r'{}\{}.jpg'.format(dir,col)
 do('Save Window frontwindow() As "{}" Type "JPEG" Width 4 Units "in" Height 4 Units "in" Resolution 120'.format(file))

 
 # close the map window
 do('close window frontwindow()')

 
 # clean up
 do('close table demographic')

 
except Exception as e:
 print("Error: {}".format(e.__cause__))



And here is a picture of the output in File Explorer:
User-added image

With a little extra effort we could put the map into a layout with a Title, Legend, Scalebar, etc and export it to an image or .pdf.



------------------------------
Bob Fortin
Software Architect and Distinguished Engineer
MapInfo Pro Development Team
------------------------------

UPDATED:  April 7, 2020