calculate length of line in mapbasic

305 Views Asked by At

I'm working on a MapBasic code and I have a problem. I wrote a code that calculates the length of the line that the user clicked on. my program calculates the length of the line. The program I write separately writes the start and end points of the line.

The first question I want to ask you is how can I show the number of decimal digits of the starting and ending coordinates of the line shown by the program as 3 digits? the returned values either show two digits or do not appear in decimal digits.

The second question I want to ask you is: the line length calculated with the program I wrote differs from the line length calculated with the calculator. what is the reason of this?

I converted the layer of the line object I draw in Mapinfo software to shape format using the Universal Translator tool.

I opened the table in ArcMap. the length of the same line in the table is very close to the length I calculate with my calculator. The result value I found in Mapinfo is not the same as the result I found in ArcMap.

I wrote another program using MapBasic. I created a dialog in the program. In my program, the user creates points in the layer with the X - Y coordinate values written in edittext. I created two points with the mapbasic program that I wrote. the program also writes the X - Y coordinates of the points on the screen after creating the point. when using the program I entered three digits of the decimal digits of the point coordinates. but the X - Y coordinates on the screen appear as two digits. I measure the distance between two points using the ruler in Mapinfo program. I also calculated the length using the X- Y coordinates that appear on the screen with the calculator. The length value that the ruler in Mapinfo finds is not the same as the length value I calculated.

When I tried the program I wrote in Mapinfo software, I set the projection of the layers I created as Türkish Coordinate Systems (3 degree k = 1 ITRF) Cenral Meridian 33.

Where am I doing wrong? Could you help me with this? thanks everyone

Picture of mapbasic program I wrote added this ask.

enter image description here

1

There are 1 best solutions below

0
On

In order to calculate length of a line you don't need Pythagoras formula, simply use ObjectLen(obje, "m")

In order to get desired number of digits have a look at function Format$. Examples from documentation:

Format$( 12345, ",#") ' returns "12,345"
Format$(-12345, ",#") ' returns "-12,345"
Format$( 12345, "$#") ' returns "$12345"
Format$(-12345, "$#") ' returns "-$12345"

Format$( 12345.678, "$,#.##") ' returns "$12,345.68"
Format$(-12345.678, "$,#.##") ' returns "-$12,345.68"

Format$( 12345.678, "$,#.##;($,#.##)") 'returns "$12,345.68"
Format$(-12345.678, "$,#.##;($,#.##)") 'returns "($12,345.68)"
Format$(12345.6789, ",#.###") ' returns "12,345.679"
Format$(12345.6789, ",#.#") ' returns "12,345.7"
Format$(-12345.6789, "#.###E+00") ' returns "-1.235e+04"
Format$( 0.054321, "#.###E+00") ' returns "5.432e-02"

Format$(-12345.6789, "#.###E-00") ' returns "-1.235e04"
Format$( 0.054321, "#.###E-00") ' returns "5.432e-02"

Format$(0.054321, "#.##%") ' returns "5.43%"
Format$(0.054321, "#.##\%") ' returns ".05%"
Format$(0.054321, "0.##\%") ' returns "0.05%"

Before you create any objects or perform calculation you should set the coordinate system, for example

Set CoordSys Earth Projection 25, 1003, "m", 7.4395833333, 46.9524055555, 600000, 200000 Bounds (-99400000, -99800000) (100600000, 100200000)

Note, you have CoordSys on several places, don't mix them

  • Set Map ... CoordSys ... which applies only for the map but not for objects in your code.
  • Commit Table ... CoordSys... which applies to saved table only.
  • SessionInfo( SESSION_INFO_COORDSYS_CLAUSE ) String result that indicates a session's CoordSys clause.