-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalueLookupHandler.go
120 lines (105 loc) · 2.89 KB
/
valueLookupHandler.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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"os/exec"
"time"
)
// Response Schemata of gdallocation script
type report struct {
Bands []bandreport `xml:"BandReport"`
}
type bandreport struct {
File string `xml:"LocationInfo>File"`
Value string `xml:"Value"`
}
// LookupHandler handles all Requests for concrete Dataset values
func LookupHandler(w http.ResponseWriter, r *http.Request) {
defer Timetrack(time.Now(), "ValueLookup")
// Get Query Parameters
q := r.URL.Query()
xcoord := q.Get("x")
ycoord := q.Get("y")
datasetname := q.Get("d")
bandname := q.Get("b")
// If Verbose is toggle print parsed output
if Verbose {
fmt.Print("Request to /value with parameters: ")
fmt.Println(q)
}
// local variables
var output []byte
var err error
tci := false
// Check if S2A Dataset
if datasetname[:8] != "SENTINEL" {
// check if TCI Dataset
if bandname[len(bandname)-11:len(bandname)-8] == "TCI" {
tci = true
}
// Get Name of dynamically named subfolder
datalocation := DataSource + datasetname + "/GRANULE/"
subfolder, err := ioutil.ReadDir(datalocation)
if err != nil {
w.WriteHeader(404)
w.Write([]byte("Cannot find Dataset"))
return
}
// Get Resolution
resolution := bandname[len(bandname)-7 : len(bandname)-5]
// Get Pixel Data
datasetlocation := DataSource + datasetname + "/GRANULE/" + subfolder[0].Name() + "/IMG_DATA/R" + resolution + "m/" + bandname
output, err = exec.Command("gdallocationinfo", "-xml", "-wgs84", datasetlocation, xcoord, ycoord).Output()
if err != nil {
fmt.Println(err.Error())
}
} else {
// check if TCI Dataset
if bandname == "TCI" {
tci = true
}
// Get Pixel Data
output, err = exec.Command("gdallocationinfo", "-xml", "-wgs84", datasetname, xcoord, ycoord).Output()
}
// Check for error while executing gdallocationinfo
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Error executing Value Lookup. Error was: " + err.Error()))
}
// Parse Output from xml to Go struct
var v report
err = xml.Unmarshal(output, &v)
if err != nil {
w.WriteHeader(500)
w.Write([]byte("Error parsing xml. Error was: " + err.Error()))
}
// Compute TCI Values into json array
if tci {
w.Write([]byte("[" + v.Bands[0].Value + "," + v.Bands[1].Value + "," + v.Bands[2].Value + "]"))
return
}
// Check if S2A Dataset
if datasetname[:8] == "SENTINEL" {
// Get and Return Value corresponding to provided bandname
for index := range v.Bands {
fmt.Println("")
// Extract Bandname from Filename
band := v.Bands[index].File
band = band[len(band)-7 : len(band)-4]
if bandname == band {
w.Write([]byte(v.Bands[index].Value))
return
}
}
} else {
if v.Bands != nil {
w.Write([]byte(v.Bands[0].Value))
return
}
}
// If Band is not found in Dataset
w.WriteHeader(404)
w.Write(append([]byte("Error while finding Band in Dataset \n Debug output below: \n \n "), output...))
}