From 1f3b51f2b396879713a5d81485026ad900f7b597 Mon Sep 17 00:00:00 2001 From: Brandon Liu Date: Tue, 28 May 2024 17:30:19 +0800 Subject: [PATCH] WIP on write --- pmtiles/write.go | 45 +++++++++++++++++++++++++++++++++++++++++++ pmtiles/write_test.go | 29 ++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) create mode 100644 pmtiles/write.go create mode 100644 pmtiles/write_test.go diff --git a/pmtiles/write.go b/pmtiles/write.go new file mode 100644 index 0000000..af9935a --- /dev/null +++ b/pmtiles/write.go @@ -0,0 +1,45 @@ +package pmtiles + +import ( + "fmt" + "log" + "os" +) + +func Write(logger *log.Logger, inputArchive string, newHeaderJsonFile string, newMetadataFile string) error { + + if newMetadataFile == "" { + if newHeaderJsonFile == "" { + return fmt.Errorf("No data to write.") + } + + // we can write the header in-place without writing the whole file. + } + + file, err := os.OpenFile(inputArchive, os.O_RDWR, 0666) + + buf := make([]byte, 127) + _, err = file.Read(buf) + if err != nil { + return err + } + originalHeader, _ := deserializeHeader(buf) + + // modify the header + + buf = serializeHeader(originalHeader) + _, err = file.WriteAt(buf, 0) + if err != nil { + return err + } + return nil +} + + +// write metadata: +// always writes in this order: +// copy the header +// copy the root directory +// write the new the metadata +// copy the leaf directories +// copy the tile data diff --git a/pmtiles/write_test.go b/pmtiles/write_test.go new file mode 100644 index 0000000..adf2fe3 --- /dev/null +++ b/pmtiles/write_test.go @@ -0,0 +1,29 @@ +package pmtiles + +import ( + // "log" + "os" + "io" + "io/ioutil" + "path/filepath" + "testing" + "github.com/stretchr/testify/assert" +) + +func TestWriteHeader(t *testing.T) { + // logger := log.New(os.Stdout, "", log.Ldate|log.Ltime|log.Lshortfile) + + tempDir, _ := ioutil.TempDir("", "testing") + defer os.RemoveAll(tempDir) + src, _ := os.Open("fixtures/test_fixture_1.pmtiles") + defer src.Close() + dest, _ := os.Create(filepath.Join(tempDir, "test.pmtiles")) + defer dest.Close() + _, _ = io.Copy(dest, src) + + assert.Nil(t, nil) + + // var input map[string]interface{} + // json.Unmarshal(b.Bytes(), &input) + // assert.Equal(t, "tippecanoe v2.5.0", input["generator"]) +}