Skip to main content

mln_map/
map_projection.rs

1// Ports include/mln/map/map_projection.hpp and src/mln/map/map_projection.cpp.
2//
3// gtests-core: added for test/map/map.test.cpp (LatLngForPixelUnwrapped,
4// PixelForLatLngAfterZoomAroundAPointAcrossTheAntimeridian).
5
6use crate::map::{Map, camera_for_lat_lngs};
7use crate::transform::Transform;
8use mln_map_types::{CameraOptions, TransformState};
9use mln_util::geo::{EdgeInsets, LatLng, ScreenCoordinate, WrapMode};
10
11pub struct MapProjection {
12    transform: Transform,
13}
14
15impl MapProjection {
16    /// `MapProjection(const Map&)`.
17    pub fn new(map: &Map) -> Self {
18        Self::from_state(map.get_transform_state())
19    }
20
21    /// `MapProjection(const TransformState&)`.
22    pub fn from_state(state: TransformState) -> Self {
23        MapProjection {
24            transform: Transform::from_state(state),
25        }
26    }
27
28    pub fn pixel_for_lat_lng(&self, lat_lng: &LatLng) -> ScreenCoordinate {
29        // The implementation is just a copy from map.cpp
30        let mut unwrapped_lat_lng = lat_lng.wrapped();
31        unwrapped_lat_lng.unwrap_for_shortest_path(&self.transform.get_lat_lng(WrapMode::Wrapped));
32        self.transform.lat_lng_to_screen_coordinate(&unwrapped_lat_lng)
33    }
34
35    pub fn lat_lng_for_pixel(&self, pixel: &ScreenCoordinate, wrap_mode: WrapMode) -> LatLng {
36        // The implementation is just a copy from map.cpp
37        self.transform.screen_coordinate_to_lat_lng(pixel, wrap_mode)
38    }
39
40    pub fn set_camera(&mut self, camera: &CameraOptions) {
41        self.transform.jump_to(camera);
42    }
43
44    pub fn get_camera(&self) -> CameraOptions {
45        self.transform.get_camera_options(None)
46    }
47
48    /// Set the underneath camera so the requested coordinates are visible
49    /// with the inset.
50    pub fn set_visible_coordinates(&mut self, lat_lngs: &[LatLng], padding: &EdgeInsets) {
51        let camera = camera_for_lat_lngs(lat_lngs, &self.transform, padding)
52            .with_bearing(Some(mln_math::rad2deg(-self.transform.get_bearing())))
53            .with_pitch(Some(mln_math::rad2deg(self.transform.get_pitch())));
54        self.transform.jump_to(&camera);
55    }
56}