forked from HSLdevcom/digitransit-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeoJsonStore.js
137 lines (124 loc) · 3.29 KB
/
GeoJsonStore.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
import cloneDeep from 'lodash/cloneDeep';
import isEmpty from 'lodash/isEmpty';
import Store from 'fluxible/addons/BaseStore';
import { getJson } from '../util/xhrPromise';
// these are metadata mappable properties
const metaTags = ['textOnly', 'name', 'popupContent'];
const MapJSON = (data, meta) => {
if (isEmpty(meta)) {
return data;
}
const tagMap = metaTags.filter(t => !!meta[t]);
data.features.forEach(feature => {
const { properties } = feature;
if (properties) {
tagMap.forEach(t => {
if (properties[meta[t]]) {
properties[t] = properties[meta[t]];
}
});
}
});
return data;
};
const styleFeatures = data => {
if (!data.features.some(feature => Array.isArray(feature.styles))) {
return data;
}
const output = {
type: 'FeatureCollection',
features: [],
};
data.features.forEach(feature => {
if (!Array.isArray(feature.styles)) {
output.features.push(cloneDeep(feature));
return;
}
const size = feature.styles.length;
feature.styles.forEach((style, index) => {
const clone = cloneDeep(feature);
delete clone.styles;
clone.style = cloneDeep(style);
if (size === 2) {
clone.style.type = index === 1 ? 'halo' : 'line';
}
output.features.push(clone);
});
});
return output;
};
class GeoJsonStore extends Store {
geoJsonData = {};
static storeName = 'GeoJsonStore';
get layers() {
return this.geoJsonLayers;
}
set layers(value) {
this.geoJsonLayers = value;
}
getGeoJsonConfig = async url => {
if (!url) {
return undefined;
}
if (!this.layers) {
try {
const response = await getJson(url);
const root = response.geoJson || response.geojson;
if (root && Array.isArray(root.layers)) {
this.layers = root.layers;
}
} catch (error) {
this.layers = [];
}
}
return this.layers;
};
getGeoJsonData = async (url, name, metadata) => {
if (!url) {
return undefined;
}
let id;
let urlArr;
if (Array.isArray(url)) {
id = `${url[0]}-array`;
urlArr = url;
} else {
id = url;
urlArr = [url];
}
if (this.geoJsonData[id] === 'pending') {
for (let i = 0; i < 30; i++) {
/* eslint-disable no-await-in-loop, no-promise-executor-return */
await new Promise(resolve => setTimeout(resolve, 100));
if (this.geoJsonData[id] !== 'pending') {
break;
}
}
}
if (!this.geoJsonData[id]) {
this.geoJsonData[id] = 'pending';
try {
const responses = await Promise.all(urlArr.map(u => getJson(u)));
let mapped;
responses.forEach(r => {
const styled = styleFeatures(r);
if (!mapped) {
mapped = MapJSON(styled, metadata);
} else {
mapped.features.push(...styled.features);
}
});
const data = {
name: name || id,
data: mapped,
};
this.geoJsonData[id] = data;
} catch (error) {
// store non falsy value to avoid new fetch
this.geoJsonData[id] = {};
}
}
return this.geoJsonData[id].name ? { ...this.geoJsonData[id] } : null;
};
}
export { GeoJsonStore as default, MapJSON, styleFeatures };