Skip to content

Commit

Permalink
pmtiles show --metadata writes raw JSON to stdout [#63] (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Oct 31, 2023
1 parent 51b7f89 commit 926183a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 28 deletions.
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ var cli struct {
} `cmd:"" help:"Convert an MBTiles or older spec version to PMTiles."`

Show struct {
Path string `arg:""`
Bucket string `help:"Remote bucket"`
Path string `arg:""`
Bucket string `help:"Remote bucket"`
Metadata bool `help:"Print only the JSON metadata."`
} `cmd:"" help:"Inspect a local or remote archive."`

Tile struct {
Expand Down Expand Up @@ -91,12 +92,12 @@ func main() {

switch ctx.Command() {
case "show <path>":
err := pmtiles.Show(logger, cli.Show.Bucket, cli.Show.Path, false, 0, 0, 0)
err := pmtiles.Show(logger, cli.Show.Bucket, cli.Show.Path, cli.Show.Metadata, false, 0, 0, 0)
if err != nil {
logger.Fatalf("Failed to show archive, %v", err)
}
case "tile <path> <z> <x> <y>":
err := pmtiles.Show(logger, cli.Tile.Bucket, cli.Tile.Path, true, cli.Tile.Z, cli.Tile.X, cli.Tile.Y)
err := pmtiles.Show(logger, cli.Tile.Bucket, cli.Tile.Path, false, true, cli.Tile.Z, cli.Tile.X, cli.Tile.Y)
if err != nil {
logger.Fatalf("Failed to show tile, %v", err)
}
Expand Down
52 changes: 28 additions & 24 deletions pmtiles/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"os"
)

func Show(logger *log.Logger, bucketURL string, key string, show_tile bool, z int, x int, y int) error {
func Show(logger *log.Logger, bucketURL string, key string, show_metadata_only bool, show_tile bool, z int, x int, y int) error {
ctx := context.Background()

bucketURL, key, err := NormalizeBucketKey(bucketURL, "", key)
Expand Down Expand Up @@ -66,20 +66,6 @@ func Show(logger *log.Logger, bucketURL string, key string, show_tile bool, z in
default:
tile_type = "Unknown"
}
fmt.Printf("pmtiles spec version: %d\n", header.SpecVersion)
// fmt.Printf("total size: %s\n", humanize.Bytes(uint64(r.Size())))
fmt.Printf("tile type: %s\n", tile_type)
fmt.Printf("bounds: %f,%f %f,%f\n", float64(header.MinLonE7)/10000000, float64(header.MinLatE7)/10000000, float64(header.MaxLonE7)/10000000, float64(header.MaxLatE7)/10000000)
fmt.Printf("min zoom: %d\n", header.MinZoom)
fmt.Printf("max zoom: %d\n", header.MaxZoom)
fmt.Printf("center: %f,%f\n", float64(header.CenterLonE7)/10000000, float64(header.CenterLatE7)/10000000)
fmt.Printf("center zoom: %d\n", header.CenterZoom)
fmt.Printf("addressed tiles count: %d\n", header.AddressedTilesCount)
fmt.Printf("tile entries count: %d\n", header.TileEntriesCount)
fmt.Printf("tile contents count: %d\n", header.TileContentsCount)
fmt.Printf("clustered: %t\n", header.Clustered)
fmt.Printf("internal compression: %d\n", header.InternalCompression)
fmt.Printf("tile compression: %d\n", header.TileCompression)

metadata_reader, err := bucket.NewRangeReader(ctx, key, int64(header.MetadataOffset), int64(header.MetadataLength))
if err != nil {
Expand All @@ -101,17 +87,35 @@ func Show(logger *log.Logger, bucketURL string, key string, show_tile bool, z in
}
metadata_reader.Close()

var metadata_map map[string]interface{}
json.Unmarshal(metadata_bytes, &metadata_map)
for k, v := range metadata_map {
switch v := v.(type) {
case string:
fmt.Println(k, v)
default:
fmt.Println(k, "<object...>")
if show_metadata_only {
fmt.Print(string(metadata_bytes))
} else {
fmt.Printf("pmtiles spec version: %d\n", header.SpecVersion)
// fmt.Printf("total size: %s\n", humanize.Bytes(uint64(r.Size())))
fmt.Printf("tile type: %s\n", tile_type)
fmt.Printf("bounds: %f,%f %f,%f\n", float64(header.MinLonE7)/10000000, float64(header.MinLatE7)/10000000, float64(header.MaxLonE7)/10000000, float64(header.MaxLatE7)/10000000)
fmt.Printf("min zoom: %d\n", header.MinZoom)
fmt.Printf("max zoom: %d\n", header.MaxZoom)
fmt.Printf("center: %f,%f\n", float64(header.CenterLonE7)/10000000, float64(header.CenterLatE7)/10000000)
fmt.Printf("center zoom: %d\n", header.CenterZoom)
fmt.Printf("addressed tiles count: %d\n", header.AddressedTilesCount)
fmt.Printf("tile entries count: %d\n", header.TileEntriesCount)
fmt.Printf("tile contents count: %d\n", header.TileContentsCount)
fmt.Printf("clustered: %t\n", header.Clustered)
fmt.Printf("internal compression: %d\n", header.InternalCompression)
fmt.Printf("tile compression: %d\n", header.TileCompression)

var metadata_map map[string]interface{}
json.Unmarshal(metadata_bytes, &metadata_map)
for k, v := range metadata_map {
switch v := v.(type) {
case string:
fmt.Println(k, v)
default:
fmt.Println(k, "<object...>")
}
}
}

} else {
// write the tile to stdout

Expand Down

0 comments on commit 926183a

Please sign in to comment.