71 lines
1.2 KiB
Go
71 lines
1.2 KiB
Go
package dem
|
|
|
|
import (
|
|
"math"
|
|
|
|
"github.com/airbusgeo/godal"
|
|
log "github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type Dem1Km struct {
|
|
File string
|
|
LT [][]float32
|
|
width, height int
|
|
wUnit float32 // degree per pixel
|
|
hUnit float32 // degree per pixel
|
|
}
|
|
|
|
var Dem1KmLT *Dem1Km
|
|
|
|
func NewDem1Km(file string) *Dem1Km {
|
|
d := &Dem1Km{
|
|
File: file,
|
|
}
|
|
|
|
if err := d.Load(); err != nil {
|
|
log.Error("cannot load DEM file", err)
|
|
}
|
|
return d
|
|
}
|
|
|
|
func (d *Dem1Km) Load() error {
|
|
hDataset, err := godal.Open(d.File)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
band := hDataset.Bands()[0]
|
|
|
|
structure := band.Structure()
|
|
|
|
for y := 1; y <= structure.SizeY; y++ {
|
|
scanline := make([]float32, structure.SizeX)
|
|
err = band.Read(0, y, scanline, structure.SizeX, 1)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
d.LT = append(d.LT, scanline)
|
|
}
|
|
|
|
d.wUnit = 360.0 / float32(structure.SizeX)
|
|
d.hUnit = 180.0 / float32(structure.SizeY)
|
|
d.width = structure.SizeX
|
|
d.height = structure.SizeY
|
|
|
|
return nil
|
|
}
|
|
|
|
func (d *Dem1Km) Elevation(lng, lat float64) float32 {
|
|
lng = math.Abs(lng + 180.0)
|
|
lat = math.Abs(lat - 90.0)
|
|
w := int(lng / float64(d.wUnit))
|
|
h := int(lat / float64(d.hUnit))
|
|
|
|
if w > d.width-1 || h > d.height-1 {
|
|
return 0.0
|
|
}
|
|
|
|
return d.LT[h][w]
|
|
|
|
}
|