Sample scripts for calculating the distance between 2 cities

Please post here with requests for cities to be added.

Moderators: Moderators, GeobytesAdmins

Sample scripts for calculating the distance between 2 cities

Postby Kas » Wed Mar 16, 2005 5:50 am

Do you sell any products which allow for calculating distances that runs on a computer not connected to the Internet?
Yes - actually we almost give away one - It is called GeoWorldMap. I say "almost give away" as we do ask for a $10 contribution in the event that you find the product useful. GeoWorldMap is a database that contains the cities of the world with their country, region, latitude, longitude and more - everything that you need to calculate the distance between two cities off line.

Here are some links that will tell you more.
GeoWorldMap data files: http://www.geobytes.com/GeoWorldMap.zip
GeoWorldMap Integration Guide http://www.geobytes.com/GeoWorldMapIntegrationGuide.zip

Then all you will need is the scripts to import the data in to a database - you can use the scripts that are linked to from our support page as a guide. Hint - just copy the scripts for GeoNetMap and remove the references to the subnets table. See http://www.geobytes.com/Support.htm

And finally the source code ...

Below are some sample scripts for calculating the "Great Circle" distance between two cities.

Please see http://www.codeguru.com/algorithms/GeoCalc.html for a great explanation on how the distance is calculated.

This first script is in C++ and will calculate the distance in Kilometers.
namespace GEO {
const double PI = 3.14159265359;
const double TWOPI = 6.28318530718;
const double DE2RA = 0.01745329252;
const double RA2DE = 57.2957795129;
const double ERAD = 6378.135;
const double ERADM = 6378135.0;
const double AVG_ERAD = 6371.0;
const double FLATTENING = 1.0/298.26; // Earth flattening
// (WGS '72)
const double EPS = 0.000000000005;
const double KM2MI = 0.621371;
}

double CGeoMap::GetDistance(double dLat1, double dLon1, double dLat2, double dLon2)
{

dLat1 *= GEO::DE2RA;
dLon1 *= GEO::DE2RA;
dLat2 *= GEO::DE2RA;
dLon2 *= GEO::DE2RA;
double d = sin(dLat1)*sin(dLat2) + cos(dLat1)*cos(dLat2)*cos(dLon1 - dLon2);
return (GEO::AVG_ERAD * acos(d));

}

And here is another in Sql. This one calculates the distance in Kilometers:
CREATE PROCEDURE CalcDistanceInKms
@OriginCityId int,
@DestinationCityId int
AS
Declare @OriginLatitude float
declare @OriginLongitude float
declare @DestinationLatitude float
declare @DestinationLongitude float
select @OriginLatitude=latitude, @OriginLongitude=longitude from Cities Where CityId=@OriginCityId
select @DestinationLatitude=latitude, @DestinationLongitude=longitude from Cities Where CityId=@DestinationCityId

select cast(FLOOR(
6377 * ACOS(round(COS(RADIANS(90-@OriginLatitude))*COS(RADIANS(90-@DestinationLatitude))+
SIN(RADIANS(90-@OriginLatitude))*SIN(RADIANS(90-@DestinationLatitude))*COS(RADIANS(@OriginLongitude-@DestinationLongitude)),15))
)as numeric(9)) as distancekms

GO

And here is the same Sql procedure to calculate the distance in Miles.

CREATE PROCEDURE CalcDistanceInMiles
@OriginCityId int,
@DestinationCityId int
AS
Declare @OriginLatitude float
declare @OriginLongitude float
declare @DestinationLatitude float
declare @DestinationLongitude float
select @OriginLatitude=latitude, @OriginLongitude=longitude from Cities Where CityId=@OriginCityId
select @DestinationLatitude=latitude, @DestinationLongitude=longitude from Cities Where CityId=@DestinationCityId

select cast(FLOOR(
3963 * ACOS(round(COS(RADIANS(90-@OriginLatitude))*COS(RADIANS(90-@DestinationLatitude))+
SIN(RADIANS(90-@OriginLatitude))*SIN(RADIANS(90-@DestinationLatitude))*COS(RADIANS(@OriginLongitude-@DestinationLongitude)),15))
)as numeric(9)) as distancemiles

GO

Excel Formula
Where
Latitude=D2
Longitude=D3
Latitude1=D4
Longitude1=D5

=FLOOR( 6377 * ACOS(COS(RADIANS(90-D2))*COS(RADIANS(90-F2))+SIN(RADIANS(90-D2))*SIN(RADIANS(90-F2))*COS(RADIANS(C2-G2))),1)

Note: If you have any distance calculation scripts for other languages, then please post them here.

Kind Regards
Kaelene McElligott
Geobytes, inc.
http://www.geobytes.com/
Geobytes - because everybody's somewhere!
Kas
Moderator
 
Posts: 37
Joined: Thu Nov 20, 2003 10:17 am

PHP code

Postby seavers » Sun Jan 06, 2008 11:59 am

A set of PHP classes to easily calculate geographical distances can be found at:

http://www.imaginerc.com/software/GeoCalc/
seavers
 
Posts: 3
Joined: Thu Jan 03, 2008 5:12 pm


Return to City Distance Tool

Who is online

Users browsing this forum: No registered users and 1 guest

cron