Skip to content
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

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

Merged
merged 2 commits into from
Sep 13, 2023
Merged
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
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)
}