1 package org.simantics.district.imports.ui;
3 import java.lang.reflect.InvocationTargetException;
4 import java.nio.file.Path;
5 import java.util.ArrayList;
6 import java.util.Collection;
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;
40 public class CSVImportWizard extends Wizard implements IImportWizard {
42 private CSVImportModel model;
44 public CSVImportWizard() {
45 setWindowTitle("Import CSV data");
46 setNeedsProgressMonitor(true);
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));
59 public boolean performFinish() {
61 getContainer().run(true, true, new IRunnableWithProgress() {
64 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
66 Path csvFile = model.getSource();
67 char delim = model.getDelimiter();
69 List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
70 monitor.beginTask("Importing CSV", rows.size());
72 // Path wktFile = model.getWKTFile();
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();
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();
113 int mappingColumn = model.getComponentMappingIndex();
114 int idColumn = model.getIdIndex();
116 double padding = model.getEdgePadding();
118 String sourceEPSGCRS = model.getSourceCRS();
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);
129 final boolean actualDoTransform = doTransform;
130 final MathTransform actualTransform = transform;
131 Simantics.getSession().syncRequest(new Write() {
134 public void perform(WriteGraph graph) throws DatabaseException {
135 graph.markUndoPoint();
137 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
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));
146 for (int k = 1; k < rows.size(); k++) {
147 CSVRecord row = rows.get(k);
149 String mappingValue = row.get(mappingColumn);
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);
159 if (zCoordColumnIndex != -1) {
160 String zs = row.get(zCoordColumnIndex);
164 z = Double.parseDouble(zs);
165 } catch (NumberFormatException e) {
166 throw new DatabaseException(e);
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();
178 coords = new double[] { xCoord, yCoord };
181 // Switch to (longitude, latitude)
184 Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue));
186 writeStringValue(graph, row, idColumn, vertex, DN.HasId);
188 graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
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);
211 String startXCoords = row.get(startXCoordColumnIndex);
212 String startYCoords = row.get(startYCoordColumnIndex);
213 String endXCoords = row.get(endXCoordColumnIndex);
214 String endYCoords = row.get(endYCoordColumnIndex);
216 double startXCoord = Double.parseDouble(startXCoords); // make negative
217 double startYCoord = Double.parseDouble(startYCoords);
219 double endXCoord = Double.parseDouble(endXCoords); // make negative
220 double endYCoord = Double.parseDouble(endYCoords);
222 double[] startCoords;
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();
230 DirectPosition2D endTargetPos = new DirectPosition2D();
231 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
232 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
233 endCoords = endRes.getCoordinate();
235 startCoords = new double[] { startXCoord , startYCoord };
236 endCoords = new double[] { endXCoord , endYCoord };
239 // Switch to (longitude, latitude)
240 flipAxes(startCoords);
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);
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);
256 } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
257 throw new DatabaseException(e);
263 } catch (Exception e) {
264 throw new InvocationTargetException(e);
269 } catch (InvocationTargetException e) {
270 Throwable t = e.getTargetException();
271 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
272 cp.setErrorMessage(t.getMessage());
273 ExceptionUtils.logAndShowError(t);
275 } catch (InterruptedException e) {
276 ExceptionUtils.logAndShowError(e);
281 private static void flipAxes(double[] coords) {
282 double tmp = coords[0];
283 coords[0] = coords[1];
287 private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
289 String stringValue = row.get(index);
290 if (!stringValue.isEmpty()) {
292 graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
293 } catch (NumberFormatException e) {
294 throw new DatabaseException(e);
300 private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
302 String stringValue = row.get(index);
303 if (!stringValue.isEmpty()) {
305 graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
306 } catch (NumberFormatException e) {
307 throw new DatabaseException(e);
313 private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
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();
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;
342 graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
343 } catch (NumberFormatException e) {
344 throw new DatabaseException(e);