ascii-grid: beta
Identify, Read, and Write an ARC/INFO ASCII Grid
motivation
I do a lot of client-side geoprocessing (see geoblaze) and wanted to add support for .asc files.
When I encountered large .asc files, I quickly ran out of memory because I was trying to load the whole file into memory.
This package was created to make it easy to read specific areas of an ASCII Grid in a memory-safe way and prevent my laptop from overheating.
install
npm install ascii-grid
docs
https://ascii-grid.github.io/ascii-grid/
usage
identifying ascii grid files
isAsciiGrid identifies ASCII GRID files in the following formats: ArrayBuffer, Buffer, DataView, Promise, String, and Uint8Array
const isAsciiGrid = require("ascii-grid/src/is-ascii-grid");
const buffer = readFileSync('./test_data/michigan_lld/michigan_lld.asc');
isAsciiGrid({ data: buffer, debug: false });
parsing ascii grid metadata
const parseAsciiGridMeta = require("ascii-grid/src/parse-ascii-grid-meta");
const buffer = readFileSync('./test_data/michigan_lld/michigan_lld.asc');
const metadata = parseAsciiGridMeta({
data: buffer,
debug: false,
cache: true,
raw: false
});
reading pixel values
const parseAsciiGridData = require("ascii-grid/src/parse-ascii-grid-data");
const result = await parseAsciiGridData({
data: buffer,
debug: true,
cache: true,
meta
});
reading pixel values within a bounding box
You can specify a bounding box to read from by specifying the zero-based index
values of the first and last row, and first and last column for each row
const parseAsciiGridData = require("ascii-grid/src/parse-ascii-grid-data");
const result = await parseAsciiGridData({
data: buffer,
debug: true,
start_column: 2,
end_column: 10,
start_row: 1,
end_row: undefined
});
reading pixel values into a flat array
Sometimes you may require the data to be returned in a one-dimensional flat array
instead of split up into rows. To do so, set flat to true like below
const parseAsciiGridData = require("ascii-grid/src/parse-ascii-grid-data");
const result = await parseAsciiGridData({
data: buffer,
flat: true
});
streaming grid points
If you don't want to save a large array of all the grid points,
but rather iterate over the points with a callback, see below:
const forEachAsciiGridPoint = require("ascii-grid/src/for-each-ascii-grid-point");
forEachAsciiGridPoint({
data: buffer,
callback: ({ c, r, num }) => {
console.log("row index is", r);
console.log("column index is", c);
console.log("value is", num);
}
});
calculating statistics
You can calculate statistics for the ASCII grid. Calculations are made by iterating
over the grid points in a memory-aware way, avoiding loading the whole grid into memory.
It uses calc-stats for the calculations.
const calcAsciiGridStats = require("ascii-grid/calc-ascii-grid-stats");
const stats = calcAsciiGridStats({ data: buffer });
writing an ASCII Grid
const writeAsciiGrid = require("ascii-grid/src/write-ascii-grid");
const result = writeAsciiGrid({
data: [
[123, 456, ...],
[789, 1011, ...],
],
ncols: 4201,
nrows: 5365,
xllcenter: -88,
yllcenter: 41.62,
cellsize: 0.0008333333333,
strict: false,
debug_level: 2,
trailing_newline: false,
fixed_digits: 6,
nodata_value: -9999
});
calculating the bounding box
You can calculate the bounding box of the ASCII Grid using floating-point arithmetic.
import calcAsciiGridBoundingBox from "ascii-grid/src/calc-ascii-grid-bounding-box";
calcAsciiGridBoundingBox({
data,
meta,
max_read_length
});
[491501, 2556440, 594634.0933000001, 2645315.3392]
calculating the precise bounding box
If precision is more important than speed, you can calculate the precise bounding box
of the the ASCII Grid avoiding floating-point arithmetic errors.
import calcAsciiGridPreciseBoundingBox from "ascii-grid/src/calc-ascii-grid-precise-bounding-box";
calcAsciiGridPreciseBoundingBox({
data,
meta,
max_read_length
})
["491501", "2556440", "594634.0933", "2645315.3392"]