-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexif-to-geojson.js
146 lines (129 loc) · 4.25 KB
/
exif-to-geojson.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/**
* Created by Nicholas Hallahan <[email protected]>
* on 5/11/14.
*/
var ExifImage = require('exif').ExifImage;
var fs = require('fs');
var flow = require('flow');
var path = require("path")
var geojson = {
type: "FeatureCollection",
features: []
};
const getAllFiles = function(dirPath, arrayOfFiles) {
files = fs.readdirSync(dirPath)
arrayOfFiles = arrayOfFiles || []
files.forEach(function(file) {
if (fs.statSync(dirPath + "/" + file).isDirectory()) {
arrayOfFiles = getAllFiles(dirPath + "/" + file, arrayOfFiles)
} else {
//arrayOfFiles.push(path.join(__dirname, dirPath, "/", file))
arrayOfFiles.push(path.join(dirPath, "/", file))
}
})
return arrayOfFiles
}
function exifToFeature(exifData) {
var gps = exifData.gps;
if (!gps.GPSLatitude) {
throw "No GPS data";
}
if (isNaN(gps.GPSLatitude[1])) {
throw "Invalid GPS data";
}
var lat = gps.GPSLatitude[0] + gps.GPSLatitude[1] / 60 + gps.GPSLatitude[2] / 3600;
var lng = gps.GPSLongitude[0] + gps.GPSLongitude[1] / 60 + gps.GPSLongitude[2] / 3600;
if (gps.GPSLatitudeRef.toLowerCase() === 's') {
lat = - lat;
}
if (gps.GPSLongitudeRef.toLowerCase() === 'w') {
lng = - lng;
}
var alt = gps.GPSAltitude;
var coord = alt ? [lng, lat, alt] : [lng, lat];
var feat = {
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": coord
}
};
feat.properties.exif = exifData;
// time the gps coordinate was taken
try{
var gpsDateArr = gps.GPSDateStamp.split(':');
var gpsDate = new Date(Date.UTC( parseInt(gpsDateArr[0]),
parseInt(gpsDateArr[1]) - 1 , // Jan is 0
parseInt(gpsDateArr[2])) );
//gps.GPSTimeStamp[0],
//gps.GPSTimeStamp[1],
//gps.GPSTimeStamp[2] ) );
feat.properties.gpsTime = gpsDate.getTime();
feat.properties.gpsTimeStr = gpsDate.toString();
} catch{
var imgStr = exifData.exif.CreateDate;
imgStr = imgStr.replace(':','-').replace(':','-');
var imgDate = new Date(imgStr);
feat.properties.gpsTime = imgDate.getTime();
feat.properties.gpsTimeStr = imgDate.toString();
}
// time the actual picture was taken.
// NH FIXME: We are assuming the pic was taken in the current timezone?
var imgStr = exifData.exif.CreateDate;
imgStr = imgStr.replace(':','-').replace(':','-');
var imgDate = new Date(imgStr);
feat.properties.imgTime = imgDate.getTime();
feat.properties.imgTimeStr = imgDate.toString();
return feat;
}
function processImage(imgPath, cb) {
try {
new ExifImage({ image : imgPath }, function (error, exifData) {
if (error)
console.error('Error for ' + imgPath + ': ' + error.message);
else {
// NH - Get rid of properties that the exif decoder does not
// correctly decode. Wastes a bunch of space...
delete exifData.exif.MakerNote;
delete exifData.exif.UserComment;
delete exifData.makernote;
//delete exifData.gps.GPSTimeStamp;
try {
var feat = exifToFeature(exifData);
feat.properties.imgPath = imgPath;
geojson.features.push(feat);
} catch (err) {
console.log(err + ' in ' + imgPath + '. Skipping...');
}
cb();
}
});
} catch (error) {
console.error(JSON.stringify(error,null,2));
cb();
}
}
console.log('Reading images in img directory...');
var files = getAllFiles("./hikes");
//console.log(files);
/**
* Make sure we don't have anything that isn't jpg...
*/
var imgPaths = [];
files.forEach(function (file) {
if (file.slice(-3).toLowerCase() === 'jpg' || file.slice(-4).toLowerCase() === 'jpeg') {
imgPaths.push(file);
}
});
flow.exec(function(){
for (var i = 0, len = imgPaths.length; i < len; ++i) {
var imgPath = imgPaths[i];
processImage(imgPath, this.MULTI());
}
}, function() {
var outputPath = 'exif.geojson';
fs.writeFileSync(outputPath, JSON.stringify(geojson, null, 2));
console.log('Wrote GeoJSON to: ' + outputPath);
console.log('Able to process ' + geojson.features.length + ' out of ' + imgPaths.length + ' images.');
});