forked from plepe/overpass-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloadOsmFile.js
86 lines (71 loc) · 2.23 KB
/
loadOsmFile.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
const fs = require('fs')
const DOMParser = require('xmldom').DOMParser
const bzip2 = require('bzip2')
const convertFromXML = require('./convertFromXML')
module.exports = function loadOsmFile (url, callback) {
if (typeof location === 'undefined' && !url.match(/^(http:|https:|)\/\//)) {
fs.readFile(url,
(err, content) => {
let data
if (err) {
return callback(err)
}
if (url.match(/\.osm(\.bz2)?$/)) {
if (url.match(/\.osm\.bz2$/)) {
data = bzip2.simple(bzip2.array(content))
content = ''
for (let i = 0; i < data.byteLength; i++) {
content += String.fromCharCode(data[i])
}
content = decodeURIComponent(escape(content))
}
data = new DOMParser().parseFromString(content.toString(), 'text/xml')
data = convertFromXML(data.getElementsByTagName('osm')[0])
} else {
data = JSON.parse(content)
}
callback(null, data)
}
)
return
}
let req = new window.XMLHttpRequest()
req.onreadystatechange = function () {
if (req.readyState === 4) {
if (req.status === 200) {
let data
if (url.match(/\.osm\.bz2$/)) {
let content = new Uint8Array(req.response)
data = bzip2.simple(bzip2.array(content))
content = ''
for (let i = 0; i < data.byteLength; i++) {
content += String.fromCharCode(data[i])
}
content = decodeURIComponent(escape(content))
data = new DOMParser().parseFromString(content.toString(), 'text/xml')
data = convertFromXML(data.getElementsByTagName('osm')[0])
} else if (req.responseXML) {
data = convertFromXML(req.responseXML.firstChild)
} else {
data = JSON.parse(req.responseText)
}
callback(null, data)
} else {
callback(req)
}
}
}
if (url.substr(0, 2) === '//') {
if (typeof location === 'undefined') {
url = 'https:' + url
} else {
url = window.location.protocol + url
}
}
if (url.match(/\.osm\.bz2$/)) {
req.responseType = 'arraybuffer'
}
req.overrideMimeType('text/xml')
req.open('GET', url)
req.send()
}