MapInfo Pro Developers User Group

  • 1.  Read and access TAB file attributes data in Python

    Posted 10-25-2021 11:08
    Hello,

    I've written a code in Python that reads files in CSV format, that were initially created as TAB files in MapInfo Pro (2019). Instead of reading CSV files, I would like to adapt the code so that it directly reads TAB files.

    Does anyone know how could I implement it such that I can access the attributes' values of the TAB file for further process ?

    Here below is the piece of code that reads a CSV file.
    def loadConcatFile(fileName):
          
        df = pd.read_csv(fileName, skiprows=[0], names=['sliceId', 'offset', 'Ln', 'Lt','Z_m'], sep=',')
        df = df.applymap(float)
        df['sliceId'] = df.applymap(int)
           
        num = df['sliceId'].values
        sliceId = df['sliceId'].unique()
        d = df['offset'].values
        ln = df['Ln'].values
        lt = df['Lt'].values
        Z = df['Z_m'].values
    
    ....​


    Thanks for your help !



    ------------------------------
    Annie Royen
    RTI Exploration
    Tarascon
    ------------------------------


  • 2.  RE: Read and access TAB file attributes data in Python

    Posted 10-26-2021 11:12
    Hi. Use EFAL dll

    Regards





  • 3.  RE: Read and access TAB file attributes data in Python

    Employee
    Posted 10-27-2021 08:28
    Hi Annie,
    You can use the GDAL OGR module to read and access TAB file in Python within MapInfo Pro. Check below example.

    ### Description: Opens the first table in the Catalog using ogr.
    ### Description: Prints the field names and attributes.
    ### Category: GDAL
    
    import os
    from osgeo import ogr
    
    if pro.Catalog.Tables.Count > 0:
        path = pro.Catalog.Tables[0].FullPath
    
        dataSource = ogr.Open(path)
    
        # Check to see if file is found.
        if dataSource is None:
            print ('Could not open %s' % (path))
        else:
            print ('Opened %s' % (path))
            layer = dataSource.GetLayer()
            featureCount = layer.GetFeatureCount()
            print ("Number of features in %s: %d" % (os.path.basename(path),featureCount))
            field_names = [field.name for field in layer.schema]
            
            for feature in layer:
                print("---------------------------------------------------------")
                for field_name in field_names:
                    print ("{}: {}".format(field_name, feature.GetField(field_name)))
            layer.ResetReading()
    

    Thanks
    Anshul



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



  • 4.  RE: Read and access TAB file attributes data in Python

    Posted 06-23-2025 16:42
    Edited by Peter Møller 06-24-2025 00:59

    Hi Anshui,

    I feel like this is probably the best place to ask. Please do let me know if otherwise. 

    For some reason, I need to use OGR (see below example) to extract certain customized metadata stored in MapInfo ".tab" file, i.e. a date stamp. This customized metadata is proven to be available when I open and see using MapInfo Pro (or with notepad). And in same way I can almost extract all other attributes such as "GeomType", "LayerName", etc. , but just not the fields from Metadata (not even the regular fields like "\IsReadOnly", "Map Catalog").

    driver = ogr.GetDriverByName("MapInfo File")
    datasource = driver.Open(file_path, update=0 or 1)
    layer_metadata = datasource.GetLayerByIndex(0).GetMetadata()

    Ideally, it should just work with one line like this: 

    datasource.GetMetadataItem("Our_Own_Metadata_Field")
    Can you please try at your end and give me some suggestions? Thank you so much. 



    ------------------------------
    Ferhad Pakzat

    Bc Hydro And Power Author
    Burnaby BC
    ------------------------------



  • 5.  RE: Read and access TAB file attributes data in Python

    Employee
    Posted 06-24-2025 01:03

    Hey Ferhad

    You can consider using the MapBasic function GetMetadata$() to extract the necessary metadata.

    isReadOnly = eval('GetMetadata$("\IsReadOnly")')



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



  • 6.  RE: Read and access TAB file attributes data in Python

    Posted 06-24-2025 12:43

    Hi Peter,

    Thanks for your quick respond. I've already tried with MapBasic functions. It worked fine and I'll definitely use it that way for large portion of my work. But I'm just doing a little experiment to see if I can achieve what I need by using secondary (or more) thread for scenarios like downloading tables, checking metadata etc. However, I've heard (and experienced) that MapBasic commands are not so thread-safe. (correct me if I'm wrong).

    And so far OGR (not GDAL) way of opening table seems to be working fine on the non-major threads. But you guys know better of course. :) 

    Thanks again. 



    ------------------------------
    Ferhad Pakzat

    Bc Hydro And Power Author
    Burnaby BC
    ------------------------------



  • 7.  RE: Read and access TAB file attributes data in Python

    Posted 06-26-2025 13:36

    Just some updates:

    • Tried reading SHP file (ogr.GetDriverByName("ESRI Shapefile")) works fine and reading metadata correctly.
    • Tried reading with another format of MapInfo file (ogr.GetDriverByName("MapInfo EFAL")) and also failed.

    Still no clues why. Maybe I'm missing sth. 



    ------------------------------
    Ferhad Pakzat

    Bc Hydro And Power Author
    Burnaby BC
    ------------------------------



  • 8.  RE: Read and access TAB file attributes data in Python

    Employee
    Posted 06-27-2025 03:01

    Hey

    I can see that it says: "Note that relatively few formats return any metadata at this time." on the GDAL Documentation page for GetMetadata.

    Did you try one of these methods to see if you can get a list of metadata (GetMetadata()) or domains (GetMetadataDomainList())?

    Do they throw an error, or do they just not return anything?



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



  • 9.  RE: Read and access TAB file attributes data in Python

    Posted 06-27-2025 14:00

    OGR way works slightly better than GDAL, but they both don't return any metadata at dataset level or layer level. So does GetMetadataDomainList(). There was nothing returned. They don't throw errors just empty returns. I guess the documentation was right. Thanks. 



    ------------------------------
    Ferhad Pakzat

    Bc Hydro And Power Author
    Burnaby BC
    ------------------------------