use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class DataController method writeRawData.
private void writeRawData(RequestSimpleParameterSet parameters, HttpServletResponse response) throws InternalServerException, ResourceNotFoundException, BadRequestException {
if (!dataService.supportsRawData()) {
throw new BadRequestException("Querying of raw timeseries data is not supported " + "by the underlying service!");
}
final RawDataService rawDataService = dataService.getRawDataService();
try (InputStream inputStream = rawDataService.getRawData(parameters)) {
if (inputStream == null) {
throw new ResourceNotFoundException("No raw data found.");
}
response.setContentType(parameters.getRawFormat());
IOUtils.copyLarge(inputStream, response.getOutputStream());
} catch (IOException e) {
throw new InternalServerException("Error while querying raw data", e);
}
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class DataController method getSeriesReport.
@RequestMapping(value = "/{seriesId}/data", produces = { "application/pdf" }, method = RequestMethod.GET)
public void getSeriesReport(HttpServletResponse response, @PathVariable String seriesId, @RequestHeader(value = Parameters.HttpHeader.ACCEPT_LANGUAGE) String locale, @RequestParam(required = false) MultiValueMap<String, String> query) throws Exception {
RequestUtils.overrideQueryLocaleWhenSet(locale, query);
IoParameters map = QueryParameters.createFromQuery(query);
LOGGER.debug("get data collection report for '{}' with query: {}", seriesId, map);
RequestSimpleParameterSet parameters = RequestSimpleParameterSet.createForSingleSeries(seriesId, map);
checkAgainstTimespanRestriction(parameters.getTimespan());
checkForUnknownDatasetIds(map, seriesId);
final String datasetType = parameters.getValueType();
String outputFormat = MimeType.APPLICATION_PDF.toString();
response.setContentType(outputFormat);
createIoFactory(datasetType).withSimpleRequest(parameters).createHandler(outputFormat).writeBinary(response.getOutputStream());
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class DataController method getSeriesCollectionReport.
@RequestMapping(value = "/data", produces = { "application/pdf" }, method = RequestMethod.POST)
public void getSeriesCollectionReport(HttpServletResponse response, @RequestHeader(value = Parameters.HttpHeader.ACCEPT_LANGUAGE) String locale, @RequestBody RequestStyledParameterSet parameters) throws Exception {
RequestUtils.overrideQueryLocaleWhenSet(locale, parameters);
IoParameters map = QueryParameters.createFromQuery(parameters);
LOGGER.debug("get data collection report with query: {}", map);
checkForUnknownSeriesIds(parameters, parameters.getDatasets());
checkAgainstTimespanRestriction(parameters.getTimespan());
final String datasetType = parameters.getValueType();
String outputFormat = MimeType.APPLICATION_PDF.toString();
response.setContentType(outputFormat);
createIoFactory(datasetType).withStyledRequest(map.mergeToStyledParameterSet(parameters)).withSimpleRequest(map.mergeToSimpleParameterSet(parameters)).createHandler(outputFormat).writeBinary(response.getOutputStream());
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class QuantityIoFactory method createHandler.
@Override
public IoHandler<Data<QuantityValue>> createHandler(String outputMimeType) {
IoParameters parameters = getParameters();
Constants.MimeType mimeType = Constants.MimeType.toInstance(outputMimeType);
if (mimeType == Constants.MimeType.IMAGE_PNG) {
return createMultiChartRenderer(mimeType);
} else if (mimeType == Constants.MimeType.APPLICATION_PDF) {
ChartIoHandler imgRenderer = createMultiChartRenderer(mimeType);
return new PDFReportGenerator(parameters, createProcessChain(), imgRenderer);
} else if (isCsvOutput(mimeType)) {
CsvIoHandler<QuantityValue> handler = new SimpleCsvIoHandler<>(parameters, createProcessChain(), getMetadatas());
boolean zipOutput = parameters.getAsBoolean(Parameters.ZIP, false);
handler.setZipOutput(zipOutput || mimeType == Constants.MimeType.APPLICATION_ZIP);
return handler;
}
String msg = "The requested media type '" + outputMimeType + "' is not supported.";
IllegalArgumentException exception = new IllegalArgumentException(msg);
throw exception;
}
use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.
the class LargestTriangleThreeBucketsGeneralizer method generalizeData.
private Data<QuantityValue> generalizeData(final QuantityValue[] data, final DatasetMetadata<QuantityValue> metadata) {
final int dataLength = data.length;
// Bucket size. Leave room for start and end data points
double bucketSize = ((double) dataLength - 2) / (maxOutputValues - 2);
int pointIndex = 0;
Data<QuantityValue> sampled = new Data<>(metadata);
sampled.addNewValue(data[pointIndex]);
for (int bucketIndex = 0; bucketIndex < (maxOutputValues - 2); bucketIndex++) {
// get the range for this bucket
int rangeOff = (int) Math.floor((bucketIndex + 0) * bucketSize) + 1;
int rangeTo = (int) Math.floor((bucketIndex + 1) * bucketSize) + 1;
// first point of triangle
QuantityValue triangleLeft = data[pointIndex];
if (triangleLeft.isNoDataValue()) {
addNodataValue(sampled, triangleLeft.getTimestamp());
pointIndex = rangeTo - 1;
continue;
}
// last point of triangle (next bucket's average)
BucketAverage triangleRight = calculateBucketAverage(bucketIndex + 1, bucketSize, data);
// init fallback value
BucketAverage avgCurrentBucket = calculateBucketAverage(bucketIndex, bucketSize, data);
long fallBackTimestamp = avgCurrentBucket.toTimeseriesValue().getTimestamp().getMillis();
QuantityValue maxAreaPoint = createQuantityValue(fallBackTimestamp, triangleLeft.getTimestamp());
double area;
int amountOfNodataValues = 0;
double maxArea = area = -1;
int nextPointIndex = 0;
for (; rangeOff < rangeTo; rangeOff++) {
// if (triangleRight.isNoDataBucket()) {
// triangleRight = // TODO
// }
// calculate triangle area over three buckets
final QuantityValue triangleMiddle = data[rangeOff];
if (triangleMiddle.isNoDataValue()) {
amountOfNodataValues++;
if (isExceededGapThreshold(amountOfNodataValues, bucketSize)) {
if (triangleMiddle.isNoDataValue()) {
maxAreaPoint = avgCurrentBucket.toTimeseriesValue();
LOGGER.debug("No data value for bucket {}.", bucketIndex);
pointIndex = rangeTo - 1;
break;
}
}
} else {
area = calcTriangleArea(triangleLeft, triangleRight, triangleMiddle);
if (area > maxArea) {
maxArea = area;
maxAreaPoint = triangleMiddle;
nextPointIndex = rangeOff;
}
}
}
// Pick this point from the Bucket
sampled.addNewValue(maxAreaPoint);
// This a is the next a
pointIndex = nextPointIndex;
}
// Always add last value
sampled.addNewValue(data[dataLength - 1]);
return sampled;
}
Aggregations