GSAK (Geocaching Swiss Army Knife)
Contents - Index

Gcalc (function)


GCalc( sData, sAction ) : string

The GCalc function allows for quite a few manipulations and calculations using longitude and latitude. It is a "black box" for this type of functionality and more "actions" will be added as required.

sData - This is the data string you provide as input for the function. Individual elements are separated by a ; (semicolon). Currently, sData should only contain one or two pairs of latitude or longitude, depending on your required action. Latitude and Longitude are input as a combined string. For more information on supported formats see this topic.  This makes it very versatile to use for coordinates that come from a variety of sources. For example the following coordinate pairs are valid input (and the same point, except for rounding differences):

S32° 25.920 E115° 46.039
-32.432 115.76731666


sAction - This is the action you want to perform on sData. Current actions supported are:

FormatBG - Format the input coordinates in British Grid. sData should contain only one pair of coordinates.

FormatDegrees - Format the input coordinates in decimal degrees. sData should contain only one pair of coordinates.

FormatMinutes - Format the input coordinates in decimal Minutes. sData should contain only one pair of coordinates.

FormatSeconds - Format the input coordinates in decimal seconds. sData should contain only one pair of coordinates.

FormatUTM - Format the input coordinates in UTM. sData should contain only one pair of coordinates.

CalcDistance - Calculate the distance between two coordinates using the "Greater Circle" method ("Haversine" formula). sData should contain two pairs of coordinates. The result is kms or miles depending on your system settings (see Tools=>Options=>General)

CalcDistanceHigh - Calculate the distance between two coordinates (using the High accuracy "Vincenty's algorithm"). sData should contain two pairs of coordinates. The result is kms or miles depending on your system settings (see Tools=>Options=>General)

CalcBearing1 - Calculate the bearing in degrees between two coordinates. sData should contain two pairs of coordinates, the first pair being your starting position and your second pair being your destination.

CalcBearing2 - Calculate the bearing direction (N,S,E,W,NE,....) between two coordinates. sData should contain two pairs of coordinates, the first pair being your starting position and your second pair being your destination.

Project - Project a point by providing distance and bearing from a starting point. Sdata should be in the format "FromCoordinates;BearingDegrees;Distance;units" For units K = Kilometers, N = Nautical miles, M = miles, T = Meters, Y = Yards, F = Feet.  

ProjectHigh - Same as the "Project" action but where as the existing action of "project" uses the great circle method (most common). The "ProjectHigh" uses Vincenty's algorithm for high accuracy calculations.  

In the case of the Format actions, the result is returned as the latitude and longitude separated by a ; (semi colon). Use the Extract function to pull out the distinct lat and lon from the result, or just use the replace function to replace the semi colon with a space if you just want to display the results.

In the case of the Calc actions only one data element is returned. For distance and bearing1 you can use the Val function on the result to convert to a true numeric for numeric calculations. You can then use the Str function to display these values rounded to the number of decimal places you feel is appropriate. 

If there is any type of error in your sData (invalid coordinates, data missing, etc) the function will return an error string starting with *Error*. You can test the very first position of the result for an asterisk to determine if your calculation ended in an error or not

Example, show the distance between two caches in your database:
 

Mfilter Where=code = "GCXXXX"
$lat1 = $d_latitude
$lon1 = $d_longitude
Mfilter where=code = "GCYYYY"
$lat2 = $d_latitude
$lon2 = $d_longitude
CancelFilter

$data = $lat1 + " " + $lon1 + ";" + $lat2 + " " + $lon2
$DistanceString = GCalc($data,"CalcDistance")
# Display error message and abort the macro if an error
If Left($DistanceString,1) = "*"
  Cancel msg=$DistanceString
EndIf
$DistanceNumeric = Val($DistanceString)

# now show the distance rounded to 2 decimal places
$DistanceRounded = Alltrim(Str($DistanceNumeric,10,2))

MsgOk Msg="Distance between GCXXXX and GCYYYY is : $DistanceRounded"


Example, show the current coordinates in UTM format.
 

$data = $d_latitude + " " + $d_longitude 
$utm = GCalc($data,"FormatUTM")
# Display error message and abort the macro if an error
If Left($utm,1) = "*"
  Cancel msg=$utm
EndIf
# Replace semi colons with space for display
$utm = Replace(";"," ",$utm,true)
MsgOk Msg=$utm
 

Alpha List         Category List
Copyright 2004-2019 CWE Computer Services  
Privacy Policy Contact