make-geo-json
Version:
Converts polylines, wkts, wkbs, polygons, circles, rectangles, as well as standard geoJSON into geoJSON MultiPolygon Feature Objects. Converts wkb_list and FeatureCollection to arrays.
166 lines (152 loc) • 5.26 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.prepKml = exports.iterateFeatColl = exports.removeZAxis = exports.dropTheZ = undefined;
var _flatten = require('lodash/flatten');
var _flatten2 = _interopRequireDefault(_flatten);
var _dropRight = require('lodash/dropRight');
var _dropRight2 = _interopRequireDefault(_dropRight);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/* import togeojson from '@mapbox/togeojson';
* import fs from 'fs';
* import { kmlData } from './kmlToGeoJSON';
* // eslint-disable-next-line prefer-destructuring
* const DOMParser = require('xmldom').DOMParser; */
// Commented section is for extracting useable data from KMZ (zipped)
// May be used here later, or moved somewhere else
/* export const read_a_kml = (doc) => {
* const unzippedKml = new DOMParser().parseFromString(fs.readFileSync(doc, 'utf8'));
* const converted = togeojson.kml(unzippedKml);
* // console.log('I work!', JSON.stringify(converted, null, 2));
* return converted;
* };
* // console.log(process.cwd());
* // read_a_kml('./test/pointAndLineStringKML.kml'); */
const dropTheZ = exports.dropTheZ = arrayOfCoords => {
const goodCoords = [];
for (let i = 0; i < arrayOfCoords.length; i += 1) {
const noZ = (0, _dropRight2.default)(arrayOfCoords[i]);
goodCoords.push(noZ);
}
return goodCoords;
};
let kmlArray = [];
let newFeat = {};
const removeZAxis = exports.removeZAxis = feature => {
if (feature.type === 'Feature') {
switch (feature.geometry.type) {
case 'Point':
if (feature.geometry.coordinates.length === 2) {
const deezCoords = feature.geometry.coordinates;
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: deezCoords
}
};
kmlArray.push(newFeat);
break;
}
if (feature.geometry.coordinates.length === 3) {
feature.geometry.coordinates.pop();
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: feature.geometry.coordinates
}
};
kmlArray.push(newFeat);
break;
} else {
throw Error(`Ensure Geometry - invalid point geometry ${feature.geometry}`);
}
case 'LineString':
if (feature.geometry.coordinates[0].length === 2) {
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: feature.geometry.coordinates
}
};
kmlArray.push(newFeat);
break;
}
if (feature.geometry.coordinates[0].length === 3) {
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: dropTheZ(feature.geometry.coordinates)
}
};
kmlArray.push(newFeat);
break;
} else {
throw Error(`Ensure Geometry - invalid line string geometry ${feature.geometry}`);
}
case 'Polygon':
const flattenedCoords = (0, _flatten2.default)(feature.geometry.coordinates);
const LineStringCoords = dropTheZ(flattenedCoords);
if (flattenedCoords[0].length === 3) {
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: [LineStringCoords]
}
};
kmlArray.push(newFeat);
break;
}
if (flattenedCoords[0].length === 2) {
newFeat = {
type: feature.type,
properties: feature.properties,
geometry: {
type: feature.geometry.type,
coordinates: feature.geometry.coordinates
}
};
kmlArray.push(newFeat);
break;
} else {
throw Error(`Ensure Geometry - invalid polygon geometry ${feature.geometry}`);
}
default:
throw Error(`Ensure Geometry - invalid geometry ${feature.geometry}`);
}
}
};
const iterateFeatColl = exports.iterateFeatColl = feature => {
for (let i = 0; i < feature.features.length; i += 1) {
for (let j = 0; j < feature.features[i].geometry.geometries.length; j += 1) {
const newGeometry = feature.features[i].geometry.geometries[j];
const strippedFeature = {
type: 'Feature',
properties: feature.features[i].properties ? feature.features[i].properties : {},
geometry: newGeometry
};
removeZAxis(strippedFeature);
}
}
};
const prepKml = exports.prepKml = input => {
if (input.type === 'FeatureCollection') {
iterateFeatColl(input);
}
if (input.type === 'Feature') {
removeZAxis(input);
}
const newArray = kmlArray;
kmlArray = [];
return newArray;
};