-- -- PhotoGpsData.lua -- http://regex.info/blog/lightroom-goodies/gps/ -- jfriedl@yahoo.com -- -- Version 20100726.2 -- -- -- Import this file into your code, and call -- TieShadowGPS(photo) -- on your LrPhoto object, and subsequent calls for GPS metadata will -- reflect the shadow GPS metadata maintained by my plugin. -- -- -- In addition to the latitude and longitude normally found in the table returned by -- photo:getRawMetadata('gps') -- if there is shadow data available, all data is returned: -- -- { -- latitude = 23.456, -- longitude = 78.91011, -- altitude = 123, -- in meters -- speed = 45.6, -- in kph -- bearing = 123, -- in degrees -- time = 184784722, -- Coco timestamp for the image, possibly adjusted for clock skew -- isShadow = true, -- } -- -- Note: all non-nil fields are of type == 'number' except isShadow. -- local TARGET_PLUGIN_ID = "info.regex.lightroom.gps" local TARGET_PLUGIN = _PLUGIN.id == TARGET_PLUGIN_ID and _PLUGIN or TARGET_PLUGIN_ID local LR2 = (import 'LrApplication'.versionTable().major == 2) local LrTasks = import 'LrTasks' local function my_getRawMetadata(self, key) if key == 'gps' or key == 'gpsAltitude' then -- -- Need to trap errors in Lr2. In Lr3, final arg of true means don't throw -- local success, shadow if LR2 then success, shadow = LrTasks.pcall(self.getPropertyForPlugin, self, TARGET_PLUGIN, 'data') else success = true shadow = self:getPropertyForPlugin(TARGET_PLUGIN, 'data', nil, true) end if success and shadow then if not shadow.latitude or not shadow.longitude then -- present-but-empty shadow data: override the "real" GPS data with nothingness return nil else if key == 'gpsAltitude' then return shadow.altitude else shadow.isShadow = true return shadow end end end end return self:orig_getRawMetadata(key) end local function pretty_degree(value, whenPos, whenNeg) local dir value = tonumber(value) if value >= 0 then dir = whenPos else dir = whenNeg value = -value end local D, Mx = math.modf(value) local M, Sx = math.modf(Mx * 60) local S = tonumber(string.format("%.2f", Sx * 60)) return LOC("$$$/x1613=^1^D^2\'^3\" ^4", D, M, S, dir) end local function my_getFormattedMetadata(self, key) if key == 'gps' then local gps = my_getRawMetadata(self, 'gps') if gps and gps.latitude and gps.longitude then return pretty_degree(gps.latitude, "N", "S") .. " " .. pretty_degree(gps.longitude, "E", "W") else return nil end elseif key == 'gpsAltitude' then local altitude = my_getRawMetadata(self, 'gpsAltitude') if altitude then return LOC("$$$/xxx=^1 m", string.format('%.1f', altitude)) else return nil end else return self:orig_getFormattedMetadata(key) end end -- -- returns an error string on error, or nil on success -- function TieShadowGPS(photo) if type(photo) ~= 'table' or not photo.getRawMetadata then return "argument to TieShadowGPS() should be an LrPhoto object" end if not photo.orig_getRawMetadata then photo.orig_getRawMetadata = photo.getRawMetadata photo.orig_getFormattedMetadata = photo.getFormattedMetadata end photo.getRawMetadata = my_getRawMetadata photo.getFormattedMetadata = my_getFormattedMetadata return nil end