-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding a --tilejson arg to the show command (#110)
* adding funcs to read tilejson * adding tilejson option to show command * refactoring tilejson creation into function
- Loading branch information
Showing
5 changed files
with
120 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package pmtiles | ||
|
||
import ( | ||
"encoding/json" | ||
) | ||
|
||
func CreateTilejson(header HeaderV3, metadata_bytes []byte, tileUrl string) ([]byte, error) { | ||
var metadata_map map[string]interface{} | ||
json.Unmarshal(metadata_bytes, &metadata_map) | ||
|
||
tilejson := make(map[string]interface{}) | ||
|
||
tilejson["tilejson"] = "3.0.0" | ||
tilejson["scheme"] = "xyz" | ||
tilejson["tiles"] = []string{tileUrl} | ||
tilejson["vector_layers"] = metadata_map["vector_layers"] | ||
tilejson["attribution"] = metadata_map["attribution"] | ||
tilejson["description"] = metadata_map["description"] | ||
tilejson["name"] = metadata_map["name"] | ||
tilejson["version"] = metadata_map["version"] | ||
|
||
E7 := 10000000.0 | ||
tilejson["bounds"] = []float64{float64(header.MinLonE7) / E7, float64(header.MinLatE7) / E7, float64(header.MaxLonE7) / E7, float64(header.MaxLatE7) / E7} | ||
tilejson["center"] = []interface{}{float64(header.CenterLonE7) / E7, float64(header.CenterLatE7) / E7, header.CenterZoom} | ||
tilejson["minzoom"] = header.MinZoom | ||
tilejson["maxzoom"] = header.MaxZoom | ||
|
||
return json.MarshalIndent(tilejson, "", "\t") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package pmtiles | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCreateTilejson(t *testing.T) { | ||
// Define test inputs | ||
header := HeaderV3{ | ||
MinZoom: 0.0, | ||
MaxZoom: 14.0, | ||
MinLonE7: -1144000000, | ||
MinLatE7: 479000000, | ||
MaxLonE7: -1139000000, | ||
MaxLatE7: 483000000, | ||
CenterLonE7: -1141500000, | ||
CenterLatE7: 481000000, | ||
} | ||
metadataBytes := []byte(` | ||
{ | ||
"vector_layers": [{"id": "layer1"}], | ||
"attribution": "Attribution", | ||
"description": "Description", | ||
"name": "Name", | ||
"version": "1.0" | ||
}`) | ||
tileURL := "https://example.com/tiles.pmtiles/{z}/{x}/{y}" | ||
|
||
// Call the function | ||
tilejsonBytes, err := CreateTilejson(header, metadataBytes, tileURL) | ||
|
||
// Check for errors | ||
if err != nil { | ||
t.Errorf("CreateTilejson returned an error: %v", err) | ||
} | ||
|
||
// Parse the tilejsonBytes to check the output | ||
var tilejson map[string]interface{} | ||
err = json.Unmarshal(tilejsonBytes, &tilejson) | ||
if err != nil { | ||
t.Errorf("Failed to parse the generated TileJSON: %v", err) | ||
} | ||
|
||
assert.Equal(t, "3.0.0", tilejson["tilejson"]) | ||
assert.Equal(t, "xyz", tilejson["scheme"]) | ||
assert.Equal(t, []interface{}{"https://example.com/tiles.pmtiles/{z}/{x}/{y}"}, tilejson["tiles"]) | ||
assert.Equal(t, []interface{}{map[string]interface{}{"id": "layer1"}}, tilejson["vector_layers"]) | ||
assert.Equal(t, "Attribution", tilejson["attribution"]) | ||
assert.Equal(t, "Description", tilejson["description"]) | ||
assert.Equal(t, "Name", tilejson["name"]) | ||
assert.Equal(t, "1.0", tilejson["version"]) | ||
|
||
assert.Equal(t, []interface{}{-114.400000, 47.900000, -113.900000, 48.300000}, tilejson["bounds"]) | ||
assert.Equal(t, []interface{}{-114.150000, 48.100000, 0.0}, tilejson["center"]) | ||
assert.Equal(t, 0.0, tilejson["minzoom"]) | ||
assert.Equal(t, 14.0, tilejson["maxzoom"]) | ||
} |