1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
5 * Copyright (C) 2011-2014, Rob Norris <rw_norris@hotmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 * This uses EXIF information from images to create waypoints at those positions
26 * The initial implementation uses libexif, which keeps Viking a pure C program.
27 * Now libgexiv2 is available (in C as a wrapper around the more powerful libexiv2 [C++]) so this is the preferred build.
28 * The attentative reader will notice the use of gexiv2 is a lot simpler as well.
29 * For the time being the libexif code + build is still made available.
32 #include "geotag_exif.h"
44 #include <glib/gi18n.h>
45 #include <glib/gstdio.h>
47 #include <gexiv2/gexiv2.h>
50 #include <libexif/exif-data.h>
51 #include "libjpeg/jpeg-data.h"
56 * Attempt to get a single comment from the various exif fields
58 static gchar* geotag_get_exif_comment ( GExiv2Metadata *gemd )
61 // Try various options to create a comment
63 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.ImageDescription" ) )
64 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.ImageDescription" ) );
66 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPComment" ) )
67 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPComment" ) );
69 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPSubject" ) )
70 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPSubject" ) );
72 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.DateTimeOriginal" ) )
73 return g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.DateTimeOriginal" ) );
75 // Otherwise nothing found
82 * Attempt to get a single comment from the various exif fields
84 static gchar* geotag_get_exif_comment ( ExifData *ed )
89 // Try various options to create a comment
91 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_IMAGE_DESCRIPTION);
93 exif_entry_get_value ( ee, str, 128 );
94 return g_strdup ( str );
97 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_COMMENT);
99 exif_entry_get_value ( ee, str, 128 );
100 return g_strdup ( str );
103 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_SUBJECT);
105 exif_entry_get_value ( ee, str, 128 );
106 return g_strdup ( str );
109 // Consider using these for existing GPS info??
110 //#define EXIF_TAG_GPS_TIME_STAMP 0x0007
111 //#define EXIF_TAG_GPS_DATE_STAMP 0x001d
112 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL);
114 exif_entry_get_value ( ee, str, 128 );
115 return g_strdup ( str );
118 // Otherwise nothing found
123 * Handles 3 part location Rationals
124 * Handles 1 part rational (must specify 0 for the offset)
126 static gdouble Rational2Double ( unsigned char *data, int offset, ExifByteOrder order )
128 // Explaination from GPS Correlate 'exif-gps.cpp' v 1.6.1
129 // What we are trying to do here is convert the three rationals:
133 // dd/v is easy: result = dd/v.
138 // ss/v is sorta easy.
140 // -- / 3600 = result
142 // Each part is added to the final number.
145 er = exif_get_rational (data, order);
146 ans = (gdouble)er.numerator / (gdouble)er.denominator;
150 er = exif_get_rational (data+(1*offset), order);
151 ans = ans + ( ( (gdouble)er.numerator / (gdouble)er.denominator ) / 60.0 );
152 er = exif_get_rational (data+(2*offset), order);
153 ans = ans + ( ( (gdouble)er.numerator / (gdouble)er.denominator ) / 3600.0 );
158 static struct LatLon get_latlon ( ExifData *ed )
160 struct LatLon ll = { 0.0, 0.0 };
161 const struct LatLon ll0 = { 0.0, 0.0 };
166 // Lat & Long is necessary to form a waypoint.
168 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE);
169 if ( ! ( ee && ee->components == 3 && ee->format == EXIF_FORMAT_RATIONAL ) )
172 ll.lat = Rational2Double ( ee->data,
173 exif_format_get_size(ee->format),
174 exif_data_get_byte_order(ed) );
176 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE_REF);
178 exif_entry_get_value ( ee, str, 128 );
183 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE);
184 if ( ! ( ee && ee->components == 3 && ee->format == EXIF_FORMAT_RATIONAL ) )
187 ll.lon = Rational2Double ( ee->data,
188 exif_format_get_size(ee->format),
189 exif_data_get_byte_order(ed) );
191 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE_REF);
193 exif_entry_get_value ( ee, str, 128 );
203 * a_geotag_get_position:
205 * @filename: The (JPG) file with EXIF information in it
207 * Returns: The position in LatLon format.
208 * It will be 0,0 if some kind of failure occurs.
210 struct LatLon a_geotag_get_position ( const gchar *filename )
212 struct LatLon ll = { 0.0, 0.0 };
214 #ifdef HAVE_LIBGEXIV2
215 GExiv2Metadata *gemd = gexiv2_metadata_new ();
216 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
220 if ( gexiv2_metadata_get_gps_info ( gemd, &lon, &lat, &alt ) ) {
225 gexiv2_metadata_free ( gemd );
228 // open image with libexif
229 ExifData *ed = exif_data_new_from_file ( filename );
231 // Detect EXIF load failure
235 ExifEntry *ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
236 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
237 if ( ! ( ee && ee->components == 4 ) )
240 ll = get_latlon ( ed );
243 // Finished with EXIF
244 exif_data_free ( ed );
252 * a_geotag_create_waypoint_from_file:
253 * @filename: The image file to process
254 * @vcmode: The current location mode to use in the positioning of Waypoint
255 * @name: Returns a name for the Waypoint (can be NULL)
257 * Returns: An allocated Waypoint or NULL if Waypoint could not be generated (e.g. no EXIF info)
260 VikWaypoint* a_geotag_create_waypoint_from_file ( const gchar *filename, VikCoordMode vcmode, gchar **name )
262 // Default return values (for failures)
264 VikWaypoint *wp = NULL;
266 #ifdef HAVE_LIBGEXIV2
267 GExiv2Metadata *gemd = gexiv2_metadata_new ();
268 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
272 if ( gexiv2_metadata_get_gps_info ( gemd, &lon, &lat, &alt ) ) {
278 // Now create Waypoint with acquired information
280 wp = vik_waypoint_new();
282 // Set info from exif values
284 vik_coord_load_from_latlon ( &(wp->coord), vcmode, &ll );
288 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPTitle" ) )
289 *name = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPTitle" ) );
290 wp->comment = geotag_get_exif_comment ( gemd );
292 vik_waypoint_set_image ( wp, filename );
295 gexiv2_metadata_free ( gemd );
299 //ExifLog *log = NULL;
301 // open image with libexif
302 ExifData *ed = exif_data_new_from_file ( filename );
304 // Detect EXIF load failure
306 // return with no Waypoint
314 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
315 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
316 if ( ! ( ee && ee->components == 4 ) )
318 // Could test for these versions explicitly but may have byte order issues...
319 //if ( ! ( ee->data[0] == 2 && ee->data[2] == 0 && ee->data[3] == 0 ) )
322 ll = get_latlon ( ed );
324 // Hopefully won't have valid images at 0,0!
325 if ( ll.lat == 0.0 && ll.lon == 0.0 )
329 // Not worried if none of the other fields exist, as can default the values to something
332 gdouble alt = VIK_DEFAULT_ALTITUDE;
333 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_ALTITUDE);
334 if ( ee && ee->components == 1 && ee->format == EXIF_FORMAT_RATIONAL ) {
335 alt = Rational2Double ( ee->data,
337 exif_data_get_byte_order(ed) );
339 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_ALTITUDE_REF);
340 if ( ee && ee->components == 1 && ee->format == EXIF_FORMAT_BYTE && ee->data[0] == 1 )
345 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_TITLE);
347 exif_entry_get_value ( ee, str, 128 );
348 *name = g_strdup ( str );
352 // Now create Waypoint with acquired information
354 wp = vik_waypoint_new();
356 // Set info from exif values
358 vik_coord_load_from_latlon ( &(wp->coord), vcmode, &ll );
362 wp->comment = geotag_get_exif_comment ( ed );
364 vik_waypoint_set_image ( wp, filename );
367 // Finished with EXIF
368 exif_data_free ( ed );
376 * a_geotag_waypoint_positioned:
377 * @filename: The image file to process
378 * @coord: The location for positioning the Waypoint
379 * @name: Returns a name for the Waypoint (can be NULL)
380 * @waypoint: An existing waypoint to update (can be NULL to generate a new waypoint)
382 * Returns: An allocated waypoint if the input waypoint is NULL,
383 * otherwise the passed in waypoint is updated
385 * Here EXIF processing is used to get non position related information (i.e. just the comment)
388 VikWaypoint* a_geotag_waypoint_positioned ( const gchar *filename, VikCoord coord, gdouble alt, gchar **name, VikWaypoint *wp )
392 // Need to create waypoint
393 wp = vik_waypoint_new();
399 #ifdef HAVE_LIBGEXIV2
400 GExiv2Metadata *gemd = gexiv2_metadata_new ();
401 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
402 wp->comment = geotag_get_exif_comment ( gemd );
403 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Image.XPTitle" ) )
404 *name = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.XPTitle" ) );
406 gexiv2_metadata_free ( gemd );
409 ExifData *ed = exif_data_new_from_file ( filename );
411 // Set info from exif values
413 wp->comment = geotag_get_exif_comment ( ed );
418 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_0], EXIF_TAG_XP_TITLE);
420 exif_entry_get_value ( ee, str, 128 );
421 *name = g_strdup ( str );
424 // Finished with EXIF
425 exif_data_free ( ed );
430 vik_waypoint_set_image ( wp, filename );
436 * a_geotag_get_exif_date_from_file:
437 * @filename: The image file to process
438 * @has_GPS_info: Returns whether the file has existing GPS information
440 * Returns: An allocated string with the date and time in EXIF_DATE_FORMAT, otherwise NULL if some kind of failure
442 * Here EXIF processing is used to get time information
445 gchar* a_geotag_get_exif_date_from_file ( const gchar *filename, gboolean *has_GPS_info )
447 gchar* datetime = NULL;
448 *has_GPS_info = FALSE;
450 #ifdef HAVE_LIBGEXIV2
451 GExiv2Metadata *gemd = gexiv2_metadata_new ();
452 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
454 *has_GPS_info = ( gexiv2_metadata_get_gps_longitude(gemd,&lon) && gexiv2_metadata_get_gps_latitude(gemd,&lat) );
456 // Prefer 'Photo' version over 'Image'
457 if ( gexiv2_metadata_has_tag ( gemd, "Exif.Photo.DateTimeOriginal" ) )
458 datetime = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Photo.DateTimeOriginal" ) );
460 datetime = g_strdup ( gexiv2_metadata_get_tag_interpreted_string ( gemd, "Exif.Image.DateTimeOriginal" ) );
462 gexiv2_metadata_free ( gemd );
465 ExifData *ed = exif_data_new_from_file ( filename );
467 // Detect EXIF load failure
474 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_EXIF], EXIF_TAG_DATE_TIME_ORIGINAL);
476 exif_entry_get_value ( ee, str, 128 );
477 datetime = g_strdup ( str );
482 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_VERSION_ID);
483 // Confirm this has a GPS Id - normally "2.0.0.0" or "2.2.0.0"
484 if ( ee && ee->components == 4 )
485 *has_GPS_info = TRUE;
487 // Check other basic GPS fields exist too
488 // I have encountered some images which have just the EXIF_TAG_GPS_VERSION_ID but nothing else
489 // So to confirm check more EXIF GPS TAGS:
490 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LATITUDE);
492 *has_GPS_info = FALSE;
493 ee = exif_content_get_entry (ed->ifd[EXIF_IFD_GPS], EXIF_TAG_GPS_LONGITUDE);
495 *has_GPS_info = FALSE;
497 exif_data_free ( ed );
505 /**! If the entry doesn't exist, create it.
506 * Based on exif command line action_create_value function in exif 0.6.20
508 static ExifEntry* my_exif_create_value (ExifData *ed, ExifTag tag, ExifIfd ifd)
510 ExifEntry *e = exif_content_get_entry (ed->ifd[ifd], tag);
512 e = exif_entry_new ();
513 exif_content_add_entry (ed->ifd[ifd], e);
515 exif_entry_initialize (e, tag);
517 // exif_entry_initialize doesn't seem to do much, especially for the GPS tags
518 // so have to setup fields ourselves:
521 if ( tag == EXIF_TAG_GPS_VERSION_ID ) {
522 e->format = EXIF_FORMAT_BYTE;
524 e->size = sizeof (char) * e->components;
527 e->data = g_malloc (e->size);
529 if ( tag == EXIF_TAG_GPS_MAP_DATUM ||
530 tag == EXIF_TAG_GPS_LATITUDE_REF || tag == EXIF_TAG_GPS_LONGITUDE_REF ||
531 tag == EXIF_TAG_GPS_PROCESSING_METHOD ) {
532 e->format = EXIF_FORMAT_ASCII;
533 // NB Allocation is handled later on when the actual string used is known
535 if ( tag == EXIF_TAG_GPS_LATITUDE || tag == EXIF_TAG_GPS_LONGITUDE ) {
536 e->format = EXIF_FORMAT_RATIONAL;
538 e->size = sizeof (ExifRational) * e->components;
541 e->data = g_malloc (e->size);
543 if ( tag == EXIF_TAG_GPS_ALTITUDE ) {
544 e->format = EXIF_FORMAT_RATIONAL;
546 e->size = sizeof (ExifRational) * e->components;
549 e->data = g_malloc (e->size);
551 if ( tag == EXIF_TAG_GPS_ALTITUDE_REF ) {
553 e->size = sizeof (char) * e->components;
556 e->data = g_malloc (e->size);
558 /* The entry has been added to the IFD, so we can unref it */
559 //exif_entry_unref(e);
560 // Crashes later on, when saving to jpeg if the above unref is enabled!!
561 // ?Some other malloc problem somewhere?
566 /** Heavily based on convert_arg_to_entry from exif command line tool.
567 * But without ExifLog, exitting, use of g_* io functions
568 * and can take a gdouble value instead of a string
570 static void convert_to_entry (const char *set_value, gdouble gdvalue, ExifEntry *e, ExifByteOrder o)
572 unsigned int i, numcomponents;
573 char *value_p = NULL;
576 * ASCII strings are handled separately,
577 * since they don't require any conversion.
579 if (e->format == EXIF_FORMAT_ASCII ||
580 e->tag == EXIF_TAG_USER_COMMENT) {
581 if (e->data) g_free (e->data);
582 e->components = strlen (set_value) + 1;
583 if (e->tag == EXIF_TAG_USER_COMMENT)
584 e->components += 8 - 1;
585 e->size = sizeof (char) * e->components;
586 e->data = g_malloc (e->size);
588 g_warning (_("Not enough memory."));
591 if (e->tag == EXIF_TAG_USER_COMMENT) {
592 /* assume ASCII charset */
593 /* TODO: get this from the current locale */
594 memcpy ((char *) e->data, "ASCII\0\0\0", 8);
595 memcpy ((char *) e->data + 8, set_value,
598 strcpy ((char *) e->data, set_value);
603 * Make sure we can handle this entry
605 if ((e->components == 0) && *set_value) {
606 g_warning (_("Setting a value for this tag is unsupported!"));
610 gboolean use_string = (set_value != NULL);
612 /* Copy the string so we can modify it */
613 buf = g_strdup (set_value);
616 value_p = strtok (buf, " ");
619 numcomponents = e->components;
620 for (i = 0; i < numcomponents; ++i) {
625 g_warning (_("Too few components specified (need %d, found %d)\n"), numcomponents, i);
628 if (!isdigit(*value_p) && (*value_p != '+') && (*value_p != '-')) {
629 g_warning (_("Numeric value expected\n"));
634 s = exif_format_get_size (e->format);
636 case EXIF_FORMAT_ASCII:
637 g_warning (_("This shouldn't happen!"));
640 case EXIF_FORMAT_SHORT:
641 exif_set_short (e->data + (s * i), o, atoi (value_p));
643 case EXIF_FORMAT_SSHORT:
644 exif_set_sshort (e->data + (s * i), o, atoi (value_p));
646 case EXIF_FORMAT_RATIONAL: {
650 if ( use_string && value_p )
651 val = fabs (atol (value_p));
653 val = fabs (gdvalue);
656 // One (or first) part rational
658 // Sneak peek into tag as location tags need rounding down to give just the degrees part
659 if ( e->tag == EXIF_TAG_GPS_LATITUDE || e->tag == EXIF_TAG_GPS_LONGITUDE ) {
660 er.numerator = (ExifLong) floor ( val );
661 er.denominator = 1.0;
664 // I don't see any point in doing anything too complicated here,
665 // such as trying to work out the 'best' denominator
666 // For the moment use KISS principle.
667 // Fix a precision of 1/100 metre as that's more than enough for GPS accuracy especially altitudes!
668 er.denominator = 100.0;
669 er.numerator = (ExifLong) (val * er.denominator);
673 // Now for Location 3 part rationals do Mins and Seconds format
675 // Rounded down minutes
677 er.denominator = 1.0;
678 er.numerator = (ExifLong) ( (int) floor ( ( val - floor (val) ) * 60.0 ) );
683 er.denominator = 100.0;
685 // Fractional minute.
686 double FracPart = ((val - floor(val)) * 60) - (double)(int) floor ( ( val - floor (val) ) * 60.0 );
687 er.numerator = (ExifLong) ( (int)floor(FracPart * 6000) ); // Convert to seconds.
689 exif_set_rational (e->data + (s * i), o, er );
692 case EXIF_FORMAT_LONG:
693 exif_set_long (e->data + (s * i), o, atol (value_p));
695 case EXIF_FORMAT_SLONG:
696 exif_set_slong (e->data + (s * i), o, atol (value_p));
698 case EXIF_FORMAT_BYTE:
699 case EXIF_FORMAT_SBYTE:
700 case EXIF_FORMAT_UNDEFINED: /* treat as byte array */
701 e->data[s * i] = atoi (value_p);
703 case EXIF_FORMAT_FLOAT:
704 case EXIF_FORMAT_DOUBLE:
705 case EXIF_FORMAT_SRATIONAL:
707 g_warning (_("Not yet implemented!"));
712 value_p = strtok (NULL, " ");
720 g_warning (_("Warning; Too many components specified!"));
725 * a_geotag_write_exif_gps:
726 * @filename: The image file to save information in
727 * @coord: The location
728 * @alt: The elevation
730 * Returns: A value indicating success: 0, or some other value for failure
733 gint a_geotag_write_exif_gps ( const gchar *filename, VikCoord coord, gdouble alt, gboolean no_change_mtime )
735 gint result = 0; // OK so far...
737 // Save mtime for later use
738 struct stat stat_save;
739 if ( no_change_mtime )
740 if ( stat ( filename, &stat_save ) != 0 )
741 g_warning ( "%s couldn't read: %s", __FUNCTION__, filename );
743 #ifdef HAVE_LIBGEXIV2
744 GExiv2Metadata *gemd = gexiv2_metadata_new ();
745 if ( gexiv2_metadata_open_path ( gemd, filename, NULL ) ) {
747 vik_coord_to_latlon ( &coord, &ll );
748 if ( ! gexiv2_metadata_set_gps_info ( gemd, ll.lon, ll.lat, alt ) ) {
749 result = 1; // Failed
752 GError *error = NULL;
753 if ( ! gexiv2_metadata_save_file ( gemd, filename, &error ) ) {
755 g_warning ( "Write EXIF failure:%s" , error->message );
756 g_error_free ( error );
760 gexiv2_metadata_free ( gemd );
764 Appears libexif doesn't actually support writing EXIF data directly to files
765 Thus embed command line exif writing method within Viking
766 (for example this is done by Enlightment - http://www.enlightenment.org/ )
767 This appears to be JPEG only, but is probably 99% of our use case
768 Alternatively consider using libexiv2 and C++...
771 // Actual EXIF settings here...
774 /* Parse the JPEG file. */
775 jdata = jpeg_data_new ();
776 jpeg_data_load_file (jdata, filename);
778 // Get current values
779 ExifData *ed = exif_data_new_from_file ( filename );
781 ed = exif_data_new ();
783 // Update ExifData with our new settings
786 // I don't understand it, but when saving the 'ed' nothing gets set after putting in the GPS ID tag - so it must come last
787 // (unless of course there is some bug in the setting of the ID, that prevents subsequent tags)
790 ee = my_exif_create_value (ed, EXIF_TAG_GPS_ALTITUDE, EXIF_IFD_GPS);
791 convert_to_entry ( NULL, alt, ee, exif_data_get_byte_order(ed) );
793 // byte 0 meaning "sea level" or 1 if the value is negative.
794 ee = my_exif_create_value (ed, EXIF_TAG_GPS_ALTITUDE_REF, EXIF_IFD_GPS);
795 convert_to_entry ( alt < 0.0 ? "1" : "0", 0.0, ee, exif_data_get_byte_order(ed) );
797 ee = my_exif_create_value (ed, EXIF_TAG_GPS_PROCESSING_METHOD, EXIF_IFD_GPS);
798 // see http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/GPS.html
799 convert_to_entry ( "MANUAL", 0.0, ee, exif_data_get_byte_order(ed) );
801 ee = my_exif_create_value (ed, EXIF_TAG_GPS_MAP_DATUM, EXIF_IFD_GPS);
802 convert_to_entry ( "WGS-84", 0.0, ee, exif_data_get_byte_order(ed) );
805 vik_coord_to_latlon ( &coord, &ll );
807 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LATITUDE_REF, EXIF_IFD_GPS);
809 convert_to_entry ( ll.lat < 0.0 ? "S" : "N", 0.0, ee, exif_data_get_byte_order(ed) );
811 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LATITUDE, EXIF_IFD_GPS);
812 convert_to_entry ( NULL, ll.lat, ee, exif_data_get_byte_order(ed) );
814 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LONGITUDE_REF, EXIF_IFD_GPS);
816 convert_to_entry ( ll.lon < 0.0 ? "W" : "E", 0.0, ee, exif_data_get_byte_order(ed) );
818 ee = my_exif_create_value (ed, EXIF_TAG_GPS_LONGITUDE, EXIF_IFD_GPS);
819 convert_to_entry ( NULL, ll.lon, ee, exif_data_get_byte_order(ed) );
821 ee = my_exif_create_value (ed, EXIF_TAG_GPS_VERSION_ID, EXIF_IFD_GPS);
822 //convert_to_entry ( "2 0 0 0", 0.0, ee, exif_data_get_byte_order(ed) );
823 convert_to_entry ( "2 2 0 0", 0.0, ee, exif_data_get_byte_order(ed) );
825 jpeg_data_set_exif_data (jdata, ed);
828 /* Save the modified image. */
829 result = jpeg_data_save_file (jdata, filename);
831 // Convert result from 1 for success, 0 for failure into our scheme
834 jpeg_data_unref (jdata);
837 // Epic fail - file probably not a JPEG
841 exif_data_free ( ed );
845 if ( no_change_mtime ) {
846 // Restore mtime, using the saved value
847 struct stat stat_tmp;
849 (void)stat ( filename, &stat_tmp );
850 utb.actime = stat_tmp.st_atime;
851 utb.modtime = stat_save.st_mtime;
852 // Not security critical, thus potential Time of Check Time of Use race condition is not bad
854 if ( g_utime ( filename, &utb ) != 0 )
855 g_warning ( "%s couldn't set time on: %s", __FUNCTION__, filename );