Longitude

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
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Longitude

Post by Sinnix »

I hate Mondays.

Okay... I work for a mapping company and they give me a number.
ie: -631824.69
This number represents a longitude in degrees, minutes and seconds. I need to break this number into it's respective components to store in my database.

Everything behind the period, and the two digits in front of the period are 'seconds'. This will always be two characters a dot and then another two characters. (24.69)

The first two numbers in front of the seconds are 'minutes'. No more than two characters ever. (18 )

The remaining two digits (63) are the degrees. These can be anywhere from 1 to 3 characters.

If the number is negative it is either east or west for longitude, or north and south for latitude. I've already got this one covered.


Code: Select all

-631824.69

"-" = west
"63" = degrees (can be from 1 to 3 characters)
"18" = minutes
"24.69" = seconds
I'm sorry if I made this confusing... any help would be greatly appreciated! ;) thanks!
Sinnix
Forum Commoner
Posts: 43
Joined: Mon Apr 28, 2003 11:01 am
Location: Ottawa, ON Canada
Contact:

Post by Sinnix »

:P nevermind... apparently my IQ comes back up to normal after lunch...

Code: Select all

<?php
	$string = ("31824.69");	//longitude
	$occularjuice=strrev($string);

	eregi("([[]]{2}.[[]]{2})([[]]{2})([[]]{1,3})", $occularjuice, $output);

	$degrees = strrev($output[3]);
	$minutes = strrev($output[2]);
	$seconds = strrev($output[1]);

	if ($string < 0) 
	{
		$direction = "W";
	}
	else
	{
		$direction = "E";
	}
	echo ("Degrees: ".$degrees."º<br>");
	echo ("Minutes: ".$minutes.""<br>");
	echo ("Seconds: ".$seconds."'<br>");
	echo ("Direction: ".$direction."<br><br>");
	
	$juicy = ($degrees."º ".$minutes."" ".$seconds."' ".$direction);
	echo $juicy;

?>
outputs... 3º 18" 24.69' E 8)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

There's also a non regex solution, 'cause you can use substr():

Code: Select all

<?php
$string = -631824.69;

$seconds = substr($string, -5);
$minutes = substr($string, -7, -5);
$degrees = abs(substr($string, -10, -7));

$direction = ($string < 0) ? 'W' : 'E';

$juicy = $degrees.'º '.$minutes.'" '.$seconds.''' '.$direction; 
echo $juicy;
?>
Mac
Post Reply