]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java
Edge styling (e.g. width, color etc)
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / CSVImportWizard.java
1 package org.simantics.district.imports.ui;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.nio.file.Path;
5 import java.util.List;
6
7 import org.apache.commons.csv.CSVRecord;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.jface.operation.IRunnableWithProgress;
10 import org.eclipse.jface.viewers.IStructuredSelection;
11 import org.eclipse.jface.wizard.Wizard;
12 import org.eclipse.jface.wizard.WizardPage;
13 import org.eclipse.ui.IImportWizard;
14 import org.eclipse.ui.IWorkbench;
15 import org.geotools.geometry.DirectPosition2D;
16 import org.geotools.referencing.CRS;
17 import org.opengis.geometry.DirectPosition;
18 import org.opengis.geometry.MismatchedDimensionException;
19 import org.opengis.referencing.crs.CoordinateReferenceSystem;
20 import org.opengis.referencing.operation.MathTransform;
21 import org.opengis.referencing.operation.TransformException;
22 import org.simantics.Simantics;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.Resource;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.request.Write;
28 import org.simantics.district.imports.DistrictImportUtils;
29 import org.simantics.district.network.DistrictNetworkUtil;
30 import org.simantics.district.network.ontology.DistrictNetworkResource;
31 import org.simantics.district.network.ui.DNEdgeBuilder;
32 import org.simantics.maps.MapScalingTransform;
33 import org.simantics.utils.ui.ExceptionUtils;
34
35 public class CSVImportWizard extends Wizard implements IImportWizard {
36
37     private CSVImportModel model;
38     
39     public CSVImportWizard() {
40         setWindowTitle("Import CSV data");
41     }
42     
43     
44     @Override
45     public void init(IWorkbench workbench, IStructuredSelection selection) {
46         model = new CSVImportModel();
47         addPage(new CSVImportWizardFirstPage(model));
48         addPage(new CSVImportWizardPage(model));
49         addPage(new ComponentMappingPage(model));
50     }
51     
52     @Override
53     public boolean performFinish() {
54         try {
55             getContainer().run(true, true, new IRunnableWithProgress() {
56
57                 @Override
58                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
59                     monitor.beginTask("Importing CSV", 1000);
60                     try {
61                         Path csvFile = model.getSource();
62                         char delim = model.getDelimiter();
63                         
64                         List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
65                         
66     //                    Path wktFile = model.getWKTFile();
67                         
68                         int xCoordColumnIndex = model.getXCoordIndex();
69                         int yCoordColumnIndex = model.getYCoordIndex();
70                         int zCoordColumnIndex = model.getZCoordIndex();
71                         int supplyTempColumnIndex = model.getSupplyTempIndex();
72                         int returnTempColumnIndex = model.getReturnTempIndex();
73                         int supplyPressureColumnIndex = model.getSupplyPressureIndex();
74                         int returnPressureColumnIndex = model.getReturnPressureIndex();
75                         int dpIndex = model.getDeltaPressureIndex();
76                         int dtIndex = model.getDeltaTemperatureIndex();
77                         int heatPowerIndex = model.getHeatPowerIndex();
78                         int valvePositionIndex = model.getValvePositionIndx();
79                         int nominalHeadMIndex = model.getNominalHeadMIndex();
80                         int nominalHeadBIndex = model.getNominalHeadBIndex();
81                         int nominalFlowIndex = model.getNominalFlowIndex();
82                         int maximumHeadMIndex = model.getMaximumHeadMIndex();
83                         int heatLoadDsIndex = model.getHeatLoadDsIndex();
84                         int massFlowIndex = model.getMassFlowIndex();
85                         int volFlowIndex = model.getVolFlowIndex();
86                         int velocityIndex = model.getVelocityIndex();
87                         int flowAreaIndex = model.getFlowAreaIndex();
88                         int nominalPressureLossIndex = model.getNominalPressureLossIndex();
89                         
90                         int startXCoordColumnIndex = model.getStartXCoordIndex();
91                         int startYCoordColumnIndex = model.getStartYCoordIndex();
92                         int startZValueColumnIndex = model.getStartZCoordIndex();
93                         int endXCoordColumnIndex = model.getEndXCoordIndex();
94                         int endYCoordColumnIndex = model.getEndYCoordIndex();
95                         int endZValueColumnIndex = model.getEndZCoordIndex();
96                         int diameterColumnIndex= model.getDiameterIndex();
97                         int outerDiameterColumnIndex = model.getOuterDiamterIndex();
98                         int nominalMassFlowIndex = model.getNominalMassFlowIndex();
99                         int tGroundIndex = model.gettGroundIndex();
100                         int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex();
101                         int kReturnIndex = model.getkReturnIndex();
102                         int kSupplyIndex = model.getkSupplyIndex();
103                         
104                         int mappingColumn = model.getComponentMappingIndex();
105                         int idColumn = model.getIdIndex();
106                         
107                         double padding = model.getEdgePadding();
108                         
109                         String sourceEPSGCRS = model.getSourceCRS();
110                         
111                         MathTransform transform = null;
112                         boolean doTransform = false;
113                         // if sourceEPSGCRS is empty || null then ignore transformation
114                         if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
115                             CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
116                             CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
117                             transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
118                             doTransform = true;
119                         }
120                         final boolean actualDoTransform = doTransform;
121                         final MathTransform actualTransform = transform;
122                         Simantics.getSession().syncRequest(new Write() {
123                             
124                             @Override
125                             public void perform(WriteGraph graph) throws DatabaseException {
126                                 graph.markUndoPoint();
127                                 
128                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
129                                 
130                                 for (int k = 1; k < rows.size(); k++) {
131                                     CSVRecord row = rows.get(k);
132                                     
133                                     String mappingValue = row.get(mappingColumn);
134     
135                                     try {
136                                         if (model.isVertexImport()) {
137                                             String xCoords = row.get(xCoordColumnIndex);
138                                             String yCoords = row.get(yCoordColumnIndex);
139                                             double xCoord = - Double.parseDouble(xCoords); // make negative for now
140                                             double yCoord = Double.parseDouble(yCoords);
141                                             
142                                             double z = 0;
143                                             if (zCoordColumnIndex != -1) {
144                                                 String zs = row.get(zCoordColumnIndex);
145                                                 
146                                                 if (!zs.isEmpty()) {
147                                                     try {
148                                                         z = Double.parseDouble(zs);
149                                                     } catch (NumberFormatException e) {
150                                                         throw new DatabaseException(e);
151                                                     }
152                                                 }
153                                             }
154
155                                             double[] coords;
156                                             if (actualDoTransform) {
157                                                 DirectPosition2D targetPos = new DirectPosition2D();
158                                                 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
159                                                 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
160                                                 coords = res.getCoordinate();
161                                             } else {
162                                                 coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() };
163                                             }
164                                             Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue));
165                                             
166                                             writeStringValue(graph, row, idColumn, vertex, DN.HasId);
167                                             
168                                             graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
169                                             
170                                             writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
171                                             writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
172                                             writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
173                                             writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
174                                             writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
175                                             writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
176                                             writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
177                                             writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
178                                             writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
179                                             writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
180                                             writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
181                                             writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
182                                             writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
183                                             writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
184                                             writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
185                                             writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
186                                             writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
187                                             writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
188
189                                         } else {
190                                             String startXCoords = row.get(startXCoordColumnIndex);
191                                             String startYCoords = row.get(startYCoordColumnIndex);
192                                             String endXCoords = row.get(endXCoordColumnIndex);
193                                             String endYCoords = row.get(endYCoordColumnIndex);
194                                             
195                                             double startXCoord = - Double.parseDouble(startXCoords); // make negative
196                                             double startYCoord = Double.parseDouble(startYCoords);
197                                             
198                                             double endXCoord = - Double.parseDouble(endXCoords); // make negative
199                                             double endYCoord = Double.parseDouble(endYCoords);
200                                             
201                                             double[] startCoords;
202                                             double[] endCoords;
203                                             if (actualDoTransform) {
204                                                 DirectPosition2D startTargetPos = new DirectPosition2D();
205                                                 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
206                                                 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
207                                                 startCoords = startRes.getCoordinate();
208                                                 
209                                                 DirectPosition2D endTargetPos = new DirectPosition2D();
210                                                 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
211                                                 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
212                                                 endCoords = endRes.getCoordinate();
213                                             } else {
214                                                 startCoords = new double[] { startXCoord / MapScalingTransform.getScaleX(), startYCoord / MapScalingTransform.getScaleY() };
215                                                 endCoords = new double[] { endXCoord / MapScalingTransform.getScaleX(), endYCoord / MapScalingTransform.getScaleY() };
216                                             }
217                                              
218                                             Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, padding);
219                                             writeStringValue(graph, row, idColumn, edge, DN.HasId);
220                                             
221                                             writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
222                                             writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
223                                             writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
224                                             writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
225                                             writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
226                                             writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
227                                             writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
228                                         }
229                                     } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
230                                         throw new DatabaseException(e);
231                                     }
232                                 }
233                             }
234                         });
235                     } catch (Exception e) {
236                         throw new InvocationTargetException(e);
237                     }
238                 }
239             });
240             return true;
241         } catch (InvocationTargetException e) {
242             Throwable t = e.getTargetException();
243             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
244             cp.setErrorMessage(t.getMessage());
245             ExceptionUtils.logAndShowError(t);
246             return false;
247         } catch (InterruptedException e) {
248             ExceptionUtils.logAndShowError(e);
249             return false;
250         }
251     }
252     
253     private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
254         if (index != -1) {
255             String stringValue = row.get(index);
256             if (!stringValue.isEmpty()) {
257                 try {
258                     graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
259                 } catch (NumberFormatException e) {
260                     throw new DatabaseException(e);
261                 }
262             }
263         }
264     }
265     
266     private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
267         if (index != -1) {
268             String stringValue = row.get(index);
269             if (!stringValue.isEmpty()) {
270                 try {
271                     graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
272                 } catch (NumberFormatException e) {
273                     throw new DatabaseException(e);
274                 }
275             }
276         }
277     }
278 }