]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Fix elevation bounding box profile shift & elevation transform fixes 63/3063/1
authorjsimomaa <jani.simomaa@gmail.com>
Tue, 6 Aug 2019 08:27:51 +0000 (11:27 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Tue, 6 Aug 2019 08:27:51 +0000 (11:27 +0300)
gitlab #45
APROS-15524

Change-Id: Ic01a79fd23df1d737a99b79c1222f5f2f0eee280

org.simantics.district.geotools/META-INF/MANIFEST.MF
org.simantics.district.network.ui/src/org/simantics/district/network/ui/contributions/ChangeRoutePointToVertexHandler.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/ElevationServerNode.java
org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java
org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffInterface.java
org.simantics.maps.elevation.server/src/org/simantics/maps/elevation/server/TiffTileInterface.java

index 6bf0695b7317bbcb91ed02b32525bfd2e605be39..1cdbfcaa30357ead0ea9d35348981e5e286d357e 100644 (file)
@@ -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,
index edecb859791cee9a14488726e144cc32cb77879f..f9e37c088bc2751b8e9c339a66d312f1409df0df 100644 (file)
@@ -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<Resource> leftEdge = DNEdgeBuilder.create(graph, diagram, currentStartVertexCoords, 0, midVertexCoords, 0, detailedLeftEdgeGeometryCoords, 0.001);
-                                                    Optional<Resource> rightEdge = DNEdgeBuilder.create(graph, diagram, midVertexCoords, 0, currentEndVertexCoords, 0, detailedRightEdgeGeometryCoords, 0.001);
-                                                    
+                                                    Optional<Resource> leftEdge = DNEdgeBuilder.create(graph, diagram, currentStartVertexCoords, Double.MAX_VALUE, midVertexCoords, Double.MAX_VALUE, detailedLeftEdgeGeometryCoords, 0.001);
+                                                    Optional<Resource> rightEdge = DNEdgeBuilder.create(graph, diagram, midVertexCoords, Double.MAX_VALUE, currentEndVertexCoords, Double.MAX_VALUE, detailedRightEdgeGeometryCoords, 0.001);
                                                 }
                                             }
                                         });
index c26c175907635f583b469a18ac465cebac443141..ea9f9863169163b1baac8c985e72f9eda8aded44 100644 (file)
@@ -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);
         }
         
index 4d527c1a1c5fe038af161fafe706f668a133f267..2cd87d69a2b0a65980e85f4972b94a76e8e2691a 100644 (file)
@@ -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);
             }
         });
 
index 596c3667543cd78a714925f9386fbcbe54ebe0c7..ae73e56bc2119806cd816e069dc0732e2e489e7d 100644 (file)
@@ -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);
index f679762cb8998f221d0f9b7fe44a8608599b9248..60d7e5650a0c20e3addfdb195427922fe9508ee0 100644 (file)
@@ -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();
     }
 }
index b85f234c72d935ef4121877e8d9cc299d73b3fed..6f24ad5b65073eec799ac66247c90180d14b9f4e 100644 (file)
@@ -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 {