]> gerrit.simantics Code Review - simantics/district.git/blobdiff - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
Add support for importing regions to district models
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
index 7e2a0e1e23c8cbbb4cd258792f7201a02b044dbc..083b58d5fa1d91c7fad40bc53d2fc939aab1d9e5 100644 (file)
@@ -256,6 +256,7 @@ public class DistrictImportUtils {
         int flowAreaIndex = model.getFlowAreaIndex();
         int nominalPressureLossIndex = model.getNominalPressureLossIndex();
         int addressIndex = model.getAddressIndex();
+        int regionIndex = model.getRegionIndex();
         
         int mappingColumn = model.getComponentMappingIndex();
         int idColumn = model.getIdIndex();
@@ -345,6 +346,7 @@ public class DistrictImportUtils {
                             writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
                             writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
                             writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
+                            writeStringValue(graph, row, regionIndex, vertex, DN.HasRegion);
                         } catch (DatabaseException | MismatchedDimensionException | TransformException e) {
                             throw new RuntimeException(e);
                         }
@@ -366,6 +368,7 @@ public class DistrictImportUtils {
         Path csvFile = model.getSource();
         char delim = model.getDelimiter();
 
+        Set<String> writtenIds = new HashSet<>();
         
         int startXCoordColumnIndex = model.getStartXCoordIndex();
         int startYCoordColumnIndex = model.getStartYCoordIndex();
@@ -382,6 +385,7 @@ public class DistrictImportUtils {
         int kSupplyIndex = model.getkSupplyIndex();
         int lengthIndex = model.getLengthIndex();
         int detailedGeometryIndex = model.getDetailedGeometryIndex();
+        int regionIndex = model.getRegionIndex();
         
         int mappingColumn = model.getComponentMappingIndex();
         int idColumn = model.getIdIndex();
@@ -435,60 +439,66 @@ public class DistrictImportUtils {
                     
                     DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> {
                         try {
-                            String mappingValue = row.get(mappingColumn);
-                            
-                            String startXCoords = row.get(startXCoordColumnIndex);
-                            String startYCoords = row.get(startYCoordColumnIndex);
-                            String startZCoords = row.get(startZValueColumnIndex);
-                            String endXCoords = row.get(endXCoordColumnIndex);
-                            String endYCoords = row.get(endYCoordColumnIndex);
-                            String endZCoords = row.get(endZValueColumnIndex);
-                            
-                            double startXCoord = Double.parseDouble(startXCoords); // make negative
-                            double startYCoord = Double.parseDouble(startYCoords);
-                            double startZCoord = Double.parseDouble(startZCoords);
                             
-                            double endXCoord = Double.parseDouble(endXCoords); // make negative
-                            double endYCoord = Double.parseDouble(endYCoords);
-                            double endZCoord = Double.parseDouble(endZCoords);
-                            
-                            double[] startCoords;
-                            double[] endCoords;
-                            if (actualDoTransform) {
-                                DirectPosition2D startTargetPos = new DirectPosition2D();
-                                DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
-                                DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
-                                startCoords = startRes.getCoordinate();
+                            String idValue = row.get(idColumn);
+                            if (!writtenIds.contains(idValue)) {
+                                writtenIds.add(idValue);
+                                String mappingValue = row.get(mappingColumn);
                                 
-                                DirectPosition2D endTargetPos = new DirectPosition2D();
-                                DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
-                                DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
-                                endCoords = endRes.getCoordinate();
-                            } else {
-                                startCoords = new double[] { startXCoord , startYCoord };
-                                endCoords = new double[] { endXCoord , endYCoord };
-                            }
-                            
-                            // Switch to (longitude, latitude)
-                            flipAxes(startCoords);
-                            flipAxes(endCoords);
-                            
-                            Optional<Resource> oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true);
-                            if (oedge.isPresent()) {
-                                Resource edge = oedge.get();
-                                writeStringValue(graph, row, idColumn, edge, DN.HasId);
+                                String startXCoords = row.get(startXCoordColumnIndex);
+                                String startYCoords = row.get(startYCoordColumnIndex);
+                                String startZCoords = row.get(startZValueColumnIndex);
+                                String endXCoords = row.get(endXCoordColumnIndex);
+                                String endYCoords = row.get(endYCoordColumnIndex);
+                                String endZCoords = row.get(endZValueColumnIndex);
+                                
+                                double startXCoord = Double.parseDouble(startXCoords); // make negative
+                                double startYCoord = Double.parseDouble(startYCoords);
+                                double startZCoord = Double.parseDouble(startZCoords);
+                                
+                                double endXCoord = Double.parseDouble(endXCoords); // make negative
+                                double endYCoord = Double.parseDouble(endYCoords);
+                                double endZCoord = Double.parseDouble(endZCoords);
+                                
+                                double[] startCoords;
+                                double[] endCoords;
+                                if (actualDoTransform) {
+                                    DirectPosition2D startTargetPos = new DirectPosition2D();
+                                    DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
+                                    DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
+                                    startCoords = startRes.getCoordinate();
+                                    
+                                    DirectPosition2D endTargetPos = new DirectPosition2D();
+                                    DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
+                                    DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
+                                    endCoords = endRes.getCoordinate();
+                                } else {
+                                    startCoords = new double[] { startXCoord , startYCoord };
+                                    endCoords = new double[] { endXCoord , endYCoord };
+                                }
+                                
+                                // Switch to (longitude, latitude)
+                                flipAxes(startCoords);
+                                flipAxes(endCoords);
                                 
-                                writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
-                                writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
-                                writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
-                                writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
-                                writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
-                                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);
+                                Optional<Resource> oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true);
+                                if (oedge.isPresent()) {
+                                    Resource edge = oedge.get();
+                                    
+                                    writeStringValue(graph, row, idColumn, edge, DN.HasId);
+                                    
+                                    writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
+                                    writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
+                                    writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
+                                    writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
+                                    writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
+                                    writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
+                                    writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
+                                    writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
+                                    writeStringValue(graph, row, regionIndex, edge, DN.HasRegion);
+                                    writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
+                                }
                             }
-                            
                             return true;
                         } catch (DatabaseException | MismatchedDimensionException | TransformException e) {
                             throw new RuntimeException(e);