Source: OverpassObject.js

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