1 package org.simantics.maps.elevation.server;
3 import java.awt.image.DataBuffer;
4 import java.nio.file.Path;
6 import org.geotools.coverage.grid.GridCoverage2D;
7 import org.geotools.gce.geotiff.GeoTiffReader;
8 import org.geotools.geometry.Envelope2D;
9 import org.opengis.geometry.DirectPosition;
10 import org.opengis.referencing.crs.CoordinateReferenceSystem;
11 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory;
14 public class TiffInterface {
16 private static final Logger LOGGER = LoggerFactory.getLogger(TiffInterface.class);
18 private final Path tifPath;
19 private GridCoverage2D coverage;
20 private CoordinateReferenceSystem crs;
22 private boolean init = false;
24 public TiffInterface(Path tifPath) {
25 this.tifPath = tifPath;
29 private void loadMetadata() {
30 GeoTiffReader reader = null;
32 reader = new GeoTiffReader(this.tifPath.toFile());
33 this.coverage = reader.read(null);
34 this.crs = coverage.getCoordinateReferenceSystem();
36 } catch (Exception e) {
37 LOGGER.error("Could not load {}", tifPath, e);
45 public Number lookup(DirectPosition pos) {
47 Object r = coverage.evaluate(pos);
48 final int dataType = coverage.getRenderedImage().getSampleModel().getDataType();
50 case DataBuffer.TYPE_BYTE: return new Byte(((byte[]) r)[0]);
51 case DataBuffer.TYPE_SHORT: // Fall through
52 case DataBuffer.TYPE_USHORT: // Fall through
53 case DataBuffer.TYPE_INT: return new Integer(((int[]) r)[0]);
54 case DataBuffer.TYPE_FLOAT: return new Float(((float[]) r)[0]);
55 case DataBuffer.TYPE_DOUBLE: return new Double(((double[]) r)[0]);
60 private void ensureInit() {
62 throw new IllegalStateException("Interface is not initialized for " + this.tifPath);
67 coverage.dispose(true);
70 public Envelope2D getCornerCoords() {
71 return coverage.getEnvelope2D();
74 public CoordinateReferenceSystem getCRS() {