package main
import (
"context"
"log"
"log/slog"
"time"
"github.com/paulmach/orb"
"github.com/paulmach/orb/encoding/wkt"
"github.com/tilebox/tilebox-go/datasets/v1"
"github.com/tilebox/tilebox-go/query"
)
func main() {
ctx := context.Background()
client := datasets.NewClient()
// select a dataset
dataset, err := client.Datasets.Get(ctx, "open_data.copernicus.sentinel2_msi")
if err != nil {
log.Fatalf("Failed to get dataset: %v", err)
}
// select a collection
collection, err := client.Collections.Get(ctx, dataset.ID, "S2A_S2MSI1C")
if err != nil {
log.Fatalf("Failed to get collection: %v", err)
}
// load data from a collection in a given time range and spatial extent
colorado := orb.Polygon{
{{-109.05, 41.00}, {-109.045, 37.0}, {-102.05, 37.0}, {-102.05, 41.00}, {-109.05, 41.00}},
}
startDate := time.Date(2025, time.March, 1, 0, 0, 0, 0, time.UTC)
endDate := time.Date(2025, time.April, 1, 0, 0, 0, 0, time.UTC)
march2025 := query.NewTimeInterval(startDate, endDate)
// You have to use tilebox-generate to generate the dataset type
var datapointsOverColorado []*v1.Sentinel2Msi
err = client.Datapoints.QueryInto(ctx,
dataset.ID,
&datapointsOverColorado,
datasets.WithCollectionIDs(collection.ID),
datasets.WithTemporalExtent(march2025),
datasets.WithSpatialExtent(colorado),
)
if err != nil {
log.Fatalf("Failed to query datapoints: %v", err)
}
slog.Info("Found datapoints over Colorado in March 2025", slog.Int("count", len(datapointsOverColorado)))
slog.Info("First datapoint over Colorado",
slog.String("id", datapointsOverColorado[0].GetId().AsUUID().String()),
slog.Time("event time", datapointsOverColorado[0].GetTime().AsTime()),
slog.Time("ingestion time", datapointsOverColorado[0].GetIngestionTime().AsTime()),
slog.String("geometry", wkt.MarshalString(datapointsOverColorado[0].GetGeometry().AsGeometry())),
slog.String("granule name", datapointsOverColorado[0].GetGranuleName()),
slog.String("processing level", datapointsOverColorado[0].GetProcessingLevel().String()),
slog.String("product type", datapointsOverColorado[0].GetProductType()),
// and so on...
)
}