parquet_geospatial/
testing.rs

1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements.  See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership.  The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License.  You may obtain a copy of the License at
8//
9//   http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied.  See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18//! Testing utilities for geospatial Parquet types
19
20/// Build well-known binary representing a point with the given XY coordinate
21pub fn wkb_point_xy(x: f64, y: f64) -> Vec<u8> {
22    let mut item: [u8; 21] = [0; 21];
23    item[0] = 0x01;
24    item[1] = 0x01;
25    item[5..13].copy_from_slice(x.to_le_bytes().as_slice());
26    item[13..21].copy_from_slice(y.to_le_bytes().as_slice());
27    item.to_vec()
28}
29
30/// Build well-known binary representing a point with the given XYZM coordinate
31pub fn wkb_point_xyzm(x: f64, y: f64, z: f64, m: f64) -> Vec<u8> {
32    let mut item: [u8; 37] = [0; 37];
33    item[0] = 0x01;
34    item[1..5].copy_from_slice(3001_u32.to_le_bytes().as_slice());
35    item[5..13].copy_from_slice(x.to_le_bytes().as_slice());
36    item[13..21].copy_from_slice(y.to_le_bytes().as_slice());
37    item[21..29].copy_from_slice(z.to_le_bytes().as_slice());
38    item[29..37].copy_from_slice(m.to_le_bytes().as_slice());
39    item.to_vec()
40}
41
42#[cfg(test)]
43mod test {
44
45    use wkb::reader::Wkb;
46
47    use super::*;
48
49    #[test]
50    fn test_wkb_item() {
51        let bytes = wkb_point_xy(1.0, 2.0);
52        let geometry = Wkb::try_new(&bytes).unwrap();
53        let mut wkt = String::new();
54        wkt::to_wkt::write_geometry(&mut wkt, &geometry).unwrap();
55        assert_eq!(wkt, "POINT(1 2)");
56    }
57
58    #[test]
59    fn test_wkb_point_xyzm() {
60        let bytes = wkb_point_xyzm(1.0, 2.0, 3.0, 4.0);
61        let geometry = Wkb::try_new(&bytes).unwrap();
62        let mut wkt = String::new();
63        wkt::to_wkt::write_geometry(&mut wkt, &geometry).unwrap();
64        assert_eq!(wkt, "POINT ZM(1 2 3 4)");
65    }
66}