This commit is contained in:
nuknal
2024-08-19 18:05:55 +08:00
parent 85f9f44653
commit ea23839bf2
6 changed files with 116 additions and 1 deletions

70
pkg/dem/dem.go Normal file
View File

@@ -0,0 +1,70 @@
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]
}