X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2Fui%2FCSVImportWizard.java;h=76c30d98113adb381a39b1c38c47b80b479ab54b;hb=ac6ad8ff2b20a10e85e7b12c1af670500daebdd4;hp=703ade176923886f2691d9fe47d034d9f5ef33b7;hpb=231b9da5dfd8129e24cb5a9d2ec87ff9afa7afd9;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index 703ade17..76c30d98 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -2,6 +2,8 @@ package org.simantics.district.imports.ui; import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import org.apache.commons.csv.CSVRecord; @@ -23,13 +25,16 @@ import org.simantics.Simantics; import org.simantics.databoard.Bindings; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.request.Write; +import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.imports.DistrictImportUtils; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; import org.simantics.district.network.ui.DNEdgeBuilder; -import org.simantics.maps.MapScalingTransform; +import org.simantics.district.network.ui.DNEdgeBuilder.ResourceVertex; +import org.simantics.layer0.Layer0; import org.simantics.utils.ui.ExceptionUtils; public class CSVImportWizard extends Wizard implements IImportWizard { @@ -38,6 +43,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { public CSVImportWizard() { setWindowTitle("Import CSV data"); + setNeedsProgressMonitor(true); } @@ -61,14 +67,31 @@ public class CSVImportWizard extends Wizard implements IImportWizard { char delim = model.getDelimiter(); List rows = DistrictImportUtils.readRows(csvFile, delim, -1); + monitor.beginTask("Importing CSV", rows.size()); // Path wktFile = model.getWKTFile(); int xCoordColumnIndex = model.getXCoordIndex(); int yCoordColumnIndex = model.getYCoordIndex(); int zCoordColumnIndex = model.getZCoordIndex(); - int tempColumnIndex = model.getTempIndex(); - int pressureColumnIndex = model.getPressureIndex(); + int supplyTempColumnIndex = model.getSupplyTempIndex(); + int returnTempColumnIndex = model.getReturnTempIndex(); + int supplyPressureColumnIndex = model.getSupplyPressureIndex(); + int returnPressureColumnIndex = model.getReturnPressureIndex(); + int dpIndex = model.getDeltaPressureIndex(); + int dtIndex = model.getDeltaTemperatureIndex(); + int heatPowerIndex = model.getHeatPowerIndex(); + int valvePositionIndex = model.getValvePositionIndx(); + int nominalHeadMIndex = model.getNominalHeadMIndex(); + int nominalHeadBIndex = model.getNominalHeadBIndex(); + int nominalFlowIndex = model.getNominalFlowIndex(); + int maximumHeadMIndex = model.getMaximumHeadMIndex(); + int heatLoadDsIndex = model.getHeatLoadDsIndex(); + int massFlowIndex = model.getMassFlowIndex(); + int volFlowIndex = model.getVolFlowIndex(); + int velocityIndex = model.getVelocityIndex(); + int flowAreaIndex = model.getFlowAreaIndex(); + int nominalPressureLossIndex = model.getNominalPressureLossIndex(); int startXCoordColumnIndex = model.getStartXCoordIndex(); int startYCoordColumnIndex = model.getStartYCoordIndex(); @@ -79,8 +102,15 @@ public class CSVImportWizard extends Wizard implements IImportWizard { int diameterColumnIndex= model.getDiameterIndex(); int outerDiameterColumnIndex = model.getOuterDiamterIndex(); int nominalMassFlowIndex = model.getNominalMassFlowIndex(); + int tGroundIndex = model.gettGroundIndex(); + int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex(); + int kReturnIndex = model.getkReturnIndex(); + int kSupplyIndex = model.getkSupplyIndex(); int mappingColumn = model.getComponentMappingIndex(); + int idColumn = model.getIdIndex(); + + double padding = model.getEdgePadding(); String sourceEPSGCRS = model.getSourceCRS(); @@ -99,9 +129,17 @@ public class CSVImportWizard extends Wizard implements IImportWizard { @Override public void perform(WriteGraph graph) throws DatabaseException { + graph.markUndoPoint(); DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); + Collection vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); + List vv = new ArrayList<>(vertices.size()); + for (Resource vertex : vertices) { + double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); + vv.add(new ResourceVertex(vertex, existingCoords)); + } + for (int k = 1; k < rows.size(); k++) { CSVRecord row = rows.get(k); @@ -114,13 +152,16 @@ public class CSVImportWizard extends Wizard implements IImportWizard { double xCoord = Double.parseDouble(xCoords); double yCoord = Double.parseDouble(yCoords); - String zs = row.get(zCoordColumnIndex); double z = 0; - if (!zs.isEmpty()) { - try { - z = Double.parseDouble(zs); - } catch (NumberFormatException e) { - throw new DatabaseException(e); + if (zCoordColumnIndex != -1) { + String zs = row.get(zCoordColumnIndex); + + if (!zs.isEmpty()) { + try { + z = Double.parseDouble(zs); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } } } @@ -131,30 +172,32 @@ public class CSVImportWizard extends Wizard implements IImportWizard { DirectPosition res = actualTransform.transform(sourcePos, targetPos); coords = res.getCoordinate(); } else { - coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() }; + coords = new double[] { xCoord, yCoord }; } - Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue)); + Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue)); + + writeStringValue(graph, row, idColumn, vertex, DN.HasId); graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE); - String tempS = row.get(tempColumnIndex); - if (!tempS.isEmpty()) { - try { - double temp = Double.parseDouble(tempS); - graph.claimLiteral(vertex, DN.Vertex_HasTemperature, temp, Bindings.DOUBLE); - } catch (NumberFormatException e) { - throw new DatabaseException(e); - } - } - String pressureS = row.get(pressureColumnIndex); - if (!pressureS.isEmpty()) { - try { - double pressure = Double.parseDouble(pressureS); - graph.claimLiteral(vertex, DN.Vertex_HasPressure, pressure, Bindings.DOUBLE); - } catch (NumberFormatException e) { - throw new DatabaseException(e); - } - } + writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature); + writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature); + writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure); + writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure); + writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure); + writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature); + writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower); + writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition); + writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM); + writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB); + writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow); + writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM); + writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs); + writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow); + writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow); + writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity); + writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea); + writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss); } else { String startXCoords = row.get(startXCoordColumnIndex); @@ -162,10 +205,10 @@ public class CSVImportWizard extends Wizard implements IImportWizard { String endXCoords = row.get(endXCoordColumnIndex); String endYCoords = row.get(endYCoordColumnIndex); - double startXCoord = Double.parseDouble(startXCoords); + double startXCoord = Double.parseDouble(startXCoords); // make negative double startYCoord = Double.parseDouble(startYCoords); - double endXCoord = Double.parseDouble(endXCoords); + double endXCoord = Double.parseDouble(endXCoords); // make negative double endYCoord = Double.parseDouble(endYCoords); double[] startCoords; @@ -181,44 +224,25 @@ public class CSVImportWizard extends Wizard implements IImportWizard { DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos); endCoords = endRes.getCoordinate(); } else { - startCoords = new double[] { startXCoord / MapScalingTransform.getScaleX(), startYCoord / MapScalingTransform.getScaleY() }; - endCoords = new double[] { endXCoord / MapScalingTransform.getScaleX(), endYCoord / MapScalingTransform.getScaleY() }; + startCoords = new double[] { startXCoord , startYCoord }; + endCoords = new double[] { endXCoord , endYCoord }; } + + Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding); + writeStringValue(graph, row, idColumn, edge, DN.HasId); - Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, 0.0000001); - graph.claim(edge, DistrictNetworkResource.getInstance(graph).HasMapping, model.getComponentMappings().get(mappingValue)); - - String diameterS = row.get(diameterColumnIndex); - if (!diameterS.isEmpty()) { - try { - double diameter = Double.parseDouble(diameterS); - graph.claimLiteral(edge, DN.Edge_HasDiameter, diameter); - } catch (NumberFormatException e) { - throw new DatabaseException(e); - } - } - String outerDiameterS = row.get(outerDiameterColumnIndex); - if (!outerDiameterS.isEmpty()) { - try { - double outerDiameter = Double.parseDouble(outerDiameterS); - graph.claimLiteral(edge, DN.Edge_HasOuterDiameter, outerDiameter); - } catch (NumberFormatException e) { - throw new DatabaseException(e); - } - } - String nominalMassFlowS = row.get(nominalMassFlowIndex); - if (!nominalMassFlowS.isEmpty()) { - try { - double nominalMassFlow = Double.parseDouble(nominalMassFlowS); - graph.claimLiteral(edge, DN.Edge_HasNominalMassFlow, nominalMassFlow); - } catch (NumberFormatException e) { - throw new DatabaseException(e); - } - } + 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); } } catch (MismatchedDimensionException | TransformException | DatabaseException e) { throw new DatabaseException(e); } + monitor.worked(1); } } }); @@ -239,4 +263,30 @@ public class CSVImportWizard extends Wizard implements IImportWizard { return false; } } + + private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + try { + graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + } + } + + private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + try { + graph.claimLiteral(subject, relation, stringValue, Bindings.STRING); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + } + } }