Search in sources :

Example 1 with GridCoordinates2D

use of org.geotools.coverage.grid.GridCoordinates2D in project polymap4-core by Polymap4.

the class PredefinedColorMapEditor method main.

// Test ***********************************************
public static void main(String[] args) throws Exception {
    File f = new File("/home/falko/Data/ncrast/elevation_4326.tif");
    AbstractGridFormat format = GridFormatFinder.findFormat(f);
    AbstractGridCoverage2DReader reader = format.getReader(f);
    String[] names = reader.getGridCoverageNames();
    GridCoverage2D grid = reader.read(names[0], null);
    GridGeometry2D geometry = grid.getGridGeometry();
    GridEnvelope gridRange = geometry.getGridRange();
    int w = gridRange.getHigh(0);
    int h = gridRange.getHigh(1);
    // all
    Timer timer = new Timer();
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    int c = 0;
    double[] buf = new double[1];
    for (int x = 0; x < w; x++) {
        for (int y = 0; y < h; y++) {
            double[] value = grid.evaluate(new GridCoordinates2D(x, y), buf);
            min = value[0] < min ? value[0] : min;
            max = value[0] > max ? value[0] : max;
            c++;
        }
    }
    System.out.println("min/max: " + min + ".." + max + " (" + c + " in " + timer.elapsedTime() + "ms)");
    // random
    timer.start();
    double[] minMax = new PredefinedColorMapEditor().minMax(grid);
    System.out.println("min/max: " + minMax[0] + ".." + minMax[1] + " (" + c + " in " + timer.elapsedTime() + "ms)");
// final DefaultProcessor proc = new DefaultProcessor(null);
// for (Operation o : proc.getOperations() ){
// System.out.println(o.getName());
// System.out.println(o.getDescription());
// System.out.println();
// }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) AbstractGridCoverage2DReader(org.geotools.coverage.grid.io.AbstractGridCoverage2DReader) Timer(org.polymap.core.runtime.Timer) AbstractGridFormat(org.geotools.coverage.grid.io.AbstractGridFormat) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) File(java.io.File)

Example 2 with GridCoordinates2D

use of org.geotools.coverage.grid.GridCoordinates2D in project OpenTripPlanner by opentripplanner.

the class DynamicTile method getSamples.

public Sample[] getSamples() {
    Sample[] ret = new Sample[width * height];
    long t0 = System.currentTimeMillis();
    CoordinateReferenceSystem crs = gg.getCoordinateReferenceSystem2D();
    try {
        MathTransform tr = CRS.findMathTransform(crs, DefaultGeographicCRS.WGS84);
        // grid coordinate object to be reused for examining each cell
        GridCoordinates2D coord = new GridCoordinates2D();
        int i = 0, ns = 0;
        for (int gy = 0; gy < height; gy++) {
            for (int gx = 0; gx < width; gx++) {
                coord.x = gx;
                coord.y = gy;
                // find coordinates for current raster cell in tile CRS
                DirectPosition sourcePos = gg.gridToWorld(coord);
                // convert coordinates in tile CRS to WGS84
                tr.transform(sourcePos, sourcePos);
                // axis order can vary
                double lon = sourcePos.getOrdinate(0);
                double lat = sourcePos.getOrdinate(1);
                Sample s = ss.getSample(lon, lat);
                if (s != null)
                    ns++;
                ret[i] = s;
                i++;
            }
        }
        LOG.debug("finished preparing tile. number of samples: {}", ns);
    } catch (Exception e) {
        LOG.error(e.getMessage());
        return null;
    }
    long t1 = System.currentTimeMillis();
    LOG.debug("filled in tile image from SPT in {}msec", t1 - t0);
    return ret;
}
Also used : DirectPosition(org.opengis.geometry.DirectPosition) MathTransform(org.opengis.referencing.operation.MathTransform) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D) CoordinateReferenceSystem(org.opengis.referencing.crs.CoordinateReferenceSystem)

Example 3 with GridCoordinates2D

use of org.geotools.coverage.grid.GridCoordinates2D in project polymap4-core by Polymap4.

the class PredefinedColorMap method minMax.

/**
 * Row/col iteration.
 *
 * @deprecated Slower than {@link #minMax3(GridCoverage2D, IProgressMonitor)} and
 *             white-locks sometimes for unknown reason.
 * @return double[] {min, max, novalue}
 */
protected double[] minMax(GridCoverage2D grid, IProgressMonitor monitor) {
    // really, someday I have to learn about all this stuff :/
    GridGeometry2D geometry = grid.getGridGeometry();
    GridEnvelope gridRange = geometry.getGridRange();
    int w = gridRange.getHigh(0);
    monitor.beginTask("calculating min/max", w);
    @SuppressWarnings("hiding") double // see OmsRasterReader
    novalue = -9999.0;
    double min = Double.POSITIVE_INFINITY;
    double max = Double.NEGATIVE_INFINITY;
    // FIXME http://github.com/Polymap4/polymap4-p4/issues/129 does not come to an end
    // without the limit
    double[] values = new double[1];
    int startX = gridRange.getLow(0);
    int endX = Math.min(2000, gridRange.getHigh(0));
    int startY = gridRange.getLow(1);
    int endY = Math.min(2000, gridRange.getHigh(1));
    // XXX using an iterator could be faster
    for (int x = startX; x < endX; x++) {
        for (int y = startY; y < endY; y++) {
            grid.evaluate(new GridCoordinates2D(x, y), values);
            if (values[0] == novalue) {
                continue;
            }
            if (values[0] < novalue) {
                throw new IllegalStateException("XXX value < novalue : " + values[0]);
            }
            if (values[0] < min) {
                min = values[0];
            }
            if (values[0] > max) {
                max = values[0];
            }
        }
        monitor.worked(1);
    }
    monitor.done();
    // XXX check novalue
    return new double[] { min, max, novalue };
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D)

Example 4 with GridCoordinates2D

use of org.geotools.coverage.grid.GridCoordinates2D in project polymap4-core by Polymap4.

the class PredefinedColorMapEditor method minMax.

protected double[] minMax(GridCoverage2D grid) {
    GridGeometry2D geometry = grid.getGridGeometry();
    GridEnvelope gridRange = geometry.getGridRange();
    int w = gridRange.getHigh(0);
    int h = gridRange.getHigh(1);
    Timer timer = new Timer();
    Random random = new Random();
    double min = Double.MAX_VALUE;
    double max = Double.MIN_VALUE;
    double[] values = new double[1];
    for (int i = 0; i < 10000 || timer.elapsedTime() < 1000; i++) {
        grid.evaluate(new GridCoordinates2D(random.nextInt(w), random.nextInt(h)), values);
        min = values[0] < min ? values[0] : min;
        max = values[0] > max ? values[0] : max;
    }
    return new double[] { min, max };
}
Also used : GridGeometry2D(org.geotools.coverage.grid.GridGeometry2D) Timer(org.polymap.core.runtime.Timer) Random(java.util.Random) GridEnvelope(org.opengis.coverage.grid.GridEnvelope) GridCoordinates2D(org.geotools.coverage.grid.GridCoordinates2D)

Aggregations

GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)4 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)3 GridEnvelope (org.opengis.coverage.grid.GridEnvelope)3 Timer (org.polymap.core.runtime.Timer)2 File (java.io.File)1 Random (java.util.Random)1 GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)1 AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)1 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)1 DirectPosition (org.opengis.geometry.DirectPosition)1 CoordinateReferenceSystem (org.opengis.referencing.crs.CoordinateReferenceSystem)1 MathTransform (org.opengis.referencing.operation.MathTransform)1