Skip to main content

Customer is running Unidata 7.2. I am running Unidata 8.3.Looks like Universe is able to calculate the distance between 2 points with GCDISTANCE(). I don't see this on Unidata.

GCDISTANCE (lat1,lon1,lat2,lon2)
Parameters
Parameter     Description
lat1     Latitude of the first point.
lon1     Longitude of the first point.
lat2     Latitude of the second point.
lon2     Longitude of the second point.
Example

PRINT GCDISTANCE(39.7, -105, 38.9, 121.6)

This function returns:

10073112.4749

Has anyone been able to calculate the distance between 2 points ? Trying to calculate the distance with a Basic program which exports GPS tracking data. Maybe there is a way of calling google maps.



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------

Customer is running Unidata 7.2. I am running Unidata 8.3.Looks like Universe is able to calculate the distance between 2 points with GCDISTANCE(). I don't see this on Unidata.

GCDISTANCE (lat1,lon1,lat2,lon2)
Parameters
Parameter     Description
lat1     Latitude of the first point.
lon1     Longitude of the first point.
lat2     Latitude of the second point.
lon2     Longitude of the second point.
Example

PRINT GCDISTANCE(39.7, -105, 38.9, 121.6)

This function returns:

10073112.4749

Has anyone been able to calculate the distance between 2 points ? Trying to calculate the distance with a Basic program which exports GPS tracking data. Maybe there is a way of calling google maps.



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------

The following code was coded and tested on UV 11.3.5

I do not have access to a UniData server to test it - let me know the results of your own testing.

Program GCDISTANCE.MANUAL

----

* Testing the calculation for GCI Distance in BASIC code
* against the UV 11.3.x GCIDISTANCE() function

deffun GCI.DIST.MAN(a,b,c,d)

lat1 = 39.7
lon1 = -105
lat2 = 38.9
lon2 = 121.6
rslt = GCI.DIST.MAN( lat1, lon1, lat2, lon2)

crt "local GCI.DIST.MAN() = ":rslt

rslt2 = GCDISTANCE(lat1, lon1, lat2, lon2)

crt "GCDISTANCE() = ":rslt2


end

** -- Local function code --

function GCI.DIST.MAN( lat1.VALUE, lon1.VALUE, lat2.VALUE, lon2.VALUE)

* -- Setup our operating environment --

* According to https://www.inchcalculator.com/convert/degree-to-radian/
* 1 degree = 0.01745329251994'

equ degree.CONV to 0.01745329251994
* Maximise our calculation depth
precision 14

* -- Now perform our calculation --

* Converted the Python logic from
*    https://www.geeksforgeeks.org/program-distance-two-points-earth/

* First, convert from degrees to radians
lat1.RADIAN = lat1.VALUE * degree.CONV
lon1.RADIAN = lon1.VALUE * degree.CONV
lat2.RADIAN = lat2.VALUE * degree.CONV
lon2.RADIAN = lon2.VALUE * degree.CONV

* Now use the Haversine formula
lat.DIFF = lat2.RADIAN - lat1.RADIAN
lon.DIFF = lon2.RADIAN - lon1.RADIAN

* As we want to use RADIANS not degrees we need to tell the compiler
$options RADIANS

a = pwr(sin(lat.DIFF / 2),2) + cos(lat1.RADIAN) * cos(lat2.RADIAN) * pwr(sin(lon.DIFF / 2),2)

c = 2 * asin(sqrt(a))

* Radius of earth in kilometers. Use 3956 for miles
r = 6371 * 1000   ;* Radius in meters

dist = (c * r)

return(dist)

----

Running this produces the following output:

>GCDISTANCE.MANUAL
local GCI.DIST.MAN() = 10073112.4749
GCDISTANCE() = 10073112.4749
>

So it is possible to code the calculation



------------------------------
Gregor Scott
Software Architect
Pentana Solutions Pty Ltd
Mount Waverley VIC AU
------------------------------

The following code was coded and tested on UV 11.3.5

I do not have access to a UniData server to test it - let me know the results of your own testing.

Program GCDISTANCE.MANUAL

----

* Testing the calculation for GCI Distance in BASIC code
* against the UV 11.3.x GCIDISTANCE() function

deffun GCI.DIST.MAN(a,b,c,d)

lat1 = 39.7
lon1 = -105
lat2 = 38.9
lon2 = 121.6
rslt = GCI.DIST.MAN( lat1, lon1, lat2, lon2)

crt "local GCI.DIST.MAN() = ":rslt

rslt2 = GCDISTANCE(lat1, lon1, lat2, lon2)

crt "GCDISTANCE() = ":rslt2


end

** -- Local function code --

function GCI.DIST.MAN( lat1.VALUE, lon1.VALUE, lat2.VALUE, lon2.VALUE)

* -- Setup our operating environment --

* According to https://www.inchcalculator.com/convert/degree-to-radian/
* 1 degree = 0.01745329251994'

equ degree.CONV to 0.01745329251994
* Maximise our calculation depth
precision 14

* -- Now perform our calculation --

* Converted the Python logic from
*    https://www.geeksforgeeks.org/program-distance-two-points-earth/

* First, convert from degrees to radians
lat1.RADIAN = lat1.VALUE * degree.CONV
lon1.RADIAN = lon1.VALUE * degree.CONV
lat2.RADIAN = lat2.VALUE * degree.CONV
lon2.RADIAN = lon2.VALUE * degree.CONV

* Now use the Haversine formula
lat.DIFF = lat2.RADIAN - lat1.RADIAN
lon.DIFF = lon2.RADIAN - lon1.RADIAN

* As we want to use RADIANS not degrees we need to tell the compiler
$options RADIANS

a = pwr(sin(lat.DIFF / 2),2) + cos(lat1.RADIAN) * cos(lat2.RADIAN) * pwr(sin(lon.DIFF / 2),2)

c = 2 * asin(sqrt(a))

* Radius of earth in kilometers. Use 3956 for miles
r = 6371 * 1000   ;* Radius in meters

dist = (c * r)

return(dist)

----

Running this produces the following output:

>GCDISTANCE.MANUAL
local GCI.DIST.MAN() = 10073112.4749
GCDISTANCE() = 10073112.4749
>

So it is possible to code the calculation



------------------------------
Gregor Scott
Software Architect
Pentana Solutions Pty Ltd
Mount Waverley VIC AU
------------------------------

I had no idea you could do a local function in Universe Basic! I'm going to have to explore that some more. Does anyone know how long has that been possible?



------------------------------
Joe Goldthwaite
Consultant
Phoenix AZ US
------------------------------

I had no idea you could do a local function in Universe Basic! I'm going to have to explore that some more. Does anyone know how long has that been possible?



------------------------------
Joe Goldthwaite
Consultant
Phoenix AZ US
------------------------------

The ability to define local functions and subroutines was introduced in 11.2.

There is a compiler definition "U2_LOCALCALL" that can be checked for using "$IFDEF U2_LOCALCALL" to determine when it is possible to include local functions & subs in your code



------------------------------
Gregor Scott
Software Architect
Pentana Solutions Pty Ltd
Mount Waverley VIC AU
------------------------------

Customer is running Unidata 7.2. I am running Unidata 8.3.Looks like Universe is able to calculate the distance between 2 points with GCDISTANCE(). I don't see this on Unidata.

GCDISTANCE (lat1,lon1,lat2,lon2)
Parameters
Parameter     Description
lat1     Latitude of the first point.
lon1     Longitude of the first point.
lat2     Latitude of the second point.
lon2     Longitude of the second point.
Example

PRINT GCDISTANCE(39.7, -105, 38.9, 121.6)

This function returns:

10073112.4749

Has anyone been able to calculate the distance between 2 points ? Trying to calculate the distance with a Basic program which exports GPS tracking data. Maybe there is a way of calling google maps.



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------

David, Here is a version of that calculation written in Unidata:

SUBROUTINE getDistance(DISTANCE,UOM,LATITUDE1,LONGITUDE1,LATITUDE2,LONGITUDE2)
* Calcuate distance between 2 Geo points 1.start 2.dest set latitude & longitude
* Uses Haversine formula * courtesy of the internet * msapp * 02/24/2014
* UOM is (K)ilometers or (M)iles
*
EARTH.RADIUS = 6371  ;* (km)
PI = 3.14159265
DEG2RAD = PI / 180
*
DLAT = DEG2RAD * (LATITUDE2 - LATITUDE1)
DLON = DEG2RAD * (LONGITUDE2 - LONGITUDE1)
*
A = SIN(DLAT / 2) * SIN(DLAT / 2) + COS(DEG2RAD * LATITUDE1) * COS(DEG2RAD * LATITUDE2) * SIN(DLON / 2) * SIN(DLON / 2)
C = 2 * ASIN(SQRT(A))
*
D.KM = EARTH.RADIUS * C ;* Kilometers
D.MI = 3956 * C         ;* Miles
*
BEGIN CASE
  CASE UPCASE(UOM)[1,1] = "M"  ;* Miles
    DISTANCE = D.MI
  CASE UPCASE(UOM)[1,1] = "K"  ;* Kilometers
    DISTANCE = D.KM
  CASE 1
    DISTANCE = D.MI            ;* Default
END CASE
*
RETURN



------------------------------
Mark Sapp
Senior Software Developer
Rocket Internal - All Brands
------------------------------

The following code was coded and tested on UV 11.3.5

I do not have access to a UniData server to test it - let me know the results of your own testing.

Program GCDISTANCE.MANUAL

----

* Testing the calculation for GCI Distance in BASIC code
* against the UV 11.3.x GCIDISTANCE() function

deffun GCI.DIST.MAN(a,b,c,d)

lat1 = 39.7
lon1 = -105
lat2 = 38.9
lon2 = 121.6
rslt = GCI.DIST.MAN( lat1, lon1, lat2, lon2)

crt "local GCI.DIST.MAN() = ":rslt

rslt2 = GCDISTANCE(lat1, lon1, lat2, lon2)

crt "GCDISTANCE() = ":rslt2


end

** -- Local function code --

function GCI.DIST.MAN( lat1.VALUE, lon1.VALUE, lat2.VALUE, lon2.VALUE)

* -- Setup our operating environment --

* According to https://www.inchcalculator.com/convert/degree-to-radian/
* 1 degree = 0.01745329251994'

equ degree.CONV to 0.01745329251994
* Maximise our calculation depth
precision 14

* -- Now perform our calculation --

* Converted the Python logic from
*    https://www.geeksforgeeks.org/program-distance-two-points-earth/

* First, convert from degrees to radians
lat1.RADIAN = lat1.VALUE * degree.CONV
lon1.RADIAN = lon1.VALUE * degree.CONV
lat2.RADIAN = lat2.VALUE * degree.CONV
lon2.RADIAN = lon2.VALUE * degree.CONV

* Now use the Haversine formula
lat.DIFF = lat2.RADIAN - lat1.RADIAN
lon.DIFF = lon2.RADIAN - lon1.RADIAN

* As we want to use RADIANS not degrees we need to tell the compiler
$options RADIANS

a = pwr(sin(lat.DIFF / 2),2) + cos(lat1.RADIAN) * cos(lat2.RADIAN) * pwr(sin(lon.DIFF / 2),2)

c = 2 * asin(sqrt(a))

* Radius of earth in kilometers. Use 3956 for miles
r = 6371 * 1000   ;* Radius in meters

dist = (c * r)

return(dist)

----

Running this produces the following output:

>GCDISTANCE.MANUAL
local GCI.DIST.MAN() = 10073112.4749
GCDISTANCE() = 10073112.4749
>

So it is possible to code the calculation



------------------------------
Gregor Scott
Software Architect
Pentana Solutions Pty Ltd
Mount Waverley VIC AU
------------------------------

I was not sure how to get deffun to work with this program. So I used your program and pulled out the parts needed. I compiled with the I option so that all reserved words in UniBasic are case insensitive. 

The program I ran is attached. Here are the results.

25195121.10811678320169

The distance should be 10,073,112 meters.



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------

David, Here is a version of that calculation written in Unidata:

SUBROUTINE getDistance(DISTANCE,UOM,LATITUDE1,LONGITUDE1,LATITUDE2,LONGITUDE2)
* Calcuate distance between 2 Geo points 1.start 2.dest set latitude & longitude
* Uses Haversine formula * courtesy of the internet * msapp * 02/24/2014
* UOM is (K)ilometers or (M)iles
*
EARTH.RADIUS = 6371  ;* (km)
PI = 3.14159265
DEG2RAD = PI / 180
*
DLAT = DEG2RAD * (LATITUDE2 - LATITUDE1)
DLON = DEG2RAD * (LONGITUDE2 - LONGITUDE1)
*
A = SIN(DLAT / 2) * SIN(DLAT / 2) + COS(DEG2RAD * LATITUDE1) * COS(DEG2RAD * LATITUDE2) * SIN(DLON / 2) * SIN(DLON / 2)
C = 2 * ASIN(SQRT(A))
*
D.KM = EARTH.RADIUS * C ;* Kilometers
D.MI = 3956 * C         ;* Miles
*
BEGIN CASE
  CASE UPCASE(UOM)[1,1] = "M"  ;* Miles
    DISTANCE = D.MI
  CASE UPCASE(UOM)[1,1] = "K"  ;* Kilometers
    DISTANCE = D.KM
  CASE 1
    DISTANCE = D.MI            ;* Default
END CASE
*
RETURN



------------------------------
Mark Sapp
Senior Software Developer
Rocket Internal - All Brands
------------------------------

Perfect. Thanks!



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------

Customer is running Unidata 7.2. I am running Unidata 8.3.Looks like Universe is able to calculate the distance between 2 points with GCDISTANCE(). I don't see this on Unidata.

GCDISTANCE (lat1,lon1,lat2,lon2)
Parameters
Parameter     Description
lat1     Latitude of the first point.
lon1     Longitude of the first point.
lat2     Latitude of the second point.
lon2     Longitude of the second point.
Example

PRINT GCDISTANCE(39.7, -105, 38.9, 121.6)

This function returns:

10073112.4749

Has anyone been able to calculate the distance between 2 points ? Trying to calculate the distance with a Basic program which exports GPS tracking data. Maybe there is a way of calling google maps.



------------------------------
David Rivera
Self Registered
FORT LAUDERDALE FL US
------------------------------
I would seriously consider using Python if you can. It makes the problem
ridiculously easy. (Method & function have the same name. Can be different.)
 
1. Use pip from shell to install geopy: python -m pip install geopy
 
2. I put the following in PY\\TestFunctions\\gcdistance.py:
 
from geopy.distance import great_circle
def gcdistance(lat1,lon1,lat2,lon2):
    point1=(lat1,lon1)
    point2=(lat2,lon2)
    return great_circle(point1,point2).meters
 
3. Here's a basic program to call this Python function:
 
PROGRAM GCDISTANCE
precision 9
distanceMethod = PyImport('TestFunctions.gcdistance')
distance = PyCallMethod(distanceMethod,'gcdistance',39.7,-105,38.9,121.6)
print distance
 
4. Here's the output of the program:
 
RUN GCDISTANCE
10073126.7047268


------------------------------
Phillip Potter
V.P. R&D
Data Management Associates Inc DMA
Cincinnati OH US
------------------------------

I had no idea you could do a local function in Universe Basic! I'm going to have to explore that some more. Does anyone know how long has that been possible?



------------------------------
Joe Goldthwaite
Consultant
Phoenix AZ US
------------------------------

The deffun function has been around since at least UV10.3, we use it for a few functions particularly a regex one. In fact I think I've seen it mentioned in my UV8 Vmark Pocket Guide. Very handy.



------------------------------
Peter Cheney
Software Superstar
Firstmac
Brisbane Qld Australia
------------------------------


David, Here is a version of that calculation written in Unidata:

SUBROUTINE getDistance(DISTANCE,UOM,LATITUDE1,LONGITUDE1,LATITUDE2,LONGITUDE2)
* Calcuate distance between 2 Geo points 1.start 2.dest set latitude & longitude
* Uses Haversine formula * courtesy of the internet * msapp * 02/24/2014
* UOM is (K)ilometers or (M)iles
*
EARTH.RADIUS = 6371  ;* (km)
PI = 3.14159265
DEG2RAD = PI / 180
*
DLAT = DEG2RAD * (LATITUDE2 - LATITUDE1)
DLON = DEG2RAD * (LONGITUDE2 - LONGITUDE1)
*
A = SIN(DLAT / 2) * SIN(DLAT / 2) + COS(DEG2RAD * LATITUDE1) * COS(DEG2RAD * LATITUDE2) * SIN(DLON / 2) * SIN(DLON / 2)
C = 2 * ASIN(SQRT(A))
*
D.KM = EARTH.RADIUS * C ;* Kilometers
D.MI = 3956 * C         ;* Miles
*
BEGIN CASE
  CASE UPCASE(UOM)[1,1] = "M"  ;* Miles
    DISTANCE = D.MI
  CASE UPCASE(UOM)[1,1] = "K"  ;* Kilometers
    DISTANCE = D.KM
  CASE 1
    DISTANCE = D.MI            ;* Default
END CASE
*
RETURN



------------------------------
Mark Sapp
Senior Software Developer
Rocket Internal - All Brands
------------------------------

I do something similar to Mark Sapp.

Here's a good explanation of haversine formula if you care: youtu.be/Zovvx2dGu_w



------------------------------
Chuck Stevenson
DBA / SW Developer
Pomeroy
US
------------------------------