Skip to content

Commit 14e925f

Browse files
committed
Support more mbtiles metadata
1 parent f99e629 commit 14e925f

File tree

2 files changed

+123
-18
lines changed

2 files changed

+123
-18
lines changed

src/main/groovy/geoscript/layer/MBTiles.groovy

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -88,29 +88,17 @@ class MBTiles extends ImageTileLayer {
8888
* @param description The description of the layer
8989
*/
9090
MBTiles(java.util.Map options = [:], File file, String name, String description) {
91-
9291
this.file = file
9392
this.tiles = new MBTilesFile(file)
9493
this.bounds = mercatorBounds
9594
this.proj = mercatorProj
9695
this.name = name
9796

98-
String type = options.get("type", "base_layer")
99-
String version = options.get("version", "1.0")
100-
String format = options.get("format", "png")
101-
String attribution = options.get("attribution","Created with GeoScript")
102-
Bounds metadataBounds = options.get("bounds", latLonBounds)
103-
10497
tiles.init()
105-
MBTilesMetadata metadata = new MBTilesMetadata()
106-
metadata.name = name
107-
metadata.description = description
108-
metadata.formatStr = format
109-
metadata.version = version
110-
metadata.typeStr = type
111-
metadata.bounds = metadataBounds.env
112-
metadata.attribution = attribution
113-
tiles.saveMetaData(metadata)
98+
99+
Map metadataOptions = [name: name, description: description]
100+
metadataOptions.putAll(options)
101+
setMetadata(metadataOptions)
114102
}
115103

116104
/**
@@ -201,10 +189,52 @@ class MBTiles extends ImageTileLayer {
201189
format: metadata.formatStr,
202190
version: metadata.version,
203191
attribution: metadata.attribution,
204-
bounds: metadata.boundsStr
192+
bounds: metadata.boundsStr,
193+
minZoom: metadata.minZoom,
194+
maxZoom: metadata.maxZoom
205195
]
206196
}
207197

198+
MBTiles setMetadata(Map values) {
199+
200+
MBTilesMetadata existingMetadata = tiles.loadMetaData()
201+
202+
String name = values.get("name", existingMetadata.name ?: this.name)
203+
String description = values.get("description", existingMetadata.description ?: "MBTiles")
204+
String type = values.get("type", existingMetadata.typeStr ?: "base_layer")
205+
String version = values.get("version", existingMetadata.version ?: "1.0")
206+
String format = values.get("format", existingMetadata.formatStr ?: "png")
207+
String attribution = values.get("attribution", existingMetadata.attribution ?: "Created with GeoScript")
208+
String minZoom = values.get("minZoom", existingMetadata.minZoom ?: "0")
209+
String maxZoom = values.get("maxZoom", existingMetadata.maxZoom ?: "19")
210+
Bounds metadataBounds = getBounds(values.get("bounds", existingMetadata.bounds ? new Bounds(existingMetadata.bounds) : latLonBounds))
211+
if (!metadataBounds.proj) {
212+
metadataBounds.proj = new Projection("EPSG:4326")
213+
}
214+
215+
MBTilesMetadata metadata = new MBTilesMetadata()
216+
metadata.name = name
217+
metadata.description = description
218+
metadata.formatStr = format
219+
metadata.version = version
220+
metadata.typeStr = type
221+
metadata.bounds = metadataBounds.env
222+
metadata.attribution = attribution
223+
metadata.minZoomStr = minZoom
224+
metadata.maxZoomStr = maxZoom
225+
tiles.saveMetaData(metadata)
226+
227+
this
228+
}
229+
230+
private Bounds getBounds(def value) {
231+
if (value instanceof Bounds) {
232+
value
233+
} else {
234+
Bounds.fromString(value)
235+
}
236+
}
237+
208238
/**
209239
* Get the number of tiles per zoom level.
210240
* @return A List of Maps with zoom, tiles, total, and percent keys
@@ -274,7 +304,9 @@ class MBTiles extends ImageTileLayer {
274304
File file = params.get("file") instanceof File ? params.get("file") as File : new File(params.get("file"))
275305
if (!file.exists() || file.length() == 0 || (params.get("name") && params.get("description"))) {
276306
String name = file.name.replaceAll(".mbtiles","")
277-
new MBTiles(file, params.get("name", name), params.get("description", name))
307+
Map options = params.findAll { String key, value -> !(key in ["file","name","description","type"]) }
308+
options.type = params.mbtilesType ?: "base_layer"
309+
new MBTiles(options, file, params.get("name", name), params.get("description", name))
278310
} else {
279311
new MBTiles(file)
280312
}

src/test/groovy/geoscript/layer/MBTilesTest.groovy

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,79 @@ class MBTilesTest {
343343
assertEquals "-180.0,-85.0511,180.0,85.0511", metadata.bounds
344344
}
345345

346+
@Test
347+
void setMetadata() {
348+
// Create a new MBTiles
349+
File file = new File(folder, "world.mbtiles")
350+
MBTiles layer = new MBTiles(file, "World", "The world in tiles")
351+
Map metadata = layer.metadata
352+
assertEquals "base_layer", metadata.type
353+
assertEquals "World", metadata.name
354+
assertEquals "The world in tiles", metadata.description
355+
assertEquals "png", metadata.format
356+
assertEquals "1.0", metadata.version
357+
assertEquals "Created with GeoScript", metadata.attribution
358+
assertEquals "-179.99,-85.0511,179.99,85.0511", metadata.bounds
359+
assertEquals 0, metadata.minZoom
360+
assertEquals 19, metadata.maxZoom
361+
362+
// Opening the same file shouldn't change the metadata
363+
layer = new MBTiles(file)
364+
metadata = layer.metadata
365+
assertEquals "base_layer", metadata.type
366+
assertEquals "World", metadata.name
367+
assertEquals "The world in tiles", metadata.description
368+
assertEquals "png", metadata.format
369+
assertEquals "1.0", metadata.version
370+
assertEquals "Created with GeoScript", metadata.attribution
371+
assertEquals "-179.99,-85.0511,179.99,85.0511", metadata.bounds
372+
assertEquals 0, metadata.minZoom
373+
assertEquals 19, metadata.maxZoom
374+
375+
// Change the metadata
376+
layer.setMetadata([
377+
name: "world",
378+
version: "2.0",
379+
attribution: "GeoScript",
380+
minZoom: 1,
381+
maxZoom: 6
382+
])
383+
metadata = layer.metadata
384+
assertEquals "base_layer", metadata.type
385+
assertEquals "world", metadata.name
386+
assertEquals "The world in tiles", metadata.description
387+
assertEquals "png", metadata.format
388+
assertEquals "2.0", metadata.version
389+
assertEquals "GeoScript", metadata.attribution
390+
assertEquals "-179.99,-85.0511,179.99,85.0511", metadata.bounds
391+
assertEquals 1, metadata.minZoom
392+
assertEquals 6, metadata.maxZoom
393+
}
394+
395+
@Test
396+
void createWithMetadata() {
397+
File file = new File(folder, "test.mbtiles")
398+
MBTiles layer = new MBTiles.Factory().create([
399+
type: "mbtiles",
400+
file: file,
401+
name: "states",
402+
mbtilesType: "overlay",
403+
description: "USA States",
404+
attribution: "Groovy",
405+
version: "3.0",
406+
format: "JPEG",
407+
bounds: "-135.648049,20.93138,-56.898049,52.355946"
408+
])
409+
Map metadata = layer.metadata
410+
assertEquals "overlay", metadata.type
411+
assertEquals "states", metadata.name
412+
assertEquals "USA States", metadata.description
413+
assertEquals "jpeg", metadata.format
414+
assertEquals "3.0", metadata.version
415+
assertEquals "Groovy", metadata.attribution
416+
assertEquals "-135.648049,20.93138,-56.898049,52.355946", metadata.bounds
417+
}
418+
346419
@Test
347420
void tileCounts() {
348421
File file = new File(getClass().getClassLoader().getResource("states.mbtiles").toURI())

0 commit comments

Comments
 (0)