Troubleshooting
Problem
Calculation of distance between two locations.
Symptom
PART: Platform
Cause
Resolving The Problem
How does Sterling calculate the distance between two locations?
The simplest answer to the question is: Using the Latitude and the Longitude of the 'From' and 'To' locations.
The latitude and longitude information can be stored in two tables: YFS_Person_Info and YFS_Zip_Code_Location. The application first looks into the YFS_Person_Info record to find the latitude and longitude information. If it is not found, the YFS_Zip_Code_Location table is looked into.
Once the Latitude and Longitude information is retrieved, here is the easiest way to explain the calculation of the distance: The Earth is assumed to be a sphere. Once we have the Latitude and Longitude information, the 'From' and 'To' locations are two points on the surface of the sphere. The distance between the two points is calculated as the length of the arc on the surface of the sphere (trigonometric calculation).
To get more details into the calculation of the distance, here is the actual product code which calculates the distance between two locations:
private static double calculateDistance(YFSContext oEnv,final YFS_Person_Info oShipTo,final YFS_Person_Info oShipFrom) {
double distance = 0;
if(oShipTo != null && oShipFrom != null)
{
double latShipTo=0;
double longShipTo=0;
double latShipNode=0;
double longShipNode=0;
boolean fromFound = false;
boolean toFound = false;
if (!YFCCommon.isVoid(oShipFrom.getLatitude()) && !YFCCommon.isVoid(oShipFrom.getLongitude())) {
latShipNode = oShipFrom.getLatitude().doubleValue();
longShipNode = oShipFrom.getLongitude().doubleValue();
fromFound = true;
} else {
YFS_Zip_Code_Location oFromZipLocation = getZipCodeLocation(oEnv,oShipFrom);
if(oFromZipLocation==null)
cat.debug("FromZipCode and Country not found in YFS_ZIP_CODE_LOCATION Table)" + oShipFrom.getZip_Code() + " " + oShipFrom.getCountry() );
if (oFromZipLocation != null) {
latShipNode = oFromZipLocation.getLatitude();
longShipNode = oFromZipLocation.getLongitude();
fromFound=true;
}
}
if (!YFCCommon.isVoid(oShipTo.getLatitude()) && !YFCCommon.isVoid(oShipTo.getLongitude())) {
latShipTo=oShipTo.getLatitude().doubleValue();
longShipTo = oShipTo.getLongitude().doubleValue();
toFound = true;
} else {
YFS_Zip_Code_Location oToZipLocation = getZipCodeLocation(oEnv,oShipTo);
if(oToZipLocation==null)
cat.debug("ToZipCode and County not found in YFS_ZIP_CODE_LOCATION Table)" + oShipTo.getZip_Code() + " " + oShipTo.getCountry() );
if (oToZipLocation!=null) {
latShipTo=oToZipLocation.getLatitude();
longShipTo = oToZipLocation.getLongitude();
toFound=true;
}
}
if (toFound && fromFound){
double r = 6378.1 / 1.609344;
double dlat = 0;
double dlon = 0;
double a = 0;
double d = 0;
double pi = 4 * Math.atan2(1.0,1.0);
//Convert into radians
longShipTo = pi * longShipTo / 180;
latShipTo = pi * latShipTo / 180;
longShipNode = pi * longShipNode / 180;
latShipNode = pi * latShipNode / 180;
//Calculate distance
dlon = longShipNode - longShipTo;
dlat = latShipNode - latShipTo;
double xx = Math.sin(dlat / 2);
double yy = Math.sin(dlon / 2);
a = xx*xx + Math.cos(latShipTo) * Math.cos(latShipNode) * yy*yy;
double arg1 = Math.sqrt(a);
double arg2 = Math.sqrt(1-a);
d = 2 * Math.atan2(arg1,arg2);
distance = r * d;
}
}
return distance;
}
Historical Number
PRI49193
Product Synonym
[<p><b>]Fact[</b><p>];
Was this topic helpful?
Document Information
Modified date:
16 June 2018
UID
swg21549335