osm2geojson-lite
Version:
a lightweight yet faster osm (either in xml or in json formats) to geojson convertor - 4x faster than xmldom + osmtogeojson in most situations - implemented in pure JavaScript without any 3rd party dependency
88 lines (87 loc) • 2.87 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Way = void 0;
const osm_object_1 = require("./osm-object");
const late_binder_1 = require("./late-binder");
const utils_1 = require("./utils");
const polytags_json_1 = __importDefault(require("./polytags.json"));
class Way extends osm_object_1.OsmObject {
constructor(id, refElems) {
super('way', id, refElems);
this.latLngArray = [];
this.isPolygon = false;
}
addLatLng(latLng) {
this.latLngArray.push(latLng);
}
setLatLngArray(latLngArray) {
this.latLngArray = latLngArray;
}
addNodeRef(ref) {
const binder = new late_binder_1.LateBinder(this.latLngArray, (id) => {
const node = this.refElems.get(`node/${id}`);
if (node) {
node.refCount++;
return node.getLatLng();
}
}, this, [ref]);
this.latLngArray.push(binder);
this.refElems.addBinder(binder);
}
addTags(tags) {
super.addTags(tags);
for (const [k, v] of Object.entries(tags)) {
this.analyzeTag(k, v);
}
}
addTag(k, v) {
super.addTag(k, v);
this.analyzeTag(k, v);
}
toCoordsArray() {
return this.latLngArray.map((latLng) => [latLng.lon, latLng.lat]);
}
toFeatureArray() {
let coordsArrayString = this.toCoordsArray();
if (coordsArrayString.length > 1) {
const coordsArray = (0, utils_1.strArrayArrayToFloat)(coordsArrayString);
const feature = {
type: 'Feature',
id: this.getCompositeId(),
properties: this.getProps(),
geometry: {
type: 'LineString',
coordinates: coordsArray,
},
};
if (this.isPolygon && (0, utils_1.isRing)(coordsArray)) {
if ((0, utils_1.ringDirection)(coordsArray) !== 'counterclockwise') {
coordsArray.reverse();
}
feature.geometry = {
type: 'Polygon',
coordinates: [coordsArray],
};
return [feature];
}
return [feature];
}
return [];
}
analyzeTag(k, v) {
const o = polytags_json_1.default[k];
if (o) {
this.isPolygon = true;
if (o.whitelist) {
this.isPolygon = o.whitelist.indexOf(v) >= 0 ? true : false;
}
else if (o.blacklist) {
this.isPolygon = o.blacklist.indexOf(v) >= 0 ? false : true;
}
}
}
}
exports.Way = Way;