Search in sources :

Example 46 with Data

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);
    }
}
Also used : RawDataService(org.n52.series.spi.srv.RawDataService) InputStream(java.io.InputStream) InternalServerException(org.n52.web.exception.InternalServerException) BadRequestException(org.n52.web.exception.BadRequestException) IOException(java.io.IOException) ResourceNotFoundException(org.n52.web.exception.ResourceNotFoundException)

Example 47 with Data

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());
}
Also used : RequestSimpleParameterSet(org.n52.io.request.RequestSimpleParameterSet) IoParameters(org.n52.io.request.IoParameters) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 48 with Data

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());
}
Also used : IoParameters(org.n52.io.request.IoParameters) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 49 with Data

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;
}
Also used : SimpleCsvIoHandler(org.n52.io.handler.simple.SimpleCsvIoHandler) QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) ChartIoHandler(org.n52.io.type.quantity.handler.img.ChartIoHandler) Constants(org.n52.io.Constants) IoParameters(org.n52.io.request.IoParameters) PDFReportGenerator(org.n52.io.type.quantity.handler.report.PDFReportGenerator)

Example 50 with Data

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;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) Data(org.n52.io.response.dataset.Data)

Aggregations

IoParameters (org.n52.io.request.IoParameters)25 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)18 Data (org.n52.io.response.dataset.Data)13 IOException (java.io.IOException)12 QuantityValue (org.n52.io.response.dataset.quantity.QuantityValue)11 HashMap (java.util.HashMap)6 RequestSimpleParameterSet (org.n52.io.request.RequestSimpleParameterSet)6 QuantityData (org.n52.io.response.dataset.quantity.QuantityData)6 InputStream (java.io.InputStream)5 XmlObject (org.apache.xmlbeans.XmlObject)5 IoParseException (org.n52.io.IoParseException)5 File (java.io.File)4 Test (org.junit.Test)4 InternalServerException (org.n52.web.exception.InternalServerException)4 ModelAndView (org.springframework.web.servlet.ModelAndView)4 BigDecimal (java.math.BigDecimal)3 ElasticsearchAwareTest (org.n52.iceland.statistics.basetests.ElasticsearchAwareTest)3 ResultTimeClassifiedData (org.n52.io.format.ResultTimeClassifiedData)3 RawDataService (org.n52.series.spi.srv.RawDataService)3 DecodingException (org.n52.svalbard.decode.exception.DecodingException)3