-
Notifications
You must be signed in to change notification settings - Fork 3
/
GeoRSSToGeoJSON.js
120 lines (114 loc) · 2.62 KB
/
GeoRSSToGeoJSON.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
export function parse(dom, options) {
const g = {
type: 'FeatureCollection',
features: [],
}
const items = get(dom, 'item')
for (const item of Array.from(items)) {
const feature = processOne(item)
if (feature) {
g.features.push(feature)
}
}
return g
}
function get(x, y) {
return x.getElementsByTagName(y)
}
function get1(x, y) {
const n = get(x, y)
return n.length ? n[0] : null
}
function norm(el) {
if (el.normalize) {
el.normalize()
}
return el
}
function nodeVal(x) {
if (x) {
norm(x)
}
return x?.firstChild?.nodeValue
}
function attr(x, y) {
return x.getAttribute(y)
}
function geom(node) {
function p(c) {
return parseFloat(c)
}
function r(c) {
return c.reverse().map(p)
} // we have latlon we want lonlat
function e(f) {
const _ = []
for (let i = 0; i < f.length; i += 2) {
_.push(r(f.slice(i, i + 2)))
}
return _
}
let type
let coordinates
if (get1(node, 'geo:long')) {
type = 'Point'
coordinates = [
p(nodeVal(get1(node, 'geo:long'))),
p(nodeVal(get1(node, 'geo:lat'))),
]
} else if (get1(node, 'long')) {
type = 'Point'
coordinates = [p(nodeVal(get1(node, 'long'))), p(nodeVal(get1(node, 'lat')))]
} else if (get1(node, 'georss:point')) {
type = 'Point'
coordinates = r(nodeVal(get1(node, 'georss:point')).split(' '))
} else if (get1(node, 'point')) {
type = 'Point'
coordinates = r(nodeVal(get1(node, 'point')).split(' '))
} else {
const line = get1(node, 'georss:line')
const poly = get1(node, 'georss:polygon')
if (line || poly) {
type = line ? 'LineString' : 'Polygon'
const tag = line ? 'georss:line' : 'georss:polygon'
coordinates = nodeVal(get1(node, tag)).split(' ')
if (coordinates.length % 2 !== 0) return
coordinates = e(coordinates)
if (poly) {
coordinates = [coordinates]
}
}
}
if (type && coordinates) {
return {
type: type,
coordinates: coordinates,
}
}
}
function processOne(node) {
const geometry = geom(node)
// TODO collect and fire errors
if (!geometry) return
const f = {
type: 'Feature',
geometry: geometry,
properties: {
title: nodeVal(get1(node, 'title')),
description: nodeVal(get1(node, 'description')),
link: nodeVal(get1(node, 'link')),
},
}
let media = get1(node, 'media:content')
let mime
if (!media) {
media = get1(node, 'enclosure')
}
if (media) {
mime = attr(media, 'type')
if (mime.indexOf('image') !== -1) {
f.properties.img = attr(media, 'url') // How not to invent a key?
}
}
return f
}