Miles to Kilometers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Miles to Kilometers

Post 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?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
jmut
Forum Regular
Posts: 945
Joined: Tue Jul 05, 2005 3:54 am
Location: Sofia, Bulgaria
Contact:

Post by jmut »

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

yeah i know the miles to km conversion, but not how to go about it in this function
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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));
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

The most common results that I have found so far is

Miles - 69.172
Kilometers - 111.325

Can anyone confirm?
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post 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 :)
Post Reply