-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
111 lines (98 loc) · 2.49 KB
/
main.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
package main
import (
"database/sql"
"fmt"
"net/http"
"strconv"
"strings"
_ "github.com/lib/pq"
)
var db *sql.DB
const (
dbUser = "heorhi"
dbName = "openstreetmap"
minLatitude = -90.0
maxLatitude = 90.0
minLongitude = -180.0
maxLongitude = 180.0
errorBboxIsRequired = "The parameter bbox is required, and must be of the form min_lon,min_lat,max_lon,max_lat."
errorWrongLongitudeLatitude = "The latitudes must be between -90 and 90, longitudes between -180 and 180 and the minima must be less than the maxima."
)
func init() {
dbinfo := fmt.Sprintf("user=%s dbname=%s sslmode=disable",
dbUser, dbName)
db, _ = sql.Open("postgres", dbinfo)
}
func main() {
http.HandleFunc("/api/0.6/map", fastmap)
http.ListenAndServe(":3001", nil)
}
func fastmap(w http.ResponseWriter, r *http.Request) {
qp := r.URL.Query()
bbox := qp.Get("bbox")
if len(bbox) == 0 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
cs := strings.Split(bbox, ",")
if len(cs) != 4 {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
minlonString, minlatString, maxlonString, maxlatString := cs[0], cs[1], cs[2], cs[3]
minlon, err := strconv.ParseFloat(minlonString, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
minlat, err := strconv.ParseFloat(minlatString, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
maxlon, err := strconv.ParseFloat(maxlonString, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
maxlat, err := strconv.ParseFloat(maxlatString, 64)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorBboxIsRequired))
return
}
if minlon > maxlon || minlat > maxlat ||
minlon < minLongitude || minlon > maxLongitude ||
maxlon < minLongitude || maxlon > maxLongitude ||
minlat < minLatitude || minlat > minLatitude ||
maxlat < minLatitude || maxlat > minLatitude {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte(errorWrongLongitudeLatitude))
return
}
fastmapQuery := fmt.Sprintf(fastmapQueryf,
minlon,
minlat,
maxlon,
maxlat,
)
rows, err := db.Query(fastmapQuery)
checkErr(err)
defer rows.Close()
w.Header().Set("Content-Type", "application/xml")
for rows.Next() {
node := make([]byte, 0)
rows.Scan(&node)
w.Write(node)
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}