|
| 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_io::geometry::Bbox; |
| 16 | +use databend_common_io::geometry::ewkb_to_bbox; |
| 17 | +use databend_common_io::geometry::geometry_from_str; |
| 18 | +use databend_common_io::wkb::make_point; |
| 19 | + |
| 20 | +#[test] |
| 21 | +fn test_extract_bbox_point() { |
| 22 | + let point = make_point(-74.0, 40.7); |
| 23 | + let bbox = ewkb_to_bbox(&point).unwrap().bbox.unwrap(); |
| 24 | + assert_eq!(bbox, Bbox::from_corners(-74.0, 40.7, -74.0, 40.7)); |
| 25 | +} |
| 26 | + |
| 27 | +#[test] |
| 28 | +fn test_extract_bbox_point_with_srid() { |
| 29 | + let ewkb = geometry_from_str("SRID=4326;POINT(-122.35 37.55)", None).unwrap(); |
| 30 | + let result = ewkb_to_bbox(&ewkb).unwrap(); |
| 31 | + assert_eq!( |
| 32 | + result.bbox.unwrap(), |
| 33 | + Bbox::from_corners(-122.35, 37.55, -122.35, 37.55) |
| 34 | + ); |
| 35 | + assert_eq!(result.srid, Some(4326)); |
| 36 | +} |
| 37 | + |
| 38 | +#[test] |
| 39 | +fn test_extract_bbox_linestring() { |
| 40 | + let ewkb = geometry_from_str("LINESTRING(0 0, 10 5, 20 3)", None).unwrap(); |
| 41 | + let bbox = ewkb_to_bbox(&ewkb).unwrap().bbox.unwrap(); |
| 42 | + assert_eq!(bbox, Bbox::from_corners(0.0, 0.0, 20.0, 5.0)); |
| 43 | +} |
| 44 | + |
| 45 | +#[test] |
| 46 | +fn test_extract_bbox_polygon() { |
| 47 | + let ewkb = geometry_from_str( |
| 48 | + "POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 8 2, 8 8, 2 8, 2 2))", |
| 49 | + None, |
| 50 | + ) |
| 51 | + .unwrap(); |
| 52 | + let bbox = ewkb_to_bbox(&ewkb).unwrap().bbox.unwrap(); |
| 53 | + assert_eq!(bbox, Bbox::from_corners(0.0, 0.0, 10.0, 10.0)); |
| 54 | +} |
| 55 | + |
| 56 | +#[test] |
| 57 | +fn test_extract_bbox_point_z() { |
| 58 | + // EWKB PointZ(1 2 3), little-endian, Z flag 0x80000000. Databend never stores |
| 59 | + // 3D geometries today (all ingest paths funnel through to_ewkb(xy)), but the |
| 60 | + // EWKB parser must still derive a correct 2D bbox if one ever appears. |
| 61 | + let ewkb: &[u8] = &[ |
| 62 | + 0x01, // little-endian |
| 63 | + 0x01, 0x00, 0x00, 0x80, // type = Point | Z flag |
| 64 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xF0, 0x3F, // x = 1.0 |
| 65 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, // y = 2.0 |
| 66 | + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x40, // z = 3.0 |
| 67 | + ]; |
| 68 | + let bbox = ewkb_to_bbox(ewkb).unwrap().bbox.unwrap(); |
| 69 | + assert_eq!(bbox, Bbox::from_corners(1.0, 2.0, 1.0, 2.0)); |
| 70 | +} |
0 commit comments