UNPKG

isoxml-angular

Version:

JavaScript library to parse and generate ISOXML (ISO11783-10) files

118 lines (117 loc) 6.14 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ExtendedGrid = void 0; const classRegistry_1 = require("../../classRegistry"); const Grid_1 = require("../../baseEntities/Grid"); const DefaultGridParamsGenerator_1 = require("./DefaultGridParamsGenerator"); const CellCenterBasedGridGenerator_1 = require("./CellCenterBasedGridGenerator"); const GRID_CELL_SIZE = 10; // meters class ExtendedGrid extends Grid_1.Grid { constructor(attributes, isoxmlManager) { super(attributes, isoxmlManager); this.tag = "GRD" /* TAGS.Grid */; } static fromXML(xml, isoxmlManager, internalId) { return __awaiter(this, void 0, void 0, function* () { const entity = yield Grid_1.Grid.fromXML(xml, isoxmlManager, internalId, ExtendedGrid); const filename = entity.attributes.Filename; entity.binaryData = yield isoxmlManager.getParsedFile(`${filename}.bin`, true); const nRows = entity.attributes.GridMaximumRow; const nCols = entity.attributes.GridMaximumColumn; const bytesPerElem = entity.attributes.GridType === "1" /* GridGridTypeEnum.GridType1 */ ? 1 : 4; const expectedSize = nRows * nCols * bytesPerElem; if (!entity.binaryData) { isoxmlManager.addWarning(`[${internalId}] Missing grid file ${filename}.bin`); } else if (expectedSize !== entity.binaryData.length) { isoxmlManager.addWarning(`[${internalId}] Invalid size of grid file ${filename}.bin: ` + `expected ${expectedSize} bytes, but real size is ${entity.binaryData.length}`); } return entity; }); } static fromGeoJSON(geoJSON, isoxmlManager, treatmentZoneCode) { const gridParamsGenerator = isoxmlManager.options.gridParamsGenerator || (0, DefaultGridParamsGenerator_1.createGridParamsGenerator)(GRID_CELL_SIZE, GRID_CELL_SIZE); const gridParams = gridParamsGenerator(geoJSON); const gridGenerator = isoxmlManager.options.gridGenerator || CellCenterBasedGridGenerator_1.cellCenterBasedGridGenerator; const { minX, minY, numCols, numRows, cellWidth, cellHeight } = gridParams; const buffer = gridGenerator(geoJSON, gridParams); const filename = isoxmlManager.generateUniqueFilename("GRD" /* TAGS.Grid */); isoxmlManager.addFileToSave(new Uint8Array(buffer), `${filename}.bin`); const entity = new ExtendedGrid(Object.assign({ GridMinimumNorthPosition: minY, GridMinimumEastPosition: minX, GridCellNorthSize: cellHeight, GridCellEastSize: cellWidth, GridMaximumColumn: numCols, GridMaximumRow: numRows, Filename: filename, Filelength: numCols * numRows * 4, GridType: "2" /* GridGridTypeEnum.GridType2 */ }, typeof treatmentZoneCode === 'number' && { TreatmentZoneCode: treatmentZoneCode }), isoxmlManager); entity.binaryData = new Uint8Array(buffer); return entity; } toXML() { this.isoxmlManager.addFileToSave(this.binaryData, `${this.attributes.Filename}.bin`); return super.toXML(); } toGeoJSON() { const cells = new Int32Array(this.binaryData.buffer); const rows = this.attributes.GridMaximumRow; const cols = this.attributes.GridMaximumColumn; const w = this.attributes.GridCellEastSize; const h = this.attributes.GridCellNorthSize; const minX = this.attributes.GridMinimumEastPosition; const minY = this.attributes.GridMinimumNorthPosition; const features = new Array(rows * cols); for (let y = 0; y < rows; y++) { for (let x = 0; x < cols; x++) { const dose = cells[y * cols + x]; if (dose === 0.0) { features[y * cols + x] = null; continue; } features[y * cols + x] = { type: 'Feature', properties: { DOSE: dose }, geometry: { type: 'Polygon', coordinates: [[ [minX + x * w, minY + y * h], [minX + x * w + w, minY + y * h], [minX + x * w + w, minY + y * h + h], [minX + x * w, minY + y * h + h], [minX + x * w, minY + y * h] ]] } }; } } return { type: 'FeatureCollection', features: features.filter(e => e) }; } // the result will be cached getAllReferencedTZNCodes() { if (!this.allReferencedTZNCodes) { if (this.attributes.GridType === "1" /* GridGridTypeEnum.GridType1 */) { const codes = new Set(); for (let i = 0; i < this.binaryData.length; i++) { codes.add(this.binaryData[i]); } this.allReferencedTZNCodes = [...codes]; } else { this.allReferencedTZNCodes = [this.attributes.TreatmentZoneCode]; } } return this.allReferencedTZNCodes; } } exports.ExtendedGrid = ExtendedGrid; (0, classRegistry_1.registerEntityClass)('main', "GRD" /* TAGS.Grid */, ExtendedGrid);