Skip to content

Instantly share code, notes, and snippets.

@qgustavor
Forked from tylor/index.js
Last active August 29, 2015 14:18
Show Gist options
  • Save qgustavor/d2f3944d75cfca64e0f0 to your computer and use it in GitHub Desktop.
Save qgustavor/d2f3944d75cfca64e0f0 to your computer and use it in GitHub Desktop.
/**
* This script takes a directory of images, looks for GPS
* coordinates in each file, and then writes the filename and
* decimal degrees to CSV. If you're pulling images off of your
* iPhone, using Image Capture is a quick way to move them onto
* your computer and get started.
*
* Make sure you have imagemagick installed:
* $ brew install imagemagick
* Download these files and install dependencies:
* $ npm install
* Run:
* $ node index.js ~/Pictures/iphone/ > images.csv
*
* Graph them on a map using something like TileMill, here's a good starter:
* http://mapbox.com/tilemill/docs/crashcourse/point-data/
*/
var im = require('imagemagick');
var fs = require('fs');
var async = require('async');
var path = require('path');
console.log(['file', 'latitude', 'longitude'].join(','));
fs.readdir(process.argv[2], function(err, files) {
if (err) throw err;
// Limit im.readMetadata since it doesn't like having too many open file descriptors.
async.eachLimit(files, 50, readData, function(err) {
if (err) throw err;
// done!
});
});
var readData = function(file, callback) {
if (file.match(/\.jpg/i) !== null) {
im.readMetadata(path.join(process.argv[2], file), function(err, metadata) {
if (err) throw err;
if (typeof metadata.exif !== 'undefined' && typeof metadata.exif.gpsLatitude !== 'undefined' && typeof metadata.exif.gpsLongitude !== 'undefined') {
var degreeLatitude = metadata.exif.gpsLatitude.split(', ')
var degreeLongitude = metadata.exif.gpsLongitude.split(', ')
var latitude = ConvertDMSToDD(parseInt(degreeLatitude[0].split('/')), parseInt(degreeLatitude[1].split('/'))/100, parseInt(degreeLatitude[2].split('/')), metadata.exif.gpsLatitudeRef);
var longitude = ConvertDMSToDD(parseInt(degreeLongitude[0].split('/')), parseInt(degreeLongitude[1].split('/'))/100, parseInt(degreeLongitude[2].split('/')), metadata.exif.gpsLongitudeRef);
console.log([file, latitude, longitude].join(','));
}
callback();
});
}
else {
callback();
}
};
// http://stackoverflow.com/questions/1140189/converting-latitude-and-longitude-to-decimal-values
var ConvertDMSToDD = function(days, minutes, seconds, direction) {
var dd = days + minutes/60 + seconds/(60*60);
// Invert south and west.
if (direction == 'S' || direction == 'W') {
dd = dd * -1;
}
return dd;
}
{
"name": "images",
"version": "0.0.0",
"main": "index.js",
"license": "MIT",
"dependencies": {
"imagemagick": "~0.1.3",
"async": "~0.2.7"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment