Repository navigation
Description
Before you start
- I searched the existing issues and the roadmap and did not find a duplicate request.
What problem does this solve?
layer.addUrl with "bytes" (#2428, #2669) downloads one whole GRIB record. A plugin that wants a forecast for one Brazilian state still downloads and holds the whole globe:
- The source: the BrasilData plugin's BAM forecast (CPTEC/INPE) reads one record every 6 hours. Each is a global 2000 × 1000 grid with simple packing at 16 bits a value: 4,000,084 bytes.
- Ten days: 40 records, 160 MB downloaded, and 40 global layers of 2 million
f32cells (about 320 MB) in the project until the plugin crops and sums them. The plugin crops each one withraster.project. - Not on the server: CPTEC publishes no regional cut. Its only folder for BAM is the raw global output.
With simple packing and no bitmap, value k starts at bit k × bits-per-value of the data section, so each row of the grid is one contiguous run of bytes. I measured this on a BAM rain record (GPOSNMC20261126002026112606P.fct.TQ0666L064.grb, the prec range from its .inv):
| Header (sections 0–3 plus the start of section 4) | One row | Rows of the area with 0.4° of margin | Bytes of the area | |
|---|---|---|---|---|
| Viçosa (MG) | 79 B | 4,000 B | 9 | 36 kB (0.9%) |
| Minas Gerais | 79 B | 4,000 B | 55 | 220 kB (5.5%) |
| Brazil | 79 B | 4,000 B | 223 | 892 kB (22%) |
Proposed solution
layer.addUrl with "bytes" and "bbox": [west, south, east, north] reads only the rows of the box from a GRIB record.
bbox already means this for a cloud-optimised GeoTIFF: a window as a raster layer, in degrees, or "view". This adds the same key for GRIB, with the same meaning.
- Reads, in order:
- The first 4 KB of the range, or more when the section lengths say so: sections 0 to 3 and the start of the data section, which give the grid, the scanning mode, the packing and the bits per value. GRIB2's sections 1 to 5 come the same way.
- The bitmap section, when there is one, whole. It is one bit a point, 1/16 of a 16-bit field. Counting its set bits gives the value index where each row starts.
- The bytes of the box's rows in the data section: one
Rangerequest for the run from the first row to the last.
- Laid out like the whole record: the rows are decoded, cut to the box's columns and laid out north up, with the same band name and geotransform. A global grid read from −180° keeps that layout, so a box across the antimeridian works too, since full rows are read.
- Which records:
- Windowed: a single field on a regular latitude/longitude grid. GRIB1 needs grid point data with simple packing; GRIB2 needs template 5.0 with grid template 3.0, in either case with i consecutive, including alternating rows.
- Read whole, and said: complex packing (5.2, 5.3), JPEG 2000, PNG, CCSDS, j-consecutive scanning and ranges that hold several messages. The notice names the reason: "complex packing (template 5.3) can't be read row by row: the whole record was downloaded (4.0 MB)". Rows can't be located without decoding those packings, so this falls back instead of refusing. GFS, MERGE and ECMWF open data use these packings; BAM and others with simple packing gain the most.
- Notice: "read 55 of 1,000 rows (220 kB of 4.0 MB)".
- A plugin's download: the request is known by its address with the range and the box marked in it. The match builds on fix(plugins): a GRIB byte range a plugin command asked for lands for it (#2428) #3003, so a plugin command runs again when its rows land.
- The way back: the layer's source keeps the address, the range and the box, so a saved project reopens the same window.
- Where: desktop (off the frame, through the existing download path, with progress and Cancel) and the command line. In the browser,
bytesis still refused ([Feature]: Read GRIB without GDAL, and a byte range of a remote GRIB file (layer.addUrl "bytes") #2428 part 3).
Choices in this issue:
- The key: reuse
bbox, as for COG windows, rather than a new key. A plugin already knowsbbox, and"view"works the same. - The fallback: read the whole record with a notice, rather than refuse. A plugin can then ask for a box on any GRIB record without first knowing the packing.
- The data request: fetch the rows as one range, not one request per row. BAM's rows are contiguous: 55 rows are one 220 kB request.
- The bitmap: read it whole when present, rather than read a part of it. It is small next to the field, and locating a row in it needs all the bits before that row anyway.
What BrasilData then does: the same layer.addUrl as today, plus "bbox": [area west − 0.4, south − 0.4, east + 0.4, north + 0.4], with no raster.project crop per record. Ten days over Minas Gerais drop from 160 MB to about 9 MB.
Crates: data (decoding rows from a header, a bitmap and a run of the data section) and engine (the requests in layer.addUrl and the marked address). No new dependency.
Testing:
- In
grib_tests.rs: a record built in the test, read as rows of a box against the whole record read then cropped. Cases:- GRIB1 and GRIB2;
- 8, 12 and 16 bits (rows that start mid-byte);
- with and without a bitmap;
- alternating rows;
- a global grid and a box across the antimeridian;
- the first and the last rows;
- a box outside the grid (an error that names it);
- each fallback with its notice.
- In
engine: the three requests against a local server, a short answer, and a plugin command'slayer.addUrlwithbytesandbbox. - Checked by hand: BAM's rain over Minas Gerais read by rows against the whole record cropped, cell by cell.
Estimated scope
Medium: the decoding is a small step from the current reader. The requests and the match of a plugin's download take more care.
Alternatives considered
- Leave it to the plugin: the plugin crops each whole record, as BrasilData does today. This works, but it downloads and holds 4 to 110 times more than the area needs: Brazil, Minas Gerais and Viçosa above.
- Binary
host.fetchwith aRange: each plugin would decode GRIB itself, andhost.fetchis text of at most 2 MB. - terrio's GRIB driver (opengeos/terrio), which reads remote GRIB by range. RustGIS doesn't depend on it yet ([Feature]: Native NetCDF-4/HDF5 reading, without GDAL and in the browser: how would you like it architected? #396), as in [Feature]: Read GRIB without GDAL, and a byte range of a remote GRIB file (layer.addUrl "bytes") #2428. When it does,
bboxonbytesstays as it is.
Activity
feat(data): layer.addUrl reads only the rows of a box from a GRIB rec…
fix(data): first review of the GRIB rows in the app (opengeos#3055, p…
feat(data): read the rows of a GRIB record's box off the frame in the…