prom85
prom85

Reputation: 17858

Convert degree/minutes/seconds to valid EXIF interface string

If I know the degress, minutes, and seconds of a location, how do I convert them to a valid location for ExifInterface.TAG_GPS_LATITUDE and ExifInterface.TAG_GPS_LONGITUDE?

I found following: https://developer.android.com/reference/android/media/ExifInterface.html#TAG_GPS_LATITUDE

But I'm not sure if I understand the format correctly. There is written following:

String. Format is "num1/denom1,num2/denom2,num3/denom3".

I'm not sure which fractions to use for each values... Always 1? Like in following code example:

String exifLatitude1 = degress+ "/1," + minutes + "/1," + seconds + "/1";

I often see strings with /1000 for the seconds, so I'm not sure if following is correct instead of the example above:

String exifLatitude2 = degress+ "/1," + minutes + "/1," + seconds + "/1000";

Can anyone tell me, which one is correct?

Upvotes: 2

Views: 605

Answers (1)

k3b
k3b

Reputation: 14755

My working solution uses milliseconds/1000

  • -79.948862 becomes
  • -79 degrees, 56 minutes, 55903 millisecs (equals 55.903 seconds)
  • 79/1,56/1,55903/1000

I have never checked if 79/1,56/1,56/1 would be ok, too.

I am using this code: from https://github.com/k3b/APhotoManager/blob/FDroid/app/src/main/java/de/k3b/android/util/ExifGps.java

public static boolean saveLatLon(File filePath, double latitude, double longitude) {
    exif = new ExifInterface(filePath.getAbsolutePath());
    debugExif(sb, "old", exif, filePath);

    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE, convert(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LATITUDE_REF, latitudeRef(latitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE, convert(longitude));
    exif.setAttribute(ExifInterface.TAG_GPS_LONGITUDE_REF, longitudeRef(longitude));

    exif.saveAttributes();
}

/**
 * convert latitude into DMS (degree minute second) format. For instance<br/>
 * -79.948862 becomes<br/>
 *  79/1,56/1,55903/1000<br/>
 * It works for latitude and longitude<br/>
 * @param latitude could be longitude.
 * @return
 */
private static final String convert(double latitude) {
    latitude=Math.abs(latitude);
    int degree = (int) latitude;
    latitude *= 60;
    latitude -= (degree * 60.0d);
    int minute = (int) latitude;
    latitude *= 60;
    latitude -= (minute * 60.0d);
    int second = (int) (latitude*1000.0d);

    StringBuilder sb = new StringBuilder(20);
    sb.append(degree);
    sb.append("/1,");
    sb.append(minute);
    sb.append("/1,");
    sb.append(second);
    sb.append("/1000");
    return sb.toString();
}

private static StringBuilder createDebugStringBuilder(File filePath) {
    return new StringBuilder("Set Exif to file='").append(filePath.getAbsolutePath()).append("'\n\t");
}

private static String latitudeRef(double latitude) {
    return latitude<0.0d?"S":"N";
}

Upvotes: 3

Related Questions