Skip to content

Commit

Permalink
Merge pull request #206 from WorldBank-Transport/develop
Browse files Browse the repository at this point in the history
Release new version
  • Loading branch information
olafveerman authored May 21, 2019
2 parents f5630fc + caefe54 commit 31829ec
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import _ from 'lodash';
import c from 'classnames';

import config from '../../../config';
import { limitHelper, getPropInsensitive } from '../../../utils/utils';
import { limitHelper, getPropInsensitive, readFileAsJSON } from '../../../utils/utils';
import { t } from '../../../utils/i18n';
import { postFormdata, fetchJSON } from '../../../actions';
import { showGlobalLoading, hideGlobalLoading } from '../../global-loading';
Expand Down Expand Up @@ -413,22 +413,3 @@ ModalOrigins.propTypes = {
};

export default ModalOrigins;

function readFileAsJSON (file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();

reader.onerror = err => reject(err);

reader.onload = e => {
try {
let json = JSON.parse(e.target.result);
return resolve(json);
} catch (err) {
return reject(err);
}
};

reader.readAsText(file);
});
}
30 changes: 28 additions & 2 deletions app/assets/scripts/components/project/source-modals/modal-poi.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import c from 'classnames';
import _ from 'lodash';

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

this.setState({ fileFields });
showGlobalLoading();

// File contents.
readFileAsJSON(file)
.then(res => {
const isInvalid = res.features.some(feat => {
const invalidLon = coordExtract(feat.geometry.coordinates, 'lon').some(coord => coord > 180 || coord < -180);
const invalidLat = coordExtract(feat.geometry.coordinates, 'lat').some(coord => coord > 90 || coord < -90);
return invalidLon || invalidLat;
});

hideGlobalLoading();

if (isInvalid) {
let msg = t('Invalid coordinates found on selected file. Ensure that the projection is EPSG:4326 (WGS84)');
this.props._showAlert('danger', <p>{msg}</p>, true);
return;
}

this.setState({ fileFields });
})
.catch(err => {
hideGlobalLoading();
let msg = err instanceof Error ? err.message : t('Invalid file selected: Not a valid GeoJSON file');

return this.props._showAlert('danger', <p>{msg}</p>, true);
});
}

onSubtypeChange (id, event) {
Expand Down
41 changes: 41 additions & 0 deletions app/assets/scripts/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,44 @@ export function getPropInsensitive (object, prop) {
// If not found return prop.
return Object.keys(object).find(k => k.toLowerCase() === prop) || prop;
}

export function readFileAsJSON (file) {
return new Promise((resolve, reject) => {
let reader = new FileReader();

reader.onerror = err => reject(err);

reader.onload = e => {
try {
let json = JSON.parse(e.target.result);
return resolve(json);
} catch (err) {
return reject(err);
}
};

reader.readAsText(file);
});
}

/**
* Extracts all the coordinates of a type from an array.
* Assumes Longitude to be the first position and latitude to be the secons.
*
* @param {array} arr The array which can be flat or deep like a multipolygon
* @param {string} what What to search for (lon|lat)
*
* @returns {array}
*/
export function coordExtract (arr, what) {
const mapp = {lat: 1, lon: 0};
if (arr.length === 2 &&
typeof arr[0] === 'number' &&
typeof arr[1] === 'number') {
return [arr[mapp[what]]];
} else {
return arr.reduce((acc, v) => {
return [...acc, ...coordExtract(v, what)];
}, []);
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ram-frontend",
"version": "0.8.2",
"version": "0.9.0",
"description": "User interface of the Rural Accessibility Map",
"repository": {
"type": "git",
Expand Down

0 comments on commit 31829ec

Please sign in to comment.