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