Search in sources :

Example 1 with QuantityValue

use of org.n52.shetland.ogc.om.values.QuantityValue in project series-rest-api by 52North.

the class LargestTriangleThreeBucketsGeneralizer method calculateBucketAverage.

private BucketAverage calculateBucketAverage(int bucketIndex, double bucketSize, QuantityValue[] data) {
    int dataLength = data.length;
    int avgRangeStart = (int) Math.floor((bucketIndex + 0) * bucketSize) + 1;
    int avgRangeEnd = (int) Math.floor((bucketIndex + 1) * bucketSize) + 1;
    avgRangeEnd = avgRangeEnd < dataLength ? avgRangeEnd : dataLength;
    double avgRangeLength = avgRangeEnd - avgRangeStart;
    Double avgValue = 0d;
    Double avgTimestamp = 0d;
    int amountOfNodataValues = 0;
    boolean noDataThresholdExceeded = false;
    for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
        final QuantityValue current = data[avgRangeStart];
        avgTimestamp += current.getTimestamp();
        if (noDataThresholdExceeded) {
            // keep on calc avg timestamp
            continue;
        }
        if (current.isNoDataValue()) {
            amountOfNodataValues++;
            if (amountOfNodataValues == noDataGapThreshold) {
                noDataThresholdExceeded = true;
            }
        } else {
            avgValue += current.getValue();
        }
    }
    avgTimestamp /= avgRangeLength;
    avgValue /= avgRangeLength;
    return new BucketAverage(avgTimestamp, avgValue);
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue)

Example 2 with QuantityValue

use of org.n52.shetland.ogc.om.values.QuantityValue in project series-rest-api by 52North.

the class LargestTriangleThreeBucketsGeneralizer method generalizeData.

private QuantityData generalizeData(QuantityValue[] data) {
    int dataLength = data.length;
    // Bucket size. Leave room for start and end data points
    double bucketSize = ((double) dataLength - 2) / (maxOutputValues - 2);
    int pointIndex = 0;
    QuantityData sampled = new QuantityData();
    sampled.addValues(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();
        QuantityValue maxAreaPoint = new QuantityValue(fallBackTimestamp, null);
        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.addValues(maxAreaPoint);
        // This a is the next a
        pointIndex = nextPointIndex;
    }
    // Always add last value
    sampled.addValues(data[dataLength - 1]);
    return sampled;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Example 3 with QuantityValue

use of org.n52.shetland.ogc.om.values.QuantityValue in project series-rest-api by 52North.

the class HighchartFormatter method formatSeries.

private List<Number[]> formatSeries(QuantityData timeseries) {
    List<Number[]> series = new ArrayList<>();
    for (QuantityValue currentValue : timeseries.getValues()) {
        List<Number> list = new ArrayList<>();
        list.add(currentValue.getTimestamp());
        list.add(currentValue.getValue());
        if (currentValue.isSetGeometry()) {
            Coordinate coordinate = currentValue.getGeometry().getCoordinate();
            list.add(coordinate.x);
            list.add(coordinate.y);
            if (!Double.isNaN(coordinate.z)) {
                list.add(coordinate.z);
            }
        }
        series.add(list.toArray(new Number[0]));
    }
    return series;
}
Also used : Coordinate(com.vividsolutions.jts.geom.Coordinate) QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) ArrayList(java.util.ArrayList)

Example 4 with QuantityValue

use of org.n52.shetland.ogc.om.values.QuantityValue in project series-rest-api by 52North.

the class DouglasPeuckerGeneralizer method generalize.

private QuantityData generalize(QuantityData 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 + ")!");
    }
    QuantityData generalizedTimeseries = new QuantityData();
    QuantityValue[] generalizedValues = recursiveGeneralize(timeseries);
    generalizedTimeseries.addValues(generalizedValues);
    // add first element if new list is empty
    if (generalizedValues.length == 0) /* && originalValues.length > 0*/
    {
        generalizedTimeseries.addValues(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.addValues(lastOriginialValue);
        }
    }
    return generalizedTimeseries;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Example 5 with QuantityValue

use of org.n52.shetland.ogc.om.values.QuantityValue in project series-rest-api by 52North.

the class DouglasPeuckerGeneralizer method recursiveGeneralize.

private QuantityValue[] recursiveGeneralize(QuantityData timeseries) {
    QuantityValue[] values = getValueArray(timeseries);
    QuantityValue startValue = getFirstValue(timeseries);
    QuantityValue endValue = getLastValue(timeseries);
    Line2D.Double line = createTendencyLine(startValue, endValue);
    // find the point of maximum distance to the line
    int index = 0;
    double maxDist = 0;
    double distance;
    // start and end value are not mentioned
    for (int i = 1; i < values.length - 1; i++) {
        QuantityValue timeseriesValue = values[i];
        distance = calculateDistance(line, timeseriesValue);
        if (distance > maxDist) {
            index = i;
            maxDist = distance;
        }
    }
    if (maxDist < toleranceValue) {
        return getValueArray(timeseries);
    } else {
        // split and handle both parts separately
        QuantityData generalizedData = new QuantityData();
        QuantityData firstPartToBeGeneralized = new QuantityData();
        QuantityData restPartToBeGeneralized = new QuantityData();
        firstPartToBeGeneralized.addValues(Arrays.copyOfRange(values, 0, index));
        restPartToBeGeneralized.addValues(Arrays.copyOfRange(values, index + 1, values.length));
        generalizedData.addValues(recursiveGeneralize(firstPartToBeGeneralized));
        generalizedData.addValues(recursiveGeneralize(restPartToBeGeneralized));
        return getValueArray(generalizedData);
    }
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) Line2D(java.awt.geom.Line2D) QuantityData(org.n52.io.response.dataset.quantity.QuantityData)

Aggregations

QuantityValue (org.n52.shetland.ogc.om.values.QuantityValue)21 QuantityValue (org.n52.io.response.dataset.quantity.QuantityValue)8 CountValue (org.n52.shetland.ogc.om.values.CountValue)7 CategoryValue (org.n52.shetland.ogc.om.values.CategoryValue)6 XmlObject (org.apache.xmlbeans.XmlObject)5 Test (org.junit.Test)5 BooleanValue (org.n52.shetland.ogc.om.values.BooleanValue)5 TextValue (org.n52.shetland.ogc.om.values.TextValue)5 QuantityData (org.n52.io.response.dataset.quantity.QuantityData)4 SingleObservationValue (org.n52.shetland.ogc.om.SingleObservationValue)4 GeometryValue (org.n52.shetland.ogc.om.values.GeometryValue)4 BigDecimal (java.math.BigDecimal)3 List (java.util.List)3 XmlBoolean (org.apache.xmlbeans.XmlBoolean)3 XmlInteger (org.apache.xmlbeans.XmlInteger)3 XmlString (org.apache.xmlbeans.XmlString)3 Geometry (org.locationtech.jts.geom.Geometry)3 SweBoolean (org.n52.shetland.ogc.swe.simpleType.SweBoolean)3 Coordinate (com.vividsolutions.jts.geom.Coordinate)2 ArrayList (java.util.ArrayList)2