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

    Posted 05-20-2020 08:37
    Edited by Navjot Kaur 05-20-2020 08:38
    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 05-20-2020 09:23
    Edited by Anshul Goel 05-20-2020 09:26

    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 05-20-2020 08:29
    Edited by Peter Møller 01-04-2022 08:39
    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.precisely.com/communities/community-home/digestviewer/viewthread?GroupId=19&MessageKey=72f33381-f319-497b-9b74-ee6a6c906a7d

    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
    ------------------------------



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

    Posted 12-21-2021 13:54
    Hi Bob, 
    Thank you for sharing the above information. 
    Currently, one of our client uses Mapinfo Pro Version 2011 to maintain Inventory locations and Customer's Database and for proximity Searches, creating basic Maps, and demographic Maps.
    Along with MapInfo, they use a third-party add-on tool called 'Smart Tools' - the tool only executes queries and display data, its objective is to bring locations associated with the output file from the Location Charting application and map it on top of MapInfo created Layers.

    The client recently upgraded their Mapinfo Pro Version to 2019 and since then facing the following problem.
    -The Map Legend which displays the Design code and the matching location pin color is unable to load and appear on the banner.
    -Smart .exe file was not able to load with the MapInfo application.
     
    Is it possible after the move on Python from MapBasic the third-party add-on tool isn't able to retrieve and display the legends correctly?
    Please advise. 
    Regards

    ------------------------------
    Hrishi M
    Knowledge Community Shared Account
    ------------------------------



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

    Employee
    Posted 12-22-2021 02:51
    Hi Hrishi

    Did they jump from MapInfo Pro v(20)11 to MapInfo Pro v2019? MapInfo Pro v11 is around 10 years old and based on the 32-bit version of MapInfo Pro.

    I'm just trying to make sure we have the facts right

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



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

    Posted 12-23-2021 04:08
    Hi Peter, 
    Thank for your response.
    Yes, they upgrading directly to Mapinfo 2019 or now 2020. 
    Regards

    ------------------------------
    Hrishi M
    Knowledge Community Shared Account
    ------------------------------



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

    Employee
    Posted 12-23-2021 04:23
    Hi
    Reading your post again, I can see you wrote that the "smart....exe" wasn't able to start. Sounds as if this tool is a standalone application that connects to MapInfo Pro. When we moved from 32-bit to 64-bit, we also made some minor changes to the way third-party application would connect to MapInfo Pro.

    1. The MapInfo Pro executable is no longer named "mapInfow.exe" but "MapInfoPro.exe"
    2. When connecting to MapInfo Pro Object through code, you should use "MapInfo.Application.x64" instead of "MapInfo.Application"

    I suppose these two issues could prevent this third-party application from "connecting" to the newer versions of MapInfo Pro. Would you be able to reach out to the vendor of this tool?

    With regards to your second question (The Map Legend which displays the Design code and the matching location pin color is unable to load and appear on the banner.), I'd need a bit more details on what you are seeing or not seeing on your screen, and how you are trying to create these legends.

    We have made some changes over time to the way we work with legends, not only when we moved to 64-bit but also prior to this move.

    Can you maybe share some screenshots of what you saw earlier in MapInfo Pro v11 and what you currently are seeing in MapInfo Pro v2019/v2021?


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



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

    Posted 12-23-2021 05:12
    Hi Peter, 
    Thank you 
    Unfortunately, we don't have access to the source code nor able to reach out to the vendor of the third-party app. Kindly let me know any workaround if available.

    The SMART tool is also called the 'Showings Program' as it's used merely to bring on the locations associated with 1 or many showings in a contract – to read an output file from the Location Charting application to map out those specific locations in Mapinfo.

    I am hereby sharing the screenshot of what the client is currently viewing after the upgrade, earlier the client was able to see the location details and the legends clearly but after the upgrade, they were unable to see any details.  

     


    ------------------------------
    Hrishi M
    Knowledge Community Shared Account
    ------------------------------



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

    Employee
    Posted 01-05-2022 04:07
    Hi

    I guess the layout and legends were earlier created by the Smart app and as this tool no longer runs in MapInfo Pro, the legends aren't updated correctly/at all.

    You will need to get the functionality of the Smart app implemented in a different way if you can't reach out to the developer of that app.

    To me it sounds as if you have one location to which a number of other locations have been associated. The tool in question helps you show these associated locations on the map and in a legend. Is that close to what the tool did?

    Anyway, this has nothing to do with the support for Python.

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



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

    Posted 01-11-2022 11:09
    Edited by Peter Møller 01-12-2022 01:44
    Hi,

    Thanks for the rundown of the python features which are now included in Mapinfo Pro. 

    I am wanting to use python commands to assess the raster operations (specifically "RasterAnalysis.Polygonise") I have an input raster and also a levels file which I want to use for the polygonisation method, however, I am unable to figure out how to call and write the function, I currently have the following:

    import clr
    import os
    clr.AddReference("MapInfo.RasterEngine.IO")
    clr.AddReference("MapInfo.RasterEngine.Common")
    clr.AddReference("MapInfo.RasterEngine.Operations")
    from MapInfo.RasterEngine.Operations import RasterAnalysis
    from MapInfo.RasterEngine.Common import PolygoniseParameters

    input = "Raster_FilePath.asc"
    output = "RasterPolygonise_FilePath.TAB"
    levels = "Intervals_FilePath.level"

    RasterAnalysis.Polygonise(input,output,PolygoniseParameters((levels)))

    Any help would be much appreciated. 
    Thanks

    ------------------------------
    Luke Lynx
    Knowledge Community Shared Account
    ------------------------------