GIScience/ohsome-api

Inconsistent CSV output

mcauer opened this issue · 1 comments

Bug Description

The way the CSV output is configured differs between endpoints.
Sometimes fields are quoted with double-quotes (/elements/count) and s.t. not (/elements/count/groupBy/boundary)
E.g.:

elements/count/

curl -X GET "https://api.ohsome.org/v1/elements/count?bboxes=8.67%2C49.39%2C8.71%2C49.42&filter=type%3Away%20and%20natural%3D*&format=csv&time=2014-01-01%2F2017-01-01%2FP1Y" -H  "accept: application/json"

# Copyright URL: https://ohsome.org/copyrights
# Copyright Text: © OpenStreetMap contributors
# API Version: 1.6.3
timestamp;value
"2014-01-01T00:00:00Z";"42.0"
"2015-01-01T00:00:00Z";"42.0"
"2016-01-01T00:00:00Z";"43.0"
"2017-01-01T00:00:00Z";"43.0"

elements/count/groupBy/boundary

curl -X GET "https://api.ohsome.org/v1/elements/count/groupBy/boundary?bboxes=8.67%2C49.39%2C8.71%2C49.42&filter=type%3Away%20and%20natural%3D*&format=csv&time=2014-01-01%2F2017-01-01%2FP1Y" -H  "accept: application/json"

# Copyright URL: https://ohsome.org/copyrights
# Copyright Text: © OpenStreetMap contributors
# API Version: 1.6.3
timestamp;boundary1
2014-01-01T00:00:00Z;42.0
2015-01-01T00:00:00Z;42.0
2016-01-01T00:00:00Z;43.0
2017-01-01T00:00:00Z;43.0

General Information

Please include the following general information about the issue and list any additional steps needed to reproduce the bug.

  • Version of the ohsome API : v1.6.3
  • Which API instance was requested: https://api.ohsome.org/v1`
  • Affected endpoint(s) /elements/count and /elements/count/groupbBy/boundary may be more
  • URL of your request (and request body if applicable): see above
  • Used HTTP method [GET or POST]: see above
  • Utilized tool/library for the request [e.g. cURL, Postman, ohsome-py, etc.]: see above

Expected Behaviour

Expected is that all ohsomeAPI CSV output follows the same encoding

Further Information

Error Messages, Logs, Screenshots

If applicable, add printed error messages, log files or screenshots to help explain your problem.

Additional Information

Add any other further information about the problem here.

Notes

Please consider to upload a file if request parameters, responses, error messages or logs are too long or poorly displayed in your bug report.

The issue seems to be that the grouping results are created differently than the regular results, see

if (resultSet instanceof ElementsResult[]) {
ElementsResult[] rs = (ElementsResult[]) resultSet;
writer.writeNext(new String[] {"timestamp", "value"}, false);
for (ElementsResult elementsResult : rs) {
writer.writeNext(new String[] {elementsResult.getTimestamp(),
String.valueOf(elementsResult.getValue())});
}
vs.
} else if (resultSet instanceof GroupByResult[]) {
GroupByObject[] rs = (GroupByResult[]) resultSet;
if (resultSet.length == 0) {
writer.writeNext(new String[] {"timestamp"}, false);
} else {
var rows = createCsvResponseForElementsGroupBy(rs);
writer.writeNext(rows.getLeft().toArray(new String[rows.getLeft().size()]), false);
writer.writeAll(rows.getRight(), false);
}
}
and
private ImmutablePair<List<String>, List<String[]>> createCsvResponseForElementsGroupBy(
GroupByObject[] resultSet) {
List<String> columnNames = new LinkedList<>();
columnNames.add("timestamp");
List<String[]> rows = new LinkedList<>();
for (int i = 0; i < resultSet.length; i++) {
GroupByResult groupByResult = (GroupByResult) resultSet[i];
Object groupByObject = groupByResult.getGroupByObject();
if (groupByObject instanceof Object[]) {
Object[] groupByObjectArr = (Object[]) groupByObject;
columnNames.add(groupByObjectArr[0].toString() + "_" + groupByObjectArr[1].toString());
} else {
columnNames.add(groupByObject.toString());
}
for (int j = 0; j < groupByResult.getResult().length; j++) {
ElementsResult elemResult = (ElementsResult) groupByResult.getResult()[j];
if (i == 0) {
String[] row = new String[resultSet.length + 1];
row[0] = elemResult.getTimestamp();
row[1] = String.valueOf(elemResult.getValue());
rows.add(row);
} else {
rows.get(j)[i + 1] = String.valueOf(elemResult.getValue());
}
}
}
return new ImmutablePair<>(columnNames, rows);
}
.