]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.network/src/org/simantics/district/network/DistrictNetworkUtil.java
Support searching nearby vertices in network diagram
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / DistrictNetworkUtil.java
index fde334f770d89d7a68cdfe84361692b62bcbf5ba..2bb29660930b82c9a30e010a118538fe73230c96 100644 (file)
@@ -1,8 +1,14 @@
 package org.simantics.district.network;
 
+import java.awt.geom.Rectangle2D;
+import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
+import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
 import org.simantics.datatypes.literal.RGB;
 import org.simantics.datatypes.literal.RGB.Integer;
@@ -10,12 +16,16 @@ import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.BinaryRead;
+import org.simantics.db.common.request.IndexRoot;
+import org.simantics.db.common.request.ObjectsWithType;
 import org.simantics.db.common.request.ResourceRead;
 import org.simantics.db.common.utils.OrderedSetUtils;
 import org.simantics.db.exception.BindingException;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.exception.ManyObjectsForFunctionalRelationException;
 import org.simantics.db.exception.ServiceException;
+import org.simantics.db.indexing.IndexUtils;
 import org.simantics.db.layer0.request.PossibleVariable;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.diagram.stubs.DiagramResource;
@@ -24,16 +34,26 @@ 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;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.vividsolutions.jts.geom.Envelope;
+import com.vividsolutions.jts.index.quadtree.Quadtree;
 
 public class DistrictNetworkUtil {
 
-    public static Resource createEdge(WriteGraph graph, Resource composite) throws DatabaseException {
-        return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping));
+    private static final Logger LOGGER = LoggerFactory.getLogger(DistrictNetworkUtil.class);
+
+    public static Resource createEdge(WriteGraph graph, Resource composite, double[] detailedGeometryCoords) throws DatabaseException {
+        return createEdge(graph, composite, graph.getPossibleObject(composite, DistrictNetworkResource.getInstance(graph).EdgeDefaultMapping), detailedGeometryCoords);
     }
 
-    public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping) throws DatabaseException {
+    public static Resource createEdge(WriteGraph graph, Resource composite, Resource mapping, double[] detailedGeometryCoords) throws DatabaseException {
         Layer0 L0 = Layer0.getInstance(graph);
         DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
         if (mapping == null) {
@@ -60,15 +80,49 @@ public class DistrictNetworkUtil {
             });
         }
         
+        // add detailed geometry (if any)
+        graph.claimLiteral(edge, DN.Edge_HasGeometry, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
         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);
@@ -249,6 +303,24 @@ public class DistrictNetworkUtil {
                 DistrictNetworkResource.getInstance(graph).Diagram_backgroundColor,
                 Bindings.getBindingUnchecked(RGB.Integer.class));
     }
+    
+    public static Resource createNetworkDiagram(WriteGraph graph, Resource target, Resource compositeType, String defaultName, Resource defaultEdgeMapping, Resource defaultVertexMapping, Resource rightClickVertexMapping, Resource leftClickVertexMapping, Resource crs) throws DatabaseException {
+        Resource composite = NewCompositeActionFactory.createComposite(graph, target, defaultName, compositeType);
+
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram);
+        graph.claim(diagram, DN.EdgeDefaultMapping, defaultEdgeMapping);
+        graph.claim(diagram, DN.VertexDefaultMapping, defaultVertexMapping);
+        graph.claim(diagram, DN.RightClickDefaultMapping, rightClickVertexMapping);
+        graph.claim(diagram, DN.LeftClickDefaultMapping, leftClickVertexMapping);
+        graph.claim(diagram, DN.HasSpatialRefSystem, crs);
+        
+        // Generated name prefix from composite name
+        String compositeName = graph.getRelatedValue2(composite, Layer0.getInstance(graph).HasName, Bindings.STRING);
+        graph.claimLiteral(diagram, Layer0X.getInstance(graph).HasGeneratedNamePrefix, "N" + compositeName.substring(compositeName.length() - 1, compositeName.length()));
+        
+        return composite;
+    }
 
     public static final class MappedComponentRequest extends ResourceRead<Resource> {
         public MappedComponentRequest(Resource element) {
@@ -261,4 +333,137 @@ public class DistrictNetworkUtil {
         }
     }
 
+    public static class ResourceVertex {
+        
+        public final boolean isConsumer;
+        public final Resource vertex;
+        public final double[] coords;
+        
+        public ResourceVertex(Resource vertex, double[] coords, boolean isConsumer) {
+            this.vertex = vertex;
+            this.coords = coords;
+            this.isConsumer = isConsumer;
+        }
+    }
+    
+    public static void changeMappingType(WriteGraph graph, Resource newMapping, List<Resource> elements) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        for (Resource element : elements) {
+            graph.deny(element, DN.HasMapping);
+            graph.claim(element, DN.HasMapping, newMapping);
+        }
+    }
+
+    public static Stream<Resource> findDNElementsById(ReadGraph graph, Resource context, String idToFind) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); 
+        return IndexUtils.findByType(graph,
+            graph.syncRequest(new IndexRoot(context)),
+            DN.Element
+        ).stream().filter(element -> {
+            try {
+                String id = graph.getPossibleRelatedValue(element, DN.HasId, Bindings.STRING);
+                return id != null && id.contains(idToFind);
+            } catch (DatabaseException e) {
+                LOGGER.error("Could not read id for element {]", element, e);
+                return false;
+            }
+        });
+    }
+    
+    public static Resource findDNElementById(ReadGraph graph, Resource context, String idToFind) throws DatabaseException {
+        List<Resource> elements = findDNElementsById(graph, context, idToFind).collect(Collectors.toList());
+        if (elements.size() == 1) {
+            return elements.iterator().next();
+        }
+        return null;
+    }
+    
+    public static List<Resource> findDNElementByXYCoordinates(ReadGraph graph, Resource context, double lat, double lon, double padding) throws DatabaseException {
+        DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        List<Resource> results = new ArrayList<>();
+        Collection<Resource> vertices = IndexUtils.findByType(graph, graph.syncRequest(new IndexRoot(context)), DN.Vertex);
+        Rectangle2D rect = new Rectangle2D.Double(lat, lon, padding, padding);
+        for (Resource vertex : vertices) {
+            double[] location = graph.getRelatedValue(vertex, DIA.HasLocation, Bindings.DOUBLE_ARRAY);
+            if (rect.contains(location[0], location[1])) {
+                results.add(vertex);
+            }
+        }
+        return results;
+    }
+
+    public static List<ResourceVertex> nearbyResourceVertices(ReadGraph graph, Resource diagramResource, Resource vertex, Double padding) throws DatabaseException {
+        double halfPadding = padding / 2;
+        
+        Quadtree existingVertices = graph.syncRequest(new ExistingVerticesRead(diagramResource, halfPadding), TransientCacheListener.instance());
+        double[] coords = graph.getRelatedValue(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
+        double x1 = coords[0] - halfPadding;
+        double y1= coords[1] - halfPadding;
+        double x2 = coords[0] + halfPadding;
+        double y2= coords[1] + halfPadding;
+        Envelope e = new Envelope(x1, x2, y1, y2);
+        
+        List<?> result = existingVertices.query(e);
+        @SuppressWarnings("unchecked")
+        List<ResourceVertex> vertices = (List<ResourceVertex>) result;
+        
+        Rectangle2D vertexRectangle = new Rectangle2D.Double(coords[0] - halfPadding, coords[1] - halfPadding, padding, padding);
+        
+        // let's sort by distance
+        List<ResourceVertex> sortedVertices = vertices.stream().filter(rv -> {
+            if (rv.vertex.equals(vertex))
+                return false;
+            
+            Rectangle2D nearbyRectangle = new Rectangle2D.Double(rv.coords[0] - halfPadding, rv.coords[1] - halfPadding, padding, padding);
+            return vertexRectangle.intersects(nearbyRectangle);
+        }).sorted((o1, o2) -> {
+            double disto1 = Math.sqrt((Math.pow(coords[0] - o1.coords[0], 2) + (Math.pow(coords[1] - o1.coords[1], 2))));
+            double disto2 = Math.sqrt((Math.pow(coords[0] - o2.coords[0], 2) + (Math.pow(coords[1] - o2.coords[1], 2))));
+            
+            if (o1.vertex.getResourceId() == 2554883) {
+                System.err.println("here we are");
+            }
+            
+            return Double.compare(disto1, disto2);
+        }).collect(Collectors.toList());
+        return sortedVertices;
+    }
+
+    public static List<Resource> nearbyVertices(ReadGraph graph, Resource vertex, double padding) throws DatabaseException {
+        Resource diagramResource = graph.getSingleObject(vertex, Layer0.getInstance(graph).PartOf);
+        return nearbyResourceVertices(graph, diagramResource, vertex, padding)
+                .stream()
+                .map(rv -> rv.vertex)
+                .collect(Collectors.toList());
+    }
+
+    public static Quadtree existingVertices(Resource diagramResource, Double padding) throws DatabaseException {
+        Quadtree vv = Simantics.getSession().syncRequest(new ExistingVerticesRead(diagramResource, padding));
+        return vv;
+    }
+
+    public static class ExistingVerticesRead extends BinaryRead<Resource, Double, Quadtree> {
+
+        public ExistingVerticesRead(Resource diagramResource, Double padding) {
+            super(diagramResource, padding);
+        }
+
+        @Override
+        public Quadtree perform(ReadGraph graph) throws DatabaseException {
+            Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(parameter, Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
+            Quadtree vv = new Quadtree();
+            for (Resource vertex : vertices) {
+                double[] coords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
+                double x1 = coords[0] - parameter2;
+                double y1= coords[1] - parameter2;
+                double x2 = coords[0] + parameter2;
+                double y2= coords[1] + parameter2;
+                Envelope e = new Envelope(x1, x2, y1, y2);
+                vv.insert(e, new ResourceVertex(vertex, coords, true));
+            }
+            return vv;
+        }
+    }
+
 }