Search in sources :

Example 1 with GridCoverage2D

use of org.geotools.coverage.grid.GridCoverage2D in project sldeditor by robward-scisys.

the class RasterReader method createRGBStyle.

/**
 * Creates the rgb style.
 *
 * @param reader the reader
 * @param raster the raster
 * @return the style
 */
private Style createRGBStyle(AbstractGridCoverage2DReader reader, WritableRaster raster) {
    RasterSymbolizer sym = sf.getDefaultRasterSymbolizer();
    GridCoverage2D cov = null;
    try {
        cov = reader.read(null);
    } catch (IOException giveUp) {
        throw new RuntimeException(giveUp);
    }
    // We need at least three bands to create an RGB style
    int numBands = cov.getNumSampleDimensions();
    if (numBands < 3) {
        createRGBImageSymbol(sym, cov, raster);
    } else {
        createRGBChannelSymbol(sym, cov, numBands);
    }
    return SLD.wrapSymbolizers(sym);
}
Also used : RasterSymbolizer(org.geotools.styling.RasterSymbolizer) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) IOException(java.io.IOException)

Example 2 with GridCoverage2D

use of org.geotools.coverage.grid.GridCoverage2D 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 3 with GridCoverage2D

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

the class ElevationModule method buildGraph.

@Override
public void buildGraph(Graph graph, HashMap<Class<?>, Object> extra) {
    gridCoverageFactory.setGraph(graph);
    Coverage gridCov = gridCoverageFactory.getGridCoverage();
    // If gridCov is a GridCoverage2D, apply a bilinear interpolator. Otherwise, just use the
    // coverage as is (note: UnifiedGridCoverages created by NEDGridCoverageFactoryImpl handle
    // interpolation internally)
    coverage = (gridCov instanceof GridCoverage2D) ? Interpolator2D.create((GridCoverage2D) gridCov, new InterpolationBilinear()) : gridCov;
    log.info("Setting street elevation profiles from digital elevation model...");
    List<StreetEdge> edgesWithElevation = new ArrayList<StreetEdge>();
    int nProcessed = 0;
    int nTotal = graph.countEdges();
    for (Vertex gv : graph.getVertices()) {
        for (Edge ee : gv.getOutgoing()) {
            if (ee instanceof StreetWithElevationEdge) {
                StreetWithElevationEdge edgeWithElevation = (StreetWithElevationEdge) ee;
                processEdge(graph, edgeWithElevation);
                if (edgeWithElevation.getElevationProfile() != null && !edgeWithElevation.isElevationFlattened()) {
                    edgesWithElevation.add(edgeWithElevation);
                }
                nProcessed += 1;
                if (nProcessed % 50000 == 0) {
                    log.info("set elevation on {}/{} edges", nProcessed, nTotal);
                    double failurePercentage = nPointsOutsideDEM / nPointsEvaluated * 100;
                    if (failurePercentage > 50) {
                        log.warn("Fetching elevation failed at {}/{} points ({}%)", nPointsOutsideDEM, nPointsEvaluated, failurePercentage);
                        log.warn("Elevation is missing at a large number of points. DEM may be for the wrong region. " + "If it is unprojected, perhaps the axes are not in (longitude, latitude) order.");
                    }
                }
            }
        }
    }
    @SuppressWarnings("unchecked") HashMap<Vertex, Double> extraElevation = (HashMap<Vertex, Double>) extra.get(ElevationPoint.class);
    assignMissingElevations(graph, edgesWithElevation, extraElevation);
}
Also used : Vertex(org.opentripplanner.routing.graph.Vertex) GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Coverage(org.opengis.coverage.Coverage) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) ElevationPoint(org.opentripplanner.graph_builder.module.extra_elevation_data.ElevationPoint) StreetWithElevationEdge(org.opentripplanner.routing.edgetype.StreetWithElevationEdge) ElevationPoint(org.opentripplanner.graph_builder.module.extra_elevation_data.ElevationPoint) InterpolationBilinear(javax.media.jai.InterpolationBilinear) StreetWithElevationEdge(org.opentripplanner.routing.edgetype.StreetWithElevationEdge) StreetEdge(org.opentripplanner.routing.edgetype.StreetEdge) Edge(org.opentripplanner.routing.graph.Edge)

Example 4 with GridCoverage2D

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

the class NEDGridCoverageFactoryImpl method getGridCoverage.

/**
 * @return a GeoTools grid coverage for the entire area of interest, lazy-creating it on the first call.
 */
public Coverage getGridCoverage() {
    if (unifiedCoverage == null) {
        loadVerticalDatum();
        tileSource.setGraph(graph);
        tileSource.setCacheDirectory(cacheDirectory);
        List<File> paths = tileSource.getNEDTiles();
        // Make one grid coverage for each NED tile, adding them all to a single UnifiedGridCoverage.
        for (File path : paths) {
            GeotiffGridCoverageFactoryImpl factory = new GeotiffGridCoverageFactoryImpl(path);
            // TODO might bicubic interpolation give better results?
            GridCoverage2D regionCoverage = Interpolator2D.create(factory.getGridCoverage(), new InterpolationBilinear());
            if (unifiedCoverage == null) {
                unifiedCoverage = new UnifiedGridCoverage("unified", regionCoverage, datums);
            } else {
                unifiedCoverage.add(regionCoverage);
            }
        }
    }
    return unifiedCoverage;
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) InterpolationBilinear(javax.media.jai.InterpolationBilinear) File(java.io.File)

Example 5 with GridCoverage2D

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

the class Renderer method getResponse.

public Response getResponse(TileRequest tileRequest, TimeSurface surfA, TimeSurface surfB, RenderRequest renderRequest) throws Exception {
    Tile tile = tileCache.get(tileRequest);
    BufferedImage image;
    switch(renderRequest.layer) {
        case DIFFERENCE:
            image = tile.linearCombination(1, surfA, -1, surfB, 0, renderRequest);
            break;
        case HAGERSTRAND:
            long elapsed = Math.abs(surfB.dateTime - surfA.dateTime);
            image = tile.linearCombination(-1, surfA, -1, surfB, elapsed / 60, renderRequest);
            break;
        case TRAVELTIME:
        default:
            image = tile.generateImage(surfA, renderRequest);
    }
    // of course this will make it useless as a raster for analysis, but it's good for animations.
    if (renderRequest.timestamp) {
        DateFormat df = DateFormat.getDateTimeInstance();
        df.setTimeZone(TimeZone.getTimeZone("America/New_York"));
        String ds = df.format(new Date(surfA.dateTime * 1000));
        shadowWrite(image, ds, String.format("%f, %f", surfA.lat, surfA.lon));
        Graphics2D g2d = image.createGraphics();
        g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC));
        BufferedImage legend = Tile.getLegend(renderRequest.style, 300, 50);
        g2d.drawImage(legend, 0, image.getHeight() - 50, null);
        g2d.dispose();
    }
    // geotiff kludge
    if (renderRequest.format.toString().equals("image/geotiff")) {
        GridCoverage2D gc = tile.getGridCoverage2D(image);
        return generateStreamingGeotiffResponse(gc);
    } else {
        return generateStreamingImageResponse(image, renderRequest.format);
    }
}
Also used : GridCoverage2D(org.geotools.coverage.grid.GridCoverage2D) DateFormat(java.text.DateFormat) Tile(org.opentripplanner.analyst.core.Tile) BufferedImage(java.awt.image.BufferedImage) Date(java.util.Date) Graphics2D(java.awt.Graphics2D)

Aggregations

GridCoverage2D (org.geotools.coverage.grid.GridCoverage2D)5 File (java.io.File)2 InterpolationBilinear (javax.media.jai.InterpolationBilinear)2 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1 IOException (java.io.IOException)1 DateFormat (java.text.DateFormat)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 GridCoordinates2D (org.geotools.coverage.grid.GridCoordinates2D)1 GridGeometry2D (org.geotools.coverage.grid.GridGeometry2D)1 AbstractGridCoverage2DReader (org.geotools.coverage.grid.io.AbstractGridCoverage2DReader)1 AbstractGridFormat (org.geotools.coverage.grid.io.AbstractGridFormat)1 RasterSymbolizer (org.geotools.styling.RasterSymbolizer)1 Coverage (org.opengis.coverage.Coverage)1 GridEnvelope (org.opengis.coverage.grid.GridEnvelope)1 Tile (org.opentripplanner.analyst.core.Tile)1 ElevationPoint (org.opentripplanner.graph_builder.module.extra_elevation_data.ElevationPoint)1 StreetEdge (org.opentripplanner.routing.edgetype.StreetEdge)1