-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson.go
231 lines (203 loc) · 5.47 KB
/
geojson.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package tz
import (
"encoding/json"
"fmt"
"github.com/catmullet/tz/geodb"
"math"
"sort"
"time"
)
const (
timeZonesFilename = "combined-with-oceans.json.snappy"
)
type GeoJsonLookup interface {
TimeZone(lat, lon float64) string
Location(lat, lon float64) (*time.Location, error)
}
type Collection struct {
Features []*Feature
}
type Feature struct {
Geometry Geometry
Properties map[string]string
}
type Geometry struct {
Coordinates []Coordinates
MaxPoint Point
MinPoint Point
}
type Coordinates struct {
Polygon []Point
MaxPoint Point
MinPoint Point
}
type Point struct {
Lon float64
Lat float64
}
var polygonType struct {
Type string
Geometries []*Geometry
}
var polygon struct {
Coordinates [][][]float64
}
var multiPolygon struct {
Coordinates [][][][]float64
}
func NewTZ() (GeoJsonLookup, error) {
var (
fc = &Collection{Features: make([]*Feature, 500)}
)
if b, err := newLocalGeoStorage(geodb.GeoDbEmbedDirectory).LoadFile(timeZonesFilename, &fc); err != nil || len(b) == 0 {
return nil, fmt.Errorf("failed to load file, %w", err)
}
for i := range fc.Features {
f := fc.Features[i]
sort.SliceStable(f.Geometry.Coordinates, func(i, j int) bool {
return f.Geometry.Coordinates[i].MinPoint.Lon <= f.Geometry.Coordinates[j].MinPoint.Lon
})
}
sort.SliceStable(fc.Features, func(i, j int) bool {
return fc.Features[i].Geometry.MinPoint.Lon <= fc.Features[j].Geometry.MinPoint.Lon
})
return fc, nil
}
func (g *Geometry) UnmarshalJSON(data []byte) (err error) {
if err := json.Unmarshal(data, &polygonType); err != nil {
return err
}
if g.MaxPoint.Lat == 0.0 && g.MaxPoint.Lon == 0.0 {
g.MaxPoint = Point{Lon: -180.0, Lat: -90.0}
}
if g.MinPoint.Lat == 0.0 && g.MinPoint.Lon == 0.0 {
g.MinPoint = Point{Lon: 180.0, Lat: 90.0}
}
switch polygonType.Type {
case "Polygon":
if err := json.Unmarshal(data, &polygon); err != nil {
return err
}
coord := Coordinates{Polygon: make([]Point, len(polygon.Coordinates[0])), MaxPoint: Point{Lon: -180.0,
Lat: -90.0}, MinPoint: Point{Lon: 180.0, Lat: 90.0}}
for i, v := range polygon.Coordinates[0] {
lon := v[0]
lat := v[1]
coord.Polygon[i].Lon = lon
coord.Polygon[i].Lat = lat
updateMaxMin(&coord.MaxPoint, &coord.MinPoint, lat, lon)
updateMaxMin(&g.MaxPoint, &g.MinPoint, lat, lon)
}
g.Coordinates = append(g.Coordinates, coord)
return nil
case "MultiPolygon":
if err := json.Unmarshal(data, &multiPolygon); err != nil {
return err
}
g.Coordinates = make([]Coordinates, len(multiPolygon.Coordinates))
for j, poly := range multiPolygon.Coordinates {
coord := Coordinates{Polygon: make([]Point, len(poly[0])), MaxPoint: Point{Lon: -180.0,
Lat: -90.0}, MinPoint: Point{Lon: 180.0, Lat: 90.0}}
for i, v := range poly[0] {
lon := v[0]
lat := v[1]
coord.Polygon[i].Lon = lon
coord.Polygon[i].Lat = lat
updateMaxMin(&coord.MaxPoint, &coord.MinPoint, lat, lon)
updateMaxMin(&g.MaxPoint, &g.MinPoint, lat, lon)
}
g.Coordinates[j] = coord
}
return nil
default:
return nil
}
}
func updateMaxMin(maxPoint, minPoint *Point, lat, lon float64) {
if maxPoint.Lat < lat {
maxPoint.Lat = lat
}
if maxPoint.Lon < lon {
maxPoint.Lon = lon
}
if minPoint.Lat > lat {
minPoint.Lat = lat
}
if minPoint.Lon > lon {
minPoint.Lon = lon
}
}
func (fc Collection) Location(lat, lon float64) (*time.Location, error) {
if tz := fc.TimeZone(lat, lon); tz != "" {
return time.LoadLocation(tz)
}
return nil, fmt.Errorf("failed to find time zone")
}
// TimeZone Recurse over lower find function for lat lon.
// First shrinking the polygon for search and if we find it return it. If we didn't find it search on full polygon.
func (fc Collection) TimeZone(lat, lon float64) string {
var start, end = 0.001, 0.0001
if result := fc.find(lat, lon, start); result != "" {
return result
}
return fc.find(lat, lon, end)
}
func (fc Collection) find(lat, lon, percentage float64) string {
for _, feat := range fc.Features {
f := feat
properties := f.Properties
if f.Geometry.MinPoint.Lat <= lat &&
f.Geometry.MinPoint.Lon <= lon &&
f.Geometry.MaxPoint.Lat >= lat &&
f.Geometry.MaxPoint.Lon >= lon {
for _, c := range f.Geometry.Coordinates {
coord := c
if coord.MinPoint.Lat <= lat &&
coord.MinPoint.Lon <= lon &&
coord.MaxPoint.Lat >= lat &&
coord.MaxPoint.Lon >= lon {
if coord.contains(Point{lon, lat},
// get a percentage of the polygon, either shrinking it or leaving it alone.
int(math.Max(float64(len(coord.Polygon))*percentage, 1))) {
return properties["tzid"]
}
}
}
}
}
return ""
}
func (c Coordinates) contains(point Point, indexjump int) bool {
var polygon = c.Polygon
if windingNumber(point.Lat, point.Lon, polygon, indexjump) == 0 {
return false
}
return true
}
func windingNumber(lat, lon float64, polygon []Point, indexjump int) int {
if len(polygon) < 3 {
return 0
}
var wn = 0
var edgeCount = len(polygon) - indexjump
for i, j := 0, indexjump; i < edgeCount; i, j = i+indexjump, j+indexjump {
var apLat, apLon, bLat, bLon = polygon[i].Lat, polygon[i].Lon, polygon[j].Lat, polygon[j].Lon
if apLat <= lat {
if bLat > lat {
if isLeft(lat, lon, apLat, apLon, bLat, bLon) > 0 {
wn++
}
}
} else {
if polygon[j].Lat <= lat {
if isLeft(lat, lon, apLat, apLon, bLat, bLon) < 0 {
wn--
}
}
}
}
return wn
}
func isLeft(lat, lon, latA, lonA, latB, lonB float64) float64 {
return (lonB-lonA)*(lat-latA) - (lon-lonA)*(latB-latA)
}