MapInfo Pro

Expand all | Collapse all

Python on Mapinfo Pro 2019

  • 1.  Python on Mapinfo Pro 2019

    Posted 6 days ago
    Hello,

    While I can see some info on Python for MI pro 17.0.3 , I can't find anything as how to get started in Python on Mapinfo 2019.
    There nothing in the user guide, nothing on the release notes, etc...

    Would you have a procedure on how to get started with Python on Mapinfo Pro 2019 please?
    Any code sample are also welcome.

    Thanks.

    ------------------------------
    Alban Spella-Barberet
    Senior Planner - Wireless RF/TX Engineering & Deployment Standards
    NBN Co Limited
    North Sydney
    ------------------------------


  • 2.  RE: Python on Mapinfo Pro 2019

    Pitney Bowes
    Posted 6 days ago
    Edited by Shweta Shukla 6 days ago
    Hello Alban,

    You  will find the sample code in "C:\Program Files\MapInfo\MapBasic\Samples\RIBBONINTERFACE\Python" for starting with Python.

    If you don't have the latest MapBasic, you can download the 2019 version of MapBasic from Link. This contains a short description about the new additions and the Installation instructions.

    Regards,
    Shweta

    ------------------------------
    Shweta Shukla
    Pitney Bowes Software India
    Noida
    ------------------------------



  • 3.  RE: Python on Mapinfo Pro 2019

    Pitney Bowes
    Posted 5 days ago
    Alban,

    Please also take a look at ExtensibilityReferenceHelp that comes with MapBasic 2019 install. It has information on how to setup Python environment for MapInfo Pro. For 2019.1 release we are planning to greatly simplify the process.

    Andrei

    ------------------------------
    Andrei Veselov
    Pitney Bowes Software Inc.
    Troy NY
    ------------------------------



  • 4.  RE: Python on Mapinfo Pro 2019

    Pitney Bowes
    Posted yesterday
    Here is the section on setting up to use Python in Pro 2019. Starting in 2019.1, Pro will take care of installing and setting up Python, so this will get easier soon.

    Using Python Language

    MapInfo Pro Extensibility Reference

    This topic contains the following sections:

    • Using Python Language
    • Python Setup
    • Installing GDAL Python Bindings
    • Machine Environment Setup
    • Setup Python Debugging using Visual Studio Code (VSCODE)
    • Related Articles
    • Creating a MapInfo Pro Python Add-in
    • Debugging the Python Add-in in VSCODE
    Using Python Language

    MapInfo Pro now supports writing add-ins in the Python language. This allows developers to take advantage of the Python language and its existing libraries/modules when writing MapInfo Pro add-ins. Python, with its feature rich set of libraries, is a very popular platform for Data Analysis and Visualization. We have provided Python developers and data analysts an easier way to work with MapInfo data by providing access to MapInfo Pro table data. While you can make use of any of the libraries and functionality available to Python, the ability to perform data analysis on MapInfo Pro table attribute data using the rich set of available data analysis tools (for example, Pandas, SciPy, Matplotlib, etc.) adds more capabilities and functionalities to existing ones.

    Python Setup

    • Get the latest 64-bit installer of Python version 3.7 version from: http://www.python.org/downloads/

    • Make sure to use the Windows x86-64 executable installer under Downloads.

    • Run the installer as an Admin and choose Custom Install.

    • Make sure you are installing for ALL Users (Install Directory will default to: C:\Program Files\Python37).

    • Check the box for setting PATH variable and pre-compiled binaries.

    Installing GDAL Python Bindings

    To install Python Bindings for GDAL, download GDAL-3.0.1-cp37-cp37m-win_amd64.whl (or latest for Python 3.7 and amd64) from http://www.lfd.uci.edu/~gohlke/pythonlibs/#gdal

    Once you have the .WHL wheel file, open a command prompt with Administrator privileges and run the following command:

    pip install (path to downloaded file)

    This will install the GDAL Python Bindings, which will allow you to open Native Tab files using the MITAB driver only.

    Machine Environment Setup

    Set the following two SYSTEM environment variables to the Python installation directory (for example, C:\Program Files\Python37):

    • PYTHONPATH

    • PYTHONHOME

    Setup Python Debugging using Visual Studio Code (VSCODE)

    1. Install the Python and pylint extensions and restart VSCODE.

    2. Initialize configurations. A configuration drives VS Code's behavior during a debugging session. Configurations are defined in a launch.json file that's stored in a .vscode folder in your workspace.

    3. To initialize debug configurations, first select the Debug View in the sidebar. If you don't yet have any configurations defined, you'll see "No Configurations" in the drop-down list, and a dot on the settings icon.

    4. To generate a launch.json file with Python configurations:

      • Select the Settings button or use the Debug > Open configurations menu command.

      • In the Select Environment drop-down list that appears, select Python.

      The Python extension then creates and opens a launch.json file that contains number of pre-defined configurations. You can modify configurations (to add arguments, for example), and also add custom configurations.
      Python: Attach or Python: Remote Attach configuration must be defined like this.
      {
          "name": "Python: Remote Attach",
          "type": "python",
          "request": "attach",
          "port": 5678,
          "host": "localhost",
          "pathMappings": [
              {
                  "localRoot": "${workspaceFolder}",
                  "remoteRoot": "${workspaceFolder}"
              }
          ]
      }

    5. After the initial configuration is done, setup VSCODE for remote debugging.

      • Install ptvsd using python -m pip install --upgrade ptvsd into your environment.

      • For debuggingm the default Port for the ptvsd use is 5678. You may need to open the port for debugging in the appropriate firewall or other networking configuration.

    For debuggingm the default Port for the ptvsd use is 5678. You may need to open the port for debugging in the appropriate firewall or other networking configuration.

    For more details refer to VSCODE python debugging : https://code.visualstudio.com/docs/python/debugging

    Related Articles

    Creating a MapInfo Pro Python Add-in

    To create a MapInfo Pro add-in in python:

    1. Set up your development and debugging environment, Setting Up MapInfo Pro Python Add-in.

    2. Use one of the existing python add-in templates to start with your add-in. The MapBasic version 2019 installation contains two python add-in templates for creating a simple MapInfo Pro add-in or a layout custom frame add-in in python.

      • SAMPLES\RIBBONINTERFACE\Python\py_addin_templates\Simple

      • SAMPLES\RIBBONINTERFACE\Python\py_addin_templates\CustomFrame

    3. Copy one of the add-in templates listed above and start modifying it. Start with renaming the files (PY and MB) to your add-in name.

    4. Rename the module reference as per your new file names. The PY file contains TODO comments to help you modify the code as per your requirement.

    5. To load the add-in into MapInfo Pro, compile the MB file using MapBasic version 2019 and run the MBX.

    Note that the name of the MapBasic MB file should be the same as the main python module file. Also, the class below should be present in the main Python module file to load the add-in.

    # this class is needed with same name in order to load the python addin and can be copied 
    # as it is when creating another addin.
    class main():
        def __init__(self, imapinfopro):
            self._imapinfopro = imapinfopro
    
        def load(self):
            try:
                # uncomment these lines to debug the python script using VSCODE
                # Install ptvsd package into your environment using "pip install ptvsd"
                # Debug in VSCODE with Python: Attach configuration
    
                # ptvsd.enable_attach()
                # ptvsd.wait_for_attach()
    
                # here initialize the addin class
                if self._imapinfopro:
                    # obtain the handle to current application if needed
                    thisApplication = self._imapinfopro.GetMapBasicApplication(os.path.splitext(__file__)[0] + ".mbx")
                    # TODO: change your addin class name here.
                    self._addin = MyAddin(self._imapinfopro, thisApplication)
            except Exception as e:
                CommonUtil.sprint("Failed to load: {}".format(e))
    
        def unload(self):
            try:
                if self._addin:
                    self._addin.unload()
                    del self._addin
                self._addin = None
            except Exception as e:
                CommonUtil.sprint("Failed to unload: {}".format(e))
    
        def __del__(self):
            self._imapinfopro = None
            pass

    Debugging the Python Add-in in VSCODE

    1. In VSCODE, open the folder of your python add-in or any python add-in sample (for example, .\SAMPLES\RIBBONINTERFACE\Python\HelloPython).

    2. Compile the add-in *.mbx using MapBasic IDE or Notepad++, etc.

    3. In your python add-in source code, add the following lines:

      import ptvsd 
      # Allow other computers to attach to ptvsd default port.
      ptvsd.enable_attach()
      # Pause the program until a remote debugger is attached
      ptvsd.wait_for_attach()
      In case you are debugging an existing sample uncomment the code snippet above.

    4. Run the add-in MBX in MapInfo Pro v17.0.3 or above. Now once MBX is run in MapInfo Pro, you will notice a wait cursor denoting that the process in waiting for some debugger to attach to it.

    5. Switch to Debug View in VS Code, and select Python: Attach configuration.

    6. Set a breakpoint in the code where you want to start debugging. Setting a single breakpoint on the statement immediately following the ptvsd.wait_for_attach() line may not work. Set at least one other breakpoint on another statement.

    7. Start the VS Code debugger using the modified Python Attach configuration. VS Code should stop on your locally-set breakpoints, allowing you to step through the code, examine variables, and perform all other debugging actions. Expressions that you enter in the Debug Console are run on the remote computer as well.

    8. During remote debugging, the debugging toolbar appears. On this toolbar, the disconnect button (Shift+F5 ) stops the debugger and allows the remote program to run to completion.



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