Skip to content

draft: style rendering #1754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 114 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ itertools = "0.14"
json-patch = "4"
lambda-web = { version = "0.2.1", features = ["actix4"] }
log = "0.4"
maplibre_native = "0.1.1"
martin-tile-utils = { path = "./martin-tile-utils", version = "0.6.0" }
mbtiles = { path = "./mbtiles", version = "0.12.0" }
md5 = "0.7.0"
Expand Down
1 change: 0 additions & 1 deletion docs/src/config-file.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,6 @@ fonts:
- /path/to/font_dir

# Publish MapLibre style files
# In the future, the style files will be used for the server-side rendering as well
styles:
paths:
# publish all *.json files in this directory
Expand Down
4 changes: 2 additions & 2 deletions docs/src/sources-styles.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ To edit these styles, we recommend using <https://maputnik.github.io/editor/>.
### API

Martin can serve [MapLibre Style Spec](https://maplibre.org/maplibre-style-spec/).

Currently, Martin will use any valid [`JSON`](https://json.org) file as a style,
but in the future, we may optimise Martin which may result in additional restrictions.

Expand All @@ -21,5 +22,4 @@ A restart of Martin is required to see new styles.

### Server-side raster tile rendering

This is not implemented yet, but there is a plan to add it.
Please see <https://github.com/maplibre/martin/issues/978> for more information.
TODO, document
2 changes: 2 additions & 0 deletions martin/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ mbtiles = ["dep:mbtiles"]
pmtiles = ["dep:pmtiles"]
cog = ["dep:tiff", "dep:png"]
styles = ["dep:walkdir", "tokio/fs"]
styles_rendering = ["styles", "dep:maplibre_native"]
postgres = ["dep:deadpool-postgres", "dep:json-patch", "dep:postgis", "dep:postgres", "dep:postgres-protocol", "dep:semver", "dep:tokio-postgres-rustls"]
sprites = ["dep:spreet", "tokio/fs"]
bless-tests = []
Expand All @@ -82,6 +83,7 @@ itertools.workspace = true
json-patch = { workspace = true, optional = true }
lambda-web = { workspace = true, optional = true }
log.workspace = true
maplibre_native = { workspace = true, optional = true }
martin-tile-utils.workspace = true
mbtiles = { workspace = true, optional = true }
moka.workspace = true
Expand Down
2 changes: 2 additions & 0 deletions martin/src/srv/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ pub fn router(cfg: &mut web::ServiceConfig, #[allow(unused_variables)] usr_cfg:

#[cfg(feature = "styles")]
cfg.service(crate::srv::styles::get_style_json);
#[cfg(feature = "styles_rendering")]
cfg.service(crate::srv::styles::get_style_rendered);

#[cfg(feature = "webui")]
{
Expand Down
33 changes: 33 additions & 0 deletions martin/src/srv/styles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,36 @@ async fn get_style_json(path: Path<StyleRequest>, styles: Data<StyleSources>) ->
}
}
}

#[cfg(feature = "styles_rendering")]
#[derive(Deserialize, Debug)]
struct StyleRenderRequest {
style_id: String,
z: u8,
x: u32,
y: u32,
}

#[cfg(feature = "styles_rendering")]
#[route("/style/{style_id}/{z}/{x}/{y}.png", method = "GET")]
async fn get_style_rendered(
path: Path<StyleRenderRequest>,
styles: Data<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 xyz = martin_tile_utils::TileCoord {
z: path.z,
x: path.x,
y: path.y,
};
log::trace!("Rendering style {style_id} ({style_path:?}) at {xyz}");

HttpResponse::Ok()
.content_type(ContentType.png())
.body(styles.render(path, xyz))
}
19 changes: 19 additions & 0 deletions martin/src/styles/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use std::fmt::Debug;
use std::path::{Path, PathBuf};

use crate::config::UnrecognizedValues;

use crate::file_config::{ConfigExtras, FileConfigEnum, FileError, FileResult};

#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
Expand Down Expand Up @@ -96,6 +97,23 @@ impl StyleSources {
Some(item.path.clone())
}

// assumptions:
// - martin is not an interacive renderer (think 60fps, embedded)
// - We are not rendering the same tile all the time (instead, it is cached)
//
// For now, we only use a static renderer which is optimized for our kind of usage
// In the future, we may consider adding support for smarter rendering including a pool of renderers.
#[cfg(feature = "styles_rendering")]
pub fn render(
&self,
path: &Path,
zxy: martin_tile_utils::TileCoord,
) -> StyleResult<Vec<u8>, Error> {
let mut map = maplibre_native::ImageRendererOptions::new().build_static_renderer();
map.set_style_path(path);
Ok(map.render_static(zxy.z, zxy.x, zxy.y))
}

/// an external representation of the internal catalog
#[must_use]
pub fn get_catalog(&self) -> StyleCatalog {
Expand Down Expand Up @@ -187,6 +205,7 @@ mod tests {
use crate::file_config::FileConfigSrc;

use super::*;

#[test]
fn test_add_single_source() {
use std::fs::File;
Expand Down
Loading