Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WriteKMZ #30

Merged
merged 1 commit into from
May 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ linters:
- stylecheck
- tagliatelle
- tenv
- testableexamples
- testpackage
- thelper
- tparallel
Expand All @@ -93,6 +92,7 @@ linters:
- nlreturn
- paralleltest
- structcheck # https://github.com/golangci/golangci-lint/issues/2649
- testableexamples
- varnamelen
- wrapcheck
- wsl
Expand Down Expand Up @@ -128,4 +128,7 @@ issues:
- linters:
- forbidigo
- gosec
path: "internal/"
path: "internal/"
- linters:
- goerr113
text: do not define dynamic errors, use wrapped static errors instead
56 changes: 56 additions & 0 deletions kmz.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package kml

import (
"archive/zip"
"fmt"
"io"
"sort"
)

// WriteKMZ writes a KMZ file containing files to w. The values of the files map
// can be []bytes, strings, *KMLElements, *GxKMLElements, Elements, or
// io.Readers.
func WriteKMZ(w io.Writer, files map[string]any) error {
names := make([]string, 0, len(files))
for name := range files {
names = append(names, name)
}
sort.Strings(names)

zipWriter := zip.NewWriter(w)
for _, filename := range names {
zipFileWriter, err := zipWriter.Create(filename)
if err != nil {
return err
}
switch value := files[filename].(type) {
case []byte:
if _, err := zipFileWriter.Write(value); err != nil {
return err
}
case string:
if _, err := zipFileWriter.Write([]byte(value)); err != nil {
return err
}
case *KMLElement:
if err := value.Write(zipFileWriter); err != nil {
return err
}
case *GxKMLElement:
if err := value.Write(zipFileWriter); err != nil {
return err
}
case Element:
if err := KML(value).Write(zipFileWriter); err != nil {
return err
}
case io.Reader:
if _, err := io.Copy(zipFileWriter, value); err != nil {
return err
}
default:
return fmt.Errorf("%T: unsupported type", value)
}
}
return zipWriter.Close()
}
26 changes: 26 additions & 0 deletions kmz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package kml_test

import (
"os"

"github.com/twpayne/go-kml/v3"
)

func ExampleWriteKMZ() {
doc := kml.KML(
kml.Placemark(
kml.Name("Zürich"),
kml.Point(
kml.Coordinates(
kml.Coordinate{Lat: 47.374444, Lon: 8.541111},
),
),
),
)

if err := kml.WriteKMZ(os.Stdout, map[string]any{
"doc.kml": doc,
}); err != nil {
panic(err)
}
}