]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network.ui/src/org/simantics/district/network/ui/nodes/NetworkDrawingNode.java
Make it possible to import CSV data via SCL & create models
[simantics/district.git] / org.simantics.district.network.ui / src / org / simantics / district / network / ui / nodes / NetworkDrawingNode.java
index b94639e2311080c9881044f0228a4c06e14bbc6d..4d527c1a1c5fe038af161fafe706f668a133f267 100644 (file)
@@ -11,6 +11,7 @@ import java.awt.geom.Rectangle2D;
 import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
+import java.util.concurrent.TimeUnit;
 
 import org.simantics.Simantics;
 import org.simantics.db.Resource;
@@ -20,15 +21,17 @@ import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.request.Write;
 import org.simantics.diagram.elements.DiagramNodeUtil;
 import org.simantics.diagram.ui.DiagramModelHints;
+import org.simantics.district.network.DNEdgeBuilder;
 import org.simantics.district.network.DistrictNetworkUtil;
 import org.simantics.district.network.ModelledCRS;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
-import org.simantics.district.network.ui.DNEdgeBuilder;
 import org.simantics.district.network.ui.NetworkDrawingParticipant;
 import org.simantics.g2d.canvas.Hints;
 import org.simantics.g2d.canvas.ICanvasContext;
 import org.simantics.g2d.canvas.IToolMode;
 import org.simantics.g2d.diagram.IDiagram;
+import org.simantics.maps.elevation.server.SingletonTiffTileInterface;
+import org.simantics.maps.elevation.server.prefs.MapsElevationServerPreferences;
 import org.simantics.scenegraph.g2d.G2DNode;
 import org.simantics.scenegraph.g2d.events.EventTypes;
 import org.simantics.scenegraph.g2d.events.KeyEvent.KeyPressedEvent;
@@ -38,9 +41,14 @@ import org.simantics.scenegraph.g2d.events.MouseEvent.MouseDoubleClickedEvent;
 import org.simantics.scenegraph.g2d.events.MouseEvent.MouseMovedEvent;
 import org.simantics.scenegraph.utils.GeometryUtils;
 import org.simantics.scenegraph.utils.NodeUtil;
+import org.simantics.utils.threads.ThreadUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 public class NetworkDrawingNode extends G2DNode {
 
+    private static final Logger LOGGER = LoggerFactory.getLogger(NetworkDrawingNode.class);
+
     static class DrawingNode {
 
         private List<Point2D> routeNodes = new ArrayList<>();
@@ -163,15 +171,39 @@ public class NetworkDrawingNode extends G2DNode {
                 double scale = getTransform().getScaleY();
                 double x = ModelledCRS.xToLongitude(pos.getX() / scale);
                 double y = ModelledCRS.yToLatitude(-pos.getY() / scale);
-                Simantics.getSession().asyncRequest(new Write() {
-                    
-                    @Override
-                    public void perform(WriteGraph graph) throws DatabaseException {
-                        graph.markUndoPoint();
-                        Resource defaultMapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
-                        DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, 0, defaultMapping); // TODO: elevation can be fetched from e.g. elevation API
+                
+                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(() -> {
+                    Simantics.getSession().asyncRequest(new Write() {
+                        
+                        @Override
+                        public void perform(WriteGraph graph) throws DatabaseException {
+                            graph.markUndoPoint();
+                            Resource mapping = null;
+                            if (leftButton) {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).LeftClickDefaultMapping);
+                            } else {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).RightClickDefaultMapping);
+                            }
+                            if (mapping == null) {
+                               mapping = graph.getSingleObject(diagramResource, DistrictNetworkResource.getInstance(graph).VertexDefaultMapping);
+                            }
+                            DistrictNetworkUtil.createVertex(graph, diagramResource, new double[] { x, y }, felevation, mapping);
+                        }
+                    });
+                }, 100, TimeUnit.MILLISECONDS);
             }
             repaint();
             return true;
@@ -220,15 +252,28 @@ public class NetworkDrawingNode extends G2DNode {
             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, 0, endCoords, 0, detailedGeometryCoords, padding);
+                builder.create(graph, startCoords, se, endCoords, ee, detailedGeometryCoords, padding);
             }
         });
-        
+
     }
 
     @Override