From: jsjani Date: Wed, 29 Mar 2017 16:28:19 +0000 (+0300) Subject: Lots of changes to district stuff X-Git-Tag: v1.31.0~14 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=refs%2Fchanges%2F84%2F384%2F1;p=simantics%2Fdistrict.git Lots of changes to district stuff refs #6958 Change-Id: Ic44eba4146630d8364cc09c765e5413e130b34df --- diff --git a/org.simantics.district.maps/.classpath b/org.simantics.district.maps/.classpath index 751c8f2e..0e4c9142 100644 --- a/org.simantics.district.maps/.classpath +++ b/org.simantics.district.maps/.classpath @@ -1,6 +1,17 @@ + + + + + + + + + + + diff --git a/org.simantics.district.maps/META-INF/MANIFEST.MF b/org.simantics.district.maps/META-INF/MANIFEST.MF index a5c4a105..9754d757 100644 --- a/org.simantics.district.maps/META-INF/MANIFEST.MF +++ b/org.simantics.district.maps/META-INF/MANIFEST.MF @@ -1,21 +1,38 @@ Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Raster Mapping -Bundle-SymbolicName: org.simantics.district.maps +Bundle-SymbolicName: org.simantics.district.maps;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Vendor: VTT Technical Research Centre of Finland Require-Bundle: org.simantics.scenegraph, org.eclipse.core.runtime;bundle-version="3.5.0";resolution:=optional, org.eclipse.core.net;bundle-version="1.2.1";resolution:=optional, org.simantics.g2d;bundle-version="0.9.4", - org.simantics.utils.datastructures;bundle-version="1.0.0" -Export-Package: org.simantics.maps, + org.simantics.utils.datastructures;bundle-version="1.0.0", + org.eclipse.jface, + org.eclipse.ui.ide, + org.eclipse.ui.workbench +Export-Package: org.geotools.referencing, + org.opengis.referencing, + org.opengis.referencing.crs, + org.simantics.maps, org.simantics.maps.debug, org.simantics.maps.eclipse, org.simantics.maps.sg, org.simantics.maps.tile, org.simantics.maps.wms -Bundle-ClassPath: . +Bundle-ClassPath: ., + lib/commons-pool-1.5.4.jar, + lib/core-0.26.jar, + lib/GeographicLib-Java-1.44.jar, + lib/gt-metadata-16.2.jar, + lib/gt-opengis-16.2.jar, + lib/gt-referencing-16.2.jar, + lib/jai_core-1.1.3.jar, + lib/jgridshift-1.0.jar, + lib/jsr-275-1.0-beta-2.jar, + lib/gt-epsg-hsql-16.2.jar, + lib/hsqldb-2.3.0.jar Import-Package: org.simantics.scenegraph.g2d Bundle-RequiredExecutionEnvironment: JavaSE-1.6, JavaSE-1.7 diff --git a/org.simantics.district.maps/build.properties b/org.simantics.district.maps/build.properties index 34d2e4d2..5fd55626 100644 --- a/org.simantics.district.maps/build.properties +++ b/org.simantics.district.maps/build.properties @@ -1,4 +1,16 @@ source.. = src/ output.. = bin/ bin.includes = META-INF/,\ - . + .,\ + lib/commons-pool-1.5.4.jar,\ + lib/core-0.26.jar,\ + lib/GeographicLib-Java-1.44.jar,\ + lib/gt-metadata-16.2.jar,\ + lib/gt-opengis-16.2.jar,\ + lib/gt-referencing-16.2.jar,\ + lib/jai_core-1.1.3.jar,\ + lib/jgridshift-1.0.jar,\ + lib/jsr-275-1.0-beta-2.jar,\ + lib/gt-epsg-hsql-16.2.jar,\ + lib/hsqldb-2.3.0.jar,\ + plugin.xml diff --git a/org.simantics.district.maps/lib/GeographicLib-Java-1.44.jar b/org.simantics.district.maps/lib/GeographicLib-Java-1.44.jar new file mode 100644 index 00000000..338ca8e5 Binary files /dev/null and b/org.simantics.district.maps/lib/GeographicLib-Java-1.44.jar differ diff --git a/org.simantics.district.maps/lib/commons-pool-1.5.4.jar b/org.simantics.district.maps/lib/commons-pool-1.5.4.jar new file mode 100644 index 00000000..43edf996 Binary files /dev/null and b/org.simantics.district.maps/lib/commons-pool-1.5.4.jar differ diff --git a/org.simantics.district.maps/lib/core-0.26.jar b/org.simantics.district.maps/lib/core-0.26.jar new file mode 100644 index 00000000..9452f178 Binary files /dev/null and b/org.simantics.district.maps/lib/core-0.26.jar differ diff --git a/org.simantics.district.maps/lib/gt-epsg-hsql-16.2.jar b/org.simantics.district.maps/lib/gt-epsg-hsql-16.2.jar new file mode 100644 index 00000000..ac961d97 Binary files /dev/null and b/org.simantics.district.maps/lib/gt-epsg-hsql-16.2.jar differ diff --git a/org.simantics.district.maps/lib/gt-metadata-16.2.jar b/org.simantics.district.maps/lib/gt-metadata-16.2.jar new file mode 100644 index 00000000..415bbc10 Binary files /dev/null and b/org.simantics.district.maps/lib/gt-metadata-16.2.jar differ diff --git a/org.simantics.district.maps/lib/gt-opengis-16.2.jar b/org.simantics.district.maps/lib/gt-opengis-16.2.jar new file mode 100644 index 00000000..67079c94 Binary files /dev/null and b/org.simantics.district.maps/lib/gt-opengis-16.2.jar differ diff --git a/org.simantics.district.maps/lib/gt-referencing-16.2.jar b/org.simantics.district.maps/lib/gt-referencing-16.2.jar new file mode 100644 index 00000000..8426bc36 Binary files /dev/null and b/org.simantics.district.maps/lib/gt-referencing-16.2.jar differ diff --git a/org.simantics.district.maps/lib/hsqldb-2.3.0.jar b/org.simantics.district.maps/lib/hsqldb-2.3.0.jar new file mode 100644 index 00000000..5069b5e5 Binary files /dev/null and b/org.simantics.district.maps/lib/hsqldb-2.3.0.jar differ diff --git a/org.simantics.district.maps/lib/jai_core-1.1.3.jar b/org.simantics.district.maps/lib/jai_core-1.1.3.jar new file mode 100644 index 00000000..b29b8eed Binary files /dev/null and b/org.simantics.district.maps/lib/jai_core-1.1.3.jar differ diff --git a/org.simantics.district.maps/lib/jgridshift-1.0.jar b/org.simantics.district.maps/lib/jgridshift-1.0.jar new file mode 100644 index 00000000..d9b8d328 Binary files /dev/null and b/org.simantics.district.maps/lib/jgridshift-1.0.jar differ diff --git a/org.simantics.district.maps/lib/jsr-275-1.0-beta-2.jar b/org.simantics.district.maps/lib/jsr-275-1.0-beta-2.jar new file mode 100644 index 00000000..68e19aa4 Binary files /dev/null and b/org.simantics.district.maps/lib/jsr-275-1.0-beta-2.jar differ diff --git a/org.simantics.district.maps/plugin.xml b/org.simantics.district.maps/plugin.xml new file mode 100644 index 00000000..0b965bec --- /dev/null +++ b/org.simantics.district.maps/plugin.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + diff --git a/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java b/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java index 3c4a9944..00f06dea 100644 --- a/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java +++ b/org.simantics.district.maps/src/org/simantics/maps/eclipse/MapPainter.java @@ -17,6 +17,7 @@ import org.simantics.g2d.canvas.impl.AbstractCanvasParticipant; import org.simantics.g2d.canvas.impl.SGNodeReflection.SGCleanup; import org.simantics.g2d.canvas.impl.SGNodeReflection.SGInit; import org.simantics.maps.sg.MapNode; +import org.simantics.maps.sg.MapScaleNode; import org.simantics.scenegraph.g2d.G2DParentNode; import org.simantics.scenegraph.g2d.events.EventHandlerReflection.EventHandler; import org.simantics.scenegraph.g2d.events.command.CommandEvent; @@ -57,6 +58,7 @@ public class MapPainter extends AbstractCanvasParticipant { }; protected MapNode node = null; + protected MapScaleNode scaleNode = null; private int scale; @@ -103,6 +105,11 @@ public class MapPainter extends AbstractCanvasParticipant { node.setScale(scale); node.setEnabled(true); node.setZIndex(Integer.MIN_VALUE + 999); // Just under the grid + + scaleNode = parent.addNode("mapScale", MapScaleNode.class); + scaleNode.setScale(scale); + scaleNode.setEnabled(true); + scaleNode.setZIndex(Integer.MAX_VALUE - 999); // Just under the grid } @SGCleanup diff --git a/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferenceInitializer.java b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferenceInitializer.java new file mode 100644 index 00000000..3350ebb2 --- /dev/null +++ b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferenceInitializer.java @@ -0,0 +1,17 @@ +package org.simantics.maps.prefs; + +import org.eclipse.core.runtime.preferences.AbstractPreferenceInitializer; +import org.eclipse.core.runtime.preferences.DefaultScope; +import org.osgi.service.prefs.Preferences; + +public class MapsClientPreferenceInitializer extends AbstractPreferenceInitializer { + + public MapsClientPreferenceInitializer() { + } + + @Override + public void initializeDefaultPreferences() { + Preferences node = DefaultScope.INSTANCE.getNode(MapsClientPreferences.P_NODE); + node.put(MapsClientPreferences.P_TILESERVER_URL, "http://localhost:8088"); + } +} diff --git a/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferencePage.java b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferencePage.java new file mode 100644 index 00000000..ba168368 --- /dev/null +++ b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferencePage.java @@ -0,0 +1,36 @@ +package org.simantics.maps.prefs; + +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.preference.FieldEditor; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.jface.preference.StringFieldEditor; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; +import org.eclipse.ui.preferences.ScopedPreferenceStore; + +public class MapsClientPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { + + public MapsClientPreferencePage() { + super(GRID); + setDescription("Maps client preferences"); + } + + @Override + public void init(IWorkbench workbench) { + + } + + @Override + protected IPreferenceStore doGetPreferenceStore() { + return new ScopedPreferenceStore(InstanceScope.INSTANCE, MapsClientPreferences.P_NODE); + } + + @Override + protected void createFieldEditors() { + FieldEditor editor = new StringFieldEditor(MapsClientPreferences.P_TILESERVER_URL, "Tileserver URL", getFieldEditorParent()); + editor.setPreferenceStore(getPreferenceStore()); + addField(editor); + } + +} diff --git a/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferences.java b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferences.java new file mode 100644 index 00000000..5ecfe052 --- /dev/null +++ b/org.simantics.district.maps/src/org/simantics/maps/prefs/MapsClientPreferences.java @@ -0,0 +1,8 @@ +package org.simantics.maps.prefs; + +public class MapsClientPreferences { + + public static final String P_NODE = "org.simantics.district.maps"; + + public static final String P_TILESERVER_URL = "org.simantics.district.maps.prefs.tileserverURL"; +} diff --git a/org.simantics.district.maps/src/org/simantics/maps/sg/MapScaleNode.java b/org.simantics.district.maps/src/org/simantics/maps/sg/MapScaleNode.java new file mode 100644 index 00000000..a0242c11 --- /dev/null +++ b/org.simantics.district.maps/src/org/simantics/maps/sg/MapScaleNode.java @@ -0,0 +1,242 @@ +package org.simantics.maps.sg; + +import java.awt.AlphaComposite; +import java.awt.BasicStroke; +import java.awt.Color; +import java.awt.Font; +import java.awt.FontMetrics; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.util.Locale; + +import org.geotools.referencing.CRS; +import org.geotools.referencing.GeodeticCalculator; +import org.opengis.referencing.FactoryException; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.simantics.scenegraph.g2d.G2DNode; +import org.simantics.scenegraph.utils.GridUtils; + +public class MapScaleNode extends G2DNode { + + private static final long serialVersionUID = -2738682328944298290L; + + private static final Color GRAY = new Color(100, 100, 100); + + protected Boolean enabled = true; + + protected double gridSize = 1.0; + + private double scale; + + @Override + public void render(Graphics2D g) { + if (!enabled) + return; + + AffineTransform tr = g.getTransform(); + double scaleX = Math.abs(tr.getScaleX()); + double scaleY = Math.abs(tr.getScaleY()); + if (scaleX <= 0 || scaleY <= 0) { + // Make sure that we don't end up in an eternal loop below. + return; + } + double offsetX = tr.getTranslateX(); + double offsetY = tr.getTranslateY(); + g.setTransform(new AffineTransform()); + + Font rulerFont = new Font("Tahoma", Font.PLAIN, 9); + + //g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setStroke(new BasicStroke(1)); + g.setColor(new Color(0.9f, 0.9f, 0.9f, 0.75f)); + + Rectangle2D bounds = g.getClipBounds(); + if(bounds == null) return; // FIXME + + double previousText = -100; + + double minY = bounds.getMaxY() - 30; + + double scaleRight = bounds.getMaxX() - 30; + + double meterPerPixel = getMeterPerPixel(scaleRight - offsetX, minY - offsetY, scaleX, scaleY); + + double pixels = 0; + double value = 0; + for (int i = 0; i < SCALE_VALUES.length; i++) { + value = SCALE_VALUES[i]; + pixels = value / meterPerPixel; + if (pixels > 100) { + break; + } + } + + + double newScaleLeft = scaleRight - pixels; + g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.8f)); + Rectangle2D vertical = new Rectangle2D.Double(newScaleLeft, bounds.getMaxY() - 30, pixels, 20); + g.fill(vertical); + + g.setColor(GRAY); + g.setFont(rulerFont); + + + // stepX and stepY should be something between 50 and 100 + double stepX = 50; + + stepX = GridUtils.limitedEvenGridSpacing(stepX, scaleX, 100, gridSize, true); + //while(stepX * scaleX > 100) stepX /= 2; + //while(stepY * scaleY > 100) stepY /= 2; + + while(stepX * scaleX < 50) stepX *= 2; + + stepX *= scaleX; + + double gap = scaleRight -newScaleLeft; + + stepX = gap / 2 - 0.00001; + + // Horizontal ruler + double label = 0; + for(double x = newScaleLeft; x < scaleRight; x += stepX) { + String str = formatValue(label * meterPerPixel); + FontMetrics fm = g.getFontMetrics(); + Rectangle2D r = fm.getStringBounds(str, g); + if((x - r.getWidth() / 2) > previousText) { + g.setColor(Color.BLACK); + g.drawString(str, (int)(x-r.getWidth()/2), (int)(minY+1+r.getHeight())); + previousText = x+r.getWidth()/2+stepX/4; + } + + g.setColor(GRAY); + g.drawLine((int)x, (int)minY+12, (int)x, (int)minY+19); + if (x + 0.1 < scaleRight) { + if(stepX/5 > 2) { + for(double x2 = x+stepX/5; x2 < x+stepX; x2+=stepX/5) { + if(x2 > 20) { + g.drawLine((int)x2, (int)minY+15, (int)x2, (int)minY+19); + } + } + for(double x2 = x+stepX/10; x2 < x+stepX; x2+=stepX/5) { + if(x2 > 20) { + g.drawLine((int)x2, (int)minY+17, (int)x2, (int)minY+19); + } + } + } + } + label += stepX; + } + + g.setTransform(tr); + } + + @Override + public Rectangle2D getBoundsInLocal() { + return null; + } + + private static final transient int MAX_DIGITS = 0; + private static final transient double EPSILON = 0.01; + private static final transient double TRIM_THRESHOLD_MAX_VALUE = Math.pow(10, 2); + private static final transient String[] SI_UNIT_LARGE_PREFIXES = { "m", "km" }; + + private static final transient double[] SCALE_VALUES = { 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000, 20000, 50000, 100000, 200000, 500000, 1000000, 2000000, 50000000 }; + + public static String formatValue(double value) { + int magnitude = (int) Math.round(Math.log10(value)); + //System.out.println("magnitude: " + magnitude + ", " + value); + int allowedDecimals = MAX_DIGITS; + allowedDecimals -= Math.abs(magnitude); + if (allowedDecimals < 0) + allowedDecimals = 0; + + String valueStr = String.format(Locale.US, "%." + allowedDecimals + "f", value); + if (allowedDecimals > 0) { + for (int trunc = valueStr.length() - 1; trunc > 0; --trunc) { + char ch = valueStr.charAt(trunc); + if (ch == '.') { + valueStr = valueStr.substring(0, trunc); + break; + } + if (valueStr.charAt(trunc) != '0') { + valueStr = valueStr.substring(0, trunc + 1); + break; + } + } + if (Math.abs(value) + EPSILON > TRIM_THRESHOLD_MAX_VALUE) { + // Cut anything beyond a possible decimal dot out since they + // should not show anyway. This is a complete hack that tries to + // circumvent floating-point inaccuracy problems. + int dotIndex = valueStr.lastIndexOf('.'); + if (dotIndex > -1) { + valueStr = valueStr.substring(0, dotIndex); + } + } + } + + if (valueStr.equals("-0")) + valueStr = "0"; + +// if (!valueStr.equals("0")) { +// double trimValue = value; +// for (int i = 0; i < SI_UNIT_LARGE_PREFIXES.length; i++) { +// double trim = trimValue / 1000; +// if (Math.abs(trim)-EPSILON < TRIM_THRESHOLD_MAX_VALUE) { +// valueStr = valueStr.substring(0, valueStr.length() - (i + 1) * 3); +// valueStr += SI_UNIT_LARGE_PREFIXES[i]; +// break; +// } +// trimValue = trim; +// } +// } + + return valueStr; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + + public void setScale(double scale) { + this.scale = scale; + } + + @Override + public void init() { + try { + EPSG4326 = CRS.decode("EPSG:4326"); + calculator = new GeodeticCalculator(EPSG4326); + } catch (FactoryException e) { + e.printStackTrace(); + } + super.init(); + } + + GeodeticCalculator calculator; + CoordinateReferenceSystem EPSG4326; + + public Point2D scaleLeftmostPoint(double startLon, double startLat, double distance, double azimuth) { + GeodeticCalculator calculator = new GeodeticCalculator(); + calculator.setStartingGeographicPoint(startLon, startLat); + calculator.setDirection(azimuth, distance); + return calculator.getDestinationGeographicPoint(); + } + + public double getMeterPerPixel(double screenX, double screenY, double scaleX, double scaleY) { + double startLon = (screenX / scaleX) / scale; + double val = (screenY / scaleY) / scale; + val = Math.toDegrees(Math.atan(Math.sinh(Math.toRadians(val)))); + double startLat = val; + double endLon = ((screenX + 1) / scaleX) / scale; + double endLat = val; + + calculator.setStartingGeographicPoint(startLon, startLat); + calculator.setDestinationGeographicPoint(endLon, endLat); + double distance = calculator.getOrthodromicDistance(); + + return distance; + } + +} diff --git a/org.simantics.district.network.ontology/graph.tg b/org.simantics.district.network.ontology/graph.tg index eb85b78f..c1cb1b6d 100644 Binary files a/org.simantics.district.network.ontology/graph.tg and b/org.simantics.district.network.ontology/graph.tg differ diff --git a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph index ade6b1c5..5c30822b 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetwork.pgraph @@ -15,12 +15,10 @@ DN = : L0.Ontology DN.Layer + + + + getVertexMappings(ReadGraph graph, Resource resource) throws DatabaseException { - return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); + Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_VertexMapping); + return second; } public static Map getEdgeMappings(ReadGraph graph, Resource resource) throws DatabaseException { - return getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); + Map second = getNetworkMappingsByType(graph, resource , DistrictNetworkResource.getInstance(graph).Mapping_EdgeMapping); + return second; + } + + public static Map getCRSs(ReadGraph graph, Resource resource) throws DatabaseException { + Map result = getNetworkMappingsByType(graph, resource, DistrictNetworkResource.getInstance(graph).SpatialRefSystem); + return result; + } public static Map getNetworkMappingsByType(ReadGraph graph, Resource element, Resource mappingType) throws DatabaseException { @@ -196,10 +204,13 @@ public class Functions { private Map vertexMappings = new HashMap<>(); private Map edgeMappings = new HashMap<>(); private Map composites = new HashMap<>(); + private Map crss = new HashMap<>(); private Map> components = new HashMap<>(); private Resource defaultVertexMapping; private Resource defaultEdgeMapping; + private Resource defaultCRS; + private Combo compositeMappingCombo; private Combo componentMappingCombo; @@ -239,14 +250,21 @@ public class Functions { components = getComponents(graph, composites.get(0)); } + crss = getCRSs(graph, configuration); composite.getDisplay().asyncExec(() -> { vertexMappingCombo.setItems(vertexMappings.keySet().toArray(new String[vertexMappings.size()])); edgeMappingCombo.setItems(edgeMappings.keySet().toArray(new String[edgeMappings.size()])); + + crsCombo.setItems(crss.keySet().toArray(new String[crss.size()])); + compositeMappingCombo.setItems(composites.keySet().toArray(new String[composites.size()])); vertexMappingCombo.select(0); edgeMappingCombo.select(0); + + crsCombo.select(0); + if (!composites.isEmpty()) compositeMappingCombo.select(0); }); @@ -379,6 +397,11 @@ public class Functions { protected void computeResult() { defaultVertexMapping = vertexMappings.get(vertexMappingCombo.getItem(vertexMappingCombo.getSelectionIndex())); defaultEdgeMapping = edgeMappings.get(edgeMappingCombo.getItem(edgeMappingCombo.getSelectionIndex())); + defaultCRS = crss.get(crsCombo.getItem(crsCombo.getSelectionIndex())); + } + + public Resource getCRS() { + return defaultCRS; } } @@ -404,6 +427,7 @@ public class Functions { Resource diagram = graph.getSingleObject(composite, ModelingResources.getInstance(graph).CompositeToDiagram); graph.claim(diagram, DN.EdgeDefaultMapping, dialog.getDefaultEdgeMapping()); graph.claim(diagram, DN.VertexDefaultMapping, dialog.getDefaultVertexMapping()); + graph.claim(diagram, DN.HasSpatialRefSystem, dialog.getCRS()); } }); DefaultActions.asyncPerformDefaultAction(Simantics.getSession(), composite, false, false, true); diff --git a/org.simantics.district.network.ui/src/org/simantics/district/network/ui/prefs/DistrictPreferencePage.java b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/prefs/DistrictPreferencePage.java new file mode 100644 index 00000000..2d7154e6 --- /dev/null +++ b/org.simantics.district.network.ui/src/org/simantics/district/network/ui/prefs/DistrictPreferencePage.java @@ -0,0 +1,33 @@ +package org.simantics.district.network.ui.prefs; + +import org.eclipse.core.runtime.preferences.InstanceScope; +import org.eclipse.jface.preference.FieldEditorPreferencePage; +import org.eclipse.jface.preference.IPreferenceStore; +import org.eclipse.ui.IWorkbench; +import org.eclipse.ui.IWorkbenchPreferencePage; +import org.eclipse.ui.preferences.ScopedPreferenceStore; +import org.simantics.district.network.ui.internal.Activator; + +public class DistrictPreferencePage extends FieldEditorPreferencePage implements IWorkbenchPreferencePage { + + public DistrictPreferencePage() { + super(GRID); + setDescription("District preferences"); + } + + @Override + public void init(IWorkbench workbench) { + + } + + @Override + protected IPreferenceStore doGetPreferenceStore() { + return new ScopedPreferenceStore(InstanceScope.INSTANCE, Activator.PLUGIN_ID); + } + + @Override + protected void createFieldEditors() { + + } + +} diff --git a/org.simantics.district.network/META-INF/MANIFEST.MF b/org.simantics.district.network/META-INF/MANIFEST.MF index 2aaaa26e..60214a42 100644 --- a/org.simantics.district.network/META-INF/MANIFEST.MF +++ b/org.simantics.district.network/META-INF/MANIFEST.MF @@ -9,5 +9,6 @@ Require-Bundle: org.simantics.db, org.simantics.district.network.ontology, org.simantics.diagram.ontology;bundle-version="2.2.0", org.simantics.db.common, - org.simantics.db.layer0 + org.simantics.db.layer0, + org.simantics.district.maps Export-Package: org.simantics.district.network diff --git a/org.simantics.district.network/adapters.xml b/org.simantics.district.network/adapters.xml new file mode 100644 index 00000000..590741aa --- /dev/null +++ b/org.simantics.district.network/adapters.xml @@ -0,0 +1,11 @@ + + + + + + + + + \ No newline at end of file diff --git a/org.simantics.district.network/scl/Simantics/District.scl b/org.simantics.district.network/scl/Simantics/District.scl new file mode 100644 index 00000000..ed122513 --- /dev/null +++ b/org.simantics.district.network/scl/Simantics/District.scl @@ -0,0 +1,4 @@ +import "Simantics/DB" + +importJava "org.simantics.district.network.DistrictNetworkUtil" where + calculateDistance :: Resource -> Resource -> Double \ No newline at end of file diff --git a/org.simantics.district.network/src/org/simantics/district/network/CRS.java b/org.simantics.district.network/src/org/simantics/district/network/CRS.java new file mode 100644 index 00000000..e9a5a728 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/CRS.java @@ -0,0 +1,6 @@ +package org.simantics.district.network; + +public interface CRS { + + public double calculateDistance(double[] start, double[] end); +} 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 9bdec1b5..94799a97 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 @@ -3,11 +3,12 @@ package org.simantics.district.network; import java.util.Collection; import java.util.Iterator; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; import org.simantics.db.common.utils.OrderedSetUtils; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.layer0.util.RemoverUtil; import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.layer0.Layer0; @@ -20,6 +21,10 @@ public class DistrictNetworkUtil { Resource edge = graph.newResource(); graph.claim(edge, L0.InstanceOf, DN.Edge); + + Resource defaultEdgeMapping = graph.getPossibleObject(composite, DN.EdgeDefaultMapping); + graph.claim(edge, DN.HasMapping, defaultEdgeMapping); + OrderedSetUtils.add(graph, composite, edge); graph.claim(composite, L0.ConsistsOf, L0.PartOf, edge); return edge; @@ -32,6 +37,10 @@ public class DistrictNetworkUtil { Resource vertex = graph.newResource(); graph.claim(vertex, L0.InstanceOf, DN.Vertex); graph.claimLiteral(vertex, DIA.HasLocation, coords); + + Resource defaultVertexMapping = graph.getPossibleObject(composite, DN.VertexDefaultMapping); + graph.claim(vertex, DN.HasMapping, defaultVertexMapping); + OrderedSetUtils.add(graph, composite, vertex); graph.claim(composite, L0.ConsistsOf, L0.PartOf, vertex); @@ -61,4 +70,21 @@ public class DistrictNetworkUtil { } return master; } + + public static double calculateDistance(ReadGraph graph, Resource startVertex, Resource endVertex) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + Resource startComposite = graph.getSingleObject(startVertex, L0.PartOf); + Resource endComposite = graph.getSingleObject(endVertex, L0.PartOf); + if (!startComposite.equalsResource(endComposite)) { + throw new DatabaseException("Can not calculate distance between vertices on different composites! " + startVertex + " -> " + endVertex); + } + Resource crs = graph.getSingleObject(startComposite, DistrictNetworkResource.getInstance(graph).HasSpatialRefSystem); + + CRS crsClass = graph.adapt(crs, CRS.class); + + double[] startCoords = graph.getRelatedValue2(startVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); + double[] endCoords = graph.getRelatedValue2(endVertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); + + return crsClass.calculateDistance(startCoords, endCoords); + } } diff --git a/org.simantics.district.network/src/org/simantics/district/network/ModelledCRS.java b/org.simantics.district.network/src/org/simantics/district/network/ModelledCRS.java new file mode 100644 index 00000000..fd129ff0 --- /dev/null +++ b/org.simantics.district.network/src/org/simantics/district/network/ModelledCRS.java @@ -0,0 +1,30 @@ +package org.simantics.district.network; + +import org.geotools.referencing.GeodeticCalculator; +import org.opengis.referencing.FactoryException; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.district.network.ontology.DistrictNetworkResource; + +public class ModelledCRS implements CRS { + + private GeodeticCalculator calculator; + + public ModelledCRS(ReadGraph graph, Resource type) throws DatabaseException, FactoryException { + String wkt = graph.getRelatedValue2(type, DistrictNetworkResource.getInstance(graph).HasSRTEXT, Bindings.STRING); + CoordinateReferenceSystem crs = org.geotools.referencing.CRS.parseWKT(wkt); + this.calculator = new GeodeticCalculator(crs); + } + + @Override + public double calculateDistance(double[] start, double[] end) { + // TODO: fix the scale.. + calculator.setStartingGeographicPoint(start[0] / 100000, start[1] / 100000); + calculator.setDestinationGeographicPoint(end[0] / 100000, end[1] / 100000); + return calculator.getOrthodromicDistance(); + } + +}