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.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
12 import org.slf4j.Logger;
13 import org.slf4j.LoggerFactory;
15 public class TiffInterface {
17 private static final Logger LOGGER = LoggerFactory.getLogger(TiffInterface.class);
19 private final Path tifPath;
20 private GridCoverage2D coverage;
21 private CoordinateReferenceSystem crs;
23 private boolean init = false;
25 public TiffInterface(Path tifPath) {
26 this.tifPath = tifPath;
30 private void loadMetadata() {
31 GeoTiffReader reader = null;
33 reader = new GeoTiffReader(this.tifPath.toFile());
34 this.coverage = reader.read(null);
35 this.crs = coverage.getCoordinateReferenceSystem();
37 } catch (Exception e) {
38 LOGGER.error("Could not load {}", tifPath, e);
46 public Number lookup(DirectPosition pos) {
48 Object r = coverage.evaluate(pos);
49 final int dataType = coverage.getRenderedImage().getSampleModel().getDataType();
50 int pipeDepthUnderGround = MapsElevationServerPreferences.pipeDepthUnderGround();
52 case DataBuffer.TYPE_BYTE: {
53 // TODO: if the result is byte how does one subtract the pipeDepth form the value?
54 // Might not be even relevant with this use case
55 return new Byte(((byte[]) r)[0]);
57 case DataBuffer.TYPE_SHORT: // Fall through
58 case DataBuffer.TYPE_USHORT: // Fall through
59 case DataBuffer.TYPE_INT: {
60 int val = ((int[]) r)[0] - pipeDepthUnderGround;
61 return new Integer(val);
63 case DataBuffer.TYPE_FLOAT: {
64 float val = ((float[]) r)[0] - pipeDepthUnderGround;
65 return new Float(val);
67 case DataBuffer.TYPE_DOUBLE: {
68 double val = ((double[]) r)[0] - pipeDepthUnderGround;
69 return new Double(val);
75 private void ensureInit() {
77 throw new IllegalStateException("Interface is not initialized for " + this.tifPath);
82 coverage.dispose(true);
85 public Envelope2D getCornerCoords() {
86 return coverage.getEnvelope2D();
89 public CoordinateReferenceSystem getCRS() {