This repository was archived by the owner on Aug 23, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlocations_handler.go
138 lines (114 loc) · 3.07 KB
/
locations_handler.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
type Location struct {
Id string `json:"id"`
BookingLocationId string `json:"booking_location_id"`
Title string `json:"title"`
Address string `json:"address"`
Phone string `json:"phone"`
Hours string `json:"hours"`
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
}
type FeatureGeometry struct {
Type string `json:"type"`
Coordinates []float64 `json:"coordinates"`
}
type FeatureProperties struct {
Title string `json:"title"`
Address string `json:"address"`
BookingLocationId string `json:"booking_location_id"`
Phone string `json:"phone"`
Hours string `json:"hours"`
}
type Feature struct {
Type string `json:"type"`
Id string `json:"id"`
Geometry FeatureGeometry `json:"geometry"`
Properties FeatureProperties `json:"properties"`
}
type FeatureCollection struct {
Type string `json:"type"`
Features []Feature `json:"features"`
}
var (
geojson []byte
)
func NewFeatureCollection(f []Feature) FeatureCollection {
return FeatureCollection{
Type: "FeatureCollection",
Features: f,
}
}
func NewFeatureGeometry(l Location) FeatureGeometry {
return FeatureGeometry{
Type: "Point",
Coordinates: []float64{l.Lng, l.Lat},
}
}
func NewFeatureProperties(l Location) FeatureProperties {
return FeatureProperties{
Title: l.Title,
Address: l.Address,
BookingLocationId: l.BookingLocationId,
Phone: l.Phone,
Hours: l.Hours,
}
}
func NewFeature(l Location) Feature {
return Feature{
Type: "Feature",
Id: l.Id,
Geometry: NewFeatureGeometry(l),
Properties: NewFeatureProperties(l),
}
}
func loadLocationsJson(path string) []Location {
var locations []Location
data, err := ioutil.ReadFile(path)
if err != nil {
panic(fmt.Sprintf("Can't read %s", path))
}
err = json.Unmarshal(data, &locations)
if err != nil {
panic(fmt.Sprintf("Can't parse %s", path))
}
return locations
}
func loadLocations() (locations []Location) {
for _, filename := range []string{"nicab.json", "cita.json", "cas.json"} {
path := fmt.Sprintf("public/js/%s", filename)
locations = append(locations, loadLocationsJson(path)...)
}
return
}
func buildFeatures(locations []Location) (features []Feature) {
for _, location := range locations {
feature := NewFeature(location)
features = append(features, feature)
}
return
}
func generateGeojson(features []Feature) []byte {
geojson, err := json.Marshal(NewFeatureCollection(features))
if err != nil {
panic("Can't Generate GeoJSON")
}
return geojson
}
func geojsonFromLocations(locations []Location) []byte {
features := buildFeatures(locations)
return generateGeojson(features)
}
func init() {
geojson = geojsonFromLocations(loadLocations())
}
func LocationsHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.Write(geojson)
}