Skip to content

Commit

Permalink
Add endpoint for MBTiles metadata.json
Browse files Browse the repository at this point in the history
  • Loading branch information
pka committed Nov 8, 2023
1 parent f3c9814 commit 35dadf5
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 4 deletions.
4 changes: 4 additions & 0 deletions bbox-tile-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ Style JSON requests:

curl -s http://localhost:8080/xyz/ne_extracts.style.json | jq .

Metadata requests:

curl -s http://localhost:8080/xyz/mbtiles_mvt_fl/metadata.json | jq .

Map viewer examples:

x-www-browser http://127.0.0.1:8080/assets/usergrid.html?debug=1
Expand Down
2 changes: 1 addition & 1 deletion bbox-tile-server/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct TileserverCfg {
pub tilestores: Vec<TileCacheProviderCfg>,
}

#[derive(Deserialize, Debug)]
#[derive(Deserialize, Clone, Debug)]
#[serde(deny_unknown_fields)]
pub struct TileSetCfg {
pub name: String,
Expand Down
14 changes: 14 additions & 0 deletions bbox-tile-server/src/endpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ async fn xyz(
}

/// XYZ tilejson endpoint
/// TileJSON layer metadata (https://github.com/mapbox/tilejson-spec)
// xyz/{tileset}.json
async fn tilejson(
service: web::Data<TileService>,
Expand Down Expand Up @@ -56,6 +57,16 @@ async fn stylejson(
}
}

/// XYZ MBTiles metadata.json (https://github.com/mapbox/mbtiles-spec/blob/master/1.3/spec.md)
// xyz/{tileset}/metadata.json
async fn metadatajson(service: web::Data<TileService>, tileset: web::Path<String>) -> HttpResponse {
if let Ok(metadata) = service.mbtiles_metadata(&tileset).await {
HttpResponse::Ok().json(metadata)
} else {
HttpResponse::InternalServerError().finish()
}
}

/// Map tile endpoint
// map/tiles/{tileMatrixSetId}/{tileMatrix}/{tileRow}/{tileCol}
async fn map_tile(
Expand Down Expand Up @@ -280,6 +291,9 @@ impl TileService {
)
.service(web::resource("/xyz/{tileset}.style.json").route(web::get().to(stylejson)))
.service(web::resource("/xyz/{tileset}.json").route(web::get().to(tilejson)))
.service(
web::resource("/xyz/{tileset}/metadata.json").route(web::get().to(metadatajson)),
)
.service(
web::resource("/map/tiles/{tileMatrixSetId}/{tileMatrix}/{tileRow}/{tileCol}")
.route(web::get().to(map_tile)),
Expand Down
18 changes: 15 additions & 3 deletions bbox-tile-server/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use bbox_core::ogcapi::ApiLink;
use bbox_core::service::{CoreService, OgcApiService};
use bbox_core::Format;
use clap::{ArgMatches, FromArgMatches};
use martin_mbtiles::Metadata;
use once_cell::sync::OnceCell;
use serde_json::json;
use std::collections::HashMap;
Expand All @@ -41,6 +42,7 @@ pub struct TileSet {
format: Format,
pub store_reader: Option<Box<dyn TileReader>>,
pub store_writer: Option<Box<dyn TileWriter>>,
config: TileSetCfg,
cache_cfg: Option<TileStoreCfg>,
cache_limits: Option<CacheLimitCfg>,
}
Expand Down Expand Up @@ -133,10 +135,10 @@ impl OgcApiService for TileService {
.mbtiles_metadata(&ts, &format)
.await
.unwrap_or_else(error_exit);
let cache_cfg = ts.cache.map(|name| {
let cache_cfg = ts.cache.as_ref().map(|name| {
stores
.get(&name)
.unwrap_or_else(|| error_exit(ServiceError::CacheNotFound(name)))
.get(name)
.unwrap_or_else(|| error_exit(ServiceError::CacheNotFound(name.to_string())))
});
let store_writer = if let Some(config) = cache_cfg {
Some(store_writer_from_config(config, &ts.name, &format, metadata).await)
Expand All @@ -154,6 +156,7 @@ impl OgcApiService for TileService {
format,
store_reader,
store_writer,
config: ts.clone(),
cache_cfg: cache_cfg.cloned(),
cache_limits: ts.cache_limits,
};
Expand Down Expand Up @@ -369,6 +372,15 @@ impl TileService {
));
Ok(tilejson)
}

/// MBTiles metadata.json
pub async fn mbtiles_metadata(&self, tileset: &str) -> Result<Metadata, ServiceError> {
let ts = self
.tileset(tileset)
.ok_or(ServiceError::TilesetNotFound(tileset.to_string()))?;
Ok(ts.source.mbtiles_metadata(&ts.config, &ts.format).await?)
}

/// Autogenerated Style JSON (https://www.mapbox.com/mapbox-gl-style-spec/)
pub async fn stylejson(
&self,
Expand Down
1 change: 1 addition & 0 deletions docs/src/tile-server/endpoints.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ Services are available via the following HTTP endpoints:
| `/xyz/{tileset}/{z}/{x}/{y}.{format}` | XYZ endpoint |
| `/xyz/{tileset}.json` | Tilejson endpoint |
| `/xyz/{tileset}.style.json` | Generic Style JSON endpoint |
| `/xyz/{tileset}/metadata.json` | MBTiles metadata JSON |

## Request examples

Expand Down

0 comments on commit 35dadf5

Please sign in to comment.