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