Professional Documents
Culture Documents
I have been around a while and have helped out a number of people writing MapBasic
applications, be that on MapInfo-L, via gis.stackexchange.com or via emails or telephone
conversations.
So what are the typical issues MapBasic developers are experiencing? Where do they
struggle the most when trying to automate a process in MapInfo Pro with a scripted coded
with MapBasic?
In this article I'll give you a top 4 of the issues I have come across many times in my almost
2 decades of working with MapInfo Pro and MapBasic. I could have found more than 4 but
just these four do give quite a lot of input for an article.
This also means that you might be seeing a follow-up article from me later.
Messaging
1. Dealing with Coordinate Systems
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 1/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
MapInfo
Search
Pro can work with several levels of coordinate
10 systems, Try
orPremium
projections
Free as they
for 1 Month
sometimes are referred to.
The table can be shown in a map window with a different coordinate system than the
table itself.
MapInfo Pro can work with a different coordinate system than the table and the map
window
and finally can a MapBasic application be using yet another coordinate system
So what should you know about this and how can you take advantage of this in your
application?
First of all, you can get to the coordinate system used by most of these levels.
For a table you can get to it's coordinate system clause with or without bounds and you can
get to the coordinate system name.
TableInfo( some_table_id,
TAB_INFO_COORDSYS_CLAUSE_WITHOUT_BOUNDS )
For a map window you can again get to it's coordinate system clause, with and without
bounds:
For the MapInfo Pro session you can also get to the currently used coordinate system:
Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 2/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
SessionInfo( SESSION_INFO_COORDSYS_CLAUSE
Search
10 ) Try Premium Free
for 1 Month
There is also a number of functions for converting to and from the MapInfo coordsys string:
CoordSysStringToWKT$( wkt_string )
EPSGToCoordSysString$( epsg_string )
CoordSysStringToPRJ$( prj_string )
MapBasic also has a built-in function that can show a dialog and let the user pick a desired
coordinate system:
Like Comment Share 29 · 22 comments
Now how to you specify a coordinate system, for instance before extracting coordinates
from a spatial object or before creating a spatial object from coordinates? The answer is to
use the Set CoordSys statement.
This statement will set the coordinate system of the current session. The session will in most
cases be your MapBasic application, but it can also be the running MapInfo Pro session if
the statement is used within that session, eg. thru the MapBasic window.
The nice thing about the Set Coordsys statement, or in fact the Coordsys clause
specifically, is that you don't need to explicitly specify a coordinate system; you can refer to
a coordinate system of an existing item in MapInfo Pro, that is a map, a table or even the
session itself.
In most cases you will either refer to a table or a map window, depending on what you are
doing. So here is how to set the coordinate system using an existing item:
If you don't specify a coordinate system, you'll be using the default Longitude/Latitude - or
is that Latitude/Longitude ? - coordinate system. Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 3/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
One of the often used things in MapBasic is to loop thru a table to read values from the table
and then react to or modify these values.
However one seems better than the others when it comes to looping a table: Do Until ...
Loop. At least that's the one I have been using for years. You can also use Do While ...
Loop and While ... Wend. But I will recommend that you stay away from For ... Next when
looping thru a table. I'll get back to why.
Now you need a few other statements as well when looping a table:
EOT( some_table_name )
In the example above I loop the table called MY_TABLE. For each record I read the
RowID, the hidden dynamic column referring to a specific post in a table or query, and
the spatial object from the column called OBJ.
Oh, and I set my application to use the same coordinate system as the table.
Now this loop doesn't really do anything clever yet. I would need to add my logic just before
Messaging
the Fetch Next From statement. We'll get to that in a minute.
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 4/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
I also promised to explain why you shouldn't use the10 For ... NextTry
Search
loop when
Premium Free working with a
for 1 Month
table. The problem isn't as much the loop, but more that people tend to combine this loop
with a different Fetch statement; Fetch Rec nRec From.
This Fetch statement will put the table cursor at a specific row in the table, which in most
case is fine. But it has a drawback; it will fail if the specific row has been deleted.
Fetch Next From, on the other side, will skip the deleted records during the loop.
As long as your application is using hard-coded table and column names the example above
will work quite well for you. But as soon as you get into the land of dynamic table and
column names, you are heading for trouble.
So how do you read values from a table where the table name and/or column is dynamic and
therefor stored in a variable? Well, that's where the Alias variable comes into play.
If you look a the MapBasic Help system, you'll find this description for the Alias variable:
Column name.
Well, I think you do - at least the number of questions related to this variable type indicate
that you do.
An Alias variable can be used to point at a combination of a table and column name. You
will often use this variable type for dynamic table and/or column name combinations, but
you can also use it for hard code names.
Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 5/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
Normally
Search
you refer to a table and column name combination
10 by concatenating
Try Premium Free the two with
for 1 Month
a point (.), for instance: MY_TABLE.OBJ.
But if just one of these, either the table or the column name or both of them, are a variable,
you can't use this basic structure. You need to pull out the secret weapon; the Alias variable.
Notice that I set the variable outside the loop and use it within the loop. It will return the
value from the current row in the table; that is the row that the table cursor currently is
pointing at.
The finally issue I'll elaborate a bit upon in the article is how to update records within a loop.
From the examples above you now know how to read data from a table into variables to be
able to either analyse or modify this data. Just remember that the data currently in your
variable is no longer connected to the data in your table.
This also means that you don't change anything in your table just by modifying the value in
your variable. If you want to change the data in the table, you need to push the modified data
in your variable back to a column in your table. For that you'll need a SQL statement; the
Update statement.
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 6/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
From the syntax above we can see that you can update
Search
10 multiple columns
Try Premiumin a single table
Free
for 1 Month
and that you can either update all rows in the table or just a single row specified using the
RowID condition.
We will be using the RowID condition in our example and when updating table within a
loop you will almost always be using that condition to limit the Update statement to affect
only the current row.
In this example I change the Pen on the spatial object in my oFeature variable and then I
push the changed spatial object back to the current row in my table.
When I have looped thru all records I save the changes to my table using the Commit Table
statement.
Summary
So this was 4 typical questions that you might run into when building your (first) MapBasic
application. If you already have run into them, don't worry - we have all been thru these.
I might just have scratch the surface in relation to some of the functions and statements used,
so please read more about these in the MapBasic Reference Guide or in the MapBasic Help
System.
If you already have build your first MapBasic application you might have some other
"typical" issues that you have come across. Feel free to share these via the Comments
section below. I might pick some of these for a future article.
Report this
Published by
Peter Horsbøll Møller 27 articles Follow
Principal Presales Consultant & Distinguished Engineer at Precisely | Trust in D…
Published • 5y
Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 7/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
22 Comments
Most relevant
Add a comment…
Thanks Sir,
I need your help
I want to boucle a table to exploit his coordonates(longitude et altitude) to calculate the distance between
two points using this coordonates. the table is in Mapinfo and the is code on Mapbasic
this is the table :
Sorry, but I hadn't paid attention to this discussion for a while. MapBasic has a number of functions
for calculating distances between two sets of coordinates. I'd recommend that you take a look at the
Distance function.
Like Reply
thanks
Like · 2 Reply
Follow
Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 8/9
1/12/2021 (10) 4 typical questions when writing MapBasic applications | LinkedIn
rticles
Messaging
https://www.linkedin.com/pulse/4-typical-questions-when-writing-mapbasic-peter-horsbøll-møller/ 9/9