ascii-grid: beta
Identify and Read 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
usage
identify ascii grid files
isAsciiGrid identifies ASCII GRID files in the following formats: ArrayBuffer, Buffer, DataView, Promise, String, and Uint8Array
const isAsciiGrid = require("ascii-grid/is-ascii-grid");
const buffer = readFileSync('./test_data/michigan_lld/michigan_lld.asc');
isAsciiGrid({ data: buffer, debug: false });
parse ascii grid metadata
const parseAsciiGridMeta = require("ascii-grid/parse-ascii-grid-meta");
const buffer = readFileSync('./test_data/michigan_lld/michigan_lld.asc');
const metadata = parseAsciiGridMeta({
data: buffer,
debug: false,
cache: true
});
Reading Pixel Values
const parseAsciiGridData = require("ascii-grid/parse-ascii-grid-data");
const result = await parseAsciiGridData({
data: buffer,
debug: true,
cache: true
meta
});
Reading Pixel Values within 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/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/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/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 });