Search in sources :

Example 36 with Data

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

the class QuantityIoProcessChain method getData.

@Override
public DataCollection<Data<QuantityValue>> getData() {
    boolean generalize = parameters.isGeneralize();
    DataService<Data<QuantityValue>> service = generalize ? new GeneralizingQuantityService(dataService) : dataService;
    return service.getData(parameters);
}
Also used : GeneralizingQuantityService(org.n52.io.type.quantity.generalize.GeneralizingQuantityService) ResultTimeClassifiedData(org.n52.io.format.ResultTimeClassifiedData) Data(org.n52.io.response.dataset.Data)

Example 37 with Data

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

the class DouglasPeuckerGeneralizer method recursiveGeneralize.

private QuantityValue[] recursiveGeneralize(Data<QuantityValue> 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
        Data<QuantityValue> generalizedData = new Data<>();
        Data<QuantityValue> firstPartToBeGeneralized = new Data<>();
        Data<QuantityValue> restPartToBeGeneralized = new Data<>();
        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) Data(org.n52.io.response.dataset.Data) Line2D(java.awt.geom.Line2D)

Example 38 with Data

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

the class LargestTriangleThreeBucketsGeneralizerTest method getData.

private Data<QuantityValue> getData(int maxValues) {
    BigDecimal startValue = BigDecimal.valueOf(0);
    QuantityValue current = createQuantityValue(DateTime.now(), startValue);
    Data<QuantityValue> data = new Data<>();
    for (int i = 0; i < maxValues; i++) {
        data.addNewValue(current);
        current = getNextDataValue(current);
    }
    return data;
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) Data(org.n52.io.response.dataset.Data) BigDecimal(java.math.BigDecimal)

Example 39 with Data

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

the class LargestTriangleThreeBucketsGeneralizerTest method when_quotientHasNonterminatingDecimals_then_noArithmeticExceptionIsThrown.

@Test
public void when_quotientHasNonterminatingDecimals_then_noArithmeticExceptionIsThrown() throws GeneralizerException {
    // https://github.com/52North/series-rest-api/issues/446
    TvpDataCollection<Data<QuantityValue>> collection = new TvpDataCollection<>();
    collection.addNewSeries("test", getData(10000));
    long threshold = 100L;
    IoParameters defaults = IoParameters.createDefaults().extendWith("threshold", Long.toString(threshold));
    Generalizer<Data<QuantityValue>> generalizer = new LargestTriangleThreeBucketsGeneralizer(defaults);
    DataCollection<Data<QuantityValue>> generalizedData = generalizer.generalize(collection);
    assertThat(generalizedData.getSeries("test").size(), Is.is(threshold));
}
Also used : TvpDataCollection(org.n52.io.TvpDataCollection) LargestTriangleThreeBucketsGeneralizer(org.n52.io.type.quantity.generalize.LargestTriangleThreeBucketsGeneralizer) Data(org.n52.io.response.dataset.Data) IoParameters(org.n52.io.request.IoParameters) Test(org.junit.jupiter.api.Test)

Example 40 with Data

use of org.n52.io.response.dataset.Data 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 avgTimestamp = 0d;
    BigDecimal avgValue = BigDecimal.ZERO;
    int amountOfNodataValues = 0;
    boolean noDataThresholdExceeded = false;
    boolean unixTime = false;
    for (; avgRangeStart < avgRangeEnd; avgRangeStart++) {
        final QuantityValue current = data[avgRangeStart];
        avgTimestamp += current.getTimestamp().getMillis();
        unixTime = current.getTimestamp().isUnixTime();
        if (noDataThresholdExceeded) {
            // keep on calc avg timestamp
            continue;
        }
        if (current.isNoDataValue()) {
            amountOfNodataValues++;
            if (amountOfNodataValues == noDataGapThreshold) {
                noDataThresholdExceeded = true;
            }
        } else {
            avgValue = avgValue.add(current.getValue());
        }
    }
    avgTimestamp /= avgRangeLength;
    avgValue = avgValue.divide(BigDecimal.valueOf(avgRangeLength), MathContext.DECIMAL128);
    return new BucketAverage(avgTimestamp, avgValue, unixTime);
}
Also used : QuantityValue(org.n52.io.response.dataset.quantity.QuantityValue) BigDecimal(java.math.BigDecimal)

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