mln_style/sources/
image.rs1use std::sync::Arc;
5
6use crate::Edit;
7use crate::conversion::{Convertible, Error};
8use crate::expression::Functions;
9use crate::geo::LatLng;
10use crate::layer::SourceKind;
11use crate::source::{Source, SourceData, SourceTypeInfo, StyleSourceType};
12
13pub struct ImageSourceType;
15
16#[derive(Clone, Debug, PartialEq)]
18pub struct ImageSourceOptions {
19 pub url: Option<String>,
21 pub coordinates: [LatLng; 4],
23}
24
25impl StyleSourceType for ImageSourceType {
26 const INFO: SourceTypeInfo = SourceTypeInfo { name: "image", layer_kind: SourceKind::Raster, from_json: true };
27
28 type Options = ImageSourceOptions;
29
30 fn from_json(value: &Convertible<'_>, _functions: &Arc<Functions>, error: &mut Error) -> Option<SourceData<Self>> {
31 crate::conversion::source::convert_image_source(value, error)
32 }
33}
34
35impl ImageSourceType {
36 pub fn set_url(id: impl Into<String>, url: impl Into<String>) -> Edit {
39 let url = url.into();
40 Edit::update_source::<Self>(id, "an image source", |data| {
41 data.options.url = Some(url);
42 data.load += 1;
43 true
44 })
45 }
46
47 pub fn set_coordinates(id: impl Into<String>, coordinates: [LatLng; 4]) -> Edit {
50 Edit::update_source::<Self>(id, "an image source", move |data| {
51 data.options.coordinates = coordinates;
52 true
53 })
54 }
55}
56
57#[must_use]
60pub fn image_source(id: &str, coordinates: [LatLng; 4]) -> Source {
61 Source::new(id, SourceData::<ImageSourceType>::new(ImageSourceOptions { url: None, coordinates }))
62}