Skip to content

Instantly share code, notes, and snippets.

@daz
Created March 8, 2022 13:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daz/5c76c2f9066f31135d9ffa99c70e90a5 to your computer and use it in GitHub Desktop.
Save daz/5c76c2f9066f31135d9ffa99c70e90a5 to your computer and use it in GitHub Desktop.
Check if images are inside a polygon
#!/usr/bin/env node
// Example usage:
//
// ./is-inside.js base/path/to/images -21,149 -21.1,152 -24,152.2 -24.1,148.9
import glob from "glob";
import * as fs from "fs";
import * as path from "path";
import * as turf from "@turf/turf";
import * as process from "process";
import ExifReader from "exifreader";
const args = process.argv.slice(2);
const files = glob.sync(path.join(args.shift(), "**/*.JPG"));
const poly = turf.polygon([
[...args, args[0]].map(str => {
return str
.split(",")
.map(d => parseFloat(d))
.reverse();
})
]);
function isInside(file) {
const buffer = fs.readFileSync(file);
const tags = ExifReader.load(buffer, { expanded: true });
const point = turf.point([tags.gps.Longitude, tags.gps.Latitude]);
return turf.booleanPointInPolygon(point, poly);
}
files.forEach(file => {
if (isInside(file)) {
process.stdout.write(`${file}\n`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment