1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
use crate::algorithm::broadcasting::BroadcastablePrimitive;
use crate::array::LineStringArray;
use crate::array::*;
use arrow_array::types::Float64Type;
use arrow_array::OffsetSizeTrait;
use geo::Scale as _Scale;
/// An affine transformation which scales geometries up or down by a factor.
///
/// ## Performance
///
/// If you will be performing multiple transformations, like
/// [`Scale`](crate::algorithm::geo::Scale), [`Skew`](crate::algorithm::geo::Skew),
/// [`Translate`](crate::algorithm::geo::Translate), or [`Rotate`](crate::algorithm::geo::Rotate),
/// it is more efficient to compose the transformations and apply them as a single operation using
/// the [`AffineOps`](crate::algorithm::geo::AffineOps) trait.
pub trait Scale {
/// Scale geometries from its bounding box center.
///
/// # Examples
///
/// ```
/// use geo::Scale;
/// use geo::{LineString, line_string};
///
/// let ls: LineString = line_string![(x: 0., y: 0.), (x: 10., y: 10.)];
///
/// let scaled = ls.scale(2.);
///
/// assert_eq!(scaled, line_string![
/// (x: -5., y: -5.),
/// (x: 15., y: 15.)
/// ]);
/// ```
#[must_use]
fn scale(&self, scale_factor: BroadcastablePrimitive<Float64Type>) -> Self;
// /// Mutable version of [`scale`](Self::scale)
// fn scale_mut(&mut self, scale_factor: BroadcastablePrimitive<Float64Type>);
/// Scale geometries from its bounding box center, using different values for `x_factor` and
/// `y_factor` to distort the geometry's [aspect ratio](https://en.wikipedia.org/wiki/Aspect_ratio).
///
/// # Examples
///
/// ```
/// use geo::Scale;
/// use geo::{LineString, line_string};
///
/// let ls: LineString = line_string![(x: 0., y: 0.), (x: 10., y: 10.)];
///
/// let scaled = ls.scale_xy(2., 4.);
///
/// assert_eq!(scaled, line_string![
/// (x: -5., y: -15.),
/// (x: 15., y: 25.)
/// ]);
/// ```
#[must_use]
fn scale_xy(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
) -> Self;
// /// Mutable version of [`scale_xy`](Self::scale_xy).
// fn scale_xy_mut(&mut self, x_factor: BroadcastablePrimitive<Float64Type>, y_factor: BroadcastablePrimitive<Float64Type>);
/// Scale geometries around a point of `origin`.
///
/// The point of origin is *usually* given as the 2D bounding box centre of the geometry, in
/// which case you can just use [`scale`](Self::scale) or [`scale_xy`](Self::scale_xy), but
/// this method allows you to specify any point.
///
/// # Examples
///
/// ```
/// use geo::Scale;
/// use geo::{LineString, line_string};
///
/// let ls: LineString = line_string![(x: 0., y: 0.), (x: 10., y: 10.)];
///
/// let scaled = ls.scale_xy(2., 4.);
///
/// assert_eq!(scaled, line_string![
/// (x: -5., y: -15.),
/// (x: 15., y: 25.)
/// ]);
/// ```
#[must_use]
fn scale_around_point(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
origin: geo::Point,
) -> Self;
// /// Mutable version of [`scale_around_point`](Self::scale_around_point).
// fn scale_around_point_mut(&mut self, x_factor: BroadcastablePrimitive<Float64Type>, y_factor: BroadcastablePrimitive<Float64Type>, origin: geo::Point);
}
// Note: this can't (easily) be parameterized in the macro because PointArray is not generic over O
impl Scale for PointArray {
fn scale(&self, scale_factor: BroadcastablePrimitive<Float64Type>) -> Self {
let output_geoms: Vec<Option<geo::Point>> = self
.iter_geo()
.zip(&scale_factor)
.map(|(maybe_g, scale_factor)| maybe_g.map(|geom| geom.scale(scale_factor.unwrap())))
.collect();
output_geoms.into()
}
fn scale_xy(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
) -> Self {
let output_geoms: Vec<Option<geo::Point>> = self
.iter_geo()
.zip(&x_factor)
.zip(&y_factor)
.map(|((maybe_g, x_factor), y_factor)| {
maybe_g.map(|geom| geom.scale_xy(x_factor.unwrap(), y_factor.unwrap()))
})
.collect();
output_geoms.into()
}
fn scale_around_point(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
origin: geo::Point,
) -> Self {
let output_geoms: Vec<Option<geo::Point>> = self
.iter_geo()
.zip(&x_factor)
.zip(&y_factor)
.map(|((maybe_g, x_factor), y_factor)| {
maybe_g.map(|geom| {
geom.scale_around_point(x_factor.unwrap(), y_factor.unwrap(), origin)
})
})
.collect();
output_geoms.into()
}
}
/// Implementation that iterates over geo objects
macro_rules! iter_geo_impl {
($type:ty, $geo_type:ty) => {
impl<O: OffsetSizeTrait> Scale for $type {
fn scale(&self, scale_factor: BroadcastablePrimitive<Float64Type>) -> Self {
let output_geoms: Vec<Option<$geo_type>> = self
.iter_geo()
.zip(scale_factor.into_iter())
.map(|(maybe_g, scale_factor)| {
maybe_g.map(|geom| geom.scale(scale_factor.unwrap()))
})
.collect();
output_geoms.into()
}
fn scale_xy(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
) -> Self {
let output_geoms: Vec<Option<$geo_type>> = self
.iter_geo()
.zip(x_factor.into_iter())
.zip(y_factor.into_iter())
.map(|((maybe_g, x_factor), y_factor)| {
maybe_g.map(|geom| geom.scale_xy(x_factor.unwrap(), y_factor.unwrap()))
})
.collect();
output_geoms.into()
}
fn scale_around_point(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
origin: geo::Point,
) -> Self {
let output_geoms: Vec<Option<$geo_type>> = self
.iter_geo()
.zip(x_factor.into_iter())
.zip(y_factor.into_iter())
.map(|((maybe_g, x_factor), y_factor)| {
maybe_g.map(|geom| {
geom.scale_around_point(x_factor.unwrap(), y_factor.unwrap(), origin)
})
})
.collect();
output_geoms.into()
}
}
};
}
iter_geo_impl!(LineStringArray<O>, geo::LineString);
iter_geo_impl!(PolygonArray<O>, geo::Polygon);
iter_geo_impl!(MultiPointArray<O>, geo::MultiPoint);
iter_geo_impl!(MultiLineStringArray<O>, geo::MultiLineString);
iter_geo_impl!(MultiPolygonArray<O>, geo::MultiPolygon);
iter_geo_impl!(WKBArray<O>, geo::Geometry);
impl<O: OffsetSizeTrait> Scale for GeometryArray<O> {
crate::geometry_array_delegate_impl! {
fn scale(&self, scale_factor: BroadcastablePrimitive<Float64Type>) -> Self;
fn scale_xy(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>
) -> Self;
fn scale_around_point(
&self,
x_factor: BroadcastablePrimitive<Float64Type>,
y_factor: BroadcastablePrimitive<Float64Type>,
origin: geo::Point
) -> Self;
}
}