hMatoba / piexifjs

Read and modify exif in client-side or server-side JavaScript.
MIT License
572 stars 119 forks source link

How do I update latitude and longitude? #1

Closed mohsen1 closed 9 years ago

hMatoba commented 9 years ago

node example, north 37.429289 and west 122.138162.

var piexif = require("piexifjs");
var fs = require("fs");

var filename1 = "in.jpg";
var filename2 = "out.jpg";

var jpeg = fs.readFileSync(filename1);
var data = jpeg.toString("binary");

var exifObj = piexif.load(data);
exifObj["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N";
exifObj["GPS"][piexif.GPSIFD.GPSLatitude] = [37429289, 1000000];
exifObj["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "W";
exifObj["GPS"][piexif.GPSIFD.GPSLongitude] = [122138162, 1000000];
var exifbytes = piexif.dump(exifObj);

var newData = piexif.insert(exifbytes, data);
var newJpeg = new Buffer(newData, "binary");
fs.writeFileSync(filename2, newJpeg);
mohsen1 commented 9 years ago

Thank you. My lat-long in in this format:

"latitude": 36.613230478624,
"longitude": -121.93234200195,

How can I use these numbers?

hMatoba commented 9 years ago

exifObj["GPS"][piexif.GPSIFD.GPSLatitudeRef] = "N"; exifObj["GPS"][piexif.GPSIFD.GPSLatitude] = [366132304, 10000000], exifObj["GPS"][piexif.GPSIFD.GPSLongitudeRef] = "W"; exifObj["GPS"][piexif.GPSIFD.GPSLongitude] = [1219323420, 10000000];

mohsen1 commented 9 years ago

Two questions:

hMatoba commented 9 years ago

latitude >= 0: North latitude -> "N" latitude < 0: South latitude -> "S" longitude >= 0: East longitude -> "E" longitude < 0: West longitude -> "W"

mohsen1 commented 9 years ago

Thank you!

hMatoba commented 9 years ago

Anytime.

ipsips commented 8 years ago

Hi, great library guys!

The above-suggested way of assigning latitude & longitude to an array of two – [numerator, denominator] – did not work for me. The resulting image was still not bearing any GPS info when i opened it with OS X Preview app.

What did work however was representing the coordinates as multidimensional array of deegrees, minutes and seconds like this:

var data = {
  lat: 59.43553989213321,
  lng: 24.73842144012451
}

exifObj["GPS"][piexif.GPSIFD.GPSLatitudeRef] = data.lat < 0 ? 'S' : 'N'
exifObj["GPS"][piexif.GPSIFD.GPSLatitude] = degToDmsRational(data.lat)
exifObj["GPS"][piexif.GPSIFD.GPSLongitudeRef] = data.lng < 0 ? 'W' : 'E'
exifObj["GPS"][piexif.GPSIFD.GPSLongitude] = degToDmsRational(data.lng)

/**
 * degToDmsRational(59.43553989213321) -> [[59, 1], [26, 1], [794, 100]]
 */

function degToDmsRational(degFloat) {
  var minFloat = degFloat % 1 * 60
  var secFloat = minFloat % 1 * 60
  var deg = Math.floor(degFloat)
  var min = Math.floor(minFloat)
  var sec = Math.round(secFloat * 100)

  return [[deg, 1], [min, 1], [sec, 100]]
}

p.s. i ran my code in Google Chrome browser and the image was parsed with FileReader api as dataURL

hMatoba commented 8 years ago

Thank you. My way was wrong. Rational value as lat or lon has 3 length, not one length.

danhanfry commented 6 years ago

if you pass negative coordinates fails, you need to pass to positive the result


  function degToDmsRational(degFloat) {
    var minFloat = degFloat % 1 * 60
    var secFloat = minFloat % 1 * 60
    var deg = Math.floor(degFloat)
    var min = Math.floor(minFloat)
    var sec = Math.round(secFloat * 100)

    deg = Math.abs(deg) * 1
    min = Math.abs(min) * 1
    sec = Math.abs(sec) * 1

    return [[deg, 1], [min, 1], [sec, 100]]
  }
kshipp commented 6 years ago

I too had problems getting GPS to appear. ipsips's solution works for showing the lat/lon, but I still cannot get any other GPS fields to display. Any suggestions or sample code that actually works? In photos, Altitude is an object of num & den. Also, any ideas on displaying exifObj["GPS"][piexif.GPSIFD.GPSDOP] ? Please provide a working example so we can get them to work. Thanks.