-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
71 lines (59 loc) · 1.85 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
package main
import (
"fmt"
"time"
"github.com/airbusgeo/godal"
)
func main() {
t := time.Now()
t1 := t
redfile := "sentinel_example_data/T37UDU_20171111T085159_B08_10m.jp2"
nirfile := "sentinel_example_data/T37UDU_20171111T085159_B04_10m.jp2"
godal.RegisterAll()
ds_red, err := godal.Open(redfile)
if err != nil {
panic(err)
}
defer ds_red.Close()
fmt.Print("Open red channel - ")
red_structure := ds_red.Structure()
red_band := ds_red.Bands()[0]
red_pafScanline := make([]float32, red_structure.SizeX*red_structure.SizeY)
err = red_band.Read(0, 0, red_pafScanline, red_structure.SizeX, red_structure.SizeY)
if err != nil {
panic(err)
}
fmt.Println(time.Since(t1))
t1 = time.Now()
fmt.Print("Open infrared channel - ")
ds_nir, err := godal.Open(nirfile)
if err != nil {
panic(err)
}
defer ds_nir.Close()
nir_structure := ds_nir.Structure()
nir_band := ds_nir.Bands()[0]
nir_pafScanline := make([]float32, nir_structure.SizeX*nir_structure.SizeY)
err = nir_band.Read(0, 0, nir_pafScanline, nir_structure.SizeX, nir_structure.SizeY)
if err != nil {
panic(err)
}
fmt.Println(time.Since(t1))
t1 = time.Now()
fmt.Print("Calculate NDVI - ")
ndvi := make([]float32, nir_structure.SizeX*nir_structure.SizeY)
for i := range red_pafScanline {
ndvi[i] = 100 * (nir_pafScanline[i] - red_pafScanline[i]) / (nir_pafScanline[i] + red_pafScanline[i])
}
fmt.Println(time.Since(t1))
t1 = time.Now()
fmt.Print("Saving GEOTiff - ")
new, err := godal.Create(godal.GTiff, "test1.tiff", 1, godal.Int32, red_structure.SizeX, red_structure.SizeY, godal.CreationOption("COMPRESS=DEFLATE", "TILED=YES"))
gt, _ := ds_red.GeoTransform()
new.SetGeoTransform(gt)
new.SetProjection(ds_red.Projection())
new.Write(0, 0, ndvi, red_structure.SizeX, red_structure.SizeY)
new.Close()
fmt.Println(time.Since(t1))
fmt.Println("NDVI calculated in ", time.Since(t))
}