h3arrow/algorithm/
coordinates.rs

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
use crate::array::CellIndexArray;
use crate::error::Error;
use arrow::array::{Float64Array, Float64Builder};
use h3o::LatLng;

pub struct CoordinateArrays {
    pub lat: Float64Array,
    pub lng: Float64Array,
}

pub trait ToCoordinatesOp {
    /// convert to point coordinates in degrees
    fn to_coordinates(&self) -> Result<CoordinateArrays, Error>;

    /// convert to point coordinates in radians
    fn to_coordinates_radians(&self) -> Result<CoordinateArrays, Error>;
}

impl ToCoordinatesOp for CellIndexArray {
    fn to_coordinates(&self) -> Result<CoordinateArrays, Error> {
        Ok(to_coordinatearrays(self, |ll| ll.lat(), |ll| ll.lng()))
    }

    fn to_coordinates_radians(&self) -> Result<CoordinateArrays, Error> {
        Ok(to_coordinatearrays(
            self,
            |ll| ll.lat_radians(),
            |ll| ll.lng_radians(),
        ))
    }
}

fn to_coordinatearrays<ExtractLat, ExtractLng>(
    cellindexarray: &CellIndexArray,
    extract_lat: ExtractLat,
    extract_lng: ExtractLng,
) -> CoordinateArrays
where
    ExtractLat: Fn(&LatLng) -> f64,
    ExtractLng: Fn(&LatLng) -> f64,
{
    let mut lat_builder = Float64Builder::with_capacity(cellindexarray.len());
    let mut lng_builder = Float64Builder::with_capacity(cellindexarray.len());

    cellindexarray.iter().for_each(|cell| {
        if let Some(cell) = cell {
            let ll = LatLng::from(cell);
            lat_builder.append_value(extract_lat(&ll));
            lng_builder.append_value(extract_lng(&ll));
        } else {
            lat_builder.append_null();
            lng_builder.append_null();
        }
    });

    CoordinateArrays {
        lat: lat_builder.finish(),
        lng: lng_builder.finish(),
    }
}