Search in sources :

Example 51 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class DouglasPeuckerGeneralizer method generalize.

private Data<QuantityValue> generalize(Data<QuantityValue> timeseries) throws GeneralizerException {
    QuantityValue[] originalValues = getValueArray(timeseries);
    if ((originalValues.length < 3) || (toleranceValue <= 0)) {
        return timeseries;
    }
    if ((maxEntries != -1) && (originalValues.length > maxEntries)) {
        throw new GeneralizerException("Maximum number of entries exceeded (" + originalValues.length + ">" + maxEntries + ")!");
    }
    Data<QuantityValue> generalizedTimeseries = new Data<>(timeseries.getMetadata());
    QuantityValue[] generalizedValues = recursiveGeneralize(timeseries);
    generalizedTimeseries.addValues(generalizedValues);
    // add first element if new list is empty
    if (generalizedValues.length == 0) /* && originalValues.length > 0*/
    {
        generalizedTimeseries.addNewValue(originalValues[0]);
    }
    // add the last one if not already contained!
    if (generalizedValues.length > 0) /* && originalValues.length > 0*/
    {
        QuantityValue lastOriginialValue = originalValues[originalValues.length - 1];
        QuantityValue lastGeneralizedValue = generalizedValues[generalizedValues.length - 1];
        if (!lastGeneralizedValue.getTimestamp().equals(lastOriginialValue.getTimestamp())) {
            generalizedTimeseries.addNewValue(lastOriginialValue);
        }
    }
    return generalizedTimeseries;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) Data(org.n52.io.response.dataset.Data)

Example 52 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class ChartIoHandler method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<Data<QuantityValue>> data, OutputStream stream) throws IoParseException {
    try {
        writeDataToChart(data);
        ImageIO.write(createImage(), mimeType.getFormatName(), stream);
    } catch (IOException e) {
        throw new IoParseException("Could not write image to output stream.", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) IOException(java.io.IOException)

Example 53 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class PDFReportGenerator method encodeAndWriteTo.

@Override
public void encodeAndWriteTo(DataCollection<Data<QuantityValue>> data, OutputStream stream) throws IoHandlerException {
    try {
        generateOutput(data);
        DefaultConfigurationBuilder cfgBuilder = new DefaultConfigurationBuilder();
        Configuration cfg = cfgBuilder.build(document.newInputStream());
        URI baseURI = new File(".").toURI();
        FopFactory fopFactory = new FopFactoryBuilder(baseURI).setConfiguration(cfg).build();
        final String mimeType = Constants.APPLICATION_PDF;
        Fop fop = fopFactory.newFop(mimeType, stream);
        // FopFactory fopFactory = FopFactory.newInstance(cfg);
        // Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // FopFactory fopFactory = fopFactoryBuilder.build();
        // Fop fop = fopFactory.newFop(APPLICATION_PDF.getMimeType(), stream);
        // Create PDF via XSLT transformation
        TransformerFactory transFact = TransformerFactory.newInstance();
        StreamSource transformationRule = getTransforamtionRule();
        Transformer transformer = transFact.newTransformer(transformationRule);
        Source source = new StreamSource(document.newInputStream());
        Result result = new SAXResult(fop.getDefaultHandler());
        if (LOGGER.isDebugEnabled()) {
            try {
                File tempFile = File.createTempFile(TEMP_FILE_PREFIX, ".xml");
                StreamResult debugResult = new StreamResult(tempFile);
                transformer.transform(source, debugResult);
                String xslResult = XmlObject.Factory.parse(tempFile).xmlText();
                LOGGER.debug("xsl-fo input (locale '{}'): {}", i18n.getTwoDigitsLanguageCode(), xslResult);
            } catch (IOException | TransformerException | XmlException e) {
                LOGGER.error("Could not debug XSL result output!", e);
            }
        }
        // XXX debug, diagram is not embedded
        transformer.transform(source, result);
    } catch (FOPException e) {
        throw new IoParseException("Failed to create Formatting Object Processor (FOP)", e);
    } catch (ConfigurationException e) {
        throw new IoParseException("Failed to read config for Formatting Object Processor (FOP)", e);
    } catch (TransformerConfigurationException e) {
        throw new IoParseException("Invalid transform configuration. Inspect xslt!", e);
    } catch (TransformerException e) {
        throw new IoParseException("Could not generate PDF report!", e);
    }
}
Also used : IoParseException(org.n52.io.IoParseException) DefaultConfigurationBuilder(org.apache.fop.configuration.DefaultConfigurationBuilder) TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) Configuration(org.apache.fop.configuration.Configuration) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) Fop(org.apache.fop.apps.Fop) StreamSource(javax.xml.transform.stream.StreamSource) FopFactory(org.apache.fop.apps.FopFactory) IOException(java.io.IOException) URI(java.net.URI) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) FopFactoryBuilder(org.apache.fop.apps.FopFactoryBuilder) FOPException(org.apache.fop.apps.FOPException) SAXResult(javax.xml.transform.sax.SAXResult) ConfigurationException(org.apache.fop.configuration.ConfigurationException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) XmlException(org.apache.xmlbeans.XmlException) File(java.io.File) TransformerException(javax.xml.transform.TransformerException)

Example 54 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class PDFReportGenerator method addDataTable.

private void addDataTable(TimeSeries timeseries, TimeseriesMetadataOutput metadata, TvpDataCollection<Data<QuantityValue>> dataCollection) {
    TableType dataTable = timeseries.addNewTable();
    // TODO add language context
    dataTable.setLeftColHeader("Date");
    dataTable.setRightColHeader(createValueTableHeader(metadata));
    Data<QuantityValue> data = dataCollection.getSeries(metadata.getId());
    for (QuantityValue valueEntry : data.getValues()) {
        Entry entry = dataTable.addNewEntry();
        // TODO update TableType schema to allow start/end time
        entry.setTime(new DateTime(valueEntry.getTimestamp()).toString());
        BigDecimal value = valueEntry.getValue();
        entry.setValue(value != null ? value.toString() : null);
    }
}
Also used : Entry(org.n52.oxf.TableType.Entry) TableType(org.n52.oxf.TableType) QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) DateTime(org.joda.time.DateTime) BigDecimal(java.math.BigDecimal)

Example 55 with Data

use of org.n52.io.response.dataset.Data in project series-rest-api by 52North.

the class PDFReportGenerator method generateTimeseriesMetadata.

private void generateTimeseriesMetadata() {
    for (DatasetOutput<?> metadata : getAllDatasetMetadatas()) {
        TimeSeries timeseries = addTimeseries(metadata);
        // addDataTable(timeseries, metadata, data);
        addMetadata(timeseries, metadata);
    }
}
Also used : TimeSeries(org.n52.oxf.DocumentStructureType.TimeSeries)

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