Hi.
Ugly but doesn't take long to code :-)  Handles all the formats given
and more, can accept decimal lat/lon as input as well as degrees only,
degrees & minutes, degrees minutes seconds and degrees, minutes and
decimal seconds...
-Craig

function degreeLatLong($dms) {
        // returns lat,lon in decimal degrees given a string something
like:
        //   37°46'35.90"S, 176°6'19.89"E
        //   37°46'35"S,176°6'19"E
        //   37:46:35"S,176°6'19"E
        //   37d46's, 176d6'e
        //   37:47:35S,176:6:19E
        //   37.777555S, 176.111E
        //   -37.777555, 176.111

        // split into lat and lon components
        $cleanDms = eregi_replace(' *, *', ',', $dms);
        $parts = split(',', strtoupper($cleanDms));

        // strip junk from strings
        $lat = eregi_replace('[^0-9NSEW,\.+-]+', ' ', $parts[0]);
        $lat = eregi_replace('([0-9\.+-])([NSEW])', '\1 \2', $lat);
        $lon = eregi_replace('[^0-9NSEW,\.+-]+', ' ', $parts[1]);
        $lon = eregi_replace('([0-9\.+-])([NSEW])', '\1 \2', $lon);

        // split lat and lon into their component parts (e.g. degrees/
min/sec)
        $latParts = split(' ', $lat);
        $lonParts = split(' ', $lon);

        // build decimal lat (if input wasn't decimal degrees)
        if (eregi('\.', $latParts[0]))
                $latdeg = $latParts[0] + 0.0;
        else
                $latdeg = $latParts[0] + $latParts[1] / 60.0 +
$latParts[2] / 3600.0;
        // change sign if needed
        if ($latParts[count($latParts)-1] === 'S')
                $latdeg = -$latdeg;

        // build decimal lon (if input wasn't decimal degrees)
        if (eregi('\.', $lonParts[0]))
                $londeg = $lonParts[0] + 0.0;
        else
                $londeg = $lonParts[0] + $lonParts[1] / 60.0 +
$lonParts[2] / 3600.0;
        // change sign if needed
        if ($lonParts[count($lonParts)-1] === 'W')
                $londeg = -$londeg;

        // we're done
        return $latdeg . ',' . $londeg;
}


On Aug 25, 4:15 pm, Jochen Daum <[email protected]> wrote:
> Hi,
>
> On Tue, Aug 25, 2009 at 4:09 PM, Alexei Tenitski<[email protected]> wrote:
>
> > Is problem to break down string "37°46’35.90”S, 176°6’19.89”E" into
> > degrees-minutes-seconds?
>
> Yes, thats the problem. And thinking through what options or problems
> I may have overlooked in this.
>
> Kind Regards,
>
> Jochen Daum
>
> Chief Automation Officer
> Automatem Ltd
>
> Phone: 09 630 3425
> Mobile: 021 567 853
> Email: [email protected]
> Skype: jochendaum
> Website:www.automatem.co.nzhttp://twitter.com/automatemhttp://www.xing.com/go/invite/3425509.181107
>
>
>
> >Or actual math to do the conversion?
> > If latter then google for "converting coordinates to decimal degrees",
> > nothing hard there.
>
> > Alexei
>
> > On 25/08/2009 15:54, Jochen Daum wrote:
>
> >> Hi,
>
> >> does anyone know a very simple way/ library to convert
>
> >> 37°46’35.90”S, 176°6’19.89”E
>
> >> into
>
> >> -37.775911,176.102738
>
> >> I know I could write a regex, but I'm worried there are just too many
> >> combinations of special characters to look at.
>
> >> Kind Regards,
>
> >> Jochen Daum
>
> >> Chief Automation Officer
> >> Automatem Ltd
>
> >> Phone: 09 630 3425
> >> Mobile: 021 567 853
> >> Email: [email protected]
> >> Skype: jochendaum
> >> Website:www.automatem.co.nz
> >>http://twitter.com/automatem
> >>http://www.xing.com/go/invite/3425509.181107
--~--~---------~--~----~------------~-------~--~----~
NZ PHP Users Group: http://groups.google.com/group/nzphpug
To post, send email to [email protected]
To unsubscribe, send email to
[email protected]
-~----------~----~----~----~------~----~------~--~---

Reply via email to