martin 1.11.0

Blazing fast and lightweight tile server with PostGIS, MBTiles, and PMTiles support
Documentation
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
use std::str::FromStr;

use actix_web::http::header::{ContentType, LOCATION};
use actix_web::web::{Data, Path};
use actix_web::{HttpResponse, route};
use martin_core::styles::{RenderParams, StyleSources};
use martin_tile_utils::{EARTH_CIRCUMFERENCE, wgs84_to_webmercator};
use serde::Deserialize;
use tracing::{error, trace, warn};

use crate::srv::server::DebouncedWarning;
use crate::srv::styles_rendering::{ImageFormatRequest, encode_image_response};

#[derive(Deserialize, Debug)]
#[cfg_attr(feature = "unstable-schemas", derive(utoipa::IntoParams))]
#[cfg_attr(feature = "unstable-schemas", into_params(parameter_in = Path))]
struct StaticImagePath {
    style_id: String,
    /// `lon,lat,zoom[@bearing[,pitch]]` or `minLon,minLat,maxLon,maxLat`.
    #[cfg_attr(feature = "unstable-schemas", param(value_type = String))]
    camera: CameraRequest,
    /// `WIDTHxHEIGHT[@SCALEx]` - e.g. `800x600` or `400x300@2x`.
    #[cfg_attr(feature = "unstable-schemas", param(value_type = String))]
    size: SizeRequest,
    /// Output encoding. `png`, `jpg`, or `webp` (canonical names only;
    /// `.jpeg` is redirected to `.jpg` via [`redirect_static_jpeg`]).
    #[cfg_attr(feature = "unstable-schemas", param(inline))]
    format: ImageFormatRequest,
}

#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "unstable-schemas", derive(utoipa::ToSchema))]
enum CameraRequest {
    Center {
        lon: f64,
        lat: f64,
        zoom: f64,
        bearing: f64,
        pitch: f64,
    },
    BoundingBox {
        min_lon: f64,
        min_lat: f64,
        max_lon: f64,
        max_lat: f64,
    },
}

impl FromStr for CameraRequest {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Split on `@` first: bearing/pitch use commas like lon,lat,zoom does, so
        // splitting on `,` would confuse the two groups.
        if let Some((before_at, after_at)) = s.split_once('@') {
            let mut parts = before_at.splitn(3, ',');
            let lon: f64 = parts
                .next()
                .ok_or("missing lon")?
                .parse()
                .map_err(|_| "lon")?;
            let lat: f64 = parts
                .next()
                .ok_or("missing lat")?
                .parse()
                .map_err(|_| "lat")?;
            let zoom: f64 = parts
                .next()
                .ok_or("missing zoom")?
                .parse()
                .map_err(|_| "zoom")?;
            let (bearing, pitch) = if let Some((b, p)) = after_at.split_once(',') {
                (
                    b.parse::<f64>().map_err(|_| "bearing")?,
                    p.parse::<f64>().map_err(|_| "pitch")?,
                )
            } else {
                (after_at.parse::<f64>().map_err(|_| "bearing")?, 0.0)
            };
            return Ok(Self::Center {
                lon,
                lat,
                zoom,
                bearing,
                pitch,
            });
        }
        let parts: Vec<&str> = s.split(',').collect();
        match parts.len() {
            3 => Ok(Self::Center {
                lon: parts[0].parse().map_err(|_| "lon")?,
                lat: parts[1].parse().map_err(|_| "lat")?,
                zoom: parts[2].parse().map_err(|_| "zoom")?,
                bearing: 0.0,
                pitch: 0.0,
            }),
            4 => Ok(Self::BoundingBox {
                min_lon: parts[0].parse().map_err(|_| "min_lon")?,
                min_lat: parts[1].parse().map_err(|_| "min_lat")?,
                max_lon: parts[2].parse().map_err(|_| "max_lon")?,
                max_lat: parts[3].parse().map_err(|_| "max_lat")?,
            }),
            _ => Err("expected lon,lat,zoom[@bearing[,pitch]] or minLon,minLat,maxLon,maxLat"),
        }
    }
}

impl<'de> Deserialize<'de> for CameraRequest {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = <String as Deserialize>::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

impl CameraRequest {
    fn validate(self) -> Result<Self, HttpResponse> {
        if let Self::BoundingBox {
            min_lon,
            min_lat,
            max_lon,
            max_lat,
        } = self
            && (max_lon < min_lon || max_lat < min_lat)
        {
            return Err(HttpResponse::BadRequest()
                .content_type(ContentType::plaintext())
                .body("Bounding box is inverted: max must be greater than or equal to min"));
        }
        Ok(self)
    }
}

/// Parsed `{size}` path segment: `WIDTHxHEIGHT[@SCALEx]`. Bounds are
/// checked in [`Self::validate`] after deserialization so the response
/// can name which bound was hit.
#[derive(Debug, Clone, Copy)]
#[cfg_attr(feature = "unstable-schemas", derive(utoipa::ToSchema))]
struct SizeRequest {
    width: u32,
    height: u32,
    scale: f32,
}

const MAX_WIDTH: u32 = 2048;
const MAX_HEIGHT: u32 = 2048;
const MAX_SCALE: u8 = 4;

impl SizeRequest {
    fn validate(self) -> Result<Self, HttpResponse> {
        if self.width == 0 || self.height == 0 {
            return Err(HttpResponse::BadRequest()
                .content_type(ContentType::plaintext())
                .body("Image dimensions must be greater than zero"));
        }
        if self.width > MAX_WIDTH || self.height > MAX_HEIGHT {
            return Err(HttpResponse::BadRequest()
                .content_type(ContentType::plaintext())
                .body(format!(
                    "Image dimensions exceed maximum allowed ({MAX_WIDTH}x{MAX_HEIGHT})"
                )));
        }
        if !self.scale.is_finite() || self.scale <= 0.0 {
            return Err(HttpResponse::BadRequest()
                .content_type(ContentType::plaintext())
                .body("Scale factor must be a positive finite number"));
        }
        #[expect(
            clippy::cast_possible_truncation,
            clippy::cast_sign_loss,
            reason = "scale was checked to be finite and positive above"
        )]
        let scale_u8 = self.scale.round() as u8;
        if scale_u8 > MAX_SCALE {
            return Err(HttpResponse::BadRequest()
                .content_type(ContentType::plaintext())
                .body(format!(
                    "Scale factor exceeds maximum allowed ({MAX_SCALE})"
                )));
        }
        Ok(self)
    }
}

impl FromStr for SizeRequest {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let (dims, scale) = if let Some((dims, scale_str)) = s.split_once('@') {
            let scale_str = scale_str.strip_suffix('x').unwrap_or(scale_str);
            let scale: f32 = scale_str.parse().map_err(|_| "scale")?;
            (dims, scale)
        } else {
            (s, 1.0)
        };
        let (w_str, h_str) = dims.split_once('x').ok_or("expected WIDTHxHEIGHT")?;
        let width: u32 = w_str.parse().map_err(|_| "width")?;
        let height: u32 = h_str.parse().map_err(|_| "height")?;
        Ok(Self {
            width,
            height,
            scale,
        })
    }
}

impl<'de> Deserialize<'de> for SizeRequest {
    fn deserialize<D: serde::Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        let s = <String as Deserialize>::deserialize(deserializer)?;
        s.parse().map_err(serde::de::Error::custom)
    }
}

/// Render a static map image at an arbitrary camera into `{size}.{format}`.
#[cfg_attr(
    feature = "unstable-schemas",
    utoipa::path(
        get,
        path = "/style/{style_id}/static/{camera}/{size}.{format}",
        params(StaticImagePath),
        responses(
            (status = 200, description = "Rendered static map image (PNG, JPEG, or WebP)"),
            (status = 400, description = "Invalid params or size"),
            (status = 403, description = "Rendering is disabled"),
            (status = 404, description = "No matching style"),
            (status = 500, description = "Renderer or encoder failure"),
        ),
    )
)]
#[route("/style/{style_id}/static/{camera}/{size}.{format}", method = "GET")]
#[hotpath::measure]
pub async fn get_rendered_static_style(
    path: Path<StaticImagePath>,
    styles: Data<StyleSources>,
) -> HttpResponse {
    handle_static_request(&path, &styles).await
}

#[derive(Deserialize, Debug)]
struct StaticJpgRedirectPath {
    style_id: String,
    camera: String,
    size: String,
}

/// `.jpeg` to `.jpg` 301 redirect (canonical name is `.jpg`).
#[route(
    "/style/{style_id}/static/{camera}/{size}.jpeg",
    method = "GET",
    method = "HEAD"
)]
pub async fn redirect_static_jpeg(path: Path<StaticJpgRedirectPath>) -> HttpResponse {
    static WARNING: DebouncedWarning = DebouncedWarning::new();
    let StaticJpgRedirectPath {
        style_id,
        camera,
        size,
    } = path.as_ref();
    WARNING
        .once_per_hour(|| {
            warn!(
                "Request to /style/{style_id}/static/{camera}/{size}.jpeg caused unnecessary redirect. Use .jpg to avoid extra round-trip latency."
            );
        })
        .await;
    HttpResponse::MovedPermanently()
        .insert_header((
            LOCATION,
            format!("/style/{style_id}/static/{camera}/{size}.jpg"),
        ))
        .finish()
}

/// Camera resolved from a [`CameraRequest`]. WGS84 degrees.
struct Camera {
    center_lon: f64,
    center_lat: f64,
    zoom: f64,
    bearing: f64,
    pitch: f64,
}

async fn handle_static_request(path: &StaticImagePath, styles: &StyleSources) -> HttpResponse {
    let style_id = &path.style_id;
    let Some(style_path) = styles.style_json_path(style_id) else {
        return HttpResponse::NotFound()
            .content_type(ContentType::plaintext())
            .body("No such style exists");
    };

    let size = match path.size.validate() {
        Ok(size) => size,
        Err(resp) => return resp,
    };

    let camera_req = match path.camera.validate() {
        Ok(c) => c,
        Err(resp) => return resp,
    };

    let camera = resolve_camera(camera_req, size);

    trace!(
        "Rendering static image for style {style_id} at ({lon},{lat}) z{zoom} {w}x{h}@{scale}",
        lon = camera.center_lon,
        lat = camera.center_lat,
        zoom = camera.zoom,
        w = size.width,
        h = size.height,
        scale = size.scale,
    );

    let image = match render_base(styles, style_path, &camera, size).await {
        Ok(img) => img,
        Err(resp) => return resp,
    };

    encode_image_response(image.as_image(), path.format)
}

fn resolve_camera(camera: CameraRequest, size: SizeRequest) -> Camera {
    match camera {
        CameraRequest::Center {
            lon,
            lat,
            zoom,
            bearing,
            pitch,
        } => Camera {
            center_lon: lon,
            center_lat: lat,
            zoom,
            bearing,
            pitch,
        },
        CameraRequest::BoundingBox {
            min_lon,
            min_lat,
            max_lon,
            max_lat,
        } => {
            let (clon, clat, z) =
                bbox_to_center_zoom(min_lon, min_lat, max_lon, max_lat, size.width, size.height);
            Camera {
                center_lon: clon,
                center_lat: clat,
                zoom: z,
                bearing: 0.0,
                pitch: 0.0,
            }
        }
    }
}

/// Center + zoom that frames a bbox within `width × height` pixels.
fn bbox_to_center_zoom(
    min_lon: f64,
    min_lat: f64,
    max_lon: f64,
    max_lat: f64,
    width: u32,
    height: u32,
) -> (f64, f64, f64) {
    let center_lon = f64::midpoint(min_lon, max_lon);
    let center_lat = f64::midpoint(min_lat, max_lat);

    let (west, south) = wgs84_to_webmercator(min_lon, min_lat);
    let (east, north) = wgs84_to_webmercator(max_lon, max_lat);

    let mercator_width = east - west;
    let mercator_height = north - south;

    if mercator_width.abs() < 1e-10 && mercator_height.abs() < 1e-10 {
        return (center_lon, center_lat, 14.0);
    }

    let zoom_for = |range: f64, px: u32| {
        if range.abs() < 1e-10 {
            20.0
        } else {
            (EARTH_CIRCUMFERENCE * f64::from(px) / (256.0 * range)).log2()
        }
    };

    let zoom = zoom_for(mercator_width, width)
        .min(zoom_for(mercator_height, height))
        .max(0.0);

    (center_lon, center_lat, zoom)
}

async fn render_base(
    styles: &StyleSources,
    style_path: std::path::PathBuf,
    camera: &Camera,
    size: SizeRequest,
) -> Result<martin_core::styles::StaticImage, HttpResponse> {
    use martin_core::styles::StyleError;

    // The renderer multiplies (width, height) by pixel_ratio internally, so
    // pass the *logical* size - not size × scale - to avoid double-scaling.
    let params = RenderParams::new(
        style_path,
        camera.center_lat,
        camera.center_lon,
        camera.zoom,
    )
    .with_size(size.width, size.height, size.scale)
    .with_orientation(camera.bearing, camera.pitch);
    styles.render_static(params).await.map_err(|e| match e {
        StyleError::RenderingIsDisabled => {
            warn!("Failed to render static image because rendering is disabled");
            HttpResponse::Forbidden()
                .content_type(ContentType::plaintext())
                .body("Rendering is disabled")
        }
        other => {
            error!("Failed to render static image: {other}");
            HttpResponse::InternalServerError()
                .content_type(ContentType::plaintext())
                .body("Failed to render static image")
        }
    })
}

#[cfg(test)]
mod tests {
    use actix_web::body::to_bytes;
    use actix_web::dev::ServiceResponse;
    use actix_web::http::StatusCode;
    use actix_web::test::{TestRequest, call_service, init_service};
    use actix_web::{App, web};
    use martin_core::styles::StyleSources;
    use rstest::rstest;

    use super::*;

    fn one_style() -> (StyleSources, tempfile::NamedTempFile) {
        let file = tempfile::Builder::new()
            .suffix(".json")
            .tempfile()
            .expect("tempfile");
        std::fs::write(file.path(), b"{}").expect("write style");
        let mut styles = StyleSources::default();
        styles.add_style("s".to_string(), file.path().to_path_buf());
        (styles, file)
    }

    macro_rules! call {
        ($req:expr, $styles:expr) => {{
            let app = init_service(
                App::new()
                    .app_data(web::Data::new($styles))
                    .service(get_rendered_static_style),
            )
            .await;
            call_service(&app, $req.to_request()).await
        }};
    }

    async fn body_text(resp: ServiceResponse) -> String {
        let bytes = to_bytes(resp.into_body()).await.expect("body");
        String::from_utf8(bytes.to_vec()).expect("utf8")
    }

    fn get(uri: &str) -> TestRequest {
        TestRequest::get().uri(uri)
    }

    #[actix_rt::test]
    async fn unknown_style_returns_404() {
        let resp = call!(
            get("/style/missing/static/0,0,1/100x100.png"),
            StyleSources::default()
        );
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
        assert_eq!(body_text(resp).await, "No such style exists");
    }

    #[rstest]
    #[case::center("0,0,1")]
    #[case::center_with_bearing("0,0,1@45")]
    #[case::center_with_pitch("0,0,1@45,60")]
    #[case::center_negative("-122.4,37.8,12")]
    #[case::center_fractional_zoom("10.5,20.3,5.5")]
    #[case::bbox_world("-180,-90,180,90")]
    #[case::bbox_simple("-123,37,-122,38")]
    #[actix_rt::test]
    async fn valid_camera_reach_renderer(#[case] params: &str) {
        let (styles, _f) = one_style();
        let resp = call!(
            get(&format!("/style/s/static/{params}/100x100.png")),
            styles
        );
        assert_eq!(resp.status(), StatusCode::FORBIDDEN, "params={params:?}");
    }

    #[rstest]
    #[case::garbage("invalid")]
    #[case::two_parts("1,2")]
    #[case::five_parts("1,2,3,4,5")]
    #[case::non_numeric_zoom("-122.4,37.8,abc")]
    #[case::non_numeric_lat("-122.4,abc,5")]
    #[case::non_numeric_bbox("a,b,c,d")]
    #[case::non_numeric_bearing("-122.4,37.8,12@abc")]
    #[case::non_numeric_pitch("-122.4,37.8,12@45,abc")]
    #[case::trailing_at("-122.4,37.8,12@")]
    #[actix_rt::test]
    async fn invalid_camera_returns_404(#[case] params: &str) {
        let (styles, _f) = one_style();
        let resp = call!(
            get(&format!("/style/s/static/{params}/100x100.png")),
            styles
        );
        assert_eq!(resp.status(), StatusCode::NOT_FOUND, "params={params:?}");
    }

    #[rstest]
    #[case::png("800x600.png")]
    #[case::jpeg_2x("800x600@2x.jpeg")]
    #[case::jpg("256x256.jpg")]
    #[case::webp("400x300.webp")]
    #[case::scale_no_x_suffix("512x512@3.png")]
    #[case::fractional_scale("100x100@1.5x.png")]
    #[actix_rt::test]
    async fn valid_size_fmt_reaches_renderer(#[case] size: &str) {
        let (styles, _f) = one_style();
        let resp = call!(get(&format!("/style/s/static/0,0,1/{size}")), styles);
        assert_eq!(resp.status(), StatusCode::FORBIDDEN, "size={size:?}");
    }

    #[rstest]
    #[case::unsupported_format("100x100.bmp")]
    #[case::no_x_separator("800.png")]
    #[case::non_numeric_dim("axb.png")]
    #[case::empty_scale("800x600@.png")]
    #[case::non_numeric_scale("800x600@xyz.png")]
    #[actix_rt::test]
    async fn invalid_size_fmt_returns_404(#[case] size: &str) {
        let (styles, _f) = one_style();
        let resp = call!(get(&format!("/style/s/static/0,0,1/{size}")), styles);
        assert_eq!(resp.status(), StatusCode::NOT_FOUND, "size={size:?}");
    }

    #[rstest]
    #[case::zero_width("0x100.png", "Image dimensions must be greater than zero")]
    #[case::zero_height("100x0.png", "Image dimensions must be greater than zero")]
    #[case::oversize_width("9999x100.png", "Image dimensions exceed maximum")]
    #[case::oversize_height("100x9999.png", "Image dimensions exceed maximum")]
    #[case::oversize_scale("100x100@9x.png", "Scale factor exceeds maximum")]
    #[case::zero_scale("100x100@0x.png", "Scale factor must be a positive finite number")]
    #[case::negative_scale("100x100@-2x.png", "Scale factor must be a positive finite number")]
    #[case::nan_scale("100x100@nanx.png", "Scale factor must be a positive finite number")]
    #[case::pos_inf_scale("100x100@infx.png", "Scale factor must be a positive finite number")]
    #[case::neg_inf_scale("100x100@-infx.png", "Scale factor must be a positive finite number")]
    #[actix_rt::test]
    async fn dimension_violations_return_400_with_specific_message(
        #[case] size: &str,
        #[case] expected_prefix: &str,
    ) {
        let (styles, _f) = one_style();
        let resp = call!(get(&format!("/style/s/static/0,0,1/{size}")), styles);
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "size={size:?}");
        let body = body_text(resp).await;
        assert!(
            body.starts_with(expected_prefix),
            "size={size:?}: expected body to start with {expected_prefix:?}, got {body:?}"
        );
    }

    #[rstest]
    #[case::inverted_lon("10,0,-10,5")]
    #[case::inverted_lat("0,5,1,-5")]
    #[actix_rt::test]
    async fn inverted_bbox_returns_400(#[case] params: &str) {
        let (styles, _f) = one_style();
        let resp = call!(
            get(&format!("/style/s/static/{params}/200x200.png")),
            styles
        );
        assert_eq!(resp.status(), StatusCode::BAD_REQUEST, "params={params:?}");
        let body = body_text(resp).await;
        assert!(
            body.starts_with("Bounding box"),
            "params={params:?}: expected body to start with \"Bounding box\", got {body:?}"
        );
    }
}