-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
106 lines (72 loc) · 2.78 KB
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package writer
import (
"bytes"
"context"
"fmt"
"github.com/paulmach/orb/geojson"
"github.com/whosonfirst/go-whosonfirst-export/v2"
"github.com/whosonfirst/go-whosonfirst-feature/alt"
"github.com/whosonfirst/go-whosonfirst-feature/properties"
"github.com/whosonfirst/go-whosonfirst-uri"
go_writer "github.com/whosonfirst/go-writer/v3"
)
// WriteFeature will serialize and write 'f' using 'wr' using a default `whosonfirst/go-whosonfirst-export/v2.Exporter` instance.
func WriteFeature(ctx context.Context, wr go_writer.Writer, f *geojson.Feature) (int64, error) {
body, err := f.MarshalJSON()
if err != nil {
return -1, fmt.Errorf("Failed to marshal JSON, %w", err)
}
return WriteBytes(ctx, wr, body)
}
// WriteFeatureWithExporter will serialize and write 'f' using 'wr' using a custom `whosonfirst/go-whosonfirst-export/v2.Exporter` instance.
func WriteFeatureWithExporter(ctx context.Context, wr go_writer.Writer, ex export.Exporter, f *geojson.Feature) (int64, error) {
body, err := f.MarshalJSON()
if err != nil {
return -1, fmt.Errorf("Failed to marshal JSON, %w", err)
}
return WriteBytesWithExporter(ctx, wr, ex, body)
}
// WriteBytes will write 'body' using 'wr' using a default `whosonfirst/go-whosonfirst-export/v2.Exporter` instance.
func WriteBytes(ctx context.Context, wr go_writer.Writer, body []byte) (int64, error) {
ex, err := export.NewExporter(ctx, "whosonfirst://")
if err != nil {
return -1, fmt.Errorf("Failed to create new exporter, %w", err)
}
return WriteBytesWithExporter(ctx, wr, ex, body)
}
// WriteBytesWithExporter will write 'body' using 'wr' using a custom `whosonfirst/go-whosonfirst-export/v2.Exporter` instance.
func WriteBytesWithExporter(ctx context.Context, wr go_writer.Writer, ex export.Exporter, body []byte) (int64, error) {
body, err := ex.Export(ctx, body)
if err != nil {
return -1, fmt.Errorf("Failed to export data, %w", err)
}
id, err := properties.Id(body)
if err != nil {
return -1, fmt.Errorf("Failed to derive ID, %w", err)
}
// START OF put me in a function somewhere...
var rel_path string
if alt.IsAlt(body) {
alt_label, err := properties.AltLabel(body)
if err != nil {
return -1, fmt.Errorf("Failed to derive alt label, %w", err)
}
uri_args, err := uri.NewAlternateURIArgsFromAltLabel(alt_label)
if err != nil {
return -1, fmt.Errorf("Failed to derive URI args from label '%s', %w", alt_label, err)
}
rel_path, err = uri.Id2RelPath(id, uri_args)
} else {
rel_path, err = uri.Id2RelPath(id)
}
if err != nil {
return -1, fmt.Errorf("Failed to derive relative path, %w", err)
}
// END OF put me in a function somewhere...
br := bytes.NewReader(body)
_, err = wr.Write(ctx, rel_path, br)
if err != nil {
return -1, fmt.Errorf("Failed to write %s, %w", rel_path, err)
}
return id, nil
}