From 57b82de1e997f4c3de1bcacb5a8e3d442158234b Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 12 Sep 2023 17:19:33 +0800 Subject: [PATCH] mbtiles convert: default bounds to world if bounds metadata missing. [#60] * error out if bounds is degenerate (zero or less area) --- pmtiles/convert.go | 15 +++++++++++++++ pmtiles/convert_test.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/pmtiles/convert.go b/pmtiles/convert.go index 4abc492..7d076bb 100644 --- a/pmtiles/convert.go +++ b/pmtiles/convert.go @@ -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 { @@ -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 { @@ -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 } diff --git a/pmtiles/convert_test.go b/pmtiles/convert_test.go index add93f8..0ce8464 100644 --- a/pmtiles/convert_test.go +++ b/pmtiles/convert_test.go @@ -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", "
abc
", + "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", "
abc
", + "description", "a description", + "type", "overlay", + "version", "1", + "json", "{\"vector_layers\":[{\"abc\":123}],\"tilestats\":{\"def\":456}}", + "compression", "gzip", + }) + assert.NotNil(t,err) +}