使用 gdalwarp 生产 L2

This commit is contained in:
nuknal
2024-09-25 11:48:18 +08:00
parent 97a375996a
commit 2c3c58f3fc
3 changed files with 137 additions and 0 deletions

75
pkg/producer/warp.go Normal file
View File

@@ -0,0 +1,75 @@
package producer
import (
"math"
"os"
"os/exec"
"path/filepath"
"strconv"
"strings"
log "github.com/sirupsen/logrus"
"starwiz.cn/sjy01/image-proc/pkg/dem"
)
// gdalwarp -rpc -to "RPC_HEIGHT=2375" in.tif out.tif
// gdalwarp -rpc -to "RPC_DEM=/path/to/dem/gdlebm.tif" in.tif out.tif
func Warp(in, out, meta, rpb, demtif string) error {
meta, _ = filepath.Abs(meta)
m, err := ParseProductMeta(meta)
if err != nil {
return err
}
in, _ = filepath.Abs(in)
out, _ = filepath.Abs(out)
log.Infof("Warping %s to %s", in, out)
minEv, maxEv := dem.Dem1KmLT.MinMaxElevationInRect(
computeExtend(&m.Corners),
)
log.Infof("DEM min/max elevation: %f/%f", minEv, maxEv)
var cmd *exec.Cmd
if demtif == "" {
cmd = exec.Command("gdalwarp", "-rpc", "-to", "RPC_HEIGHT="+strconv.Itoa(int(maxEv)), in, out)
} else {
cmd = exec.Command("gdalwarp", "-rpc", "-to", "RPC_DEM="+demtif, in, out)
}
dir := filepath.Dir(out)
os.MkdirAll(dir, 0755)
err = cmd.Run()
if err != nil {
return err
}
sensor := "MSS"
if strings.Contains(m.ProductID, "PAN") {
sensor = "PAN"
}
GTiffToJPG(out, strings.Replace(out, ".tiff", ".jpg", 1), sensor, false)
return nil
}
func computeExtend(corners *Corners) (lng1, lat1, lng2, lat2 float64) {
lng1 = math.Min(corners.UpperLeft.Longitude, corners.LowerLeft.Longitude)
lng1 = math.Min(lng1, corners.UpperRight.Longitude)
lng1 = math.Min(lng1, corners.LowerRight.Longitude)
lat1 = math.Max(corners.UpperLeft.Latitude, corners.LowerLeft.Latitude)
lat1 = math.Max(lat1, corners.UpperRight.Latitude)
lat1 = math.Max(lat1, corners.LowerRight.Latitude)
lng2 = math.Max(corners.UpperLeft.Longitude, corners.LowerLeft.Longitude)
lng2 = math.Max(lng2, corners.UpperRight.Longitude)
lng2 = math.Max(lng2, corners.LowerRight.Longitude)
lat2 = math.Min(corners.UpperLeft.Latitude, corners.LowerLeft.Latitude)
lat2 = math.Min(lat2, corners.UpperRight.Latitude)
lat2 = math.Min(lat2, corners.LowerRight.Latitude)
return
}