Skip to content
Snippets Groups Projects
Commit 2679cdc3 authored by Timothee P's avatar Timothee P :sunflower:
Browse files

remove feature_id in csv imported file

parent 69727156
No related branches found
No related tags found
No related merge requests found
...@@ -633,10 +633,10 @@ export default { ...@@ -633,10 +633,10 @@ export default {
try { try {
fr.readAsText(this.csvFileToImport); fr.readAsText(this.csvFileToImport);
fr.onloadend = () => { fr.onloadend = () => {
let content = fr.result;
// Find csv delimiter // Find csv delimiter
const commaDelimited = fr.result.split('\n')[0].includes(','); const commaDelimited = content.split('\n')[0].includes(',');
const semicolonDelimited = fr.result.split('\n')[0].includes(';'); const semicolonDelimited = content.split('\n')[0].includes(';');
const delimiter = commaDelimited && !semicolonDelimited ? ',' : semicolonDelimited ? ';' : false; const delimiter = commaDelimited && !semicolonDelimited ? ',' : semicolonDelimited ? ';' : false;
if ((commaDelimited && semicolonDelimited) || !delimiter) { if ((commaDelimited && semicolonDelimited) || !delimiter) {
...@@ -644,32 +644,39 @@ export default { ...@@ -644,32 +644,39 @@ export default {
this.featureTypeImporting = false; this.featureTypeImporting = false;
return; return;
} }
// Check if file contains 'lat' and 'long' fields // Check if file contains 'lat' and 'long' fields
const headersLine = const headers = content
fr.result .split('\n')[0]
.split('\n')[0] .split(delimiter)
.split(delimiter) .map(el => {
.map(el => { return el.replace('\r', '');
return el.replace('\r', ''); });
}) const headersCoord = headers.filter(el => {
.filter(el => { return el === 'lat' || el === 'lon';
return el === 'lat' || el === 'lon'; });
});
// Look for 2 decimal fields in first line of csv // Look for 2 decimal fields in first line of csv
// corresponding to lon and lat // corresponding to lon and lat
const sampleLine = const sampleLine =
fr.result content
.split('\n')[1] .split('\n')[1]
.split(delimiter) .split(delimiter)
.map(el => { .map(el => {
return !isNaN(el) && el.indexOf('.') !== -1; return !isNaN(el) && el.indexOf('.') !== -1;
}) })
.filter(Boolean); .filter(Boolean);
if (sampleLine.length > 1 && headersLine.length === 2) { if (sampleLine.length > 1 && headersCoord.length === 2) {
this.csvError = null; this.csvError = null;
this.csvImport = csvToJson(fr.result, delimiter); //* remove feature_id if found in headers
const featureIdIndex = headers.indexOf('feature_id');
if (featureIdIndex > -1) {
content = content.split('\n')
.reduce((csvString, rowStr) => {
let rowArray = rowStr.split(delimiter);
rowArray.splice(featureIdIndex, 1); // remove feature_id in header and features
return csvString += rowArray.join(delimiter) + '\n';
}, '');
}
this.csvImport = csvToJson(content, delimiter);
this.featureTypeImporting = false; this.featureTypeImporting = false;
//* stock filename to import features afterward //* stock filename to import features afterward
this.SET_FILE_TO_IMPORT(this.csvFileToImport); this.SET_FILE_TO_IMPORT(this.csvFileToImport);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment