Page 1 of 1

Miles to Kilometers

Posted: Tue May 02, 2006 6:35 am
by s.dot

Code: Select all

/************************************************
	*************************************************
	** this function will return a                 **
	** $min_lat, $max_lat, $min_lon, $max_lon      **
	** for use of calculating which lattitude and  **
	** longitudes are in a given range of a        **
	** specified $lat & $lon                       **
	*************************************************
	************************************************/

	function getPoints($zip,$range){
	
		// firstly get lat & long for given zip code
		// this part is OK - returns correct data
		// $latlon is an associative array
		if(!$latlon = $this->getZipData($zip)){
			return false;
		}
		
		// declare variables for easier usage
		$lat = $latlon['lat'];
		$lon = $latlon['long'];
		
		// calculate ranges
		$lat_range = $range/69.172;
		$lon_range = abs($range/(cos($lat) * 69.172));
		
		$min_lat = $lat - $lat_range;
		$max_lat = $lat + $lat_range;
		$min_lon = $lon - $lon_range;
		$max_lon = $lon + $lon_range;
		
		return array($min_lat,$max_lat,$min_lon,$max_lon);
		
	}
This (I presume) returns the points according to $range MILES. How could I go about getting this in kilometers?

Posted: Tue May 02, 2006 6:50 am
by jmut

Posted: Tue May 02, 2006 7:00 am
by s.dot
yeah i know the miles to km conversion, but not how to go about it in this function

Posted: Tue May 02, 2006 12:11 pm
by R4000
well simple

Code: Select all

$arg1 = $arg1 * 0.64; // or what ever the converstion rate is..
$arg2 = $arg2 * 0.64; // or what ever the converstion rate is..
ect.

Posted: Tue May 02, 2006 2:40 pm
by s.dot
No. This isn't really converting a miles number to a kilometers number. The output specifies a lat & long within x miles. I need a lat and long within x kilometers

I believe the function would have to be changed in this area

Code: Select all

// calculate ranges 
$lat_range = $range/69.172; 
$lon_range = abs($range/(cos($lat) * 69.172));

Posted: Tue May 02, 2006 3:17 pm
by Christopher
The 69.172 is miles per degree, so the value for kilometers would probably be 111.321.

I think the latitude number might be slightly wrong and should be:

1 degree latitude = 110.567km = 68.703mi
1 degree longitude = 111.321km * cos(latitude) = 69.172mi * cos(latitude)

You should search to find out what it should be exactly.

Posted: Tue May 02, 2006 6:03 pm
by s.dot
The most common results that I have found so far is

Miles - 69.172
Kilometers - 111.325

Can anyone confirm?

Posted: Tue May 02, 2006 6:41 pm
by Christopher
I got 111.321 a couple of places. Note also that long. and lat. use slightly different values. I am not sure why, probably because the earth is not exactly round, but who knows.

Posted: Tue May 02, 2006 8:46 pm
by alex.barylski
Having written a flight planning application for the venerable MSFS I can tell you this much from experience:

The earth, is not perfectly round, but is what is known as an oblate spheriod: http://en.wikipedia.org/wiki/Oblate_spheroid

Because of this, latitude and longitude are not constant ratios

One nautical mile was originally defined as one minute of latitude. Therefore one degree of latutude is equal to 60 nautical miles.

Nautical miles are the common unit of measurements when working with great circle navigation as the formula is typically only used in aerospace/nautical navigation...

The same is true for longitude but *only* at the equator...as you ascend/descend in latitude the distance between longitudinal degrees becomes shorter.

http://en.wikipedia.org/w/index.php?tit ... ext=Search

If your formula is correct and you properly convert nautical miles to statuate miles...you shouldn't have a problem...

Cheers :)

Posted: Wed May 03, 2006 5:02 am
by CoderGoblin
As I have recently become aware this is a huge topic. Having found things like the Hypersine formula and the different mapping methods including WGS84,Gauss Krüger the question I needed to be answer was "how accurate do I need the result ?".

In my case distance is to only be used as an indication, not precise, with a fairly small area (OK Germany is not small but smaller than the US :wink: ). The decision was taken (by management thankfully) to simplify matters as much as possible giving only ballpark distances for the initial code (search for locations within x kilometers of y location). To this end I will be simply using lat/long with geometric functions using points in POSTGRES. Not that accurate but will give a basic idea of distance which is all I really need for now. Later versions of the code may look at getting the information more accurate but as always we start getting a tradeoff speed vs accuracy.

Posted: Wed May 03, 2006 7:34 am
by Maugrim_The_Reaper
Can of worms ;)

I think latitude distance, for precision, also differs depending on distance from equator. Another effect of the Earth's shape. You'll probably spend days trying to figure it out exactly - an imprecise approach would be more reasonable if it's not too far off the mark.

Posted: Thu May 04, 2006 2:39 am
by CoderGoblin
I think the title for this topic should change as this is now more about GPS distance rather than miles to km and would help people searching for the topic.

Some basic guidelines.

1) As Maugrim_The_Reaper mentioned, Information about Lat/Long come from one of several formats/methods, for example WGS84. This splits the globe into segments with the x distance being shorter as you go towards the poles. Map Projection

2) Mapping systems need to use a square grid. These are defined by other formats such as UTM, Gauss Krüger (mainly used in Germany) and others (the netherlands has one specific to them which breaks outside of that country).

If you have a lat/long system which you need to find the distance from you need to convert that lat/long format to a mapping format (US I guess is UTM). These coordinates can then be used to find the distance.

There are things on the net that can do these format changes for you. CPAN - Geo/Proj4 or you could develop your own. (Be prepared to get lost as I yet to find a decent page with a simple formula for the conversion :wink:). For specific countries you might find databases/classes already in existance. For Germany I am looking at OpenGeoDB which not only contains information about cities/towns in WGS84 format and additional information, such as which German state it belongs to, but also includes classes and helpers for mapping and distance calculations (and its open source).

Hopefully this will help people as this topic is far from simple.

Regards
Coder

Posted: Thu May 04, 2006 7:52 am
by s.dot
Indeed, it is difficult. I'm finding that (since i'm only doing US & Canada) the further north my search goes the more inaccurate the results are being. Although it's not much. I guess I don't need the data to be that accurate as it's just indicating particular locations. And sorting it by distance from point x will give the closest location first, and the furthest away location last.

So that is all I need & I believe I am done with this part of my script. (whew!)

However, I do believe that the longitude getting smaller as it goes towards the north (& south) poles is what's causing the data to be off a little bit. So if anyone is reading this, that particular part of the posted function could probably be tweaked.

Posted: Thu May 04, 2006 12:22 pm
by Christopher
You should be able to find an equation to find the circumfrence of a circle on a sphere on the internet somewhere. Then divide to get degrees.

Posted: Thu May 04, 2006 1:11 pm
by alex.barylski
scottayy wrote:Indeed, it is difficult. I'm finding that (since i'm only doing US & Canada) the further north my search goes the more inaccurate the results are being. Although it's not much. I guess I don't need the data to be that accurate as it's just indicating particular locations. And sorting it by distance from point x will give the closest location first, and the furthest away location last.

So that is all I need & I believe I am done with this part of my script. (whew!)

However, I do believe that the longitude getting smaller as it goes towards the north (& south) poles is what's causing the data to be off a little bit. So if anyone is reading this, that particular part of the posted function could probably be tweaked.
What are you comparing your results too?

How do you know your distances are off?

Not sure what formula you are using, but there is indeed a formula which will calculate the exact great circle distance, taking into consideration the earth is oblate, not perfectly spherical...

I got the formula originally from a pilots handbook which was extensive in detailing how the formula works...

So I am sure it's as accurate as it's going to get as I believe this is the formula GPS systems use...

Cheers :)