gis-tools-ts
Version:
A collection of geospatial tools primarily designed for WGS84, Web Mercator, and S2.
82 lines • 2.62 kB
JavaScript
import { compareIDs } from '../..';
import { open } from 'fs/promises';
/**
* @param chunk - chunk information needed to start sorting
* @returns - output file that it created
*/
export async function sortChunk(chunk) {
const { name, input, outDir, start, end, valueOffset } = chunk;
const outFile = `${outDir}/es_${name}_${start}_${end}.tmp`;
await _sortChunk(input, outFile, start, end, valueOffset);
return outFile;
}
/**
* @param input - input file
* @param output - output file
* @param start - start index
* @param end - end index
* @param valueOffset - actual value offset if merging values of multiple files
*/
async function _sortChunk(input, output, start, end, valueOffset) {
// prepare the input and output files
const inputHandle = await open(input, 'r').catch((err) => {
throw new Error(err);
});
const outputHandle = await open(output, 'a').catch((err) => {
throw new Error(err);
});
// read in the chunk
const inputBuffer = Buffer.alloc(end - start);
await inputHandle.read(inputBuffer, 0, inputBuffer.length, start);
// sort the chunk
const keys = bufferToKeys(inputBuffer);
keys.sort(keySort);
// update keys to correct offset
for (const key of keys)
key.offset += valueOffset;
// write out the sorted chunk
const sortedBuffer = keysToBuffer(keys);
await outputHandle.appendFile(sortedBuffer);
// close the files
await inputHandle.close();
await outputHandle.close();
}
/**
* @param buffer - buffer containing an encoded list of keys
* @returns - decoded list of keys
*/
export function bufferToKeys(buffer) {
// for each 16 bytes in the buffer, create a key
const keys = [];
for (let i = 0; i < buffer.length; i += 16) {
keys.push({
id: buffer.readBigUInt64LE(i),
offset: buffer.readUInt32LE(i + 8),
length: buffer.readUInt32LE(i + 12),
});
}
return keys;
}
/**
* @param keys - list of keys
* @returns - buffer containing an encoded list of keys
*/
export function keysToBuffer(keys) {
const buffer = Buffer.alloc(keys.length * 16);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
buffer.writeBigUInt64LE(key.id, i * 16);
buffer.writeUInt32LE(key.offset, i * 16 + 8);
buffer.writeUInt32LE(key.length, i * 16 + 12);
}
return buffer;
}
/**
* @param a - key A
* @param b - key B
* @returns - ordered answer between A and B
*/
export function keySort(a, b) {
return compareIDs(a.id, b.id);
}
//# sourceMappingURL=sortChunk.js.map