Skip to content

Instantly share code, notes, and snippets.

@jessearmand
Last active March 25, 2023 08:08
Show Gist options
  • Save jessearmand/84aaf0b5d2506a90e2d1f03fc15466e8 to your computer and use it in GitHub Desktop.
Save jessearmand/84aaf0b5d2506a90e2d1f03fc15466e8 to your computer and use it in GitHub Desktop.
parse_latlon (Generated by Bing)
use exif::{DateTime, Field, Reader};
use regex::Regex;
use std::env;
use std::fs::File;
use std::io::{BufReader, Error};
use std::path::PathBuf;
use walkdir::WalkDir;
fn main() -> Result<(), Error> {
let re = Regex::new(r"\.(jpg|jpeg|png|gif|mp4|mov)$").unwrap();
let mut files = Vec::new();
let args: Vec<String> = env::args().collect();
if args.len() < 2 {
println!("Please provide a directory path as an argument.");
return Ok(());
}
let dir_path = &args[1];
for entry in WalkDir::new(dir_path) {
let entry = entry?;
if !entry.file_type().is_file() {
continue;
}
let path = entry.path();
if !re.is_match(path.to_str().unwrap_or("")) {
continue;
}
files.push(path.to_owned());
}
for file in files {
let file = File::open(file)?;
let reader = &mut BufReader::new(file);
let exif_reader = Reader::new(reader).unwrap();
let latitude = exif_reader.get_field(exif::Tag::GPSLatitude, exif::In::PRIMARY);
let longitude = exif_reader.get_field(exif::Tag::GPSLongitude, exif::In::PRIMARY);
if let (Some(latitude), Some(longitude)) = (latitude, longitude) {
println!(
"Latitude: {}, Longitude: {}",
latitude.display_value().to_string(),
longitude.display_value().to_string()
);
}
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment