Skip to content

Commit 31829ec

Browse files
authored
Merge pull request #206 from WorldBank-Transport/develop
Release new version
2 parents f5630fc + caefe54 commit 31829ec

File tree

4 files changed

+71
-23
lines changed

4 files changed

+71
-23
lines changed

app/assets/scripts/components/project/source-modals/modal-origins.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import _ from 'lodash';
44
import c from 'classnames';
55

66
import config from '../../../config';
7-
import { limitHelper, getPropInsensitive } from '../../../utils/utils';
7+
import { limitHelper, getPropInsensitive, readFileAsJSON } from '../../../utils/utils';
88
import { t } from '../../../utils/i18n';
99
import { postFormdata, fetchJSON } from '../../../actions';
1010
import { showGlobalLoading, hideGlobalLoading } from '../../global-loading';
@@ -413,22 +413,3 @@ ModalOrigins.propTypes = {
413413
};
414414

415415
export default ModalOrigins;
416-
417-
function readFileAsJSON (file) {
418-
return new Promise((resolve, reject) => {
419-
let reader = new FileReader();
420-
421-
reader.onerror = err => reject(err);
422-
423-
reader.onload = e => {
424-
try {
425-
let json = JSON.parse(e.target.result);
426-
return resolve(json);
427-
} catch (err) {
428-
return reject(err);
429-
}
430-
};
431-
432-
reader.readAsText(file);
433-
});
434-
}

app/assets/scripts/components/project/source-modals/modal-poi.js

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import c from 'classnames';
44
import _ from 'lodash';
55

66
import config from '../../../config';
7-
import { limitHelper } from '../../../utils/utils';
7+
import { limitHelper, readFileAsJSON, coordExtract } from '../../../utils/utils';
88
import { t } from '../../../utils/i18n';
99
import { postFormdata, fetchJSON } from '../../../actions';
1010
import { showGlobalLoading, hideGlobalLoading } from '../../global-loading';
@@ -103,7 +103,33 @@ class ModalPoi extends ModalBase {
103103
fileFields[idx].size = file.size;
104104
fileFields[idx].uploaded = 0;
105105

106-
this.setState({ fileFields });
106+
showGlobalLoading();
107+
108+
// File contents.
109+
readFileAsJSON(file)
110+
.then(res => {
111+
const isInvalid = res.features.some(feat => {
112+
const invalidLon = coordExtract(feat.geometry.coordinates, 'lon').some(coord => coord > 180 || coord < -180);
113+
const invalidLat = coordExtract(feat.geometry.coordinates, 'lat').some(coord => coord > 90 || coord < -90);
114+
return invalidLon || invalidLat;
115+
});
116+
117+
hideGlobalLoading();
118+
119+
if (isInvalid) {
120+
let msg = t('Invalid coordinates found on selected file. Ensure that the projection is EPSG:4326 (WGS84)');
121+
this.props._showAlert('danger', <p>{msg}</p>, true);
122+
return;
123+
}
124+
125+
this.setState({ fileFields });
126+
})
127+
.catch(err => {
128+
hideGlobalLoading();
129+
let msg = err instanceof Error ? err.message : t('Invalid file selected: Not a valid GeoJSON file');
130+
131+
return this.props._showAlert('danger', <p>{msg}</p>, true);
132+
});
107133
}
108134

109135
onSubtypeChange (id, event) {

app/assets/scripts/utils/utils.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,44 @@ export function getPropInsensitive (object, prop) {
126126
// If not found return prop.
127127
return Object.keys(object).find(k => k.toLowerCase() === prop) || prop;
128128
}
129+
130+
export function readFileAsJSON (file) {
131+
return new Promise((resolve, reject) => {
132+
let reader = new FileReader();
133+
134+
reader.onerror = err => reject(err);
135+
136+
reader.onload = e => {
137+
try {
138+
let json = JSON.parse(e.target.result);
139+
return resolve(json);
140+
} catch (err) {
141+
return reject(err);
142+
}
143+
};
144+
145+
reader.readAsText(file);
146+
});
147+
}
148+
149+
/**
150+
* Extracts all the coordinates of a type from an array.
151+
* Assumes Longitude to be the first position and latitude to be the secons.
152+
*
153+
* @param {array} arr The array which can be flat or deep like a multipolygon
154+
* @param {string} what What to search for (lon|lat)
155+
*
156+
* @returns {array}
157+
*/
158+
export function coordExtract (arr, what) {
159+
const mapp = {lat: 1, lon: 0};
160+
if (arr.length === 2 &&
161+
typeof arr[0] === 'number' &&
162+
typeof arr[1] === 'number') {
163+
return [arr[mapp[what]]];
164+
} else {
165+
return arr.reduce((acc, v) => {
166+
return [...acc, ...coordExtract(v, what)];
167+
}, []);
168+
}
169+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ram-frontend",
3-
"version": "0.8.2",
3+
"version": "0.9.0",
44
"description": "User interface of the Rural Accessibility Map",
55
"repository": {
66
"type": "git",

0 commit comments

Comments
 (0)