All Downloads are FREE. Search and download functionalities are using the official Maven repository.

eu.agrosense.client.grid.util.AggregationGrid Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2008-2012 AgroSense Foundation.
 *
 * AgroSense is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * There are special exceptions to the terms and conditions of the GPLv3 as it is applied to
 * this software, see the FLOSS License Exception
 * .
 *
 * AgroSense is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with AgroSense.  If not, see .
 * 
 * Contributors:
 *    johan - initial API and implementation and/or initial documentation
 */
package eu.agrosense.client.grid.util;

import nl.cloudfarming.client.geoviewer.Grid;

/**
 * Intermediate grid where the aggregate cell values will be accumulated.
 * 
 * Acts as a facade for the transformation and aggregation strategies.
 *
 * @param  context type
 * @author johan
 */
public class AggregationGrid {
    private final int w, h;
    // indexed by (row,column), (0,0) matches top left cell/pixel.
    private final AggregationStrategy[][] grid;
    private final TransformationStrategy transformer;
    private final AggregationStrategy.Factory aggFactory;

    public AggregationGrid(int w, int h, TransformationStrategy transformer, AggregationStrategy.Factory aggFactory) {
        this.w = w;
        this.h = h;
        this.grid = new AggregationStrategy[h][w];
        this.aggFactory = aggFactory;
        this.transformer = transformer;
    }

    /**
     * Add a value to the intermediate grid.
     * 
     * @param x column index
     * @param y row index
     * @param value the value to aggregate
     * @param context
     */
    public final void add(int x, int y, double value, C context) {
        Double transformed = transformer.transformIn(value, context);
        if (transformed != null) {
            AggregationStrategy agg = grid[y][x];
            if (agg == null) {
                grid[y][x] = aggFactory.create(transformed);
            } else {
                agg.add(transformed);
            }
        }
    }

    /**
     * Read a single aggregated value from the grid.
     * 
     * @param x column index
     * @param y row index
     * @return the aggregated value, or the empty cell sentinel when no aggregate was computed.
     */
    public final double get(int x, int y) {
        AggregationStrategy agg = grid[y][x];
        if (agg != null) {
            Double transformed = transformer.transformOut(agg);
            if (transformed != null) { return transformed; }
        }
        return Grid.EMPTY_VALUE;
    }

    // convenience methods for adding all samples to an int-backed raster:
    public int[] getIntValues() {
        // merge grid into single array for use with raster:
        int[] allValues = new int[w * h];
        for (int i = 0; i < w; i++) {
            for (int j = 0; j < h; j++) {
                allValues[i + w * j] = (int) get(i, j);
            }
        }
        return allValues;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy