-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathosm.go
More file actions
165 lines (88 loc) · 2.79 KB
/
osm.go
File metadata and controls
165 lines (88 loc) · 2.79 KB
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"math"
"net/http"
"strings"
"time"
)
type OverpassResponse struct {
Elements []Element `json:"elements"`
}
type Element struct {
Type string `json:"type"`
ID int64 `json:"id"`
Lat float64 `json:"lat,omitempty"`
Lon float64 `json:"lon,omitempty"`
Nodes []int64 `json:"nodes,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Members []Member `json:"members,omitempty"`
Geometry []Point `json:"geometry,omitempty"`
}
type Member struct {
Type string `json:"type"`
Ref int64 `json:"ref"`
Role string `json:"role"`
Geometry []Point `json:"geometry,omitempty"`
}
type Point struct {
Lat float64 `json:"lat"`
Lon float64 `json:"lon"`
}
func fetchOSMData(lat, lon float64, dist float64) (*OverpassResponse, error) {
const degPerMeter = 1.0 / 111320.0
latDelta := dist * degPerMeter
lonDelta := dist * degPerMeter / math.Cos(lat*math.Pi/180.0)
s, w, n, e := lat-latDelta, lon-lonDelta, lat+latDelta, lon+lonDelta
query := fmt.Sprintf(`
[out:json][timeout:120];
(
way["highway"](%f,%f,%f,%f);
way["natural"="water"](%f,%f,%f,%f);
relation["natural"="water"](%f,%f,%f,%f);
way["waterway"="riverbank"](%f,%f,%f,%f);
way["leisure"="park"](%f,%f,%f,%f);
way["landuse"="grass"](%f,%f,%f,%f);
relation["leisure"="park"](%f,%f,%f,%f);
relation["landuse"="grass"](%f,%f,%f,%f);
);
out body geom;
`, s, w, n, e, s, w, n, e, s, w, n, e, s, w, n, e, s, w, n, e, s, w, n, e, s, w, n, e, s, w, n, e)
var lastErr error
for i := 0; i < 3; i++ {
if i > 0 {
fmt.Printf("⚠ Overpass server busy, retrying in 5 seconds... (Attempt %d/3)\n", i+1)
time.Sleep(5 * time.Second)
}
req, err := http.NewRequest("POST", "https://overpass-api.de/api/interpreter", bytes.NewBufferString(query))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("User-Agent", "CartoGo/1.0")
client := &http.Client{Timeout: 150 * time.Second}
resp, err := client.Do(req)
if err != nil {
lastErr = err
continue
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, _ := io.ReadAll(resp.Body)
lastErr = fmt.Errorf("overpass api error: %s", string(body))
if strings.Contains(string(body), "Dispatcher_Client::request_read_and_idx::timeout") || resp.StatusCode == 429 {
continue // Retry on timeout or rate limit
}
return nil, lastErr
}
var overpassResp OverpassResponse
if err := json.NewDecoder(resp.Body).Decode(&overpassResp); err != nil {
return nil, err
}
return &overpassResp, nil
}
return nil, fmt.Errorf("failed after 3 attempts: %v", lastErr)
}