UNPKG

overpass-frontend

Version:

A JavaScript (NodeJS/Browser) library to easily access data from OpenStreetMap via Overpass API or from an OSM File. The objects can directly be used with LeafletJS or exported to GeoJSON. Data will be cached in the browser memory.

72 lines (59 loc) 1.61 kB
const turf = require('../turf') const OverpassFrontend = require('../defines') const arrayToCoords = require('../arrayToCoords') const qlFunction = require('./qlFunction') module.exports = class poly extends qlFunction { constructor (str) { super() this.fun = 'poly' const m = str.match(/^\s*["']([0-9. ]*)["']\s*$/) if (!m) { throw new Error('poly function expects quoted coordinates separated by space') } const s = m[1].split(/ /g) if (s.length < 6 || s.length % 2 !== 0) { throw new Error('poly function expects minimum three pairs of coordinates') } this.value = s.map(v => parseFloat(v)) } test (ob) { return ob.intersects(this.bounds()) } toString () { return '(poly:"' + this.value.join(' ') + '")' } compileLokiJS () { return { needMatch: true } } cacheDescriptors (descriptors) { const bounds = this.bounds() descriptors.forEach(d => { const newBounds = d.bounds ? turf.intersect(d.bounds, bounds) : bounds if (newBounds === null) { delete d.bounds d.invalid = true } else { d.bounds = newBounds.geometry d.properties |= OverpassFrontend.GEOM } }) } isSupersetOf (other) { if (other.bounds) { return !!turf.difference(this.bounds(), other.bounds()) } } bounds () { if (!this._bounds) { const c = arrayToCoords(this.value) this._bounds = { type: 'Feature', geometry: { type: 'Polygon', coordinates: [c.concat([c[0]])] } } } return this._bounds } }