Skip to content

Commit f629057

Browse files
basic tile rendering
fix clippy issue
1 parent cafb167 commit f629057

File tree

6 files changed

+162
-1
lines changed

6 files changed

+162
-1
lines changed

Cargo.lock

+107-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ itertools = "0.14"
5252
json-patch = "4"
5353
lambda-web = { version = "0.2.1", features = ["actix4"] }
5454
log = "0.4"
55+
maplibre_native = "0.1.1"
5556
martin-tile-utils = { path = "./martin-tile-utils", version = "0.6.0" }
5657
mbtiles = { path = "./mbtiles", version = "0.12.0" }
5758
md5 = "0.7.0"

martin/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ mbtiles = ["dep:mbtiles"]
6060
pmtiles = ["dep:pmtiles"]
6161
cog = ["dep:tiff", "dep:png"]
6262
styles = ["dep:walkdir", "tokio/fs"]
63+
styles_rendering = ["styles", "dep:maplibre_native"]
6364
postgres = ["dep:deadpool-postgres", "dep:json-patch", "dep:postgis", "dep:postgres", "dep:postgres-protocol", "dep:semver", "dep:tokio-postgres-rustls"]
6465
sprites = ["dep:spreet", "tokio/fs"]
6566
bless-tests = []
@@ -82,6 +83,7 @@ itertools.workspace = true
8283
json-patch = { workspace = true, optional = true }
8384
lambda-web = { workspace = true, optional = true }
8485
log.workspace = true
86+
maplibre_native = { workspace = true, optional = true }
8587
martin-tile-utils.workspace = true
8688
mbtiles = { workspace = true, optional = true }
8789
moka.workspace = true

martin/src/srv/server.rs

+2
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,8 @@ pub fn router(cfg: &mut web::ServiceConfig, #[allow(unused_variables)] usr_cfg:
129129

130130
#[cfg(feature = "styles")]
131131
cfg.service(crate::srv::styles::get_style_json);
132+
#[cfg(feature = "styles_rendering")]
133+
cfg.service(crate::srv::styles::get_style_rendered);
132134

133135
#[cfg(feature = "webui")]
134136
{

martin/src/srv/styles.rs

+33
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,36 @@ async fn get_style_json(path: Path<StyleRequest>, styles: Data<StyleSources>) ->
4343
}
4444
}
4545
}
46+
47+
#[cfg(feature = "styles_rendering")]
48+
#[derive(Deserialize, Debug)]
49+
struct StyleRenderRequest {
50+
style_id: String,
51+
z: u8,
52+
x: u32,
53+
y: u32,
54+
}
55+
56+
#[cfg(feature = "styles_rendering")]
57+
#[route("/style/{style_id}/{z}/{x}/{y}.png", method = "GET")]
58+
async fn get_style_rendered(
59+
path: Path<StyleRenderRequest>,
60+
styles: Data<StyleSources>,
61+
) -> HttpResponse {
62+
let style_id = &path.style_id;
63+
let Some(style_path) = styles.style_json_path(style_id) else {
64+
return HttpResponse::NotFound()
65+
.content_type(ContentType::plaintext())
66+
.body("No such style exists");
67+
};
68+
let xyz = martin_tile_utils::TileCoord {
69+
z: path.z,
70+
x: path.x,
71+
y: path.y,
72+
};
73+
log::trace!("Rendering style {style_id} ({style_path:?}) at {xyz}");
74+
75+
HttpResponse::Ok()
76+
.content_type(ContentType.png())
77+
.body(styles.render(path, xyz))
78+
}

martin/src/styles/mod.rs

+17
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,23 @@ impl StyleSources {
8585
Some(item.path.clone())
8686
}
8787

88+
// assumptions:
89+
// - martin is not an interacive renderer (think 60fps, embedded)
90+
// - We are not rendering the same tile all the time (instead, it is cached)
91+
//
92+
// For now, we only use a static renderer which is optimized for our kind of usage
93+
// In the future, we may consider adding support for smarter rendering including a pool of renderers.
94+
#[cfg(feature = "styles_rendering")]
95+
pub fn render(
96+
&self,
97+
path: &Path,
98+
zxy: martin_tile_utils::TileCoord,
99+
) -> StyleResult<Vec<u8>, Error> {
100+
let mut map = maplibre_native::ImageRendererOptions::new().build_static_renderer();
101+
map.set_style_path(path);
102+
Ok(map.render_static(zxy.z, zxy.x, zxy.y))
103+
}
104+
88105
/// an external representation of the internal catalog
89106
#[must_use]
90107
pub fn get_catalog(&self) -> StyleCatalog {

0 commit comments

Comments
 (0)