0

I'm getting the EXIF data from an image on mobile using react-native, when reading the data I'm getting it back in the following format 33/1,53/1,87/100

How do I convert that to proper latitude

Here is my code

// Adds latitude and longitude
        let latitude = ''
        let longitude = ''
        console.log(image.exif.GPSLatitude)
        if(image.exif.GPSLatitude != null && image.exif.GPSLongitude != null){
        console.log('running')

        // latitude
        const latitudeDegrees = image.exif.GPSLatitude.slice(0, image.exif.GPSLatitude.indexOf("/"));
        const value1 = image.exif.GPSLatitude.substr(image.exif.GPSLatitude.indexOf(",") + 1);
        const latitudeMinutes = value1.slice(0, value1.indexOf("/"));
        const value2 = value1.substr(value1.indexOf(",") + 1);
        const latitudeSeconds = value2.slice(0, value2.indexOf("/"));
        latitude = { name : 'latitude', data : _helpers.convertDegreeAngleToDecimal(latitudeDegrees, latitudeMinutes, latitudeSeconds )};


        // longitude
        const longitudeDegrees = image.exif.GPSLongitude.slice(0, image.exif.GPSLongitude.indexOf("/"));
        const value1L = image.exif.GPSLongitude.substr(image.exif.GPSLongitude.indexOf(",") + 1);
        const longitudeMinutes = value1L.slice(0, value1L.indexOf("/"));
        const value2L = value1L.substr(value1L.indexOf(",") + 1);
        const longitudeSeconds = value2L.slice(0, value2L.indexOf("/"));
        longitude = { name : 'longitude', data : _helpers.convertDegreeAngleToDecimal(longitudeDegrees, longitudeMinutes, longitudeSeconds)};
    }
        console.log(latitude)
        console.log(longitude)
        const array4 = update(array3, {$push: [latitude]});
        const array5 = update(array4, {$push: [longitude]});

The helper function

// Function
module.exports = {
    convertDegreeAngleToDecimal: function (degrees,  minutes, seconds) {
        // Decimal degrees =
        //   whole number of degrees,
        //   plus minutes divided by 60,
        //   plus seconds divided by 3600

        return degrees + (minutes/60) + (seconds/3600);
    }
}

And these are the values I'm sending

33 53 87
151 12 3560

Orginal format before conversion

33/1,53/1,87/100
151/1,12/1,3560/100
Almog
  • 2,639
  • 6
  • 30
  • 59
  • 1
    What is the desired output format? (Please [edit] your question to show an example, because "proper" is a matter of opinion.) – nnnnnn Oct 04 '17 at 02:29

2 Answers2

1

I was able to get this working, you need the direction here is my final version

if(image.exif.GPSLatitude != null && image.exif.GPSLongitude != null){

            // latitude
            const latitudeDegrees = image.exif.GPSLatitude.slice(0, image.exif.GPSLatitude.indexOf("/"));
            const value1 = image.exif.GPSLatitude.substr(image.exif.GPSLatitude.indexOf(",") + 1);
            const latitudeMinutes = value1.slice(0, value1.indexOf("/"));
            const value2 = value1.substr(value1.indexOf(",") + 1);
            const latitudeSeconds = value2.slice(0, value2.indexOf("/"));
            //console.log(latitudeDegrees, latitudeMinutes, latitudeSeconds)
            latitude = { name : 'latitude', data : _helpers.convertDegreeAngleToDecimal(latitudeDegrees, latitudeMinutes, latitudeSeconds, image.exif.GPSLatitudeRef ).toString()};


            // longitude
            const longitudeDegrees = image.exif.GPSLongitude.slice(0, image.exif.GPSLongitude.indexOf("/"));
            const value1L = image.exif.GPSLongitude.substr(image.exif.GPSLongitude.indexOf(",") + 1);
            const longitudeMinutes = value1L.slice(0, value1L.indexOf("/"));
            const value2L = value1L.substr(value1L.indexOf(",") + 1);
            const longitudeSeconds = value2L.slice(0, value2L.indexOf("/"));
            //console.log(longitudeDegrees, longitudeMinutes, longitudeSeconds)
            longitude = { name : 'longitude', data : _helpers.convertDegreeAngleToDecimal(longitudeDegrees, longitudeMinutes, longitudeSeconds, image.exif.GPSLongitudeRef).toString()};
        }

convertDegreeAngleToDecimal: function (degrees, minutes, seconds, direction) {
        var results = Number(degrees) + Number(minutes)/60 + Number(seconds)/(60*60);
        if (direction == "S" || direction == "W") {
            results = results * -1;
        }
        return results;
    }
Almog
  • 2,639
  • 6
  • 30
  • 59
0

Looks like that format you have is in degrees, minutes, seconds, and you want to convert that to decimal degrees.

Adapting the solution from here: https://stackoverflow.com/a/3249890/8595398 into javascript:

function ConvertDegreeAngleToDecimal( degrees,  minutes, seconds )
{
    // Decimal degrees = 
    //   whole number of degrees, 
    //   plus minutes divided by 60, 
    //   plus seconds divided by 3600

    return degrees + (minutes/60) + (seconds/3600);
}

Which you can call like so:

console.log(ConvertDegreeAngleToDecimal(33/1,53/1,87/100));

There are a few javascript libraries around that will do the conversion, but you will have to replace the slashes in your string with the appropriate degrees, minutes, and seconds symbols: https://github.com/perfectline/geopoint

And a deeper discussion in C++ http://www.ridgesolutions.ie/index.php/2015/03/05/geotag-exif-gps-latitude-field-format/

Matthew
  • 1,630
  • 1
  • 14
  • 19
  • Thanks, this looks good, - https://stackoverflow.com/questions/8678371/how-to-convert-gps-degree-to-decimal-and-vice-versa-in-jquery-or-javascript-and How can I replace the "/" so I have the correct format? – Almog Oct 04 '17 at 02:52
  • @AlmogKoren note that the format there is a little different from yours. Yours doesn't have a separate direction – Matthew Oct 04 '17 at 02:56
  • Yes I just noticed that – Almog Oct 04 '17 at 02:56
  • Not sure I can use the other option or lib, my app is react native – Almog Oct 04 '17 at 02:58
  • I added a JavaScript function to get you started – Matthew Oct 04 '17 at 03:04
  • I just implemented this, I passing in the following data 33/1,53/1,87/100 and I'm getting back 33/1,53/1,87/100NaNNaN So I don't think it's working – Almog Oct 04 '17 at 03:15
  • Can you add that code to your question and I will take a look – Matthew Oct 04 '17 at 03:17
  • Can we see the code for _helpers.convertDegreeAngleToDecimal etc – Matthew Oct 04 '17 at 03:24
  • Just added that, I didn't format anything just copied our example am I missing something – Almog Oct 04 '17 at 03:26
  • You will need to actually split your string into the 3 components of degrees, minutes, seconds first, eg remove the slashes and divide the numerator by the denominator. – Matthew Oct 04 '17 at 03:29
  • Thanks so I did that and I'm getting the following back, which still looks off {name: "latitude", data: "330.88333333333333330.024166666666666666"} PhotoUpload.js:242 {name: "longitude", data: "1510.20.9888888888888889"} – Almog Oct 04 '17 at 04:10
  • And these are the values I'm sending 33 53 87 151 12 3560 Orginal format before conversion 33/1,53/1,87/100 151/1,12/1,3560/100 – Almog Oct 04 '17 at 04:15