-
Notifications
You must be signed in to change notification settings - Fork 2
/
feature.go
157 lines (137 loc) · 3.38 KB
/
feature.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package mbt
import (
"encoding/json"
"github.com/buckhx/diglet/mbt/mvt"
ts "github.com/buckhx/diglet/mbt/tile_system"
"github.com/buckhx/diglet/util"
)
type Coordinate struct {
Lat, Lon float64
}
// [lon, lat]
func CoordinateFromString(raw string) (c Coordinate, err error) {
var p []float64
err = json.Unmarshal([]byte(raw), &p)
if err != nil {
return
} else if len(p) != 2 {
err = util.Errorf("Coordinate string len != 2: " + raw)
} else {
c = Coordinate{Lat: p[1], Lon: p[0]}
}
return
}
type Shape struct {
Coordinates []Coordinate
}
// [[lon,lat]...]
func ShapeFromString(raw string) (shp *Shape, err error) {
var points [][]float64
err = json.Unmarshal([]byte(raw), &points)
if err != nil {
return
}
shp = MakeShape(len(points))
for i, p := range points {
if len(p) != 2 {
shp = nil
err = util.Errorf("Coordinate string len != 2 @ %d %s", i, raw)
return
}
shp.Coordinates[i] = Coordinate{Lat: p[1], Lon: p[0]}
}
return
}
func MakeShape(length int) *Shape {
return &Shape{Coordinates: make([]Coordinate, length)}
}
func NewShape(coords ...Coordinate) *Shape {
return &Shape{Coordinates: coords}
}
func (s *Shape) Append(c Coordinate) {
s.Coordinates = append(s.Coordinates, c)
}
func (s *Shape) AddCoordinate(c Coordinate) {
s.Coordinates = append(s.Coordinates, c)
}
func (s *Shape) Reverse() {
last := len(s.Coordinates) - 1
for i, c := range s.Coordinates {
s.Coordinates[i] = s.Coordinates[last-i]
s.Coordinates[last-i] = c
if i >= last/2 {
break
}
}
}
func (s *Shape) IsClockwise() bool {
sum := 0.0
for i, c := range s.Coordinates[:len(s.Coordinates)-1] {
n := s.Coordinates[i+1]
sum += (n.Lon - c.Lon) * (n.Lat + c.Lat)
}
if sum == 0 {
util.Info("Shape edge sum == 0, defaulting to clockwise == true")
}
return sum > 0
}
// Unexported b/c the PointXY are still absolute, MVT needs relative
func (s *Shape) ToTileShape(tile ts.Tile) (shp *mvt.Shape) {
shp = mvt.MakeShape(len(s.Coordinates))
for i, c := range s.Coordinates {
pixel := ts.ClippedCoords(c.Lat, c.Lon).ToPixel(tile.Z)
x := int(pixel.X - tile.ToPixel().X)
y := int(pixel.Y - tile.ToPixel().Y)
point := mvt.Point{X: x, Y: y}
//TODO: clipping
shp.Insert(i, point)
}
return
}
type Feature struct {
Id *uint64
Geometry []*Shape
Type string
Properties map[string]interface{}
}
func NewFeature(geometryType string, geometry ...*Shape) *Feature {
return &Feature{Geometry: geometry, Type: geometryType}
}
func MakeFeature(length int) *Feature {
return &Feature{Geometry: make([]*Shape, length)}
}
func (f *Feature) AddShape(s *Shape) {
f.Geometry = append(f.Geometry, s)
}
func (f *Feature) SetF64Id(id float64) {
var fid uint64 = uint64(id)
f.Id = &fid
}
func (f *Feature) Center() (avg Coordinate) {
div := 0.0
avg = Coordinate{Lat: 0, Lon: 0}
for _, shape := range f.Geometry {
for _, c := range shape.Coordinates {
avg.Lat += c.Lat
avg.Lon += c.Lon
div += 1
}
}
avg.Lat /= div
avg.Lon /= div
return
}
func (f *Feature) ToTiledShapes(tile ts.Tile) (shps []*mvt.Shape) {
shps = make([]*mvt.Shape, len(f.Geometry))
for i, shape := range f.Geometry {
shp := shape.ToTileShape(tile)
shps[i] = shp
}
return
}
func (f *Feature) ToMvtAdapter(tile ts.Tile) (adapter *mvt.Feature) {
adapter = mvt.NewFeatureAdapter(f.Id, f.Type, f.Properties)
adapter.AddShape(f.ToTiledShapes(tile)...)
return
//properties := featureValues(feature)
}