MapInfo Pro Developers User Group

Expand all | Collapse all

Using Python for scripting in MapInfo Pro

  • 1.  Using Python for scripting in MapInfo Pro

    Employee
    Posted 04-10-2020 08:27
    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.
    Examples:
    • Automate tasks that are repeated periodically
      • importing data, transforming it
      • updating data
      • querying the new version of the data
    • Generate output (images, layouts, reports) varying the data or the map location used
    • Add some ribbon buttons to call a custom dialog, create a theme

    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.
    You can get it here, but it is better to install it from inside Pro in the Marketplace:
    http://mapinfomarketplace.pitneybowes.com/product/0fa4fe2b-81c9-49af-9c7e-a98bb9f4f1ec

    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.

    Access to Raster SDK
    If you have MapInfo Advanced, you can now use Python to automate the creation, editing, analysis and visualization of raster images and grids using our MRR format or many other formats. Since there is no MapBasic support for raster, this is a big advantage.

    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


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


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

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


  • 2.  RE: Using Python for scripting in MapInfo Pro

    Posted 16 days ago
    Can you please suggest how we can create dialog Boxes with Python that is inherited by MapInfo? For example, Dialog boxes that control MapInfo as well

    ------------------------------
    Navjot Kaur
    Knowledge Community Shared Account
    ------------------------------



  • 3.  RE: Using Python for scripting in MapInfo Pro

    Employee
    Posted 15 days ago
    Hi Navjot,

    I would be happy to create a sample if you can you give me an example or idea of what you would like to do?

    We are working on several ways to request input from a user for the next release.

    For now there is the python Input() function to request a single value from the user.
    There are also many python ui modules such as pyqt5 that can be installed and used to create a dialog.

    Once you have the input from the user, you can construct and call mapbasic statements to execute Pro functionality.

    -Bob

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



  • 4.  RE: Using Python for scripting in MapInfo Pro

    Employee
    Posted 8 days ago
    Hi Navjot,

    I have created a post with example on creating a MapInfo Pro Addin In Python with ways of creating a dialog interacting with MapInfo Pro in below post. Please have a look at this and let me know if you need something specific.

    https://community.pitneybowes.com/communities/community-home/digestviewer/viewthread?GroupId=67&MessageKey=02040188-213a-4ef6-8272-33edf46f028f&CommunityKey=3c2aca7a-c3ae-4602-a142-9ee956769d55&tab=digestviewer&ReturnUrl=%2fcommunities%2fcommunity-home%2fdigestviewer%3fListKey%3d9349d4c2-4c10-48c7-a46d-1fba92dd883c%26SuccessMsg%3dThank%2byou%2bfor%2bsubmitting%2byour%2bmessage.%26CommunityKey%3d3c2aca7a-c3ae-4602-a142-9ee956769d55


    Thanks
    Anshul

    ------------------------------
    Anshul Goel
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 5.  RE: Using Python for scripting in MapInfo Pro

    Posted 8 days ago
    Edited by Navjot Kaur 8 days ago
    Thanks Anshul

    I really appreciate for your help. This is a very good sample.

    Basically I am working on creating another application with Python that opens MapInfo and then controls it. In that application, there will be MapInfo interface as such along with that there will be customized buttons/ dialog boxes interacting with MapInfo. It is something similar that was done with OLE Automation in Visual Basic Application.

    A am working with PyQT5 module. I am able to create buttons, and dialog boxes etc. But I don't know how to open MapInfo through it and then control and interact with it.

    Can you please suggest something for it.

    Thanks,
    Navjot

    ------------------------------
    Navjot Kaur
    Knowledge Community Shared Account
    ------------------------------



  • 6.  RE: Using Python for scripting in MapInfo Pro

    Employee
    Posted 8 days ago
    Edited by Anshul Goel 8 days ago

    Hi Navjot,

    From what I understood, you want to control MapInfo Pro from Python Application running out of process from MapInfo Pro.

    For doing the same you can get hold of MapInfo Pro COM object and then use the Do and Eval methods on the COM interface to execute/evaluate MapBasic. I think it would be similar to was done with OLE Automation in Visual Basic Application.

    To get this working you can install pywin32 module in MapInfo Pro Python installation by going to Python37 folder in MapInfo Pro installation directory and than double click on prompt.bat.
    This will open up command prompt and install pywin32 using "python -m pip install pywin32".

    Now, below code will give you access to MapInfo Pro COM object.

    import win32com.client

    pro = win32com.client.Dispatch("MapInfo.Application.x64")

    In MapInfo version 2019.1 we also restored the functionality of "set next document parent" via COM.


    ------------------------------
    Anshul Goel
    Knowledge Community Shared Account
    Shelton CT
    ------------------------------



  • 7.  RE: Using Python for scripting in MapInfo Pro

    Employee
    Posted 8 days ago
    Here is some quick info on how to get started using Python in MapInfo Pro.
    (I had posted this in the main Pro board, but am adding it here in case you have not seen it)

    Starting with v2019.1, Python is installed and ready to use out of the 'box'.

    See my post here for ideas on what you could do with python and why:
    https://community.pitneybowes.com/communities/community-home/digestviewer/viewthread?GroupId=19&MessageKey=72f33381-f319-497b-9b74-ee6a6c906a7d&CommunityKey=fe4d1ee2-f1c6-4307-87d2-00bea1344885&tab=digestviewer&ReturnUrl=%2fcommunities%2fcommunity-home%2fdigestviewer%3fcommunitykey%3dfe4d1ee2-f1c6-4307-87d2-00bea1344885%26tab%3ddigestviewer

    First, you will want to get two Python help applications from the MapInfo Marketplace.
    The easiest way to do that is to type 'python' into the quicksearch toolbar. (It now also searches the Marketplace.)
    Type Ctrl+Q then type python:


    You will see two Python applications:

    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.

    Python Quick Start
    This add-in has many python code examples from simple to complex.
    In addition, in includes reference materials and links to web sites and documentation to help you get going.

    Click on the search results and the Marketplace window will open in Pro. Simply click the Install Product button.

    Then do the same for the Python Quick Start add-in.

    Here is a screen shot of the reference page:

    There are over 30 examples in various categories, and you can search for specific text like a mapbasic or python keyword you want to see how to use.

    Here is one example that tells Pro to convert a .csv file to a MapInfo Table:


    The edit button opens up the script into the Python Console.


    The Run button executes the script.

    Feel free to come here with questions or ideas.

    Please give it a try!

    -Bob

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