Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stacking multiple tensors #534

Open
ashnair1 opened this issue Aug 17, 2022 · 0 comments
Open

Stacking multiple tensors #534

ashnair1 opened this issue Aug 17, 2022 · 0 comments

Comments

@ashnair1
Copy link

Hi all,

New to golang and wanted to a get a feel for the language. My goal is read a geospatial raster (using godal) as a multi dimensional tensor via gorgonia/tensor . I was able to read the raster and convert it into a list of t.Dense but I'm kinda stuck at how to merge them together. Any suggestions?

package main

import (
	"fmt"

	"github.com/airbusgeo/godal"
	t "gorgonia.org/tensor"
)

func main() {

	godal.RegisterAll()
	hDataset, err := godal.Open("data/LT5_19980329_sub.tif")
	if err != nil {
		panic(err)
	}
	structure := hDataset.Structure()
	fmt.Printf("Size is %dx%dx%d\n", structure.SizeX, structure.SizeY, structure.NBands)

	bands := hDataset.Bands()
	count := len(bands)
	fmt.Printf("Number of Bands: %d\n", count)

	bandArrays := make([]*t.Dense, 0)

	for i := range bands {
		band := bands[i]
		buf := make([]int16, structure.SizeX*structure.SizeY)
		band.Read(0, 0, buf, structure.SizeY, structure.SizeX)
		bandArray := t.New(t.WithShape(structure.SizeY, structure.SizeX, 1), t.WithBacking(buf))
		bandArrays = append(bandArrays, bandArray)
	}

	fmt.Println("DONE!")

}

Would also appreciate any tips to improve this snippet if it is suboptimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant