This repository has been archived by the owner on Jul 19, 2021. It is now read-only.
forked from paulmach/orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
layer.go
91 lines (78 loc) · 2.46 KB
/
layer.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
package mvt
import (
"github.com/planetfederal/orb/geojson"
"github.com/planetfederal/orb/maptile"
"github.com/planetfederal/orb/project"
)
const (
// DefaultExtent for mapbox vector tiles. (https://www.mapbox.com/vector-tiles/specification/)
DefaultExtent = 4096
)
// Layer is intermediate MVT layer to be encoded/decoded or projected.
type Layer struct {
Name string
Version uint32
Extent uint32
Features []*geojson.Feature
}
// NewLayer is a helper to create a Layer from a feature collection
// and a name, it sets the default extent and version to 1.
func NewLayer(name string, fc *geojson.FeatureCollection) *Layer {
return &Layer{
Name: name,
Version: 1,
Extent: DefaultExtent,
Features: fc.Features,
}
}
// ProjectToTile will project all the geometries in the layer
// to tile coordinates based on the extent and the mercator projection.
func (l *Layer) ProjectToTile(tile maptile.Tile) {
p := newProjection(tile, l.Extent)
for _, f := range l.Features {
f.Geometry = project.Geometry(f.Geometry, p.ToTile)
}
}
// ProjectToWGS84 will project all the geometries backed to WGS84 from
// the extent and mercator projection.
func (l *Layer) ProjectToWGS84(tile maptile.Tile) {
p := newProjection(tile, l.Extent)
for _, f := range l.Features {
f.Geometry = project.Geometry(f.Geometry, p.ToWGS84)
}
}
// Layers is a set of layers.
type Layers []*Layer
// NewLayers creates a set of layers given a set of feature collections.
func NewLayers(layers map[string]*geojson.FeatureCollection) Layers {
result := make(Layers, 0, len(layers))
for name, fc := range layers {
result = append(result, NewLayer(name, fc))
}
return result
}
// ToFeatureCollections converts the layers to sets of geojson
// feature collections.
func (ls Layers) ToFeatureCollections() map[string]*geojson.FeatureCollection {
result := make(map[string]*geojson.FeatureCollection, len(ls))
for _, l := range ls {
result[l.Name] = &geojson.FeatureCollection{
Features: l.Features,
}
}
return result
}
// ProjectToTile will project all the geometries in all layers
// to tile coordinates based on the extent and the mercator projection.
func (ls Layers) ProjectToTile(tile maptile.Tile) {
for _, l := range ls {
l.ProjectToTile(tile)
}
}
// ProjectToWGS84 will project all the geometries in all the layers backed
// to WGS84 from the extent and mercator projection.
func (ls Layers) ProjectToWGS84(tile maptile.Tile) {
for _, l := range ls {
l.ProjectToWGS84(tile)
}
}