forked from plepe/overpass-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOverpassObject.js
371 lines (312 loc) · 10.2 KB
/
OverpassObject.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
const ee = require('event-emitter')
var BoundingBox = require('boundingbox')
var OverpassFrontend = require('./defines')
var turf = {
difference: require('@turf/difference'),
intersect: require('@turf/intersect').default
}
/**
* Base class for representing map features.
* @property {string} id ID of this object.
* @property {number} osm_id Numeric id.
* @property {string} type Type: 'node', 'way' or 'relation'.
* @property {object} tags OpenStreetMap tags.
* @property {object} meta OpenStreetMap meta information.
* @property {object} geometry of the object
* @property {object} data Data as loaded from Overpass API.
* @property {bit_array} properties Which information about this object is known?
* @property {object[]} memberOf List of ways and relations where this object is member of.
* @property {string} memberOf.id ID of the way or relation where this way is member of.
* @property {string} memberOf.role Role of this object in the relation.
* @property {number} memberOf.sequence This object is the nth member in the way resp. relation.
* @property {BoundingBox} bounds Bounding box of this object.
* @property {Point} center Centroid of the bounding box.
*/
class OverpassObject {
constructor () {
this.data = {}
this.properties = 0
this.memberOf = []
}
memberIds () {
return []
}
member_ids () { // eslint-disable-line
console.log('called deprecated OverpassObject.member_ids() function - replace by memberIds()')
return this.memberIds()
}
notifyMemberOf (relation, role, sequence) {
this.memberOf.push({ id: relation.id, role, sequence })
}
updateData (data, options) {
if (typeof this.id === 'undefined') {
this.id = data.type.substr(0, 1) + data.id
this.type = data.type
this.osm_id = data.id
}
this.osm3sMeta = options.osm3sMeta
for (var k in data) {
this.data[k] = data[k]
}
if (data.bounds) {
this.bounds = new BoundingBox(data.bounds)
this.center = this.bounds.getCenter()
this.diagonalLength = this.bounds.diagonalLength()
} else if (data.center) {
this.bounds = new BoundingBox(data.center)
this.center = this.bounds.getCenter()
}
if (options.bbox) {
if (!this.bounds || options.bbox.intersects(this.bounds)) {
this.properties = this.properties | options.properties
} else {
this.properties = this.properties | OverpassFrontend.BBOX | OverpassFrontend.CENTER
}
} else {
this.properties = this.properties | options.properties
}
// result of a request with bbox limitation, where the object was outside
if (options.bboxNoMatch && this.bounds) {
// this.boundsPossibleMatch: record unsucessful bbox requests for an object
if (typeof this.boundsPossibleMatch === 'undefined') {
this.boundsPossibleMatch = this.bounds.toGeoJSON()
}
this.boundsPossibleMatch = turf.difference(this.boundsPossibleMatch, options.bbox.toGeoJSON())
}
// geometry is known -> no need for this.boundsPossibleMatch
if (this.geometry) {
delete this.boundsPossibleMatch
}
if (options.properties & OverpassFrontend.TAGS) {
if (typeof data.tags === 'undefined') {
this.tags = {}
} else {
this.tags = data.tags
}
}
this.errors = []
if (data.timestamp) {
this.meta = {
timestamp: data.timestamp,
version: data.version,
changeset: data.changeset,
user: data.user,
uid: data.uid
}
}
if (data.tags) {
this.tags = data.tags
}
}
notifyMemberUpdate (memberObs) {
}
/**
* Title of of this object (default: name, operator or ref or the id of the object)
* @return {string}
*/
title () {
if (!this.tags) {
return this.id
}
return this.tags.name || this.tags.operator || this.tags.ref || this.id
}
/**
* GeoJSON representation of this object
* @return {object}
*/
GeoJSON () {
return {
type: 'Feature',
id: this.type + '/' + this.osm_id,
geometry: null,
properties: this.GeoJSONProperties()
}
}
GeoJSONProperties () {
var ret = {}
var k
ret['@id'] = this.type + '/' + this.osm_id
if (this.tags) {
for (k in this.tags) {
ret[k] = this.tags[k]
}
}
if (this.meta) {
for (k in this.meta) {
ret['@' + k] = this.meta[k]
}
}
for (k in this.osm3sMeta) {
ret['@osm3s:' + k] = this.osm3sMeta[k]
}
return ret
}
/**
* Export object as GeoJSON. Missing geometry will be loaded.
* @param object options Options
* @param function callback Function which will be called with (err, result)
*/
exportGeoJSON (options, callback) {
this.overpass.get(
this.id,
{
properties: OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META | OverpassFrontend.GEOM
},
() => {},
(err) => {
if (err) {
return callback(err)
}
callback(null, this.GeoJSON(options))
}
)
}
/**
* Export object (and members) as OpenStreetMap XML
* @param object options Options
* @param DOMNode parentNode a DOM Node where the object will be appended as child. Depending on object type and options, member objects will also be appended on the same level.
* @param function callback Function which will be called with (err, dom node)
*/
exportOSMXML (options, parentNode, callback) {
if (!parentNode._alreadyIncluded) {
parentNode._alreadyIncluded = {}
}
if (this.id in parentNode._alreadyIncluded) {
return callback(null)
}
parentNode._alreadyIncluded[this.id] = true
if ((this.properties & (OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META)) !== (OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META)) {
return this.overpass.get(
this.id,
{
properties: OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META
},
() => {},
(err) => {
if (err) {
return callback(err)
}
this._exportOSMXML(options, parentNode, callback)
}
)
}
this._exportOSMXML(options, parentNode, callback)
}
_exportOSMXML (options, parentNode, callback) {
let result = parentNode.ownerDocument.createElement(this.type)
result.setAttribute('id', this.osm_id)
if (this.meta) {
result.setAttribute('version', this.meta.version)
result.setAttribute('timestamp', this.meta.timestamp)
result.setAttribute('changeset', this.meta.changeset)
result.setAttribute('uid', this.meta.uid)
result.setAttribute('user', this.meta.user)
}
if (this.tags) {
for (var k in this.tags) {
let tag = parentNode.ownerDocument.createElement('tag')
tag.setAttribute('k', k)
tag.setAttribute('v', this.tags[k])
result.appendChild(tag)
}
}
parentNode.appendChild(result)
callback(null, result)
}
/**
* Export object (and members) as OpenStreetMap JSON
* @param object options Options
* @param object elements All exported elements, include member objects. Pass an empty object. If a member element would be exported multiple times it will appear only once. For the final export, to be compatible to Overpass API, you should convert the object to an array via Object.values().
* @param function callback Function which will be called with (err, result)
*/
exportOSMJSON (conf, elements, callback) {
if (this.id in elements) {
return callback(null)
}
elements[this.id] = {}
if ((this.properties & (OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META)) !== (OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META)) {
return this.overpass.get(
this.id,
{
properties: OverpassFrontend.TAGS | OverpassFrontend.MEMBERS | OverpassFrontend.META
},
() => {},
(err) => {
if (err) {
return callback(err)
}
this._exportOSMJSON(conf, elements, callback)
}
)
}
this._exportOSMJSON(conf, elements, callback)
}
_exportOSMJSON (conf, elements, callback) {
let result = elements[this.id]
result.type = this.type
result.id = this.osm_id
if (this.meta) {
result.version = this.meta.version
result.timestamp = this.meta.timestamp
result.changeset = this.meta.changeset
result.uid = this.meta.uid
result.user = this.meta.user
}
if (this.tags && Object.keys(this.tags).length) {
result.tags = this.tags
}
callback(null, result)
}
/**
* Check whether this object intersects (or is within) the specified bounding box. Returns 0 if it does not match; 1 if the exact geometry is not known, but the object's bounding box matches; 2 exact match.
* @param {boundingbox:BoundingBox} bbox Bounding box
* @return {number}
*/
intersects (bbox) {
if (!this.bounds) {
return 0
}
if (!bbox.intersects(this.bounds)) {
return 0
}
if (this.boundsPossibleMatch) {
var remaining = turf.intersect(bbox.toGeoJSON(), this.boundsPossibleMatch)
if (!remaining || remaining.geometry.type !== 'Polygon') {
// geometry.type != Polygon: bbox matches border of this.boundsPossibleMatch
return 0
}
return 1
}
return 1
}
/**
* return a leaflet feature for this object.
* @param {object} [options] options Options will be passed to the leaflet function
* @return {L.layer}
*/
leafletFeature (options) {
return null
}
dbInsert () {
if (!this.dbData) {
this.dbData = {}
}
this.dbData.tags = this.tags
this.dbData.osmMeta = this.meta
this.dbData.id = this.id
this.dbData.type = this.type
if (this.bounds && this.bounds.minlat) {
this.dbData.minlat = this.bounds.minlat
this.dbData.minlon = this.bounds.minlon
this.dbData.maxlat = this.bounds.maxlat
this.dbData.maxlon = this.bounds.maxlon
if (this.bounds.minlon > this.bounds.maxlon) {
this.dbData.stretchLon180 = true
this.overpass.hasStretchLon180 = true
}
}
return this.dbData
}
}
ee(OverpassObject.prototype)
module.exports = OverpassObject