]> 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 54a578ddac5a837d884de0d38a1ca20c4890a6ed..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();
@@ -250,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);
@@ -307,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);
+                }
+            }
+        }
+    }
 }