-
Notifications
You must be signed in to change notification settings - Fork 1
/
globe-plotter.go
316 lines (245 loc) · 6.84 KB
/
globe-plotter.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"encoding/csv"
"encoding/json"
"image/color"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"strings"
"time"
globe "github.com/mmcloughlin/globe"
geojson "github.com/paulmach/go.geojson"
)
func main() {
port := getPort()
fs := http.FileServer(http.Dir("./static/"))
http.Handle("/", fs)
http.HandleFunc("/upload", uploadHandler)
http.ListenAndServe(port, nil)
}
func getPort() string {
var port string
if os.Getenv("PORT") != "" {
port = ":" + os.Getenv("PORT")
} else {
port = ":8080"
}
return port
}
//var templates = template.Must(template.ParseFiles("static/index.html"))
func display(w http.ResponseWriter, tmpl string, data interface{}) {
}
func createImage(filename string, uploadPath string, rgbaColors rgba, longitude float64, latitude float64, fileType string) string {
g := globe.New()
alpha := uint8(rgbaColors.A * 255)
color := color.NRGBA{rgbaColors.R, rgbaColors.G, rgbaColors.B, alpha}
g.DrawGraticule(20.0)
g.DrawLandBoundaries()
//g.DrawCountryBoundaries()
g.CenterOn(latitude, longitude)
log.Println(fileType)
if fileType == "geojson" {
drawFromGeoJSON(uploadPath, g, color)
} else if fileType == "csv" {
drawFromCSV(uploadPath, g, color)
}
pngPath := "./static/generated/" + filename + ".png"
err := g.SavePNG(pngPath, 800)
if err != nil {
log.Fatal(err)
}
return pngPath
}
// Delete a file some period of time in the future
func deleteFile(path string, seconds int) {
wait := time.Second * 20
timeout := make(chan error, 1)
go func() {
time.Sleep(wait)
var err = os.Remove(path)
timeout <- err
}()
select {
case err := <-timeout:
if err != nil {
log.Println("Error deleting file", err)
} else {
log.Println("File deleted!")
}
}
}
// For packing values into
type rgba struct {
R uint8 `json:"r"`
G uint8 `json:"g"`
B uint8 `json:"b"`
A float64 `json:"a"`
}
// Return the RGBA color as a nice struct
func getRgbaColor(rgbaStr string) rgba {
in := []byte(rgbaStr)
var raw rgba
err := json.Unmarshal(in, &raw)
if err != nil {
log.Println(err)
}
return raw
}
//This is where the action happens.
func uploadHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
//POST takes the uploaded file(s) and saves it to disk.
case "POST":
//parse the multipart form in the request
err := r.ParseMultipartForm(100000)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
uuid := r.FormValue("uuid")
rgbaValue := r.FormValue("rgba")
rgbaColors := getRgbaColor(rgbaValue)
latitude, err := strconv.ParseFloat(r.FormValue("latitude"), 64)
if err != nil {
latitude = 0.0
}
longitude, err := strconv.ParseFloat(r.FormValue("longitude"), 64)
if err != nil {
longitude = 0.0
}
log.Println("Colors:", rgbaColors)
log.Println("Latitude:", latitude)
log.Println("Longitude:", longitude)
// The reference to the form
m := r.MultipartForm
// Get the *fileheaders
files := m.File["geojson"]
fileUploaded := files[0]
isGeoJSON := strings.Contains(fileUploaded.Filename, "geojson")
isCSV := strings.Contains(fileUploaded.Filename, "csv")
fileType := ""
if isGeoJSON {
fileType = "geojson"
} else if isCSV {
fileType = "csv"
}
if fileType != "" {
var uploadPath string
log.Println("Upload file type is ", fileType)
// For each fileheader, get a handle to the actual file
file, err := fileUploaded.Open()
log.Println(file)
defer file.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Create destination file making sure the path is writeable.
uploadPath = "./upload/" + uuid + "_" + fileUploaded.Filename
dst, err := os.Create(uploadPath)
defer dst.Close()
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Copy the uploaded file to the destination file
if _, err := io.Copy(dst, file); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
log.Println("Upload successful: " + uploadPath)
// Create the image
pngPath := createImage(uuid, uploadPath, rgbaColors, longitude, latitude, fileType)
// Tidy up files
go deleteFile(uploadPath, 1)
go deleteFile(pngPath, 30)
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
}
}
func loadFeatureCollection(inputFilepath string) (*geojson.FeatureCollection, error) {
b, err := ioutil.ReadFile(inputFilepath)
if err != nil {
return nil, err
}
return geojson.UnmarshalFeatureCollection(b)
}
func drawDot(g *globe.Globe, globeColor color.NRGBA, latitude float64, longitude float64) {
size := 0.05
g.DrawDot(latitude, longitude, size, globe.Color(globeColor))
}
func drawFromGeoJSON(filePath string, g *globe.Globe, globeColor color.NRGBA) {
geojson, err := loadFeatureCollection(filePath)
if err != nil {
log.Fatal(err)
}
for _, geometries := range geojson.Features {
if geometries.Geometry.IsPoint() {
coords := geometries.Geometry.Point
// Lat, Lng, size, color
drawDot(g, globeColor, coords[0], coords[1])
}
}
}
func drawFromCSV(filePath string, g *globe.Globe, globeColor color.NRGBA) {
log.Println("drawFromCSV called")
file, err := os.Open(filePath)
if err != nil {
log.Println("Error:", err)
return
}
log.Println("csv file opened called")
// automatically call Close() at the end of current method
defer file.Close()
reader := csv.NewReader(file)
reader.Comma = ';'
lineCount := 0
latitude := -1
longitude := -1
for {
// read just one record, but we could ReadAll() as well
record, err := reader.Read()
//log.Println("Read CSV")
// end-of-file is fitted into err
if err == io.EOF {
break
} else if err != nil {
log.Println("Error:", err)
return
}
// record is an array of string so is directly printable
//fmt.Println("Record", lineCount, "is", record, "and has", len(record), "fields")
for i := 0; i < len(record); i++ {
if lineCount == 0 {
fieldNames := strings.ToLower(record[i])
fieldNamesSlice := strings.Split(fieldNames, ",")
for index, fieldName := range fieldNamesSlice {
if fieldName == "latitude" || fieldName == "lat" {
latitude = index
}
if fieldName == "longitude" || fieldName == "lon" || fieldName == "lng" {
longitude = index
}
}
}
if lineCount > 0 && latitude != -1 && longitude != -1 {
fieldNames := strings.ToLower(record[i])
fieldSlice := strings.Split(fieldNames, ",")
latitudeVal, latErr := strconv.ParseFloat(fieldSlice[latitude], 64)
longitudeVal, lonErr := strconv.ParseFloat(fieldSlice[longitude], 64)
if latErr == nil && lonErr == nil {
log.Println("Drawing CSV Dot", latitudeVal, longitudeVal)
drawDot(g, globeColor, latitudeVal, longitudeVal)
} else {
log.Println("Error with latitude or longitude", lonErr, latErr)
}
}
}
lineCount++
}
}