Skip to main content

parquet_geospatial/
testing.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: See the NOTICE file distributed with this work for additional information regarding copyright ownership
3// SPDX-FileContributor: Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements
4
5//! Testing utilities for geospatial Parquet types
6
7/// Build well-known binary representing a point with the given XY coordinate
8pub fn wkb_point_xy(x: f64, y: f64) -> Vec<u8> {
9    let mut item: [u8; 21] = [0; 21];
10    item[0] = 0x01;
11    item[1] = 0x01;
12    item[5..13].copy_from_slice(x.to_le_bytes().as_slice());
13    item[13..21].copy_from_slice(y.to_le_bytes().as_slice());
14    item.to_vec()
15}
16
17/// Build well-known binary representing a point with the given XYZM coordinate
18pub fn wkb_point_xyzm(x: f64, y: f64, z: f64, m: f64) -> Vec<u8> {
19    let mut item: [u8; 37] = [0; 37];
20    item[0] = 0x01;
21    item[1..5].copy_from_slice(3001_u32.to_le_bytes().as_slice());
22    item[5..13].copy_from_slice(x.to_le_bytes().as_slice());
23    item[13..21].copy_from_slice(y.to_le_bytes().as_slice());
24    item[21..29].copy_from_slice(z.to_le_bytes().as_slice());
25    item[29..37].copy_from_slice(m.to_le_bytes().as_slice());
26    item.to_vec()
27}
28
29#[cfg(test)]
30mod test {
31
32    use wkb::reader::Wkb;
33
34    use super::*;
35
36    #[test]
37    fn test_wkb_item() {
38        let bytes = wkb_point_xy(1.0, 2.0);
39        let geometry = Wkb::try_new(&bytes).unwrap();
40        let mut wkt = String::new();
41        wkt::to_wkt::write_geometry(&mut wkt, &geometry).unwrap();
42        assert_eq!(wkt, "POINT(1 2)");
43    }
44
45    #[test]
46    fn test_wkb_point_xyzm() {
47        let bytes = wkb_point_xyzm(1.0, 2.0, 3.0, 4.0);
48        let geometry = Wkb::try_new(&bytes).unwrap();
49        let mut wkt = String::new();
50        wkt::to_wkt::write_geometry(&mut wkt, &geometry).unwrap();
51        assert_eq!(wkt, "POINT ZM(1 2 3 4)");
52    }
53}