forked from jda/srtm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geojson.go
120 lines (114 loc) · 2.78 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
package srtm
import (
geojson "github.com/paulmach/go.geojson"
"github.com/rs/zerolog/log"
"runtime"
"sync"
"time"
)
// AddElevation returns point with 3 coordinates: [longitude, latitude, elevation]
// Param tileDir - directory of hgt-tiles
// Param point - [longitude, latitude]
func (d *SRTM) AddElevation(point []float64) ([]float64, error) {
ll := LatLng{
Latitude: point[1],
Longitude: point[0],
}
tile, err := d.loadTile(ll)
if err != nil {
log.Error().Caller().Err(err).Msgf("loadTile: latLng = %s -> error %s", ll.String(), err.Error())
return nil, err
}
tile.setLRU(time.Now())
elevation, err := tile.GetElevation(ll)
if err != nil {
log.Error().Caller().Err(err).Msgf("GetElevation: latLng = %s -> error %s", ll.String(), err.Error())
return nil, err
}
return append(point[:2], float64(elevation)), nil
}
// AddElevations returns geojson with added third coordinate (elevation)
// Param tileDir - directory of hgt-tiles
// Param geoJson - geojson for processing
// Param skipErrors - if false AddElevations use premature exit (on first bad point in geojson). if true all points will be process but bad point will not to be contains elevation coordinate
func (d *SRTM) AddElevations(geoJson *geojson.Geometry, skipErrors bool) error {
switch geoJson.Type {
case geojson.GeometryPoint:
point, err := d.AddElevation(geoJson.Point)
if err != nil && !skipErrors {
return err
}
geoJson.Point = point
return nil
case geojson.GeometryLineString:
d.process2(geoJson.LineString, runtime.NumCPU())
return nil
case geojson.GeometryMultiPoint:
d.process2(geoJson.MultiPoint, runtime.NumCPU())
return nil
case geojson.GeometryPolygon:
d.process3(geoJson.Polygon, runtime.NumCPU())
return nil
case geojson.GeometryMultiLineString:
d.process3(geoJson.MultiLineString, runtime.NumCPU())
return nil
default:
return nil
}
}
func (d *SRTM) process3(slice [][][]float64, n int) {
wg := sync.WaitGroup{}
wg.Add(n+1)
type p struct {
i int
j int
}
ch := make(chan p, n)
go func() {
for i := range slice {
for j := range slice[i] {
ch <- p{i, j}
}
}
close(ch)
wg.Done()
}()
for i := 0; i < n; i++ {
go func() {
for p := range ch {
point, err := d.AddElevation(slice[p.i][p.j])
if err != nil {
log.Error().Caller().Err(err).Msg("")
}
slice[p.i][p.j] = point
}
wg.Done()
}()
}
wg.Wait()
}
func (d *SRTM) process2(slice [][]float64, n int) {
wg := sync.WaitGroup{}
wg.Add(n+1)
ch := make(chan int, n)
go func() {
for i := range slice {
ch <- i
}
close(ch)
wg.Done()
}()
for i := 0; i < n; i++ {
go func() {
for i := range ch {
point, err := d.AddElevation(slice[i])
if err != nil {
log.Error().Caller().Err(err).Msg("")
}
slice[i] = point
}
wg.Done()
}()
}
wg.Wait()
}