From f23e175e3665d7736b50e7ae37d21ae67d718275 Mon Sep 17 00:00:00 2001 From: Esteban Zimanyi Date: Sat, 21 Nov 2026 18:33:34 +0100 Subject: [PATCH] Write a coverage in netCDF A coverage is negotiated as netCDF through application/x-netcdf, the media type /req/netcdf/coverage of OGC API - Coverages states, and written by MobilityDB's asGDALRaster in GDAL's netCDF format; the collection links its coverage in that encoding beside GeoTIFF and CoverageJSON, and the class netcdf of the Coverages draft is declared. The netCDF document names its dimensions lon and lat, or x and y in a projected system, each with a netCDF standard name, and a coverage of one period has no time dimension, so no dimension falls under the rule naming a variable after the collection's extent. Witness: TestLiveRasterCollection asks the January coverage and its bbox clip as netCDF and reads the answer's media type, the netCDF signature, and the width and height MobilityDB reads back from it. Measured: on MobilityDB master before its writer wrote a format whose library writes only to a file, asGDALRaster(r, 'netCDF') answered no document; with it, the coverage of January reads back as its 4 x 2 grid and the clip as its 2 x 2. --- api/surface.json | 1 + coverage.go | 42 ++++++++++++++++++++++------ coverage_live_test.go | 25 +++++++++++++++++ samples/conformance-declaration.json | 2 ++ 4 files changed, 62 insertions(+), 8 deletions(-) diff --git a/api/surface.json b/api/surface.json index d0f1bde..e6f6895 100644 --- a/api/surface.json +++ b/api/surface.json @@ -104,6 +104,7 @@ "functions": [ "SRID", "asCoverageJSON", + "asGDALRaster", "asTIFF", "clip", "numBands", diff --git a/coverage.go b/coverage.go index cdad5c9..a62fcaf 100644 --- a/coverage.go +++ b/coverage.go @@ -41,6 +41,7 @@ var coverageConformance = []string{ coverageConfBase + "field-selection", coverageConfBase + "geotiff", coverageConfBase + "coveragejson", + coverageConfBase + "netcdf", } // The link relations of a coverage collection and the media types of its @@ -55,16 +56,33 @@ const ( // mediaCovJSONEDR is the spelling of CoverageJSON that EDR 1.1 // (/req/covjson/content) names, the same document as mediaCovJSON mediaCovJSONEDR = "application/prs.coverage+json" + // mediaNetCDF is the media type /req/netcdf/coverage of the Coverages + // draft negotiates a netCDF coverage by + mediaNetCDF = "application/x-netcdf" ) // coverageEncodings are the encodings a coverage is written in, each with the -// SQL function writing it, in the order a client stating no preference gets -// them: GeoTIFF first, the type the collection's coverage link states. -// CoverageJSON is answered in either spelling, the one the client names. -var coverageEncodings = []struct{ media, fn string }{ - {mediaGeoTIFF, "asTIFF"}, - {mediaCovJSON, "asCoverageJSON"}, - {mediaCovJSONEDR, "asCoverageJSON"}, +// SQL function writing it and, for asGDALRaster, the GDAL format it is called +// with, in the order a client stating no preference gets them: GeoTIFF first, +// the type the collection's coverage link states. CoverageJSON is answered in +// either spelling, the one the client names. +var coverageEncodings = []struct{ media, fn, format string }{ + {mediaGeoTIFF, "asTIFF", ""}, + {mediaCovJSON, "asCoverageJSON", ""}, + {mediaCovJSONEDR, "asCoverageJSON", ""}, + {mediaNetCDF, "asGDALRaster", "netCDF"}, +} + +// coverageFormat answers the GDAL format the encoding of a media type calls +// its writer with, as #coverageEncodings states it, "" for a writer taking +// none. +func coverageFormat(media string) string { + for _, e := range coverageEncodings { + if e.media == media { + return e.format + } + } + return "" } // coverageSource is what the registry records of a raster collection: its @@ -226,6 +244,7 @@ func coverageLinks(cid string, inst *coverageInstance) []map[string]string { {"rel": "self", "href": base, "type": "application/json"}, {"rel": relCoverage, "href": base + "/coverage", "type": mediaGeoTIFF}, {"rel": relCoverage, "href": base + "/coverage", "type": mediaCovJSON}, + {"rel": relCoverage, "href": base + "/coverage", "type": mediaNetCDF}, {"rel": relSchema, "href": base + "/schema", "type": mediaSchema}, } if inst != nil { @@ -524,6 +543,7 @@ type coverageRequest struct { bands []int // the bands properties selects, nil for all media string // the media type negotiated writer string // the function writing it + format string // the GDAL format asGDALRaster writes it in, "" for none } // parseBbox reads a bbox of four numbers, minLon,minLat,maxLon,maxLat in @@ -665,8 +685,10 @@ func parseCoverageRequest(r *http.Request, nbands int) (coverageRequest, error) } } cr.media, cr.writer = negotiateCoverage(r.Header.Get("Accept")) + cr.format = coverageFormat(cr.media) if cr.media == "" { - return cr, statusErr{406, "a coverage is written as " + mediaGeoTIFF + " or " + mediaCovJSON} + return cr, statusErr{406, "a coverage is written as " + mediaGeoTIFF + ", " + mediaCovJSON + " or " + + mediaNetCDF} } return cr, nil } @@ -860,6 +882,10 @@ func coverageData(ctx context.Context, src coverageSource, period string, cr cov return nil, nil, 0, err } write := cr.writer + "(c)" + if cr.format != "" { + // The format is a name of #coverageEncodings, never a request's + write = cr.writer + "(c, '" + cr.format + "')" + } if cr.writer == "asCoverageJSON" { write = "CAST(" + write + " AS bytea)" } diff --git a/coverage_live_test.go b/coverage_live_test.go index 448b2ad..115b998 100644 --- a/coverage_live_test.go +++ b/coverage_live_test.go @@ -226,6 +226,31 @@ func TestLiveRasterCollection(t *testing.T) { t.Errorf("the January coverage is not a 4 x 2 grid: %s", rec.Body.String()) } + // The same coverage negotiated as netCDF, as /req/netcdf/coverage of the + // draft states: a netCDF document, read back by MobilityDB as the 4 x 2 + // raster of January, and a bbox clipping it as it clips the others. + for _, c := range []struct { + path string + width, height int + }{ + {"/collections/cov/coverage?datetime=2024-01-15T00:00:00Z", 4, 2}, + {"/collections/cov/coverage?datetime=2024-01-15T00:00:00Z&bbox=0,0,2,2", 2, 2}, + } { + rec = coverageGet(t, c.path, mediaNetCDF) + if rec.Code != 200 || rec.Header().Get("Content-Type") != mediaNetCDF || + !strings.HasPrefix(rec.Body.String(), "CDF") { + t.Errorf("%s as netCDF answered %d as %q: %.40q", c.path, rec.Code, rec.Header().Get("Content-Type"), + rec.Body.String()) + continue + } + var w, h int + if err := db.QueryRow(context.Background(), "SELECT width(r), height(r) FROM (SELECT "+ + "rasterFromGDALRaster($1) r) s", rec.Body.Bytes()).Scan(&w, &h); err != nil || + w != c.width || h != c.height { + t.Errorf("%s as netCDF reads back as %d x %d (%v), want %d x %d", c.path, w, h, err, c.width, c.height) + } + } + // A bbox over the first tile answers that tile's bounds. rec = coverageGet(t, "/collections/cov/coverage?datetime=2024-01-15T00:00:00Z&bbox=0,0,2,2", mediaCovJSON) if rec.Code != 200 || rec.Header().Get("Content-Bbox") != "0,0,2,2" { diff --git a/samples/conformance-declaration.json b/samples/conformance-declaration.json index ff16204..aa40c44 100644 --- a/samples/conformance-declaration.json +++ b/samples/conformance-declaration.json @@ -20,6 +20,8 @@ "http://www.opengis.net/spec/ogcapi-coverages-1/1.0/conf/geotiff", "https://www.opengis.net/spec/ogcapi-coverages-1/1.0/conf/coveragejson", "http://www.opengis.net/spec/ogcapi-coverages-1/1.0/conf/coveragejson", + "https://www.opengis.net/spec/ogcapi-coverages-1/1.0/conf/netcdf", + "http://www.opengis.net/spec/ogcapi-coverages-1/1.0/conf/netcdf", "http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/core", "http://www.opengis.net/spec/ogcapi-edr-1/1.1/conf/covjson" ]