From fec6052a494fd27d1f68798f920a7789a1c33e22 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sun, 21 Mar 2021 22:56:25 +0000 Subject: [PATCH] shapelib: Provide orig_size option This is often more convenient than `scale` plus `size` Signed-off-by: Ian Jackson --- src/shapelib-toml.rs | 6 ++++++ src/shapelib.rs | 18 ++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/shapelib-toml.rs b/src/shapelib-toml.rs index ff86c643..3a2297e2 100644 --- a/src/shapelib-toml.rs +++ b/src/shapelib-toml.rs @@ -104,6 +104,12 @@ pub struct GroupDetails { // scaled when put into GroupData pub size: Vec, + #[serde(default)] + /// If specified, the input is first scaled from `orig_size` to + /// `size`. If both `size` and `orig_size` are 2 elements, may + /// scale by different amounts in x and y. + pub orig_size: Vec, + #[serde(default)] /// Default if not supplied is `[0,0]`. pub centre: [f64; 2], diff --git a/src/shapelib.rs b/src/shapelib.rs index dee0c057..02a55645 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -197,8 +197,22 @@ impl FaceTransform { #[throws(LLE)] fn from_group(d: &GroupDetails) -> Self { let centre = d.centre; - let scale = d.scale; - FaceTransform { centre, scale: [scale,scale] } + let scale = if ! d.orig_size.is_empty() && ! d.size.is_empty() { + // by this point d.size has already been scaled by scale + izip!(&d.orig_size, &d.size) + .map(|(&orig_size, &target_size)| { + target_size / orig_size + }) + .cycle() + .take(2) + .collect::>() + .into_inner() + .unwrap() + } else { + let s = d.scale; + [s,s] + }; + FaceTransform { centre, scale } } #[throws(IE)] -- 2.30.2