2025-01-02 00:40:35 +01:00
|
|
|
|
// Copyright (c) 2022-2025 Sophie Herold
|
2023-04-19 20:02:01 +02:00
|
|
|
|
//
|
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
|
//
|
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
//
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
//
|
|
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
2025-01-02 00:40:35 +01:00
|
|
|
|
//! Metadata handling
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
mod file;
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2024-10-08 20:53:10 +02:00
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
use chrono::prelude::*;
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub use file::FileInfo;
|
2024-11-12 00:35:05 +01:00
|
|
|
|
use glib::TimeZone;
|
2025-07-07 22:18:36 +02:00
|
|
|
|
use glycin::{Frame, FrameDetails, ImageDetails, MemoryFormat};
|
2024-11-12 00:35:05 +01:00
|
|
|
|
use gufo::common::datetime::DateTime;
|
|
|
|
|
|
use gufo::common::orientation::Orientation;
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2023-12-06 23:45:02 +01:00
|
|
|
|
use crate::deps::*;
|
2023-12-04 12:52:40 +01:00
|
|
|
|
use crate::util;
|
2023-04-11 00:02:28 +02:00
|
|
|
|
use crate::util::gettext::*;
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
#[derive(Default, Debug)]
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub struct Metadata {
|
2024-01-01 22:41:56 +01:00
|
|
|
|
mime_type: Option<String>,
|
2024-11-13 17:32:27 +01:00
|
|
|
|
metadata: gufo::Metadata,
|
2023-12-04 12:52:40 +01:00
|
|
|
|
file_info: Option<FileInfo>,
|
2025-07-07 22:18:36 +02:00
|
|
|
|
image_info: Option<glycin::ImageDetails>,
|
2024-01-01 22:41:56 +01:00
|
|
|
|
frame_info: Option<FrameDetails>,
|
2024-08-05 19:01:23 +02:00
|
|
|
|
memory_format: Option<MemoryFormat>,
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
impl Metadata {
|
2024-01-01 22:41:56 +01:00
|
|
|
|
fn set_exif_bytes(&mut self, bytes: Vec<u8>) {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
let result = self.metadata.add_raw_exif(bytes);
|
2023-12-04 12:52:40 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
if let Err(err) = &result {
|
2023-12-04 12:52:40 +01:00
|
|
|
|
log::warn!("Failed to decode EXIF bytes: {err}");
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
2023-05-04 02:13:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-15 00:20:53 +01:00
|
|
|
|
fn set_xmp_bytes(&mut self, bytes: Vec<u8>) {
|
|
|
|
|
|
let result = self.metadata.add_raw_xmp(bytes);
|
|
|
|
|
|
|
|
|
|
|
|
if let Err(err) = &result {
|
|
|
|
|
|
log::warn!("Failed to decode XMP bytes: {err}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-07-07 22:18:36 +02:00
|
|
|
|
pub fn set_image_info(&mut self, image_info: ImageDetails) {
|
|
|
|
|
|
if let Some(exif_raw) = image_info
|
|
|
|
|
|
.metadata_exif()
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.get_full().ok())
|
|
|
|
|
|
{
|
2024-02-22 01:23:06 +01:00
|
|
|
|
self.set_exif_bytes(exif_raw);
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
2025-07-07 22:18:36 +02:00
|
|
|
|
if let Some(exif_xmp) = image_info
|
|
|
|
|
|
.metadata_xmp()
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.get_full().ok())
|
|
|
|
|
|
{
|
2024-11-15 00:20:53 +01:00
|
|
|
|
self.set_xmp_bytes(exif_xmp);
|
|
|
|
|
|
}
|
2024-01-01 22:41:56 +01:00
|
|
|
|
self.image_info = Some(image_info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-08-05 19:01:23 +02:00
|
|
|
|
pub fn set_frame_metadata(&mut self, frame: &Frame) {
|
|
|
|
|
|
self.frame_info = Some(frame.details().clone());
|
|
|
|
|
|
self.memory_format = Some(frame.memory_format());
|
2023-12-04 12:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn set_file_info(&mut self, file_info: FileInfo) {
|
|
|
|
|
|
self.file_info = Some(file_info);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-01 22:41:56 +01:00
|
|
|
|
pub fn set_mime_type(&mut self, mime_type: String) {
|
|
|
|
|
|
self.mime_type = Some(mime_type);
|
2023-12-04 12:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn merge(&mut self, other: Self) {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
if self.metadata.is_empty() {
|
|
|
|
|
|
self.metadata = other.metadata;
|
2023-05-04 02:13:39 +02:00
|
|
|
|
}
|
2023-12-04 12:52:40 +01:00
|
|
|
|
if self.file_info.is_none() {
|
|
|
|
|
|
self.file_info = other.file_info;
|
|
|
|
|
|
}
|
2024-01-01 22:41:56 +01:00
|
|
|
|
if self.image_info.is_none() {
|
|
|
|
|
|
self.image_info = other.image_info;
|
|
|
|
|
|
}
|
|
|
|
|
|
if self.mime_type.is_none() {
|
|
|
|
|
|
self.mime_type = other.mime_type;
|
|
|
|
|
|
}
|
|
|
|
|
|
if self.frame_info.is_none() {
|
|
|
|
|
|
self.frame_info = other.frame_info;
|
2023-12-04 12:52:40 +01:00
|
|
|
|
}
|
2024-08-05 23:43:52 +02:00
|
|
|
|
if self.memory_format.is_none() {
|
|
|
|
|
|
self.memory_format = other.memory_format;
|
|
|
|
|
|
}
|
2023-12-04 12:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn mime_type(&self) -> Option<String> {
|
2025-02-19 11:46:32 +01:00
|
|
|
|
self.mime_type.clone()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub fn format_name(&self) -> Option<String> {
|
2024-01-01 22:41:56 +01:00
|
|
|
|
self.image_info
|
|
|
|
|
|
.as_ref()
|
2025-07-07 22:18:36 +02:00
|
|
|
|
.and_then(|x| x.info_format_name().map(|x| x.to_string()))
|
2025-12-03 22:35:13 +01:00
|
|
|
|
.or_else(|| self.mime_type())
|
2023-12-04 12:52:40 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-01 22:41:56 +01:00
|
|
|
|
pub fn alpha_channel(&self) -> Option<bool> {
|
2025-07-07 22:18:36 +02:00
|
|
|
|
self.frame_info
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.info_alpha_channel())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn grayscale(&self) -> Option<bool> {
|
2025-07-07 22:18:36 +02:00
|
|
|
|
self.frame_info.as_ref().and_then(|x| x.info_grayscale())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn bit_depth(&self) -> Option<u8> {
|
2025-07-07 22:18:36 +02:00
|
|
|
|
self.frame_info.as_ref().and_then(|x| x.info_bit_depth())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn is_svg(&self) -> bool {
|
|
|
|
|
|
matches!(
|
|
|
|
|
|
self.mime_type.as_deref(),
|
|
|
|
|
|
Some("image/svg+xml") | Some("image/svg+xml-compressed")
|
|
|
|
|
|
)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn is_potentially_transparent(&self) -> bool {
|
2024-08-05 19:01:23 +02:00
|
|
|
|
self.memory_format.is_some_and(|x| x.has_alpha())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub fn transformations_applied(&self) -> bool {
|
2024-01-01 22:41:56 +01:00
|
|
|
|
self.image_info
|
|
|
|
|
|
.as_ref()
|
2025-07-07 22:18:36 +02:00
|
|
|
|
.map(|x| x.transformation_ignore_exif())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
.unwrap_or(false)
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn orientation(&self) -> Orientation {
|
2023-12-04 12:52:40 +01:00
|
|
|
|
if self.transformations_applied() {
|
2023-05-04 02:13:39 +02:00
|
|
|
|
// HEIF library already does it's transformations on its own
|
2024-11-12 00:35:05 +01:00
|
|
|
|
Orientation::Id
|
2023-05-04 02:13:39 +02:00
|
|
|
|
} else {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata.orientation().unwrap_or(Orientation::Id)
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-01-01 22:41:56 +01:00
|
|
|
|
pub fn dimensions_inch(&self) -> Option<(f64, f64)> {
|
2025-07-07 22:18:36 +02:00
|
|
|
|
self.image_info.as_ref().and_then(|x| x.dimensions_inch())
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn dimensions_text(&self) -> Option<String> {
|
|
|
|
|
|
self.image_info
|
|
|
|
|
|
.as_ref()
|
2025-07-07 22:18:36 +02:00
|
|
|
|
.and_then(|x| x.info_dimensions_text().map(|x| x.to_string()))
|
2024-01-01 22:41:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub fn file_name(&self) -> Option<String> {
|
|
|
|
|
|
self.file_info.as_ref().map(|x| x.display_name.to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-10-08 20:53:10 +02:00
|
|
|
|
/// Original host path inside flatpaks obtained via xattr
|
|
|
|
|
|
pub fn host_path(&self) -> Option<PathBuf> {
|
|
|
|
|
|
self.file_info
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.host_path.as_ref())
|
|
|
|
|
|
.and_then(|x| PathBuf::from_str(x.as_str()).ok())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-12-04 12:52:40 +01:00
|
|
|
|
pub fn file_size(&self) -> Option<String> {
|
|
|
|
|
|
self.file_info
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.file_size)
|
|
|
|
|
|
.map(|x| glib::format_size(x).to_string())
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn file_created(&self) -> Option<String> {
|
|
|
|
|
|
self.file_info
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.created.as_ref())
|
|
|
|
|
|
.and_then(util::datetime_fmt)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn file_modified(&self) -> Option<String> {
|
|
|
|
|
|
self.file_info
|
|
|
|
|
|
.as_ref()
|
|
|
|
|
|
.and_then(|x| x.modified.as_ref())
|
|
|
|
|
|
.and_then(util::datetime_fmt)
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-06 01:08:43 +01:00
|
|
|
|
pub fn originally_created(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
if let Some(date_time) = &self.metadata.date_time_original() {
|
2024-11-12 00:35:05 +01:00
|
|
|
|
let glib_date_time = match date_time {
|
|
|
|
|
|
DateTime::Naive(naive) => {
|
|
|
|
|
|
glib::DateTime::from_unix_local(naive.and_utc().timestamp()).ok()
|
|
|
|
|
|
}
|
|
|
|
|
|
DateTime::FixedOffset(fixed) => {
|
|
|
|
|
|
let naive = fixed.naive_utc();
|
|
|
|
|
|
glib::DateTime::new(
|
|
|
|
|
|
&TimeZone::from_offset(fixed.timezone().local_minus_utc() * 60),
|
|
|
|
|
|
naive.year(),
|
|
|
|
|
|
naive.month() as i32,
|
|
|
|
|
|
naive.day() as i32,
|
|
|
|
|
|
naive.hour() as i32,
|
|
|
|
|
|
naive.minute() as i32,
|
|
|
|
|
|
naive.second() as f64,
|
|
|
|
|
|
)
|
|
|
|
|
|
.ok()
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
2024-11-12 00:35:05 +01:00
|
|
|
|
};
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
if let Some(glib_date_time) = glib_date_time {
|
|
|
|
|
|
util::datetime_fmt(&glib_date_time)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
Some(date_time.to_string())
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
2024-11-12 00:35:05 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
None
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-13 17:32:27 +01:00
|
|
|
|
pub fn user_comment(&self) -> Option<String> {
|
|
|
|
|
|
self.metadata.user_comment()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-11-06 01:08:43 +01:00
|
|
|
|
pub fn f_number(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
if let Some(f_number) = self.metadata.f_number() {
|
2024-09-12 22:20:50 +02:00
|
|
|
|
return Some(
|
|
|
|
|
|
// Translators: Display of the f-number <https://en.wikipedia.org/wiki/F-number>. {} will be replaced with the number
|
2024-11-12 00:35:05 +01:00
|
|
|
|
gettext_f(r"\u{192}\u{2215}{}", [f_number.to_string()]),
|
2024-09-12 22:20:50 +02:00
|
|
|
|
);
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn exposure_time(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
if let Some((num, denom)) = self.metadata.exposure_time() {
|
2024-11-12 00:35:05 +01:00
|
|
|
|
let speed = num as f64 / denom as f64;
|
|
|
|
|
|
if speed <= 0.5 {
|
|
|
|
|
|
let exposure = format!("{:.0}", 1. / speed);
|
|
|
|
|
|
// Translators: Fractional exposure time (photography) in seconds
|
|
|
|
|
|
Some(gettext_f(r"1\u{2215}{}\u{202F}s", [exposure]))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
let exposure = if speed < 5. {
|
|
|
|
|
|
format!("{:.1}", speed)
|
2024-04-06 17:21:19 +02:00
|
|
|
|
} else {
|
2024-11-12 00:35:05 +01:00
|
|
|
|
format!("{:.0}", speed)
|
|
|
|
|
|
};
|
|
|
|
|
|
// Translators: Exposure time (photography) in seconds
|
|
|
|
|
|
Some(gettext_f(r"{}\u{202F}s", [exposure]))
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
2024-11-12 00:35:05 +01:00
|
|
|
|
} else {
|
|
|
|
|
|
None
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn iso(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata.iso_speed_rating().map(|iso| iso.to_string())
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn focal_length(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata
|
2024-11-12 00:35:05 +01:00
|
|
|
|
.focal_length()
|
|
|
|
|
|
.map(|focal_length| gettext_f(r"{}\u{202F}mm", [focal_length.to_string()]))
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-19 17:13:53 +01:00
|
|
|
|
/// Combined maker and model info
|
|
|
|
|
|
pub fn maker_model(&self) -> Option<String> {
|
|
|
|
|
|
if let Some(mut model) = self.model() {
|
|
|
|
|
|
if let Some(maker) = self.maker() {
|
|
|
|
|
|
// This is to avoid doubling the maker name
|
|
|
|
|
|
// Canon for example also puts "Canon" in the model as well
|
|
|
|
|
|
// NIKON sometimes puts "NIKON" in model and "NIKON CORPORATION" in maker
|
|
|
|
|
|
if model.split_whitespace().next() != maker.split_whitespace().next() {
|
|
|
|
|
|
model = format!("{maker} {model}");
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Some(model)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
self.maker()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn model(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata.model()
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-19 17:13:53 +01:00
|
|
|
|
pub fn maker(&self) -> Option<String> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata.make()
|
2024-11-12 00:35:05 +01:00
|
|
|
|
}
|
2022-12-19 17:13:53 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
pub fn gps_location(&self) -> Option<gufo::common::geography::Location> {
|
2024-11-13 17:32:27 +01:00
|
|
|
|
self.metadata.gps_location()
|
2022-12-19 17:13:53 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
pub fn gps_location_display(&self) -> Option<String> {
|
|
|
|
|
|
if let Some(location) = self.gps_location() {
|
|
|
|
|
|
let (lat_ref, (lat_deg, lat_min, lat_sec)) = location.lat_ref_deg_min_sec();
|
|
|
|
|
|
|
|
|
|
|
|
let lat_ref = match lat_ref {
|
|
|
|
|
|
gufo::common::geography::LatRef::North => {
|
|
|
|
|
|
// Translators: short for "north" in GPS coordinate
|
|
|
|
|
|
gettext("N")
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
2024-11-12 00:35:05 +01:00
|
|
|
|
gufo::common::geography::LatRef::South => {
|
|
|
|
|
|
// Translators: short for "south" in GPS coordinate
|
|
|
|
|
|
gettext("S")
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
let (lon_ref, (lon_deg, lon_min, lon_sec)) = location.lon_ref_deg_min_sec();
|
|
|
|
|
|
|
|
|
|
|
|
let lon_ref = match lon_ref {
|
|
|
|
|
|
gufo::common::geography::LonRef::East => {
|
|
|
|
|
|
// Translators: short for "east" in GPS coordinate
|
|
|
|
|
|
gettext("E")
|
|
|
|
|
|
}
|
|
|
|
|
|
gufo::common::geography::LonRef::West => {
|
|
|
|
|
|
// Translators: short for "west" in GPS coordinate
|
|
|
|
|
|
gettext("W")
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
let lat = format!("{lat_deg}° {lat_min}′ {lat_sec:05.2}″ {lat_ref}");
|
|
|
|
|
|
let lon = format!("{lon_deg}° {lon_min}′ {lon_sec:05.2}″ {lon_ref}");
|
|
|
|
|
|
Some(format!("{lat}\n{lon}"))
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
|
|
|
|
|
}
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
/// Outputs location as "City, County" if possible
|
|
|
|
|
|
///
|
|
|
|
|
|
/// Falls back to coordinates
|
|
|
|
|
|
pub fn gps_location_place(&self) -> Option<String> {
|
|
|
|
|
|
if let Some(location) = self.gps_location() {
|
|
|
|
|
|
if let Some(world) = libgweather::Location::world() {
|
|
|
|
|
|
let lat = location.lat.0;
|
|
|
|
|
|
let lon = location.lon.0;
|
|
|
|
|
|
let nearest_city = world.find_nearest_city(lat, lon);
|
|
|
|
|
|
let location_exact = libgweather::Location::new_detached("", None, lat, lon);
|
|
|
|
|
|
|
|
|
|
|
|
// do not use city if more than 15 km away
|
|
|
|
|
|
if nearest_city.distance(&location_exact) < 15. {
|
|
|
|
|
|
if let (Some(city), Some(country)) =
|
|
|
|
|
|
(nearest_city.city_name(), nearest_city.country_name())
|
|
|
|
|
|
{
|
|
|
|
|
|
return Some(format!("{city}, {country}"));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2022-11-06 01:08:43 +01:00
|
|
|
|
|
2024-11-12 00:35:05 +01:00
|
|
|
|
self.gps_location_display()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
None
|
2022-11-06 01:08:43 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|