Skip to main content

maplibre_native_ffi/generated/
map_projection.rs

1// Generated from C headers by tools/bindgen. Do not edit.
2use super::*;
3
4native_owner! {
5    /// Owns one native handle.
6    ///
7    /// A camera view for converting between screen points and coordinates.
8    ///
9    /// See `mln_map_projection` in the
10    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/base_8h.html).
11    pub struct MapProjectionHandle(mln_map_projection) dispose |raw| { unsafe { sys::mln_map_projection_release(raw) }; Ok(()) };
12    /// Returns the native handle value.
13    fn id;
14}
15
16impl MapProjectionHandle {
17    /// Creates a projection that converts coordinates for one view of a map.
18    ///
19    /// Create it from the `view` of an `MapSnapshot` or an `RenderFrameResult`.
20    /// The projection doesn't follow the map, so create a new one after the
21    /// map's camera or size changes.
22    ///
23    /// See `mln_map_projection_create` in the
24    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
25    pub fn create(view: &MapView) -> Result<MapProjectionHandle> {
26        let mut call = Call::global();
27        crate::abi::validate_abi_version()?;
28        let mut out_projection = sys::mln_map_projection(0);
29        let view = call.reference(&view)?;
30        call.status(|_, out_diagnostic| unsafe {
31            sys::mln_map_projection_create(view, &mut out_projection, out_diagnostic)
32        })?;
33        Ok(MapProjectionHandle::adopt(out_projection)?)
34    }
35
36    /// Reads the projection's camera.
37    ///
38    /// See `mln_map_projection_get_camera` in the
39    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
40    pub fn get_camera(&self) -> Result<MapProjectionCameraResult> {
41        let mut call = self.inner.call();
42        let mut out_result: sys::mln_map_projection_camera_result = unsafe { std::mem::zeroed() };
43        out_result.size = std::mem::size_of::<sys::mln_map_projection_camera_result>() as _;
44        call.status(|projection, out_diagnostic| unsafe {
45            sys::mln_map_projection_get_camera(projection, &mut out_result, out_diagnostic)
46        })?;
47        Ok(unsafe { from_native(out_result) }?)
48    }
49
50    /// Finds the coordinate at a screen point.
51    ///
52    /// The longitude stays between -180 and 180.
53    ///
54    /// See `mln_map_projection_lat_lng_for_pixel` in the
55    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
56    pub fn lat_lng_for_pixel(&self, point: ScreenPoint) -> Result<LatLng> {
57        let mut call = self.inner.call();
58        let mut out_coordinate: sys::mln_lat_lng = unsafe { std::mem::zeroed() };
59        let point = call.input(&point)?;
60        call.status(|projection, out_diagnostic| unsafe {
61            sys::mln_map_projection_lat_lng_for_pixel(
62                projection,
63                point,
64                &mut out_coordinate,
65                out_diagnostic,
66            )
67        })?;
68        Ok(unsafe { from_native(out_coordinate) }?)
69    }
70
71    /// Finds the coordinate at a screen point, without wrapping its longitude.
72    ///
73    /// The longitude can fall outside -180 to 180, to show which copy of the
74    /// world the point is in.
75    ///
76    /// See `mln_map_projection_lat_lng_for_pixel_unwrapped` in the
77    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
78    pub fn lat_lng_for_pixel_unwrapped(&self, point: ScreenPoint) -> Result<LatLng> {
79        let mut call = self.inner.call();
80        let mut out_coordinate: sys::mln_lat_lng = unsafe { std::mem::zeroed() };
81        let point = call.input(&point)?;
82        call.status(|projection, out_diagnostic| unsafe {
83            sys::mln_map_projection_lat_lng_for_pixel_unwrapped(
84                projection,
85                point,
86                &mut out_coordinate,
87                out_diagnostic,
88            )
89        })?;
90        Ok(unsafe { from_native(out_coordinate) }?)
91    }
92
93    /// Finds the coordinates at several screen points, as
94    /// `MapProjectionHandle::lat_lng_for_pixel()` does.
95    ///
96    /// See `mln_map_projection_lat_lngs_for_pixels` in the
97    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
98    pub fn lat_lngs_for_pixels(&self, points: &[ScreenPoint]) -> Result<Vec<LatLng>> {
99        let mut call = self.inner.call();
100        let point_count = convert::count(points.len())?;
101        let mut out_coordinates: Vec<sys::mln_lat_lng> = (0..points.len())
102            .map(|_| unsafe { std::mem::zeroed() })
103            .collect();
104        let points = call.array(points)?;
105        call.status(|projection, out_diagnostic| unsafe {
106            sys::mln_map_projection_lat_lngs_for_pixels(
107                projection,
108                points,
109                point_count,
110                out_coordinates.as_mut_ptr(),
111                out_diagnostic,
112            )
113        })?;
114        Ok(out_coordinates
115            .into_iter()
116            .map(|item| Ok(unsafe { from_native(item) }?))
117            .collect::<Result<Vec<_>>>()?)
118    }
119
120    /// Finds the coordinates at several screen points, as
121    /// `MapProjectionHandle::lat_lng_for_pixel_unwrapped()` does.
122    ///
123    /// See `mln_map_projection_lat_lngs_for_pixels_unwrapped` in the
124    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
125    pub fn lat_lngs_for_pixels_unwrapped(&self, points: &[ScreenPoint]) -> Result<Vec<LatLng>> {
126        let mut call = self.inner.call();
127        let point_count = convert::count(points.len())?;
128        let mut out_coordinates: Vec<sys::mln_lat_lng> = (0..points.len())
129            .map(|_| unsafe { std::mem::zeroed() })
130            .collect();
131        let points = call.array(points)?;
132        call.status(|projection, out_diagnostic| unsafe {
133            sys::mln_map_projection_lat_lngs_for_pixels_unwrapped(
134                projection,
135                points,
136                point_count,
137                out_coordinates.as_mut_ptr(),
138                out_diagnostic,
139            )
140        })?;
141        Ok(out_coordinates
142            .into_iter()
143            .map(|item| Ok(unsafe { from_native(item) }?))
144            .collect::<Result<Vec<_>>>()?)
145    }
146
147    /// Computes how many meters one screen pixel covers at `latitude`, at the
148    /// projection's zoom.
149    ///
150    /// The result ignores the camera's pitch.
151    ///
152    /// See `mln_map_projection_meters_per_pixel_at_latitude` in the
153    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
154    pub fn meters_per_pixel_at_latitude(&self, latitude: f64) -> Result<f64> {
155        let mut call = self.inner.call();
156        let mut out_meters_per_pixel: f64 = Default::default();
157        call.status(|projection, out_diagnostic| unsafe {
158            sys::mln_map_projection_meters_per_pixel_at_latitude(
159                projection,
160                latitude,
161                &mut out_meters_per_pixel,
162                out_diagnostic,
163            )
164        })?;
165        Ok(out_meters_per_pixel)
166    }
167
168    /// Finds the screen point of a coordinate.
169    ///
170    /// The point is in the copy of the world nearest the camera's center.
171    ///
172    /// See `mln_map_projection_pixel_for_lat_lng` in the
173    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
174    pub fn pixel_for_lat_lng(&self, coordinate: LatLng) -> Result<ScreenPoint> {
175        let mut call = self.inner.call();
176        let mut out_point: sys::mln_screen_point = unsafe { std::mem::zeroed() };
177        let coordinate = call.input(&coordinate)?;
178        call.status(|projection, out_diagnostic| unsafe {
179            sys::mln_map_projection_pixel_for_lat_lng(
180                projection,
181                coordinate,
182                &mut out_point,
183                out_diagnostic,
184            )
185        })?;
186        Ok(unsafe { from_native(out_point) }?)
187    }
188
189    /// Finds the screen points of several coordinates, as
190    /// `MapProjectionHandle::pixel_for_lat_lng()` does.
191    ///
192    /// See `mln_map_projection_pixels_for_lat_lngs` in the
193    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
194    pub fn pixels_for_lat_lngs(&self, coordinates: &[LatLng]) -> Result<Vec<ScreenPoint>> {
195        let mut call = self.inner.call();
196        let coordinate_count = convert::count(coordinates.len())?;
197        let mut out_points: Vec<sys::mln_screen_point> = (0..coordinates.len())
198            .map(|_| unsafe { std::mem::zeroed() })
199            .collect();
200        let coordinates = call.array(coordinates)?;
201        call.status(|projection, out_diagnostic| unsafe {
202            sys::mln_map_projection_pixels_for_lat_lngs(
203                projection,
204                coordinates,
205                coordinate_count,
206                out_points.as_mut_ptr(),
207                out_diagnostic,
208            )
209        })?;
210        Ok(out_points
211            .into_iter()
212            .map(|item| Ok(unsafe { from_native(item) }?))
213            .collect::<Result<Vec<_>>>()?)
214    }
215
216    /// Releases a projection.
217    ///
218    /// See `mln_map_projection_release` in the
219    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
220    pub fn release(self) -> std::result::Result<(), ReleaseError<Self>> {
221        self.consume(|projection| {
222            let mut call = Call::new(projection);
223            call.run(|projection| unsafe { sys::mln_map_projection_release(projection) });
224            Ok(())
225        })
226    }
227
228    /// Moves the projection's camera, without changing the map.
229    ///
230    /// Camera fields that `camera` doesn't set keep their values.
231    ///
232    /// See `mln_map_projection_set_camera` in the
233    /// [C API reference](https://maplibre.org/maplibre-native-ffi/reference/c/projection_8h.html).
234    pub fn set_camera(&self, camera: &CameraOptions) -> Result<()> {
235        let mut call = self.inner.call();
236        let camera = call.reference(&camera)?;
237        call.status(|projection, out_diagnostic| unsafe {
238            sys::mln_map_projection_set_camera(projection, camera, out_diagnostic)
239        })?;
240        Ok(())
241    }
242}