78 lines
2.6 KiB
JavaScript
78 lines
2.6 KiB
JavaScript
|
import { getProp, parseSequence } from './util'
|
||
|
import { polygon } from '@turf/helpers';
|
||
|
import proj4 from 'proj4';
|
||
|
|
||
|
export const LANDXML_PARCEL = 'parcel';
|
||
|
|
||
|
export function convertParcels(xmlObj, projection, opts) {
|
||
|
|
||
|
const originalPoints = getProp(xmlObj, "LandXML", "CgPoints", 0, "CgPoint");
|
||
|
const originalParcels = getProp(xmlObj, "LandXML", "Parcels", 0, "Parcel");
|
||
|
|
||
|
const points = {}
|
||
|
const polygons = []
|
||
|
|
||
|
// Parse out the points
|
||
|
for ( let pt in originalPoints ) {
|
||
|
const opt = originalPoints[pt]
|
||
|
const name = opt.$.name.replace(/ /g,'');
|
||
|
let coords = parseSequence(opt._).map(coord => parseFloat(coord))
|
||
|
coords = [coords[1], coords[0]]
|
||
|
coords = proj4(projection, 'WGS84', coords)
|
||
|
points[name] = coords
|
||
|
}
|
||
|
|
||
|
// Parse out the parcels
|
||
|
for ( let pc in originalParcels) {
|
||
|
var opc = originalParcels[pc]
|
||
|
var geom = opc.CoordGeom
|
||
|
if (geom !== undefined) {
|
||
|
let coords = []
|
||
|
// Need to store the start to add at the end
|
||
|
var endCoord = null
|
||
|
var origCoords = opc.CoordGeom[0].Line
|
||
|
|
||
|
for (let oc in origCoords) {
|
||
|
// VERY important note. This code does NOT handle the other two types
|
||
|
// of coordinate definitions, curves and irregular lines. TODO: fixme.
|
||
|
var start = origCoords[oc].Start[0].$.pntRef
|
||
|
|
||
|
// We don't use the end (it probably should be checked, because if the end isn't the start)
|
||
|
var end = origCoords[oc].End[0].$.pntRef
|
||
|
|
||
|
var startPoint = points[start]
|
||
|
if (!startPoint) {
|
||
|
// console.log('Failed to get start point', start, startPoint)
|
||
|
} else {
|
||
|
// Start creating the polygon
|
||
|
coords.push(points[start])
|
||
|
if (!endCoord) {
|
||
|
// Store the first coord, to put back on to close the polygon
|
||
|
endCoord = points[start]
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (coords.length > 2) {
|
||
|
coords.push(endCoord)
|
||
|
coords = [coords]
|
||
|
// name="101" class="Lot" state="proposed" parcelType="Single"
|
||
|
var parameters = {
|
||
|
name: opc.$.name,
|
||
|
landxmlClass: opc.$.class,
|
||
|
landxmlState: opc.$.state,
|
||
|
landxmlParcelType: opc.$.parcelType,
|
||
|
landxmlType: LANDXML_PARCEL,
|
||
|
}
|
||
|
polygons.push(polygon(coords, parameters))
|
||
|
} else {
|
||
|
// console.log("Couldn't create a polygon because we found less than 2 coords")
|
||
|
}
|
||
|
} else {
|
||
|
// console.log("No geometry")
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return polygons;
|
||
|
|
||
|
}
|