This repository has been archived by the owner on Jun 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
customLayerActions.js
196 lines (178 loc) · 5.79 KB
/
customLayerActions.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
import { setLayerManagementModalVisibility } from 'app/appActions';
import { addCustomLayer } from 'layers/layersActions';
import { addCustomGLLayer } from 'map/mapStyleActions';
import { CUSTOM_LAYERS_SUBTYPES } from 'constants';
import isURL from 'validator/lib/isURL';
import WMSCapabilities from 'wms-capabilities';
import URI from 'urijs';
export const CUSTOM_LAYER_UPLOAD_START = 'CUSTOM_LAYER_UPLOAD_START';
export const CUSTOM_LAYER_UPLOAD_SUCCESS = 'CUSTOM_LAYER_UPLOAD_SUCCESS';
export const CUSTOM_LAYER_UPLOAD_ERROR = 'CUSTOM_LAYER_UPLOAD_ERROR';
export const CUSTOM_LAYER_RESET = 'CUSTOM_LAYER_RESET';
export const loadCustomLayer = ({ token, subtype, id, url }) => {
const loadPromise = fetch(url, {
headers: {
Authorization: `Bearer ${token}`
}
})
.then((res) => {
if (!res.ok) throw new Error(res.statusText);
return res.json();
})
.then(json => ({
data: json,
subtype,
id,
url
}));
return loadPromise;
};
const loadWMSCapabilities = ({ id, url }) => {
const urlSearch = new URI(url).search((data) => {
data.request = 'GetCapabilities';
data.service = 'WMS';
});
const promise = fetch(urlSearch)
.then((res) => {
if (!res.ok) throw new Error(res.statusText);
return res.text();
})
.then((xmlString) => {
const capabilities = new WMSCapabilities(xmlString).toJSON();
if (capabilities.Capability === undefined) {
throw new Error('Error with WMS endpoint');
}
return {
id,
url,
capabilities,
subtype: CUSTOM_LAYERS_SUBTYPES.raster
};
});
return promise;
};
const getWMSURLFilterdByLayers = ({ url, capabilities }, layersActives) => {
const format = capabilities.Capability.Request.GetMap.Format.indexOf('image/png') > -1
? 'image/png'
: 'image/jpeg';
const layersFiltered = capabilities.Capability.Layer.Layer
.filter(l => layersActives.includes(l.Name));
const layersIds = layersFiltered.map(l => l.Name).join(',');
const styles = layersFiltered.map(l => l.Style[0].Name).join(',');
const tilesURL = new URI(url).search(() => ({
version: '1.1.0',
request: 'GetMap',
srs: 'EPSG:3857',
bbox: '{bbox-epsg-3857}',
width: 256,
height: 256,
transparent: true,
layers: layersIds,
styles,
format
}));
return decodeURIComponent(tilesURL.toString());
};
const saveToDirectory = ({ token, subtype, name, description, url }) => {
const savePromise = fetch(`${V2_API_ENDPOINT}/directory`, {
method: 'POST',
headers: {
Authorization: `Bearer ${token}`,
'content-type': 'application/json'
},
body: JSON.stringify({ title: name, url, description })
})
.then((res) => {
if (!res.ok) throw new Error(res.statusText);
return res.json();
})
.then(json => ({
token,
subtype,
id: json.args.id,
url: json.args.source.args.url
}));
return savePromise;
};
export const uploadCustomLayer = (subtype, url, name, description) => (dispatch, getState) => {
const state = getState();
const token = state.user.token;
if (isURL(url.trim())) {
dispatch({ type: CUSTOM_LAYER_UPLOAD_START });
const promises = [];
if (subtype !== CUSTOM_LAYERS_SUBTYPES.raster) {
promises.push(saveToDirectory);
}
if (subtype === CUSTOM_LAYERS_SUBTYPES.wms) {
promises.push(loadWMSCapabilities);
}
if (subtype === CUSTOM_LAYERS_SUBTYPES.geojson) {
promises.push(loadCustomLayer);
}
promises.reduce((p, f) => p.then(f), Promise.resolve({
token, subtype, name, url, description, id: new Date().getTime().toString()
})).then((layer) => {
const getSubLayers = ({ capabilities }) => {
const layers = capabilities && capabilities.Capability && capabilities.Capability.Layer && capabilities.Capability.Layer.Layer;
if (!layers) return [];
return layers.map(l => ({
id: l.Name,
label: l.Title,
description: l.Abstract
}));
};
dispatch({
type: CUSTOM_LAYER_UPLOAD_SUCCESS,
payload: {
...layer,
subLayers: getSubLayers(layer)
}
});
})
.catch(err =>
dispatch({
type: CUSTOM_LAYER_UPLOAD_ERROR,
payload: { error: err.message }
})
);
} else {
dispatch({
type: CUSTOM_LAYER_UPLOAD_ERROR,
payload: { error: 'Please insert a valid url' }
});
}
};
const includeSubLayersDescription = (layer) => {
const activeLayers = layer.subLayers.filter(l => layer.subLayersActives.includes(l.id));
return `${layer.description} </br> ${activeLayers.reduce((acc, l) =>
`${acc} <strong>${l.label}:</strong> ${l.description || 'No description provided'} </br></br>`, '')
}`;
};
export const confirmCustomLayer = layer => (dispatch, getState) => {
const { previewLayer } = getState().customLayer;
const newLayer = {
...previewLayer,
name: layer.name,
description: layer.description
};
if (layer.subtype === 'wms') {
if (!layer.subLayersActives || !layer.subLayersActives.length > 0) {
dispatch({
type: CUSTOM_LAYER_UPLOAD_ERROR,
payload: { error: 'Please select at least one sublayer' }
});
return null;
}
newLayer.url = getWMSURLFilterdByLayers(newLayer, layer.subLayersActives);
newLayer.description = includeSubLayersDescription(newLayer);
newLayer.subtype = CUSTOM_LAYERS_SUBTYPES.raster;
}
dispatch(setLayerManagementModalVisibility(false));
dispatch(addCustomLayer(newLayer.subtype, newLayer.id, newLayer.url, newLayer.name, newLayer.description));
dispatch(addCustomGLLayer(newLayer.subtype, newLayer.id, newLayer.url, newLayer.data));
dispatch({ type: CUSTOM_LAYER_RESET });
return null;
};
export const resetCustomLayerForm = () => ({
type: CUSTOM_LAYER_RESET
});