gistools/geometry/
convert.rs

1// import { toWM } from './s2';
2// import { toS2, toUnitScale, toVector } from './wm';
3
4use alloc::vec;
5use alloc::vec::Vec;
6
7use crate::{Feature, JSONCollection, Projection, VectorFeature, WMFeature};
8
9/// Given an input data, convert it to a vector of VectorFeature
10pub fn convert<M: Clone>(
11    projection: Projection,
12    data: &JSONCollection<M>,
13    tolerance: Option<f64>,
14    maxzoom: Option<u8>,
15    build_bbox: Option<bool>,
16) -> Vec<VectorFeature<M>> {
17    let mut res: Vec<VectorFeature<M>> = vec![];
18
19    match data {
20        JSONCollection::FeatureCollection(feature_collection) => {
21            for feature in &feature_collection.features {
22                match &feature {
23                    WMFeature::Feature(feature) => {
24                        res.extend(convert_feature(
25                            projection, feature, tolerance, maxzoom, build_bbox,
26                        ));
27                    }
28                    WMFeature::VectorFeature(feature) => {
29                        res.extend(convert_vector_feature(projection, feature, tolerance, maxzoom))
30                    }
31                }
32            }
33        }
34        JSONCollection::S2FeatureCollection(feature_collection) => {
35            for feature in &feature_collection.features {
36                res.extend(convert_vector_feature(projection, feature, tolerance, maxzoom));
37            }
38        }
39        JSONCollection::Feature(feature) => {
40            res.extend(convert_feature(projection, feature, tolerance, maxzoom, build_bbox));
41        }
42        JSONCollection::VectorFeature(feature) => {
43            res.extend(convert_vector_feature(projection, feature, tolerance, maxzoom));
44        }
45    }
46
47    res
48}
49
50/// Convert a GeoJSON Feature to the appropriate VectorFeature
51fn convert_feature<M: Clone>(
52    projection: Projection,
53    data: &Feature<M>,
54    tolerance: Option<f64>,
55    maxzoom: Option<u8>,
56    build_bbox: Option<bool>,
57) -> Vec<VectorFeature<M>> {
58    let mut vf: VectorFeature<M> = Feature::<M>::to_vector(data, build_bbox);
59    match projection {
60        Projection::S2 => vf.to_s2(tolerance, maxzoom),
61        Projection::WM => {
62            vf.to_unit_scale(tolerance, maxzoom);
63            vec![vf]
64        }
65    }
66}
67
68/// Convert a GeoJSON VectorFeature to the appropriate VectorFeature
69fn convert_vector_feature<M: Clone>(
70    projection: Projection,
71    data: &VectorFeature<M>,
72    tolerance: Option<f64>,
73    maxzoom: Option<u8>,
74) -> Vec<VectorFeature<M>> {
75    match projection {
76        Projection::S2 => data.to_s2(tolerance, maxzoom),
77        Projection::WM => {
78            let mut vf = data.to_wm();
79            vf.to_unit_scale(tolerance, maxzoom);
80            vec![vf]
81        }
82    }
83}