]> gerrit.simantics Code Review - simantics/district.git/blob - 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
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                         int detailedGeometryIndex = model.getDetailedGeometryIndex();
112                         
113                         int mappingColumn = model.getComponentMappingIndex();
114                         int idColumn = model.getIdIndex();
115                         
116                         double padding = model.getEdgePadding();
117                         
118                         String sourceEPSGCRS = model.getSourceCRS();
119                         
120                         MathTransform transform = null;
121                         boolean doTransform = false;
122                         // if sourceEPSGCRS is empty || null then ignore transformation
123                         if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
124                             CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
125                             CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
126                             transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
127                             doTransform = true;
128                         }
129                         final boolean actualDoTransform = doTransform;
130                         final MathTransform actualTransform = transform;
131                         Simantics.getSession().syncRequest(new Write() {
132                             
133                             @Override
134                             public void perform(WriteGraph graph) throws DatabaseException {
135                                 graph.markUndoPoint();
136                                 
137                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
138                                 
139                                 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
140                                 List<ResourceVertex> vv = new ArrayList<>(vertices.size());
141                                 for (Resource vertex : vertices) {
142                                     double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
143                                     vv.add(new ResourceVertex(vertex, existingCoords));
144                                 }
145                                 
146                                 for (int k = 1; k < rows.size(); k++) {
147                                     CSVRecord row = rows.get(k);
148                                     
149                                     String mappingValue = row.get(mappingColumn);
150     
151                                     try {
152                                         if (model.isVertexImport()) {
153                                             String xCoords = row.get(xCoordColumnIndex);
154                                             String yCoords = row.get(yCoordColumnIndex);
155                                             double xCoord = Double.parseDouble(xCoords);
156                                             double yCoord = Double.parseDouble(yCoords);
157                                             
158                                             double z = 0;
159                                             if (zCoordColumnIndex != -1) {
160                                                 String zs = row.get(zCoordColumnIndex);
161                                                 
162                                                 if (!zs.isEmpty()) {
163                                                     try {
164                                                         z = Double.parseDouble(zs);
165                                                     } catch (NumberFormatException e) {
166                                                         throw new DatabaseException(e);
167                                                     }
168                                                 }
169                                             }
170
171                                             double[] coords;
172                                             if (actualDoTransform) {
173                                                 DirectPosition2D targetPos = new DirectPosition2D();
174                                                 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
175                                                 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
176                                                 coords = res.getCoordinate();
177                                             } else {
178                                                 coords = new double[] { xCoord, yCoord };
179                                             }
180                                             
181                                             // Switch to (longitude, latitude)
182                                             flipAxes(coords);
183                                             
184                                             Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue));
185                                             
186                                             writeStringValue(graph, row, idColumn, vertex, DN.HasId);
187                                             
188                                             graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
189                                             
190                                             writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
191                                             writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
192                                             writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
193                                             writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
194                                             writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
195                                             writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
196                                             writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
197                                             writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
198                                             writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
199                                             writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
200                                             writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
201                                             writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
202                                             writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
203                                             writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
204                                             writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
205                                             writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
206                                             writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
207                                             writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
208                                             writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
209
210                                         } else {
211                                             String startXCoords = row.get(startXCoordColumnIndex);
212                                             String startYCoords = row.get(startYCoordColumnIndex);
213                                             String endXCoords = row.get(endXCoordColumnIndex);
214                                             String endYCoords = row.get(endYCoordColumnIndex);
215                                             
216                                             double startXCoord = Double.parseDouble(startXCoords); // make negative
217                                             double startYCoord = Double.parseDouble(startYCoords);
218                                             
219                                             double endXCoord = Double.parseDouble(endXCoords); // make negative
220                                             double endYCoord = Double.parseDouble(endYCoords);
221                                             
222                                             double[] startCoords;
223                                             double[] endCoords;
224                                             if (actualDoTransform) {
225                                                 DirectPosition2D startTargetPos = new DirectPosition2D();
226                                                 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
227                                                 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
228                                                 startCoords = startRes.getCoordinate();
229                                                 
230                                                 DirectPosition2D endTargetPos = new DirectPosition2D();
231                                                 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
232                                                 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
233                                                 endCoords = endRes.getCoordinate();
234                                             } else {
235                                                 startCoords = new double[] { startXCoord , startYCoord };
236                                                 endCoords = new double[] { endXCoord , endYCoord };
237                                             }
238                                             
239                                             // Switch to (longitude, latitude)
240                                             flipAxes(startCoords);
241                                             flipAxes(endCoords);
242                                             
243                                             Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding, true);
244                                             writeStringValue(graph, row, idColumn, edge, DN.HasId);
245                                             
246                                             writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
247                                             writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
248                                             writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
249                                             writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
250                                             writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
251                                             writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
252                                             writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
253                                             writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
254                                             writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
255                                         }
256                                     } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
257                                         throw new DatabaseException(e);
258                                     }
259                                     monitor.worked(1);
260                                 }
261                             }
262                         });
263                     } catch (Exception e) {
264                         throw new InvocationTargetException(e);
265                     }
266                 }
267             });
268             return true;
269         } catch (InvocationTargetException e) {
270             Throwable t = e.getTargetException();
271             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
272             cp.setErrorMessage(t.getMessage());
273             ExceptionUtils.logAndShowError(t);
274             return false;
275         } catch (InterruptedException e) {
276             ExceptionUtils.logAndShowError(e);
277             return false;
278         }
279     }
280
281     private static void flipAxes(double[] coords) {
282         double tmp = coords[0];
283         coords[0] = coords[1];
284         coords[1] = tmp;
285     }
286
287     private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
288         if (index != -1) {
289             String stringValue = row.get(index);
290             if (!stringValue.isEmpty()) {
291                 try {
292                     graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
293                 } catch (NumberFormatException e) {
294                     throw new DatabaseException(e);
295                 }
296             }
297         }
298     }
299     
300     private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
301         if (index != -1) {
302             String stringValue = row.get(index);
303             if (!stringValue.isEmpty()) {
304                 try {
305                     graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
306                 } catch (NumberFormatException e) {
307                     throw new DatabaseException(e);
308                 }
309             }
310         }
311     }
312
313     private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
314         if (index != -1) {
315             String stringValue = row.get(index);
316             if (!stringValue.isEmpty()) {
317                 stringValue = stringValue.substring(1, stringValue.length() - 1);
318                 String[] coordPairs = stringValue.split(";");
319                 ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
320                 for (int i = 0; i < coordPairs.length; i++) {
321                     String coordPair = coordPairs[i];
322                     String[] p = coordPair.split(" ");
323                     double x = Double.parseDouble(p[0]);
324                     double y = Double.parseDouble(p[1]);
325                     if (actualTransform != null) {
326                         DirectPosition2D targetPos = new DirectPosition2D();
327                         DirectPosition2D sourcePos = new DirectPosition2D(y, x);
328                         DirectPosition res = actualTransform.transform(sourcePos, targetPos);
329                         double[] coords = res.getCoordinate();
330                         x = coords[1];
331                         y = coords[0];
332                     }
333                     dd.add(x);
334                     dd.add(y);
335                 }
336                 double[] detailedGeometryCoords = new double[dd.size()];
337                 for (int i = 0; i < dd.size(); i++) {
338                     double d = dd.get(i);
339                     detailedGeometryCoords[i] = d;
340                 }
341                 try {
342                     graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
343                 } catch (NumberFormatException e) {
344                     throw new DatabaseException(e);
345                 }
346             }
347         }
348     }
349 }