From b27f05550f4d7e531f93ce946be71a7a95277a9c Mon Sep 17 00:00:00 2001 From: jsimomaa Date: Tue, 6 Aug 2019 11:27:51 +0300 Subject: [PATCH] Fix elevation bounding box profile shift & elevation transform fixes gitlab #45 APROS-15524 Change-Id: Ic01a79fd23df1d737a99b79c1222f5f2f0eee280 --- .../META-INF/MANIFEST.MF | 1 + .../ChangeRoutePointToVertexHandler.java | 17 ++-------- .../network/ui/nodes/ElevationServerNode.java | 2 +- .../network/ui/nodes/NetworkDrawingNode.java | 30 ++-------------- .../district/network/DistrictNetworkUtil.java | 34 +++++++++++++++++++ .../maps/elevation/server/TiffInterface.java | 19 ++++------- .../elevation/server/TiffTileInterface.java | 15 ++++---- 7 files changed, 54 insertions(+), 64 deletions(-) diff --git a/org.simantics.district.geotools/META-INF/MANIFEST.MF b/org.simantics.district.geotools/META-INF/MANIFEST.MF index 6bf0695b..1cdbfcaa 100644 --- a/org.simantics.district.geotools/META-INF/MANIFEST.MF +++ b/org.simantics.district.geotools/META-INF/MANIFEST.MF @@ -81,6 +81,7 @@ Export-Package: com.vividsolutions.jts, org.geotools.coverage.grid.io, org.geotools.gce.geotiff, org.geotools.geometry, + org.geotools.geometry.jts, org.geotools.referencing, org.opengis.geometry, org.opengis.referencing, diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeRoutePointToVertexHandler.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeRoutePointToVertexHandler.java index edecb859..f9e37c08 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeRoutePointToVertexHandler.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeRoutePointToVertexHandler.java @@ -159,21 +159,10 @@ public class ChangeRoutePointToVertexHandler { double y = finalClosestPoint.getY(); double[] midVertexCoords = new double[] { x, y }; - double elevation = 0; - if (MapsElevationServerPreferences.useElevationServer()) { - // ok! we use new elevation API to resolve possible elevations for the starting points - try { - elevation = SingletonTiffTileInterface.lookup(x, y).doubleValue(); - } catch (Exception ee) { - LOGGER.error("Could not get elevation from tiff interface", ee); - } - } - - Resource createdVertex = DistrictNetworkUtil.createVertex(graph, diagram, midVertexCoords, elevation, mapping); + Resource createdVertex = DistrictNetworkUtil.createVertex(graph, diagram, midVertexCoords, Double.MAX_VALUE, mapping); - Optional leftEdge = DNEdgeBuilder.create(graph, diagram, currentStartVertexCoords, 0, midVertexCoords, 0, detailedLeftEdgeGeometryCoords, 0.001); - Optional rightEdge = DNEdgeBuilder.create(graph, diagram, midVertexCoords, 0, currentEndVertexCoords, 0, detailedRightEdgeGeometryCoords, 0.001); - + Optional leftEdge = DNEdgeBuilder.create(graph, diagram, currentStartVertexCoords, Double.MAX_VALUE, midVertexCoords, Double.MAX_VALUE, detailedLeftEdgeGeometryCoords, 0.001); + Optional rightEdge = DNEdgeBuilder.create(graph, diagram, midVertexCoords, Double.MAX_VALUE, currentEndVertexCoords, Double.MAX_VALUE, detailedRightEdgeGeometryCoords, 0.001); } } }); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ElevationServerNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ElevationServerNode.java index c26c1759..ea9f9863 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ElevationServerNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ElevationServerNode.java @@ -52,7 +52,7 @@ public class ElevationServerNode extends G2DNode { double width = result2.getX() * scale - result.getX() * scale; double height = result2.getY() * scale - result.getY() * scale; - Rectangle2D translated = new Rectangle2D.Double(x, y, width, Math.abs(height)); + Rectangle2D translated = new Rectangle2D.Double(x, y - Math.abs(height), width, Math.abs(height)); g2d.draw(translated); } diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java index 4d527c1a..2cd87d69 100644 --- a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java @@ -172,17 +172,6 @@ public class NetworkDrawingNode extends G2DNode { double x = ModelledCRS.xToLongitude(pos.getX() / scale); double y = ModelledCRS.yToLatitude(-pos.getY() / scale); - double elevation = 0; - if (MapsElevationServerPreferences.useElevationServer()) { - // ok! we use new elevation API to resolve possible elevations for the starting points - try { - elevation = SingletonTiffTileInterface.lookup(x, y).doubleValue(); - } catch (Exception ee) { - LOGGER.error("Could not get elevation from tiff interface", ee); - } - } - final double felevation = elevation; - boolean leftButton = e.button == MouseEvent.LEFT_BUTTON; ThreadUtils.getNonBlockingWorkExecutor().schedule(() -> { @@ -200,7 +189,7 @@ public class NetworkDrawingNode extends G2DNode { if (mapping == null) { mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping); } - DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, felevation, mapping); + DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, Double.MAX_VALUE, mapping); } }); }, 100, TimeUnit.MILLISECONDS); @@ -251,26 +240,13 @@ public class NetworkDrawingNode extends G2DNode { detailedGeometryCoords[i++] = lon; detailedGeometryCoords[i++] = lat; } - - double startElevation = 0; - double endElevation = 0; - if (MapsElevationServerPreferences.useElevationServer()) { - // ok! we use new elevation API to resolve possible elevations for the starting points - try { - startElevation = SingletonTiffTileInterface.lookup(startLat, startLon).doubleValue(); - endElevation = SingletonTiffTileInterface.lookup(endLat, endLon).doubleValue(); - } catch (Exception e) { - LOGGER.error("Could not get elevation from tiff interface", e); - } - } - final double se = startElevation; - final double ee = endElevation; + DNEdgeBuilder builder = new DNEdgeBuilder(diagramResource, diagram); Simantics.getSession().asyncRequest(new WriteRequest() { @Override public void perform(WriteGraph graph) throws DatabaseException { - builder.create(graph, startCoords, se, endCoords, ee, detailedGeometryCoords, padding); + builder.create(graph, startCoords, Double.MAX_VALUE, endCoords, Double.MAX_VALUE, detailedGeometryCoords, padding); } }); diff --git a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java index 596c3667..ae73e56b 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java +++ b/org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java @@ -31,6 +31,8 @@ import org.simantics.diagram.synchronization.graph.layer.GraphLayer; import org.simantics.diagram.synchronization.graph.layer.IGraphLayerUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; +import org.simantics.maps.elevation.server.SingletonTiffTileInterface; +import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences; import org.simantics.modeling.ModelingResources; import org.simantics.modeling.adapters.NewCompositeActionFactory; import org.simantics.operation.Layer0X; @@ -77,12 +79,44 @@ public class DistrictNetworkUtil { return edge; } + /** + * @param graph + * @param composite + * @param coords + * @param elevation Double.MAX_VALUE to fetch elevation from elevation server (if enabled and has data) + * @return + * @throws DatabaseException + */ public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation) throws DatabaseException { Resource defaultVertexMapping = graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping); return createVertex(graph, composite, coords, elevation, defaultVertexMapping); } + /** + * @param graph + * @param composite + * @param coords + * @param elevation Double.MAX_VALUE to fetch elevation from elevation server (if enabled and has data) + * @param mapping + * @return + * @throws DatabaseException + */ public static Resource createVertex(WriteGraph graph, Resource composite, double[] coords, double elevation, Resource mapping) throws DatabaseException { + // Double.MAX_VALUE is our secret to lookup elevation from elevation server + if (elevation == Double.MAX_VALUE) { + // ok, resolve from server or default to 0 + if (MapsElevationServerPreferences.useElevationServer()) { + // ok! we use new elevation API to resolve possible elevations for the starting points + try { + elevation = SingletonTiffTileInterface.lookup(coords[1], coords[0]).doubleValue(); + } catch (Exception ee) { + LOGGER.error("Could not get elevation from tiff interface", ee); + } + } else { + elevation = 0; + } + } + Layer0 L0 = Layer0.getInstance(graph); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); DiagramResource DIA = DiagramResource.getInstance(graph); diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java index f679762c..60d7e565 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java @@ -1,17 +1,15 @@ package org.simantics.maps.elevation.server; -import java.awt.geom.Point2D; import java.awt.image.DataBuffer; import java.nio.file.Path; import org.geotools.coverage.grid.GridCoverage2D; import org.geotools.gce.geotiff.GeoTiffReader; import org.geotools.geometry.Envelope2D; -import org.geotools.geometry.TransformedDirectPosition; +import org.geotools.referencing.CRS; import org.opengis.geometry.DirectPosition; -import org.opengis.geometry.Envelope; import org.opengis.referencing.crs.CoordinateReferenceSystem; -import org.opengis.referencing.operation.TransformException; +import org.opengis.referencing.operation.MathTransform; import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -22,7 +20,6 @@ public class TiffInterface { private final Path tifPath; private GridCoverage2D coverage; - private CoordinateReferenceSystem crs; private boolean init = false; @@ -36,7 +33,6 @@ public class TiffInterface { try { reader = new GeoTiffReader(this.tifPath.toFile()); this.coverage = reader.read(null); - this.crs = coverage.getCoordinateReferenceSystem(); this.init = true; } catch (Exception e) { LOGGER.error("Could not load {}", tifPath, e); @@ -51,12 +47,9 @@ public class TiffInterface { ensureInit(); Envelope2D e = coverage.getEnvelope2D(); try { - TransformedDirectPosition tdp = new TransformedDirectPosition(pos.getCoordinateReferenceSystem(), crs, null); - tdp.transform(pos); - - Point2D p = tdp.toPoint2D(); - - boolean contains = e.contains(p); + MathTransform transform = CRS.findMathTransform(pos.getCoordinateReferenceSystem(), getCRS(), false); + DirectPosition target = transform.transform(pos, null); + boolean contains = e.contains(target); return contains; } catch (Exception ex) { ex.printStackTrace(); @@ -108,6 +101,6 @@ public class TiffInterface { } public CoordinateReferenceSystem getCRS() { - return crs; + return coverage.getCoordinateReferenceSystem(); } } diff --git a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java index b85f234c..6f24ad5b 100644 --- a/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java +++ b/org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java @@ -14,10 +14,10 @@ import java.util.stream.Stream; import org.geotools.geometry.DirectPosition2D; import org.geotools.geometry.Envelope2D; +import org.geotools.geometry.jts.ReferencedEnvelope; import org.geotools.referencing.CRS; import org.opengis.geometry.DirectPosition; import org.opengis.referencing.crs.CoordinateReferenceSystem; -import org.opengis.referencing.operation.MathTransform; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -75,16 +75,13 @@ public class TiffTileInterface { TiffInterface tifInterface = openTifInterface(tifFile); Envelope2D coords = tifInterface.getCornerCoords(); try { - MathTransform transform = CRS.findMathTransform(tifInterface.getCRS(), c4326); - DirectPosition2D min = new DirectPosition2D(); - DirectPosition2D max = new DirectPosition2D(); - transform.transform(new DirectPosition2D(coords.getMinX(), coords.getMinY()), min); - transform.transform(new DirectPosition2D(coords.getMaxX(), coords.getMaxY()), max); - Envelope envelope = new Envelope(min.getX(), max.getX(), min.getY(), max.getY()); + ReferencedEnvelope refEnv = new ReferencedEnvelope(coords); + ReferencedEnvelope targetEnv = refEnv.transform(c4326, false, 30); + synchronized(index) { - index.insert(envelope, tifFile); + index.insert(targetEnv, tifFile); } - envelopes.put(tifFile, envelope); + envelopes.put(tifFile, targetEnv); } catch (Exception e) { LOGGER.error("Could not initialize index for file {}", tifFile, e); } finally { -- 2.45.2