Skip to content

Commit

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

* mbtiles convert: default bounds to world if bounds metadata missing. [#60]

* error out if bounds is degenerate (zero or less area)

* convert: don't crash on empty mbtiles [#41]
  • Loading branch information
bdon committed Sep 13, 2023
1 parent 7cf54da commit c246392
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
19 changes: 19 additions & 0 deletions pmtiles/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,10 @@ func ConvertMbtiles(logger *log.Logger, input string, output string, deduplicate
}
}

if tileset.GetCardinality() == 0 {
return fmt.Errorf("No tiles in MBTiles archive.")
}

logger.Println("Pass 2: writing tiles")
resolver := NewResolver(deduplicate, header.TileType == Mvt)
{
Expand Down Expand Up @@ -578,6 +582,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 +609,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 +647,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 c246392

Please sign in to comment.