diff -ruN image_location.orig/README.txt image_location/README.txt --- image_location.orig/README.txt Thu Jan 1 01:00:00 1970 +++ image_location/README.txt Fri May 11 00:23:08 2007 @@ -0,0 +1,16 @@ +*** +*** Image Location module +*** +*** Copyright(c) 2006, Alexandru Badiu +*** Copyright(c) 2007, Lewis Thompson +*** + +image_location.module works with image.module and location.module to +automatically populate the latitude and longitude fields with GPS values from an +image with correct Exif data. + +To use this module make sure you have PHP's Exif functions available. + +http://search.cpan.org/~exiftool/Image-ExifTool-6.90/lib/Image/ExifTool/TagNames.pod#GPS_Tags +has a comprehensive list of GPS Exif tags. It should be noted that this module +currently makes use of just the GPSLatitude and GPSLongitude tags. diff -ruN image_location.orig/image_location.info image_location/image_location.info --- image_location.orig/image_location.info Thu Jan 1 01:00:00 1970 +++ image_location/image_location.info Fri May 11 00:04:56 2007 @@ -0,0 +1,11 @@ +; $Id$ +name = Image Location +description = Automatically fills in Location Latitude and Longitude fields based on Exif data. +package = Image +dependencies = image location + + +; Information added by drupal.org packaging script on 2007-03-25 +version = "5.x-1.0" +project = "image" + diff -ruN image_location.orig/image_location.install image_location/image_location.install --- image_location.orig/image_location.install Thu Jan 1 01:00:00 1970 +++ image_location/image_location.install Fri May 11 00:17:57 2007 @@ -0,0 +1,13 @@ + + * Copyright(c) 2007, Lewis Thompson + */ + +/** + * Implementation of hook_image_alter(). + * Listen out for updates to image nodes. On insert and update we + * automatically populate the latitude/longitude form fields with Exif data, if + * the user has not already done so. + * location.module handles the rest for us! + */ +function image_location_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) { + if ($node->type != 'image') { + // node was not an image + return; + } + + switch ($op) { + case 'insert': + case 'update': + /* $node->images is out of date as image.module calls image_insert() + to move the image from the temp directory to the final location before + we get a chance to read the Exif data. + We always call image_load() (as opposed to just on insert) to update + the $node->images array as the user could upload a replacement image. + */ + image_load($node); + + // qualify image filename from the _original node + $image = file_create_path($node->images['_original']); + + /* + * FIXME Currently we populate *all* empty location fields. Future work + * could involve being more sensible about this and populating a single + * field only, or a specific field. + */ + foreach ($node->locations as $index => $location) { + // check if the user has manually entered a lat/lon + if (!trim($node->locations[$index]['latitude']) && !trim($node->locations[$index]['longitude'])) { + // attempt to populate locations with Exif info + // Exif GPS tags: http://search.cpan.org/~exiftool/Image-ExifTool-6.90/lib/Image/ExifTool/TagNames.pod#GPS_Tags + if ($exif = exif_read_data($image, 'GPS')) { + if (isset($exif['GPSLatitude']) && isset($exif['GPSLongitude'])) { + // FIXME Future work: proper validity checking + $node->locations[$index]['latitude'] = _image_location_dms_to_dd($exif['GPSLatitude']); + $node->locations[$index]['longitude'] = _image_location_dms_to_dd($exif['GPSLongitude']); + } + } + } + } + break; + } +} + +/** + * Convert from Degree:Minute:Section to Decimal Degree. + * http://en.wikipedia.org/wiki/Geographic_coordinate_conversion#Conversion_from_DMS_to_Decimal_Degree + */ +function _image_location_dms_to_dd($dms) { + $deg = _image_location_convert_to_dec($dms[0]); + $min = _image_location_convert_to_dec($dms[1]); + $sec = _image_location_convert_to_dec($dms[2]); + + return $deg + ($min / 60) + ($sec / 3600); +} + +/** + * String fraction to decimal. + * Convert 'x/y' to x/y as a decimal. e.g.: + * $value = 2692/100 or 21/1 + */ +function _image_location_convert_to_dec($value) { + // explode to get x and y in an array. + $dec = explode('/', $value); + + // confirm we are not dividing by zero + if ((count($dec) == 2) && ($dec[1] > 0)) { + return $dec[0] / $dec[1]; + } else { + return ""; + } +} + +?>