MTP_Phil
asked on
Convert GPS Degrees and Minutes to decimal using Coldfusion
I have a routine that needs to run through a database and convert Degrees and Minutes into a decimal equivalent, but I have no idea how to do it. Below are two examples.
Example 1.
Position: 12° 58.405' S, 38° 30.663' W
Lng = 12° 58.405' S
Lat = 38° 30.663' W
Example 2.
Position: 41° 14.81' N, 73° 47.019' W
Lat = 41° 14.81' N
Lng = 73° 47.019' W
Example 1.
Position: 12° 58.405' S, 38° 30.663' W
Lng = 12° 58.405' S
Lat = 38° 30.663' W
Example 2.
Position: 41° 14.81' N, 73° 47.019' W
Lat = 41° 14.81' N
Lng = 73° 47.019' W
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I don't know ColdFusion, but here is an example in PHP. Maybe a PHP + CF expert will come along and convert the code.
http://www.laprbass.com/RAY_temp_mtp_phil.php
http://www.laprbass.com/RAY_temp_mtp_phil.php
<?php // RAY_temp_mtp_phil.php
error_reporting(E_ALL);
// CONVERT LAT/LON NOTATION TO GEOCODE
// SOME TEST DATA
$points[] = " 12° 58.405' S, 38° 30.663' W";
$points[] = " 41° 14.81' N, 73° 47.019' W";
$points[] = "38° 50.051' N, 77° 8.9602' W";
$geocodes[] = '41.902916,12.453389';
$geocodes[] = '38.834175,-77.149336';
// A REGULAR EXPRESSION TO REMOVE UNWANTED CHARACTERS
$rgx
= '#' // REGEX DELIMITER
. '[' // CHARACTER CLASS
. '^' // NEGATION: MATCH SET THEORY COMPLEMENT
. '0-9., NESW' // ACCEPTABLE CHARACTERS
. ']' // END CHARACTER CLASS
. '#' // REGEX DELIMITER
;
// MAKE TEST CASES
foreach ($points as $point)
{
// NORMALIZE THE DATA AND BREAK ON COMMA
$pnt = strtoupper($point);
$pnt = preg_replace($rgx, NULL, $pnt);
$arr = explode(',', $pnt);
// ENSURE ONLY SINGLE BLANKS
$lat = trim(preg_replace('#\s\s+#', ' ', $arr[0]));
$lon = trim(preg_replace('#\s\s+#', ' ', $arr[1]));
// BREAK ON BLANKS AND RECONSTRUCT A DECIMAL VALUE
$xyz = explode(' ', $lat);
$lat = $xyz[0] + $xyz[1] / 60.0;
$lat = number_format($lat,7);
if ($xyz[2] == 'S') $lat = $lat * -1.0;
// BREAK ON BLANKS AND RECONSTRUCT A DECIMAL VALUE
$xyz = explode(' ', $lon);
$lon = $xyz[0] + $xyz[1] / 60.0;
$lon = number_format($lon,7);
if ($xyz[2] == 'W') $lon = $lon * -1.0;
// SHOW THE WORK PRODUCT WITH MAP POINTERS
echo "<br/>";
echo "<a target='_blank' href=\"https://maps.google.com/maps?q=" . htmlentities($point) . "&hl=en&z=14\">POSITION: $pnt</a>";
echo ' == ';
echo "<a target='_blank' href=\"https://maps.google.com/maps?q=$lat,$lon&hl=en&z=14\">GEOCODE: $lat,$lon</a>" . PHP_EOL;
}
echo '<br/>' . PHP_EOL;
// MAKE TEST CASES TO CONVERT GEOCODE TO POINT
foreach ($geocodes as $geocode)
{
// CONSTRUCT STRING HERE
$point = NULL;
// SEPARATE LAT AND LON
$arr = explode(',', $geocode);
$lat = trim(preg_replace('#[^0-9-.]#', NULL, $arr[0]));
$lon = trim(preg_replace('#[^0-9-.]#', NULL, $arr[1]));
// COMPUTE DEGREES, MINUTES, ETC
$xyz = explode('.', $lat);
$deg = $xyz[0] . '° ';
$min = '0.' . $xyz[1];
$min = $min * 60.0;
$min = $min . "' ";
if (strpos($deg, '-') === FALSE)
{
$min .= 'N';
}
else
{
$min .= 'S';
}
$deg = str_replace('-', NULL, $deg);
$point .= $deg . $min . ', ';
// COMPUTE DEGREES, MINUTES, ETC
$xyz = explode('.', $lon);
$deg = $xyz[0] . '° ';
$min = '0.' . $xyz[1];
$min = $min * 60.0;
$min = $min . "' ";
if (strpos($deg, '-') === FALSE)
{
$min .= 'E';
}
else
{
$min .= 'W';
}
$deg = str_replace('-', NULL, $deg);
$point .= $deg . $min;
// SHOW THE WORK PRODUCT WITH MAP POINTERS
echo "<br/>";
echo "<a target='_blank' href=\"https://maps.google.com/maps?q=" . htmlentities($point) . "&hl=en&z=14\">POSITION: $point</a>";
echo ' == ';
echo "<a target='_blank' href=\"https://maps.google.com/maps?q=$lat,$lon&hl=en&z=14\">GEOCODE: $lat,$lon</a>" . PHP_EOL;
}
ASKER
Thank you for providing the math. I eventually found a way to separate the various elements and apply the math using ColdFusion.
ASKER