MapInfo Pro Developers User Group

 View Only
  • 1.  Passing data directly from Python into MapInfo

    Posted 09-26-2023 08:53

    Hi,

    Is there a way for me to pass data from a 2D python list directly into a MapInfo table? In the first case I can use Python to write to a .csv or .xlsx file and then get MapInfo to open it as a table, but I feel that passing the data directly into memory would be a much more elegant implementation.



    ------------------------------
    Romano Agostini
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Passing data directly from Python into MapInfo

    Employee
    Posted 10-05-2023 02:40

    Hi Romano,

    In the Python Quick Start add-in you can find this example:

    ### Description: This script show how to perform insert, update, and delete operation on a MapInfo Table
    ### Category: Catalog
    
    from os.path import abspath, join, dirname
    
    try:
        worldcap = pro.Catalog.OpenTable(abspath(join(dirname(get_current_module_path()), r"..\Data\World\worldcap.TAB")), "worldcap")
        if worldcap:
            print("Initial RowCount: {}".format(worldcap.RowCount()))
    
            # insert a new row
            do("Insert Into {} Values (\"Test\", \"Test\", 0)".format(worldcap.Alias))
            print("RowCount: {}".format(worldcap.RowCount()))
    
            # update a newly inserted row
            do("Update {} Set Cap_Pop = 2000 where Capital = \"Test\"".format(worldcap.Alias))
    
            # save table edits
            do("Commit Table {}".format(worldcap.Alias))
            print("RowCount: {}".format(worldcap.RowCount()))
    
            # delete newly inserted row from table
            do("Delete From {} where Capital = \"Test\"".format(worldcap.Alias))
            do("Commit Table {}".format(worldcap.Alias))
            
            # pack the table
            do("Pack Table {} Graphic Data".format(worldcap.Alias))
            print("Final RowCount: {}".format(worldcap.RowCount()))
    
            worldcap.Close()
    except Exception as e:
        print("Error: {}".format(e))

    This is a basic way where you from Python use the do method to execute MapBasic statements.

    If you check out the Table class in the MapInfo Pro Extensibility Reference Help, you may find some other ways to do this too.



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