UNPKG

isoxml

Version:

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

126 lines (125 loc) 7.1 kB
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()); }); }; import { registerEntityClass } from '../classRegistry'; import { Task } from '../baseEntities'; import { ExtendedGrid } from './Grid'; import { TAGS } from '../baseEntities/constants'; import { constructValueInformation } from '../utils'; export class ExtendedTask extends Task { constructor(attributes, isoxmlManager) { super(attributes, isoxmlManager); this.tag = TAGS.Task; } static fromXML(xml, isoxmlManager, internalId) { var _a, _b, _c, _d; return __awaiter(this, void 0, void 0, function* () { const entity = yield Task.fromXML(xml, isoxmlManager, internalId, ExtendedTask); if (((_a = entity.attributes.Grid) !== null && _a !== void 0 ? _a : []).length > 0) { const grid = entity.attributes.Grid[0]; const treatmentZoneCode = grid.attributes.TreatmentZoneCode; if (treatmentZoneCode !== undefined) { const treatmentZone = (_b = entity.attributes.TreatmentZone) === null || _b === void 0 ? void 0 : _b.find(tz => tz.attributes.TreatmentZoneCode === treatmentZoneCode); const pdvCount = (_d = (_c = treatmentZone === null || treatmentZone === void 0 ? void 0 : treatmentZone.attributes.ProcessDataVariable) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0; if (pdvCount > 0) { grid.verifyGridSize(isoxmlManager, pdvCount); } } } return entity; }); } findFreeTZNCode() { var _a; const tznCodes = new Set((_a = this.attributes.TreatmentZone) === null || _a === void 0 ? void 0 : _a.map(tzn => tzn.attributes.TreatmentZoneCode)); for (let i = 0; i < 255; i++) { if (!tznCodes.has(i)) { return i; } } } /** Adds Grid and corresponding TreatmentZones to the Task. * * Implementation notes: * - this function doesn't remove any existing TreatmentZones from the task, it just adds new TZNs. * - TZN codes are generated automatically. * - if OutOfFieldTreatmentZone in the task is not defined, * it will be created using outOfFieldValues (which are all 0 by default) * - if defaultTreatmentValues or positionLostValues are defined, corresponding TZNs will be created and used */ addGridFromGeoJSON(geoJSON, processDataVariables, geoJSONPropertyNames, // one attribute name for each processDataVariable defaultTreatmentValues, // one value for each processDataVariable positionLostValues, // one value for each processDataVariable outOfFieldValues) { var _a; const createPDVs = (values) => values.map((value, idx) => this.isoxmlManager.createEntityFromAttributes(TAGS.ProcessDataVariable, Object.assign(Object.assign({}, processDataVariables[idx].attributes), { ProcessDataValue: value }))); const arrayOfZeros = Array(processDataVariables.length).fill(0); this.attributes.TreatmentZone = (_a = this.attributes.TreatmentZone) !== null && _a !== void 0 ? _a : []; if (this.attributes.OutOfFieldTreatmentZoneCode === undefined || outOfFieldValues !== undefined) { this.attributes.OutOfFieldTreatmentZoneCode = this.findFreeTZNCode(); this.attributes.TreatmentZone.push(this.isoxmlManager.createEntityFromAttributes(TAGS.TreatmentZone, { TreatmentZoneCode: this.attributes.OutOfFieldTreatmentZoneCode, ProcessDataVariable: createPDVs(outOfFieldValues !== null && outOfFieldValues !== void 0 ? outOfFieldValues : arrayOfZeros) })); } const gridTZNCode = this.findFreeTZNCode(); this.attributes.TreatmentZone.push(this.isoxmlManager.createEntityFromAttributes(TAGS.TreatmentZone, { TreatmentZoneCode: gridTZNCode, ProcessDataVariable: createPDVs(arrayOfZeros) })); this.attributes.Grid = [ ExtendedGrid.fromGeoJSON(geoJSON, geoJSONPropertyNames, this.isoxmlManager, gridTZNCode) ]; if (defaultTreatmentValues !== undefined) { this.attributes.DefaultTreatmentZoneCode = this.findFreeTZNCode(); this.attributes.TreatmentZone.push(this.isoxmlManager.createEntityFromAttributes(TAGS.TreatmentZone, { TreatmentZoneCode: this.attributes.DefaultTreatmentZoneCode, ProcessDataVariable: createPDVs(defaultTreatmentValues) })); } if (positionLostValues !== undefined) { this.attributes.PositionLostTreatmentZoneCode = this.findFreeTZNCode(); this.attributes.TreatmentZone.push(this.isoxmlManager.createEntityFromAttributes(TAGS.TreatmentZone, { TreatmentZoneCode: this.attributes.PositionLostTreatmentZoneCode, ProcessDataVariable: createPDVs(positionLostValues) })); } } getGridAsGeoJSON(propertyNames) { if (!this.attributes.Grid) { return null; } return this.attributes.Grid[0].toGeoJSON(propertyNames); } getGridValuesDescription() { var _a; const grid = (_a = this.attributes.Grid) === null || _a === void 0 ? void 0 : _a[0]; if (!grid) { return []; } const treatmentZoneCodes = grid.getAllReferencedTZNCodes(); if (treatmentZoneCodes.length === 0) { return []; } // we assume that all the referenced TreatmentZones have the same set of ProcessDataVariables const treatmentZone = (this.attributes.TreatmentZone || []) .find(tz => tz.attributes.TreatmentZoneCode === treatmentZoneCodes[0]); if (!treatmentZone) { return []; } return (treatmentZone.attributes.ProcessDataVariable || []).map(pdv => { var _a, _b; const vpn = (_a = pdv.attributes.ValuePresentationIdRef) === null || _a === void 0 ? void 0 : _a.entity; const det = (_b = pdv.attributes.DeviceElementIdRef) === null || _b === void 0 ? void 0 : _b.entity; const pdp = det === null || det === void 0 ? void 0 : det.getDataProcess(pdv.attributes.ProcessDataDDI); return constructValueInformation(pdv.attributes.ProcessDataDDI, vpn, pdp); }); } } registerEntityClass('main', TAGS.Task, ExtendedTask);