]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java
Add edge geometry for detailed rendering in closer zoom levels
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / CSVImportWizard.java
index 490c9505ecde9e459b1c9c2547d38e0e7b41278a..8c087210400459bba772a717fc3c2ec2620bfcb1 100644 (file)
@@ -108,6 +108,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                         int kReturnIndex = model.getkReturnIndex();
                         int kSupplyIndex = model.getkSupplyIndex();
                         int lengthIndex = model.getLengthIndex();
+                        int detailedGeometryIndex = model.getDetailedGeometryIndex();
                         
                         int mappingColumn = model.getComponentMappingIndex();
                         int idColumn = model.getIdIndex();
@@ -176,6 +177,10 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                                             } else {
                                                 coords = new double[] { xCoord, yCoord };
                                             }
+                                            
+                                            // Switch to (longitude, latitude)
+                                            flipAxes(coords);
+                                            
                                             Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue));
                                             
                                             writeStringValue(graph, row, idColumn, vertex, DN.HasId);
@@ -230,7 +235,11 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                                                 startCoords = new double[] { startXCoord , startYCoord };
                                                 endCoords = new double[] { endXCoord , endYCoord };
                                             }
-
+                                            
+                                            // Switch to (longitude, latitude)
+                                            flipAxes(startCoords);
+                                            flipAxes(endCoords);
+                                            
                                             Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding, true);
                                             writeStringValue(graph, row, idColumn, edge, DN.HasId);
                                             
@@ -242,6 +251,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
                                             writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
                                             writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
                                             writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
+                                            writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
                                         }
                                     } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
                                         throw new DatabaseException(e);
@@ -267,9 +277,15 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
             return false;
         }
     }
-    
+
+    private static void flipAxes(double[] coords) {
+       double tmp = coords[0];
+       coords[0] = coords[1];
+       coords[1] = tmp;
+    }
+
     private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
-        if (index != -1) {
+       if (index != -1) {
             String stringValue = row.get(index);
             if (!stringValue.isEmpty()) {
                 try {
@@ -293,4 +309,41 @@ public class CSVImportWizard extends Wizard implements IImportWizard {
             }
         }
     }
+
+    private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
+        if (index != -1) {
+            String stringValue = row.get(index);
+            if (!stringValue.isEmpty()) {
+                stringValue = stringValue.substring(1, stringValue.length() - 1);
+                String[] coordPairs = stringValue.split(";");
+                ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
+                for (int i = 0; i < coordPairs.length; i++) {
+                    String coordPair = coordPairs[i];
+                    String[] p = coordPair.split(" ");
+                    double x = Double.parseDouble(p[0]);
+                    double y = Double.parseDouble(p[1]);
+                    if (actualTransform != null) {
+                        DirectPosition2D targetPos = new DirectPosition2D();
+                        DirectPosition2D sourcePos = new DirectPosition2D(y, x);
+                        DirectPosition res = actualTransform.transform(sourcePos, targetPos);
+                        double[] coords = res.getCoordinate();
+                        x = coords[1];
+                        y = coords[0];
+                    }
+                    dd.add(x);
+                    dd.add(y);
+                }
+                double[] detailedGeometryCoords = new double[dd.size()];
+                for (int i = 0; i < dd.size(); i++) {
+                    double d = dd.get(i);
+                    detailedGeometryCoords[i] = d;
+                }
+                try {
+                    graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
+                } catch (NumberFormatException e) {
+                    throw new DatabaseException(e);
+                }
+            }
+        }
+    }
 }