MapInfo Pro

 View Only
  • 1.  Opening a CSV with MapBasic

    Posted 17 days ago

    I really don't understand why every time I use code to open a CSV, the table opens as read only. 

    Register Table ImportPath Type ASCII Delimiter 44 Charset "WindowsLatin1" into SavePath
    Open Table SavePath

    Why if BOTH register and open commands in MAPBASIC provide Read Only clauses, do I have to register the table as one name, open it, save a copy of it, close the origina, open the copy and delete the original?!

    Unless I am (hopefully) missing something obvious, what an absolute faff!



    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------


  • 2.  RE: Opening a CSV with MapBasic

    Posted 16 days ago

    The TAB file is simply a "pointer" back to the text file. There is a check box at the bottom of the dialog stating, "Create copy in MapInfo format for read/write." Use that and problem solved.

     

    -- Steve Wallace

     

     






  • 3.  RE: Opening a CSV with MapBasic

    Posted 16 days ago

    Hi Steve. As stated I am using MapBasic, I'm not performing the task manually via dialogs. 



    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------



  • 4.  RE: Opening a CSV with MapBasic

    Employee
    Posted 16 days ago

    Hi Ryan

    As Steve wrote, a CSV file is read-only in MapInfo Pro.

    The Reigster Table statement creates a TAB that refers to the CSV file. The TAB file holds information about how to read the CSV file. That's why the table is read-only when you open it in MapInfo Pro.

    To make it editable, save a copy of the YAB referencing the CSV into a Native/NativeX file.

    The other benefit is that performance improves with a Native/NativeX TAB file compared to a CSV; Especially, a CSV with many records.

    Alternatively, you can open the CSV using OGR Vector files. This comes with some benefits. MapInfo Pro can automatically detect the separator. MapInfo Pro can detect columns with X and Y coordinates and create points when opening the file. MapInfo Pro can detect columns with WKT (Well Known Text) and convert these to spatial objects when opening the file. MapInfo Pro can write edits back to the CSV file, ie. the file is no longer read-only.

    OGR Vector Files has been supported since MapInfo Pro v2021.

    Read more in this article MapInfo Monday: Improved way to opening CSV Files in MapInfo Pro v2021



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



  • 5.  RE: Opening a CSV with MapBasic

    Posted 15 days ago

    Yeah, I get that. But seeing as both open and register statements contain a read only parameter, it would be nice if that actually did something. I'm not sure what the point is of having a table referring to a CSV AND a copy of it for editing. If I want to register/open it as editable, MapInfo should be embedding that table in the native format, not keeping it read only and insisting I make another copy. 

    Interesting about the OGR Vectors. Will investigate that some more!

    Thanks

    R



    ------------------------------
    Ryan Cook
    ORH LTD
    ------------------------------



  • 6.  RE: Opening a CSV with MapBasic

    Employee
    Posted 15 days ago

    Hi Ryan

    I can see where you come from but unfortunately, that's not how it works. The Readonly keyword only works one way: It makes a table read-only. Omitting the keyword doesn't mean that the table will be writable.

    I use this structure to achieve what you are after:

    sTempPath = PathToDirectory$(TempFileName$(""))
    sSavePath = Left$(ImportPath, Len(ImportPath) - 4) + ".TAB
    
    Register Table ImportPath 
      Type ASCII 
      Delimiter 44 
      Charset "WindowsLatin1" 
      into sTempPath
    
    Open Table sTempPath
    Commit Table PathToTableName$(sTempPath) As sSavePath
    Close Table PathToTableName$(sTempPath)
    Open Table sSavePath

    If you do it often, convert it into a function:

    Function OpenASCIIAsNativeTAB(ByVal ImportPath As String, ByVal bTitles As Logical, ByVal nDelimChar As Integer, ByVal sCharSet As String) As String
    
    Dim sSavePath, sTempPath As String
    
    OpenASCIIAsNativeTAB = ""
    
      sTempPath = PathToDirectory$(TempFileName$(""))
      sSavePath = Left$(ImportPath, Len(ImportPath) - 4) + ".TAB
    
      If bTitles Then
        Register Table ImportPath
          Type ASCII
          Delimiter nDelimChar
          Titles
          Charset sCharSet
          into sTempPath
      Else
        Register Table ImportPath
          Type ASCII
          Delimiter nDelimChar
          Charset sCharSet
          into sTempPath
      End If
    
      Open Table sTempPath
      Commit Table PathToTableName$(sTempPath) As sSavePath
      Close Table PathToTableName$(sTempPath)
      Open Table sSavePath
    
      OpenASCIIAsNativeTAB = sSavePath
    
    End Function



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