CFLib.org – Common Function Library Project

sexagesimalToDecimal(latitude, latitudeRef, longitude, longitudeRef)

Last updated December 20, 2012

author

John Allen

Version: 1 | Requires: CF9 | Library: UtilityLib

Description:
Say you have a .jpg file and it has EXIF data with its location information. Sometimes the EXIF location information is in sexagesimal format which is hard to use in popular mapping libraries. This code puts the information into the more "normal" decimal lat/lng format. Ben Nadel was the inspiration for this, see: http://www.bennadel.com/index.cfm?event=blog.viewcode&id=1832&index=1

Return Values:
A struct with keys longitude and latitude

Example:

<cfimage action="read" source="c:\foo.jpg" name="img">
<cfset exifTags = ImageGetEXIFMetaData(img)>

<cfset coordinates = sexagesimalToDecimal(
	exifTags["GPS Latitude"],
	exifTags["GPS Latitude Ref"],
	exifTags["GPS Longitude"],
	exifTags[ "GPS Longitude Ref" ]
)>
<cfdump var="#coordinates#">

Parameters:

Name Description Required
latitude Latitude in degrees/min/sec, using " and ' delimiters Yes
latitudeRef N or S Yes
longitude Longitude in degrees/min/sec, using " and ' delimiters Yes
longitudeRef E or W Yes

Full UDF Source:

/**
 * Changes a sexagesimal latitude longitude co-ordinates to decimal format
 * v0.9 by John Allen
 * v1.0 by Adam Cameron (converted to script, changed return value to be a struct rather than a specially-formatted string)
 * 
 * @param latitude 	 Latitude in degrees/min/sec, using " and ' delimiters (Required)
 * @param latitudeRef 	 N or S (Required)
 * @param longitude 	 Longitude in degrees/min/sec, using " and ' delimiters (Required)
 * @param longitudeRef 	 E or W (Required)
 * @return A struct with keys longitude and latitude 
 * @author John Allen (jallen@figleaf.com) 
 * @version 1.0, December 20, 2012 
 */
function sexagesimalToDecimal(latitude, latitudeRef, longitude, longitudeRef){
	var coordinates = {};
	var latitudeParts = listToArray(arguments.latitude, "'""");
	coordinates.latitude = (
		latitudeParts[1] +
		(latitudeParts[2] / 60) +
		(latitudeParts[3] / 3600)
	);
	if (arguments.latitudeRef == "S"){
		coordinates.latitude *= -1;
	}

	var longitudeParts = listToArray(arguments.longitude,"'""");
	coordinates.longitude = (
		longitudeParts[1] +
		(longitudeParts[2] / 60) +
		(longitudeParts[3] / 3600)
	);
	if (arguments.longitudeRef == "W"){
		coordinates.longitude *= -1;
	}
	return coordinates;
}
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson