Created
March 8, 2022 13:41
-
-
Save daz/5c76c2f9066f31135d9ffa99c70e90a5 to your computer and use it in GitHub Desktop.
Check if images are inside a polygon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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