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);
}
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();
// }
}
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);
}
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;
}
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);
}
}
Aggregations