Skip to content

Commit

Permalink
mbtiles convert: default bounds to world if bounds metadata missing. [#…
Browse files Browse the repository at this point in the history
…60]

* error out if bounds is degenerate (zero or less area)
  • Loading branch information
bdon committed Sep 12, 2023
1 parent 933ea7b commit 57b82de
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pmtiles/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,7 @@ func parse_center(center string) (int32, int32, uint8, error) {
func mbtiles_to_header_json(mbtiles_metadata []string) (HeaderV3, map[string]interface{}, error) {
header := HeaderV3{}
json_result := make(map[string]interface{})
boundsSet := false
for i := 0; i < len(mbtiles_metadata); i += 2 {
value := mbtiles_metadata[i+1]
switch key := mbtiles_metadata[i]; key {
Expand All @@ -604,10 +605,15 @@ func mbtiles_to_header_json(mbtiles_metadata []string) (HeaderV3, map[string]int
if err != nil {
return header, json_result, err
}

if min_lon >= max_lon || min_lat >= max_lat {
return header, json_result, fmt.Errorf("Error: zero-area bounds in mbtiles metadata.")
}
header.MinLonE7 = min_lon
header.MinLatE7 = min_lat
header.MaxLonE7 = max_lon
header.MaxLatE7 = max_lat
boundsSet = true
case "center":
center_lon, center_lat, center_zoom, err := parse_center(value)
if err != nil {
Expand Down Expand Up @@ -637,5 +643,14 @@ func mbtiles_to_header_json(mbtiles_metadata []string) (HeaderV3, map[string]int
json_result[key] = value
}
}

E7 := 10000000.0
if !boundsSet {
header.MinLonE7 = int32(-180 * E7)
header.MinLatE7 = int32(-85 * E7)
header.MaxLonE7 = int32(180 * E7)
header.MaxLatE7 = int32(85 * E7)
}

return header, json_result, nil
}
35 changes: 35 additions & 0 deletions pmtiles/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,38 @@ func TestMbtiles(t *testing.T) {
_, ok = json_metadata["tilestats"]
assert.True(t, ok)
}

func TestMbtilesMissingBoundsCenter(t *testing.T) {
header, _, err := mbtiles_to_header_json([]string{
"name", "test_name",
"format", "pbf",
"attribution", "<div>abc</div>",
"description", "a description",
"type", "overlay",
"version", "1",
"json", "{\"vector_layers\":[{\"abc\":123}],\"tilestats\":{\"def\":456}}",
"compression", "gzip",
})
assert.Nil(t,err)
assert.Equal(t,int32(-180*10000000), header.MinLonE7)
assert.Equal(t,int32(-85*10000000), header.MinLatE7)
assert.Equal(t,int32(180*10000000), header.MaxLonE7)
assert.Equal(t,int32(85*10000000), header.MaxLatE7)
assert.Equal(t,int32(0), header.CenterLonE7)
assert.Equal(t,int32(0), header.CenterLatE7)
}

func TestMbtilesDegenerateBounds(t *testing.T) {
_, _, err := mbtiles_to_header_json([]string{
"name", "test_name",
"format", "pbf",
"bounds", "0,0,0,0",
"attribution", "<div>abc</div>",
"description", "a description",
"type", "overlay",
"version", "1",
"json", "{\"vector_layers\":[{\"abc\":123}],\"tilestats\":{\"def\":456}}",
"compression", "gzip",
})
assert.NotNil(t,err)
}

0 comments on commit 57b82de

Please sign in to comment.