Reputation: 3
I downloaded the terrain data from the site (example https://drive.google.com/file/d/1OiFy-vLq9CvVeqNg9VogmxSPOMcpcD1W/view?usp=sharing ) and got the data. Specifically interested in the format *_DCM.tif, which, as I understand it, stores data on point - longitude, latitude and altitude.
With gdal I can convert a file to .XYZ file and take the data, but the file size becomes too large.
In this regard, 2 questions:
Which libraries have I already reviewed:
https://github.com/chai2010/tiff/tree/master/examples
https://github.com/lukeroth/gdal/blob/master/examples/tiff/tiff.go
https://github.com/airbusgeo/godal/blob/main/doc_test.go
https://pkg.go.dev/github.com/rwcarlsen/goexif/exif
https://pkg.go.dev/github.com/evanoberholster/imagemeta
UPDATE:
I found the gdallocationinfo method. For example, I have converted all tiff files to VRT and am working with it: gdallocationinfo -xml -wgs84 dsm-mosaic.vrt 28.000139 50.999861
I need "Value" - altitude
But I couldn't find an implementation on Golang in any way. I also tried to rewrite the code in Python, but without success.
I was told that calling a console command from code is a bad idea.
If anyone knows how to get information on latitude and longitude, then I will be gdal :)
Upvotes: 0
Views: 1295
Reputation: 7988
The tiff package of the standard library can't handle the encoding used, it says that the SampleFormat that was uses is not supported: tiff: unsupported feature: sample format
.
The github.com/chai2010/tiff library does the job. The example below decodes the image so we can loop over all of its pixels. The desired format uses GPS coordinates, so we have to do some conversion work, I hardcoded the offsets to keep the example concise, parsing them dynamically from the file name should not be difficult.
I am not 100% sure about the float64(x) / float64(bounds.Max.X))
part, since the values don't seem to match the screenshot you sent. Could be that some other scaling values should be used depending on the grid size, but I figure you can fix that yourself.
package main
import (
"bufio"
"image/color"
"os"
"strconv"
"github.com/chai2010/tiff"
)
func main() {
input, err := os.Open("N050E028_DSM.tif")
if err != nil {
panic(err)
}
// TODO: parse the offsets from the file name
latOffset := float64(50)
lngOffset := float64(28)
output, err := os.Create("N050E028_DSM.xyz")
if err != nil {
panic(err)
}
outputWriter := bufio.NewWriter(output)
img, err := tiff.Decode(bufio.NewReader(input))
if err != nil {
panic(err)
}
bounds := img.Bounds()
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
for x := bounds.Min.X; x < bounds.Max.X; x++ {
// Normalize X and Y to a 0 to 1 space based on the size of the image.
// Add the offsets to get coordinates.
lng := lngOffset + (float64(x) / float64(bounds.Max.X))
lat := latOffset + (float64(y) / float64(bounds.Max.Y))
height := img.At(x, y).(color.Gray16)
outputWriter.WriteString(strconv.FormatFloat(lng, 'f', 16, 64))
outputWriter.WriteString(" ")
outputWriter.WriteString(strconv.FormatFloat(lat, 'f', 16, 64))
outputWriter.WriteString(" ")
outputWriter.WriteString(strconv.Itoa(int(height.Y)))
outputWriter.WriteString("\n")
}
}
}
Upvotes: 1