-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenkml.go
183 lines (147 loc) · 3.58 KB
/
genkml.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
package main
import (
"database/sql"
"encoding/json"
"fmt"
"io"
"log"
"os"
"text/template"
_ "github.com/CodeforNRV/safe_walk_blacksburg/Godeps/_workspace/src/github.com/lib/pq"
)
type Coord struct {
Lat, Lng float64
}
type Road struct {
Name string
ColorRank string
Coords []Coord
}
// Reforms the geomerty Json into a []Coord's
func parseCoords(rawGeomJson string) []Coord {
type coord []float64
type geomJson struct {
Coords [][]coord `json:"coordinates"`
}
var geom geomJson
err := json.Unmarshal([]byte(rawGeomJson), &geom)
if err != nil {
log.Fatal(err)
}
var coords []Coord
for _, coord := range geom.Coords[0] {
coords = append(coords, Coord{
Lat: coord[0],
Lng: coord[1],
})
}
return coords
}
const (
Green = "green"
Yellow = "yellow"
Red = "red"
)
// Calculates a safety rating and returns it as a string of "green" or "yellow" or "red"
func calculateSafetyRating(hasSidewalks, hasStreetlights bool, speedlmt float64) string {
var color string
switch {
case hasSidewalks && hasStreetlights:
color = Green
case hasSidewalks && !hasStreetlights:
color = Yellow
case !hasSidewalks && !hasStreetlights:
color = Red
case !hasSidewalks && hasStreetlights:
switch {
case speedlmt < 1:
color = Red
default:
color = Red
case speedlmt <= 25:
color = Yellow
}
default:
color = Red
}
return color
}
func kmlExecute(w io.Writer) error {
host := os.Getenv("NRV_PG_HOST")
user := os.Getenv("NRV_PG_USER")
dbname := os.Getenv("NRV_PG_DBNAME")
dbpass := os.Getenv("NRV_PG_DBPASS")
db, err := sql.Open("postgres", fmt.Sprintf("host=%s user=%s dbname=%s password=%s", host, user, dbname, dbpass))
if err != nil {
return err
}
defer db.Close()
// Grab dataset
// Uses PostGIS to transform the geometry from SRID 2284 ==> SRID 4326
rows, err := db.Query("SELECT name, speedlmt, hassidewalks, hasstreetlights, ST_AsGeoJson(ST_Transform(geom,4326)) FROM roads")
if err != nil {
return err
}
defer rows.Close()
roads := make([]Road, 0, 200)
// Interate over each road
// - Calculating the safety rating as a color
// - Processing the geometry into type []Coord's
for rows.Next() {
var name string
var speedlmt float64
var hasSidewalks, hasStreetlights bool
var rawGeomJson string
err = rows.Scan(&name, &speedlmt, &hasSidewalks, &hasStreetlights, &rawGeomJson)
if err != nil {
return err
}
roads = append(roads, Road{
Name: name,
ColorRank: calculateSafetyRating(hasSidewalks, hasStreetlights, speedlmt),
Coords: parseCoords(rawGeomJson),
})
}
return kmltpl.Execute(w, kml{roads})
}
type kml struct {
Roads []Road
}
var kmltpl = template.Must(template.New("kml").Parse(`<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.1">
<Document>
<name>Safe Walk Blacksburg</name>
<description>Streets of Blacksburg ranked by walking safety</description>
<Style id="greenLine">
<LineStyle>
<color>ff009900</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="yellowLine">
<LineStyle>
<color>ff61f2f2</color>
<width>4</width>
</LineStyle>
</Style>
<Style id="redLine">
<LineStyle>
<color>ff0000ff</color>
<width>4</width>
</LineStyle>
</Style>
{{range .Roads}}
<Placemark>
<name>{{.Name}}</name>
<styleUrl>#{{.ColorRank}}Line</styleUrl>
<LineString>
<altitudeMode>relative</altitudeMode>
<coordinates>
{{range .Coords}}{{.Lat}}, {{.Lng}}, 0
{{end}}</coordinates>
</LineString>
</Placemark>
{{end}}
</Document>
</kml>
`))