esri-ascii-grid-rs

Rust library to read ESRI Ascii grid .asc files

Example ASCII Grid: ncols 4 nrows 6 xllcorner 0.0 yllcorner 0.0 cellsize 50.0 NODATA_value -9999 -9999 -9999 5 2 -9999 20 100 36 3 8 35 10 32 42 50 6 88 75 27 9 13 5 1 -9999

This library uses buffers to negate the need to load the entire ASCII grid into memory at once. The header will be loaded and will allow you to check the properties of the header. You can then either get specific values by index, coordinate or iterate over all points.

Example usage:

``rust use esri_ascii_grid_rs::ascii_file::EsriASCIIReader; let file = std::fs::File::open("test_data/test.asc").unwrap(); let mut grid = EsriASCIIReader::from_file(file).unwrap(); // Indexing the file is optional, but is recommended if you are going to be repeatedly calling anyget` function // This will build the index and cache the file positions of each line, it will take a while for large files but will drastically increase subsequent get calls grid.buildindex().unwrap() // Spot check a few values asserteq!( grid.getindex(994, 7).unwrap(), grid.header.nodatavalue().unwrap() ); asserteq!(grid.get(390000.0, 344000.0).unwrap(), 141.2700042724609375); asserteq!(grid.get(390003.0, 344003.0).unwrap(), 135.44000244140625); asserteq!(grid.getindex(3, 3).unwrap(), 135.44000244140625); asserteq!(grid.getindex(0, 0).unwrap(), 141.270004272460937_5);

// Interpolate between cells let val = grid.getinterpolate(grid.header.minx() + grid.header.cell_size()/4).unwrap();

// Iterate over every cell let header = grid.header; let gridsize = grid.header.numrows() * grid.header.numcols(); let iter = grid.intoiter(); let mut numelements = 0; for (row, col, value) in iter { numelements += 1; if row == 3 && col == 3 { let (x, y) = header.indexpos(col, row).unwrap(); asserteq!(x, 390003.0); asserteq!(y, 344003.0); asserteq!(value, 135.44000244140625); } if row == 0 && col == 0 { let (x, y) = header.indexpos(col, row).unwrap(); asserteq!(x, 390000.0); asserteq!(y, 344000.0); asserteq!(value, 141.2700042724609375); } } asserteq!(gridsize, num_elements); ```