MapInfo Pro

 View Only
  • 1.  MapBasic to create points from csv

    Posted 07-30-2024 03:12
      |   view attached

    Hi everyone,

    I have a csv file contains LATTIDUE/LONGITUDE, need to create points to display on map, I know the manual way, but need to do it in Mapbasic cause I have lots of files to do the same operation, part of codes as below, but got a error saying: unknown error encountered @ Alter Table sTabName2(Add geomerty obj)

    anything wrong with my codes? any one can help? Thanks.

    Open Table sTabFile2 As sTabName2 Interactive
     
    Alter Table sTabName2 (Add geometry obj)
    Update sTabName2  
      set  geometry = CreatePoint(LATITUDE,LONGITUDE)


    ------------------------------
    Yong Zhang
    Knowledge Community Shared Account
    Burlington MA
    ------------------------------


  • 2.  RE: MapBasic to create points from csv

    Employee
    Posted 07-30-2024 04:20

    Hi Yong

    You need to change your two statements to this:

    Create Map For Table sTabName2 CoordSys CoordSys Earth Projection 1, 104

    Update sTabName2 Set OBJ = CreatePoint(LATITUDE, LONGITUDE)

    I guessed that the coordinates are in Lat/Long WGS84. If that's not the case, you need to change the parameters after Coordsys.

    If you use MapInfo Pro v2021 or newer, you can open the CSV file using the OGR Vector File option. This will allow you to create points automatically when opening the file. Check out 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
    ------------------------------



  • 3.  RE: MapBasic to create points from csv

    Posted 08-05-2024 00:42

    Thanks Peter,

    my idea is to open the csv file which has lat/long, convert it into points map and then display it in MapInfo by MapBasic as mentioned before.

    issues found:

    1. after commit table to a new table, need to wait for the new table to be saved, then it can be opened, do we have something like sleep(1 or 2 seconds) function?
    2. the coordinates displayed on map are not correct:
      should be
    3. the  code you mentioned: Create Map For Table sTabName2 CoordSys CoordSys Earth Projection 1, 104 didn't pass on my machine. my mapinfo version is Mapinfo Pro 2023 Release build 97, after removed Table and CoordSys, it worked.the full scripts as below:
    Dim sFileName As String
    Dim sTabFile As String
    Dim sTabFile2 As String
    Dim sTabName As String 
    Dim sTabName2 As String 
    Dim nDot As Integer 
    Dim nLastBackslash As Integer
     
    sFileName = FileOpenDlg("Select CSV File", "*.csv", "CSV", "Select MDT CSV File") 
    sTabFile = RegExReplace$(sFileName,"([a-z_0-9]+)(\.csv)", "$1.TAB") 
    sTabFile2 = RegExReplace$(sFileName,"([a-z_0-9]+)(\.csv)", "$1_2.TAB")
    
    nDot = InStr(1,sFileName, ".")
    nLastBackslash = InStr(nDot-11,sFileName, "\")
    sTabName = Mid$(sFileName, nLastBackslash + 1, nDot - nLastBackslash - 1)
    sTabName2 = sTabName + "_2" 
    
    Register Table sFileName  TYPE ASCII Delimiter 44 Titles Charset "WindowsLatin1" Into sTabFile
    Open Table sTabFile As sTabName Interactive
    Commit Table sTabName As sTabFile2 TYPE NATIVE Charset "WindowsLatin1" Background supportmz off 
     
    Close Table sTabName Interactive 
    Open Table sTabFile2 As sTabName2 Interactive 
    
    Create Map For sTabName2 CoordSys Earth Projection 1, 104
    Update sTabName2   
    set  OBJ = CreatePoint(LATITUDE, LONGITUDE) 
    Commit Table sTabName2 Interactive 
    Close Table sTabName2 Interactive 
    Open Table sTabFile2 As sTabName2 Interactive 
    Add Map Auto Layer sTabName2


    ------------------------------
    Yong Zhang
    Knowledge Community Shared Account
    Burlington MA
    ------------------------------



  • 4.  RE: MapBasic to create points from csv

    Posted 08-05-2024 00:48
    Edited by Peter Møller 08-05-2024 02:17

    I also tried the OGR way:

    need help on Symbol settings, if I'd like to set different color for each file, how can I set it automatically as red below? tried to dim variable but didn't work.

    Register Table sFileName  TYPE "OGR"  TABLE sTabName  DRIVER "CSV" OpenOptions "AUTODETECT_TYPE=YES;AUTODETECT_WIDTH=STRING_ONLY;X_POSSIBLE_NAMES=lon*,x*;Y_POSSIBLE_NAMES=lat*,y*;Z_POSSIBLE_NAMES=z*"  Charset "UTF-8"  Cache On  Symbol (34,16711680,12)  Linestyle Pen (1,2,0)  Regionstyle Pen (1,2,0)  Brush (2,16777215,16777215)  Font ("Arial",0,9,0)   Into sTabFile



    ------------------------------
    Yong Zhang
    Knowledge Community Shared Account
    Burlington MA
    ------------------------------



  • 5.  RE: MapBasic to create points from csv

    Employee
    Posted 08-05-2024 02:25

    Hi

    Can you try this:

    Dim nColor As Integer
    Dim symUse As Symbol
    
    nColor = 16711680	'RED
    symUse = MakeSymbol(34, nColor,12)
    
    Register Table sFileName  
    	TYPE "OGR"
    	TABLE sTabName
    	DRIVER "CSV"
    	OpenOptions "AUTODETECT_TYPE=YES;AUTODETECT_WIDTH=STRING_ONLY;X_POSSIBLE_NAMES=lon*,x*;Y_POSSIBLE_NAMES=lat*,y*;Z_POSSIBLE_NAMES=z*"
    	Charset "UTF-8"
    	Cache On
    	Symbol symUse  Linestyle Pen (1,2,0)  Regionstyle Pen (1,2,0)  Brush (2,16777215,16777215)  Font ("Arial",0,9,0)
    	Into sTabFile


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



  • 6.  RE: MapBasic to create points from csv
    Best Answer

    Employee
    Posted 08-05-2024 02:17

    Hi

    I made a few adjustments to your code. Can you see if it works now?

    Dim sFileName As String
    Dim sTabFile As String
    Dim sTabFile2 As String
    Dim sTabName As String
    Dim sTabName2 As String
    'Dim nDot As Integer
    'Dim nLastBackslash As Integer
    
    sFileName = FileOpenDlg("Select CSV File", "*.csv", "CSV", "Select MDT CSV File")
    sTabFile = RegExReplace$(sFileName,"([a-z_0-9]+)(\.csv)", "$1.TAB")
    sTabFile2 = RegExReplace$(sFileName,"([a-z_0-9]+)(\.csv)", "$1_2.TAB")
    
    'nDot = InStr(1,sFileName, ".")
    'nLastBackslash = InStr(nDot-11,sFileName, "\")
    'sTabName = Mid$(sFileName, nLastBackslash + 1, nDot - nLastBackslash - 1)
    sTabName = PathToTableName$(sFileName)
    sTabName2 = sTabName + "_2"
    
    Register Table sFileName TYPE ASCII Delimiter 44 Titles Charset "WindowsLatin1" Into sTabFile
    Open Table sTabFile As sTabName Interactive
    'Commit Table sTabName As sTabFile2 TYPE NATIVE Charset "WindowsLatin1" Background supportmz off
    Commit Table sTabName As sTabFile2 TYPE NATIVE Charset "WindowsLatin1" supportmz off
    
    Close Table sTabName Interactive
    Open Table sTabFile2 As sTabName2 Interactive
    
    'Create Map For sTabName2 CoordSys Earth Projection 1, 104
    Create Map For sTabName2 CoordSys Earth Projection 1, 104
    Update sTabName2
       set  OBJ = CreatePoint(LONGITUDE, LATITUDE)
    
    Commit Table sTabName2 Interactive
    Add Map Auto Layer sTabName2

    1. You can use the function PathToTableName$() to get to the table name from a fully qualified file name.
    2. If you remove the Background keyword from the Commit Table statement, your code will wait ffor the process to finish and then continue with the next statement.
    3. I can't see what was wrong with the Create Map statement. I have now copied it from the MapBasic window after making a new table mappable. The error could be caused further up in your code. Not sure. I got the code above to compile
    4. The order of the LATITUDE and LONGITUDE columns in the Update statement needed to be reversed.

    Let us know if it works



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



  • 7.  RE: MapBasic to create points from csv

    Posted 08-14-2024 19:38

    Thanks. it worked when compiled in MapBasic.



    ------------------------------
    Yong Zhang
    Knowledge Community Shared Account
    Burlington MA
    ------------------------------