MapInfo Pro Developers User Group

 View Only
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 05-12-2020 17:07
    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 05-13-2020 15:35
    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 05-20-2020 05:35
    Edited by Peter Møller 02-11-2022 07:48
    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.precisely.com/communities/community-home/digestviewer/viewthread?GroupId=67&MessageKey=02040188-213a-4ef6-8272-33edf46f028f


    Thanks
    Anshul

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



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