How do get real value of latitude and longitude from GpsDirectory?

geocoding, java

Solution

If using metadata-extractor >= 2.6.0 you can use new `com.drew.lang.GeoLocation` class (changelog).

GpsDirectory gpsDirectory = metadata.getDirectory(GpsDirectory.class);
GeoLocation location = gpsDirectory.getGeoLocation();
double lat = location.getLatitude();
double lng = location.getLongitude();

If not, this is what is being done in the new class sources:

   /**
     * Converts DMS (degrees-minutes-seconds) rational values, as given 
     * in {@link com.drew.metadata.exif.GpsDirectory},
     * into a single value in degrees, as a double.
     */
    @Nullable
    public static Double degreesMinutesSecondsToDecimal(
            @NotNull final Rational degs, @NotNull final Rational mins, 
            @NotNull final Rational secs, final boolean isNegative)  {

        double decimal = Math.abs(degs.doubleValue())
                + mins.doubleValue() / 60.0d
                + secs.doubleValue() / 3600.0d;

        if (Double.isNaN(decimal))
            return null;

        if (isNegative)
            decimal *= -1;

        return decimal;
    }

where the method parameters come from:

Rational[] latitudes = getRationalArray(GpsDirectory.TAG_LATITUDE);
Rational[] longitudes = getRationalArray(GpsDirectory.TAG_LONGITUDE);
String latitudeRef = getString(GpsDirectory.TAG_LATITUDE_REF);
String longitudeRef = getString(GpsDirectory.TAG_LONGITUDE_REF);

Problem

I am writing a Java program which actually read all meta data of an image (ex. latitude, longitude and datetime). Following is the my sample code which i am running. ``` public static void findLatLong(File jpg){ try { Metadata metadata = ImageMetadataReader.readMetadata(jpg); if (metadata.containsDirectory(GpsDirectory.class)) { GpsDirectory gpsDir =(GpsDirectory)metadata.getDirectory(GpsDirectory.class); GpsDescriptor gpsDesc = new GpsDescriptor(gpsDir); System.out.println("Latitude: " + gpsDesc.getGpsLatitudeDescription()); System.out.println("Longitude : " + gpsDesc.getGpsLongitudeDescription()); System.out.println("Date : " + gpsDesc.getGpsTimeStampDescription()); System.out.println("Minute : " + gpsDesc.getDegreesMinutesSecondsDescription()); }else{ // System.out.println("----- Did not find GPS Information-------------for file " + jpg.getName() ); } } catch (ImageProcessingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } ``` Output: - Latitude: 21.0° 8.0' 22.999999999998693" Longitude : 79.0° 3.0' 12.999999999994998" Date : 14:16:30 UTC Minute : 21.0° 8.0' 22.999999999998693", 79.0° 3.0' 12.999999999994998" I suppose this all are degree format. Can anybody point me how do get real value for latitude and longitude. And also to get proper date.

Original source