landxml/lib/cg-point.js

60 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-01-22 15:56:02 +01:00
import { getProp, parseSequence, getColor } from './util'
import proj4 from 'proj4';
export const LANDXML_POINT = 'cg_point';
export function convertCGPoints(xmlObj, projection, opts) {
const features = [];
const pointsContainerNodes = getProp(xmlObj, "LandXML", "CgPoints");
if (!pointsContainerNodes) return features;
pointsContainerNodes.forEach(pc => {
const attrs = (pc.$ || {});
const baseProps = {
name: attrs.name || "",
description: attrs.description || "",
};
const pointNodes = getProp(pc, "CgPoint");
if (!Array.isArray(pointNodes)) return;
pointNodes.forEach(p => {
const name = baseProps.name ? ( baseProps.name + ' - ' + p.$.name ) : p.$.name;
const description = baseProps.description ? ( baseProps.description + ' - ' + p.$.desc ) : p.$.desc;
const featureProps = {
name: name,
description: description,
landxmlType: LANDXML_POINT,
2021-01-25 10:26:48 +01:00
landxmlColor: getColor(`point-${name}`, opts.colorOpts),
2021-01-22 15:56:02 +01:00
};
const pointFeature = convertPoint(p, projection, featureProps, opts);
features.push(pointFeature)
});
});
return features;
}
function convertPoint(cgPoint, projection, featureProps) {
let pointCoords = parseSequence(cgPoint._).map(v => parseFloat(v));
pointCoords = [pointCoords[1], pointCoords[0], pointCoords[2]];
pointCoords = proj4(projection, 'WGS84', pointCoords);
return {
type: "Feature",
geometry: {
type: "Point",
coordinates: [...pointCoords],
2021-01-22 15:56:02 +01:00
},
properties: {
...featureProps,
landxmlAltitude: pointCoords[2],
2021-01-22 15:56:02 +01:00
}
};
}