Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve csv output #103

Merged
merged 5 commits into from
Jun 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 74 additions & 2 deletions components/IdentifyViewer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const IdentifyUtils = require('../utils/IdentifyUtils');
const LayerUtils = require('../utils/LayerUtils');
const MiscUtils = require('../utils/MiscUtils');
const Icon = require('./Icon');
const JSZip = require('jszip');
require('./style/IdentifyViewer.css');

class IdentifyViewer extends React.Component {
Expand Down Expand Up @@ -216,10 +217,10 @@ class IdentifyViewer extends React.Component {
this.export(filteredResults);
}
export = (json) => {
if(this.props.exportFormat.toLowerCase() === 'json') {
if(this.state.format.toLowerCase() === 'json') {
let data = JSON.stringify(json, null, ' ');
FileSaver.saveAs(new Blob([data], {type: "text/plain;charset=utf-8"}), "results.json");
} else if(this.props.exportFormat.toLowerCase() === 'csv') {
} else if(this.state.format.toLowerCase() === 'csv') {
let csv = "";
Object.entries(json).forEach(([layerName, features]) => {
csv += layerName + "\n";
Expand All @@ -236,6 +237,55 @@ class IdentifyViewer extends React.Component {
csv += "\n";
})
FileSaver.saveAs(new Blob([csv], {type: "text/plain;charset=utf-8"}), "results.csv");
} else if(this.state.format.toLowerCase() === 'csv + zip') {
let first = true;
let file = 0 ;
let blobs = [];
let filenames = [];
Object.entries(json).forEach(([layerName, features]) => {
let csv = "";
file += 1;
if (first) {
Object.entries(features[0].properties || {}).forEach(([attrib]) => {
if(attrib !== "htmlContent") {
csv += attrib + ';' ;
}
});
if(features[0].geometry) {
csv += 'geometry';
} else if(csv !== ""){
csv = csv.slice(0, -1); // Remove trailling semi column ;
}
first = false;
csv += '\n';
}
features.forEach(feature => {
Object.entries(feature.properties || {}).forEach(([attrib, value]) => {
csv += String(value).replace('"', '""') + ';';
});
if(feature.geometry) {
csv += stringify(feature.geometry);
} else if(csv !== ""){
csv = csv.slice(0, -1); // Remove trailling semi column ;
}
csv += '\n';
});
first = true;
let blob = new Blob([csv], {type: "text/csv;charset=utf-8"});
blobs.push(blob);
filenames.push(layerName);
})
if (file > 1) {
let zip = new JSZip();
for (var i = 0; i < blobs.length; i++) {
zip.file(filenames[i] + ".csv", blobs[i]);
}
zip.generateAsync({type:"blob"}).then(function (blob){
saveAs(blob, "results.zip");
});
} else {
FileSaver.saveAs(blobs[0], filenames[0] + ".csv");
}
}
}
resultDisplayName = (layer, result) => {
Expand Down Expand Up @@ -430,6 +480,17 @@ class IdentifyViewer extends React.Component {
</div>
{attributes}
<div className="identify-buttonbox">
{
this.props.exportFormat ?
(<div>
<Message msgId="identify.exportformat" />
<select value={this.state.format} onChange={ev => this.setState({format: ev.target.value})}>
<option value="json">json</option>
<option value="csv">csv</option>
<option value="csv + zip">csv + zip</option>
</select>
</div>) : null
}
{this.props.exportFormat ? (<button className="button" onClick={this.exportResults}><Message msgId="identify.export" /></button>) : null}
</div>
</div>
Expand All @@ -453,6 +514,17 @@ class IdentifyViewer extends React.Component {
})}
</div>
<div className="identify-buttonbox">
{
this.props.exportFormat ?
(<div>
<Message msgId="identify.exportformat" />
<select value={this.state.format} onChange={ev => this.setState({format: ev.target.value})}>
<option value="json">json</option>
<option value="csv">csv</option>
<option value="csv + zip">csv + zip</option>
</select>
</div>) : null
}
{this.props.exportFormat ? (<button className="button" onClick={this.exportResults}><Message msgId="identify.export" /></button>) : null}
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"intl": "1.2.5",
"ismobilejs": "0.5.2",
"jsts": "2.0.6",
"jszip": "3.2.0",
"lodash.isempty": "4.4.0",
"lodash.isequal": "4.5.0",
"lodash.omit": "4.5.0",
Expand Down
1 change: 1 addition & 0 deletions translations/data.en-US
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"querying": "Querying...",
"noresults": "No information available for the selected point",
"export": "Export",
"exportformat": "Export format :",
"featureReport": "Feature report",
"noattributes": "No attributes",
"title": "Feature Info",
Expand Down
1 change: 1 addition & 0 deletions translations/data.fr-FR
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
"querying": "Identification en cours...",
"noresults": "Pas de résultats pour la position sélectionnée",
"export": "Exporter",
"exportformat": "Format d'export :",
"featureReport": "Données de l'objet",
"noattributes": "Pas d'attribut",
"title": "Informations sur l'objet",
Expand Down
1 change: 1 addition & 0 deletions translations/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"identify.querying",
"identify.noresults",
"identify.export",
"identify.exportformat",
"identify.featureReport",
"identify.noattributes",
"identify.title",
Expand Down