package org.simantics.maps.elevation.server;
+import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
+import java.util.Collection;
import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.stream.Collectors;
import java.util.stream.Stream;
-import com.github.benmanes.caffeine.cache.Caffeine;
-import com.github.benmanes.caffeine.cache.LoadingCache;
-import com.vividsolutions.jts.geom.Coordinate;
-import com.vividsolutions.jts.geom.Envelope;
-import com.vividsolutions.jts.index.strtree.STRtree;
-
import org.geotools.geometry.DirectPosition2D;
import org.geotools.geometry.Envelope2D;
import org.geotools.referencing.CRS;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.github.benmanes.caffeine.cache.LoadingCache;
+import com.vividsolutions.jts.geom.Coordinate;
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.index.strtree.STRtree;
+
public class TiffTileInterface {
private static final Logger LOGGER = LoggerFactory.getLogger(TiffTileInterface.class);
synchronized(index) {
index.insert(envelope, tifFile);
}
+ envelopes.put(tifFile, envelope);
} catch (Exception e) {
LOGGER.error("Could not initialize index for file {}", tifFile, e);
} finally {
});
}
+ private Map<Path, Envelope> envelopes = new ConcurrentHashMap<>();
+
+ public Collection<Rectangle2D> getBoundingBoxes() {
+ Collection<Rectangle2D> rects = envelopes.values().stream().map(env -> {
+ double x = env.getMinX();
+ double y = env.getMinY();
+ return new Rectangle2D.Double(x, y, env.getMaxX() - x, env.getMaxY() - y);
+ }).collect(Collectors.toList());
+ return rects;
+ }
+
private static CoordinateReferenceSystem c4326;
static {
public Number lookup(double x, double y) {
LOGGER.info("Looking up x={} y={}", x, y);
DirectPosition p = new DirectPosition2D(c4326, x, y);
- List<Path> tifFile = (List<Path>) index.query(new Envelope(new Coordinate(x, y)));
- if (!tifFile.isEmpty()) {
- TiffInterface tifInterface = openTifInterface(tifFile.get(0));
- try {
- return tifInterface.lookup(p);
- } finally {
- tifInterface.close();
+ List<Path> tifFiles = (List<Path>) index.query(new Envelope(new Coordinate(x, y)));
+ for (Path tifFile : tifFiles) {
+ TiffInterface tifInterface = openTifInterface(tifFile);
+ if (tifInterface.contains(p)) {
+ try {
+ return tifInterface.lookup(p);
+ } finally {
+ tifInterface.close();
+ }
+ } else {
+ System.out.println("not found");
}
- } else {
- return new Double(0); // use 0 by default for now
}
+ return new Double(0); // use 0 by default for now
}
}
\ No newline at end of file