Distances in LatLong coordinate system

configuration-examples Distances in LatLong coordinate system

Great-circle distances between two points on a sphere given their longitudes and latitudes can be calculated with the haversine formula.

The following example shows an implementation in the GeoDMS configuration. We thank PBL for making this example available.

example haversine function

template DistanceMtrFromDegrees
{
	// begin case parameters
	attribute<float64> lat1_degrees (NL);
	attribute<float64> lat2_degrees (NL);
	attribute<float64> lon1_degrees (NL);
	attribute<float64> lon2_degrees (NL);
	// end case parameters

	attribute<float64> lat1_radian (NL) := lat1_degrees * pi() / 180.0;
	attribute<float64> lat2_radian (NL) := lat2_degrees * pi() / 180.0;
	attribute<float64> lon1_radian (NL) := lon1_degrees * pi() / 180.0;
	attribute<float64> lon2_radian (NL) := lon2_degrees * pi() / 180.0;

	attribute<float64> deltaLon_radian (NL) := lon1_radian - lon2_radian;
	attribute<float64> deltaLat_radian (NL) := lat1_radian - lat2_radian;
	attribute<float64> a               (NL) := sqr(sin(deltaLat_radian/2d)) + (((cos(lat1_radian) * cos(lat2_radian))) * sqr(sin(deltaLon_radian/2d)));
	attribute<meter>   distance    (NL) := (2d * 6371000d * atan(sqrt(a) / (sqrt(1d - a))))[Meter];
}