|
| 1 | +// Copyright 2021 Datafuse Labs |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +use databend_common_exception::ErrorCode; |
| 16 | +use databend_common_exception::Result; |
| 17 | +use databend_common_io::geometry::rect_to_polygon; |
| 18 | +use geo::BoundingRect; |
| 19 | +use geo::Coord; |
| 20 | +use geo::Geometry; |
| 21 | +use geo::GeometryCollection; |
| 22 | +use geo::LineString; |
| 23 | +use geo::MultiLineString; |
| 24 | +use geo::MultiPoint; |
| 25 | +use geo::MultiPolygon; |
| 26 | +use geo::Point; |
| 27 | +use geo::Polygon; |
| 28 | +use geo::Rect; |
| 29 | + |
| 30 | +use crate::geographic::GeometryOverlay; |
| 31 | +use crate::geographic::OverlayMode; |
| 32 | + |
| 33 | +pub trait GeoAggOp: Send + Sync + 'static { |
| 34 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>>; |
| 35 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>>; |
| 36 | +} |
| 37 | + |
| 38 | +pub struct GeometryUnionAggOp; |
| 39 | + |
| 40 | +impl GeoAggOp for GeometryUnionAggOp { |
| 41 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 42 | + apply_geometry_overlay(geos, OverlayMode::Union) |
| 43 | + } |
| 44 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 45 | + apply_binary_geometry_overlay(l_geo, r_geo, OverlayMode::Union) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +pub struct GeometryIntersectionAggOp; |
| 50 | + |
| 51 | +impl GeoAggOp for GeometryIntersectionAggOp { |
| 52 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 53 | + apply_geometry_overlay(geos, OverlayMode::Intersection) |
| 54 | + } |
| 55 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 56 | + apply_binary_geometry_overlay(l_geo, r_geo, OverlayMode::Intersection) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +pub struct GeometryDifferenceAggOp; |
| 61 | + |
| 62 | +impl GeoAggOp for GeometryDifferenceAggOp { |
| 63 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 64 | + apply_geometry_overlay(geos, OverlayMode::Difference) |
| 65 | + } |
| 66 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 67 | + apply_binary_geometry_overlay(l_geo, r_geo, OverlayMode::Difference) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +pub struct GeometrySymDifferenceAggOp; |
| 72 | + |
| 73 | +impl GeoAggOp for GeometrySymDifferenceAggOp { |
| 74 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 75 | + apply_geometry_overlay(geos, OverlayMode::SymDifference) |
| 76 | + } |
| 77 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 78 | + apply_binary_geometry_overlay(l_geo, r_geo, OverlayMode::SymDifference) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +pub struct CollectAggOp; |
| 83 | + |
| 84 | +impl GeoAggOp for CollectAggOp { |
| 85 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 86 | + if geos.is_empty() { |
| 87 | + return Ok(None); |
| 88 | + } |
| 89 | + |
| 90 | + if geos.iter().all(|geo| matches!(geo, Geometry::Point(_))) { |
| 91 | + let points: Vec<Point<f64>> = geos |
| 92 | + .into_iter() |
| 93 | + .map(|geo| geo.try_into().expect("point geometry")) |
| 94 | + .collect(); |
| 95 | + let multi_point = MultiPoint::from_iter(points); |
| 96 | + return Ok(Some(Geometry::MultiPoint(multi_point))); |
| 97 | + } |
| 98 | + if geos |
| 99 | + .iter() |
| 100 | + .all(|geo| matches!(geo, Geometry::LineString(_))) |
| 101 | + { |
| 102 | + let lines: Vec<LineString<f64>> = geos |
| 103 | + .into_iter() |
| 104 | + .map(|geo| geo.try_into().expect("linestring geometry")) |
| 105 | + .collect(); |
| 106 | + let multi_line_string = MultiLineString::from_iter(lines); |
| 107 | + return Ok(Some(Geometry::MultiLineString(multi_line_string))); |
| 108 | + } |
| 109 | + if geos.iter().all(|geo| matches!(geo, Geometry::Polygon(_))) { |
| 110 | + let polygons: Vec<Polygon<f64>> = geos |
| 111 | + .into_iter() |
| 112 | + .map(|geo| geo.try_into().expect("polygon geometry")) |
| 113 | + .collect(); |
| 114 | + let multi_polygon = MultiPolygon::from_iter(polygons); |
| 115 | + return Ok(Some(Geometry::MultiPolygon(multi_polygon))); |
| 116 | + } |
| 117 | + |
| 118 | + let collection = GeometryCollection::from_iter(geos); |
| 119 | + Ok(Some(Geometry::GeometryCollection(collection))) |
| 120 | + } |
| 121 | + |
| 122 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 123 | + todo!(); |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +pub struct EnvelopeAggOp; |
| 128 | + |
| 129 | +impl GeoAggOp for EnvelopeAggOp { |
| 130 | + fn compute(geos: Vec<Geometry<f64>>) -> Result<Option<Geometry<f64>>> { |
| 131 | + let mut has_rect = false; |
| 132 | + let mut min_x = 0.0_f64; |
| 133 | + let mut min_y = 0.0_f64; |
| 134 | + let mut max_x = 0.0_f64; |
| 135 | + let mut max_y = 0.0_f64; |
| 136 | + |
| 137 | + for geo in geos { |
| 138 | + if let Some(rect) = geo.bounding_rect() { |
| 139 | + let min = rect.min(); |
| 140 | + let max = rect.max(); |
| 141 | + if !has_rect { |
| 142 | + min_x = min.x; |
| 143 | + min_y = min.y; |
| 144 | + max_x = max.x; |
| 145 | + max_y = max.y; |
| 146 | + has_rect = true; |
| 147 | + } else { |
| 148 | + min_x = min_x.min(min.x); |
| 149 | + min_y = min_y.min(min.y); |
| 150 | + max_x = max_x.max(max.x); |
| 151 | + max_y = max_y.max(max.y); |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + if !has_rect { |
| 157 | + return Ok(None); |
| 158 | + } |
| 159 | + |
| 160 | + let rect = Rect::new(Coord { x: min_x, y: min_y }, Coord { x: max_x, y: max_y }); |
| 161 | + Ok(Some(Geometry::Polygon(rect_to_polygon(rect)))) |
| 162 | + } |
| 163 | + |
| 164 | + fn binary_compute(l_geo: Geometry<f64>, r_geo: Geometry<f64>) -> Result<Option<Geometry<f64>>> { |
| 165 | + todo!(); |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +fn apply_geometry_overlay( |
| 170 | + geos: Vec<Geometry<f64>>, |
| 171 | + mode: OverlayMode, |
| 172 | +) -> Result<Option<Geometry<f64>>> { |
| 173 | + let overlay = GeometryOverlay::new(mode); |
| 174 | + overlay.apply_iter(geos) |
| 175 | +} |
| 176 | + |
| 177 | +fn apply_binary_geometry_overlay( |
| 178 | + l_geo: Geometry<f64>, |
| 179 | + r_geo: Geometry<f64>, |
| 180 | + mode: OverlayMode, |
| 181 | +) -> Result<Option<Geometry<f64>>> { |
| 182 | + let overlay = GeometryOverlay::new(mode); |
| 183 | + overlay.apply(&l_geo, &r_geo) |
| 184 | +} |
0 commit comments