Skip to content

Commit

Permalink
finish edit, writing
Browse files Browse the repository at this point in the history
  • Loading branch information
bdon committed Jan 23, 2025
1 parent cafbfd9 commit 847a02f
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
40 changes: 34 additions & 6 deletions pmtiles/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ type HeaderV3 struct {
// Omitted parts are the responsibility of the generator program and not editable.
// The formatting is aligned with the TileJSON / MBTiles specification.
type HeaderJson struct {
TileCompression string `json:"tile_compression"`
TileType string `json:"tile_type"`
MinZoom int `json:"minzoom"`
MaxZoom int `json:"maxzoom"`
TileCompression string `json:"tile_compression"`
TileType string `json:"tile_type"`
MinZoom int `json:"minzoom"`
MaxZoom int `json:"maxzoom"`
Bounds []float64 `json:"bounds"`
Center []float64 `json:"center"`
}
Expand Down Expand Up @@ -111,6 +111,23 @@ func stringifiedTileType(t TileType) string {
}
}

func stringToTileType(t string) TileType {
switch t {
case "mvt":
return Mvt
case "png":
return Png
case "jpg":
return Jpeg
case "webp":
return Webp
case "avif":
return Avif
default:
return UnknownTileType
}
}

func headerExt(header HeaderV3) string {
base := stringifiedTileType(header.TileType)
if base == "" {
Expand All @@ -130,15 +147,26 @@ func headerContentEncoding(compression Compression) (string, bool) {
}
}

func stringToCompression(s string) Compression {
switch s {
case "gzip":
return Gzip
case "br":
return Brotli
default:
return UnknownCompression
}
}

func headerToJson(header HeaderV3) HeaderJson {
compressionString, _ := headerContentEncoding(header.TileCompression)
return HeaderJson{
TileCompression: compressionString,
TileType: stringifiedTileType(header.TileType),
MinZoom: int(header.MinZoom),
MaxZoom: int(header.MaxZoom),
Bounds: []float64{float64(header.MinLonE7) / 10000000, float64(header.MinLatE7) / 10000000, float64(header.MaxLonE7) / 10000000, float64(header.MaxLatE7) / 10000000 },
Center: []float64{float64(header.CenterLonE7) / 10000000, float64(header.CenterLatE7) / 10000000, float64(header.CenterZoom)},
Bounds: []float64{float64(header.MinLonE7) / 10000000, float64(header.MinLatE7) / 10000000, float64(header.MaxLonE7) / 10000000, float64(header.MaxLatE7) / 10000000},
Center: []float64{float64(header.CenterLonE7) / 10000000, float64(header.CenterLatE7) / 10000000, float64(header.CenterZoom)},
}
}

Expand Down
22 changes: 14 additions & 8 deletions pmtiles/edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"compress/gzip"
"encoding/json"
"fmt"
"github.com/schollz/progressbar/v3"
"io"
"io/ioutil"
"log"
"os"
"github.com/schollz/progressbar/v3"
)

func Edit(logger *log.Logger, inputArchive string, newHeaderJsonFile string, newMetadataFile string) error {
Expand All @@ -30,7 +30,7 @@ func Edit(logger *log.Logger, inputArchive string, newHeaderJsonFile string, new
oldHeader, _ := deserializeHeader(buf)
newHeader := oldHeader

if (newHeaderJsonFile != "") {
if newHeaderJsonFile != "" {
newHeaderData := HeaderJson{}
data, err := ioutil.ReadFile(newHeaderJsonFile)
if err != nil {
Expand All @@ -41,12 +41,20 @@ func Edit(logger *log.Logger, inputArchive string, newHeaderJsonFile string, new
return err
}

// TODO header.TileCompression = etc
newHeader.TileType = stringToTileType(newHeaderData.TileType)
newHeader.TileCompression = stringToCompression(newHeaderData.TileCompression)
newHeader.MinZoom = uint8(newHeaderData.MinZoom)
newHeader.MaxZoom = uint8(newHeaderData.MaxZoom)
newHeader.MinLonE7 = int32(newHeaderData.Bounds[0] * 10000000)
newHeader.MinLatE7 = int32(newHeaderData.Bounds[1] * 10000000)
newHeader.MaxLonE7 = int32(newHeaderData.Bounds[2] * 10000000)
newHeader.MaxLatE7 = int32(newHeaderData.Bounds[3] * 10000000)
newHeader.CenterLonE7 = int32(newHeaderData.Center[0] * 10000000)
newHeader.CenterLatE7 = int32(newHeaderData.Center[1] * 10000000)
newHeader.CenterZoom = uint8(newHeaderData.Center[2])
}

if (newMetadataFile == "") {
if newMetadataFile == "" {
buf = serializeHeader(newHeader)
_, err = file.WriteAt(buf, 0)
if err != nil {
Expand Down Expand Up @@ -76,10 +84,8 @@ func Edit(logger *log.Logger, inputArchive string, newHeaderJsonFile string, new
newHeader.LeafDirectoryOffset = newHeader.MetadataOffset + newHeader.MetadataLength
newHeader.TileDataOffset = newHeader.LeafDirectoryOffset + newHeader.LeafDirectoryLength

fmt.Println(newHeader.RootOffset)

bar := progressbar.DefaultBytes(
int64(HeaderV3LenBytes + newHeader.RootLength + uint64(len(metadataBytes.Bytes())) + newHeader.LeafDirectoryLength + newHeader.TileDataLength),
int64(HeaderV3LenBytes+newHeader.RootLength+uint64(len(metadataBytes.Bytes()))+newHeader.LeafDirectoryLength+newHeader.TileDataLength),
"writing file",
)

Expand All @@ -100,7 +106,7 @@ func Edit(logger *log.Logger, inputArchive string, newHeaderJsonFile string, new

file.Close()
outfile.Close()
err = os.Rename(inputArchive + ".tmp", inputArchive)
err = os.Rename(inputArchive+".tmp", inputArchive)
if err != nil {
return err
}
Expand Down

0 comments on commit 847a02f

Please sign in to comment.