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();
111 int mappingColumn = model.getComponentMappingIndex();
112 int idColumn = model.getIdIndex();
114 double padding = model.getEdgePadding();
116 String sourceEPSGCRS = model.getSourceCRS();
118 MathTransform transform = null;
119 boolean doTransform = false;
120 // if sourceEPSGCRS is empty || null then ignore transformation
121 if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
122 CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
123 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
124 transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
127 final boolean actualDoTransform = doTransform;
128 final MathTransform actualTransform = transform;
129 Simantics.getSession().syncRequest(new Write() {
132 public void perform(WriteGraph graph) throws DatabaseException {
133 graph.markUndoPoint();
135 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
137 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
138 List<ResourceVertex> vv = new ArrayList<>(vertices.size());
139 for (Resource vertex : vertices) {
140 double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
141 vv.add(new ResourceVertex(vertex, existingCoords));
144 for (int k = 1; k < rows.size(); k++) {
145 CSVRecord row = rows.get(k);
147 String mappingValue = row.get(mappingColumn);
150 if (model.isVertexImport()) {
151 String xCoords = row.get(xCoordColumnIndex);
152 String yCoords = row.get(yCoordColumnIndex);
153 double xCoord = Double.parseDouble(xCoords);
154 double yCoord = Double.parseDouble(yCoords);
157 if (zCoordColumnIndex != -1) {
158 String zs = row.get(zCoordColumnIndex);
162 z = Double.parseDouble(zs);
163 } catch (NumberFormatException e) {
164 throw new DatabaseException(e);
170 if (actualDoTransform) {
171 DirectPosition2D targetPos = new DirectPosition2D();
172 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
173 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
174 coords = res.getCoordinate();
176 coords = new double[] { xCoord, yCoord };
178 Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue));
180 writeStringValue(graph, row, idColumn, vertex, DN.HasId);
182 graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
184 writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
185 writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
186 writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
187 writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
188 writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
189 writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
190 writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
191 writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
192 writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
193 writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
194 writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
195 writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
196 writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
197 writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
198 writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
199 writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
200 writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
201 writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
202 writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
205 String startXCoords = row.get(startXCoordColumnIndex);
206 String startYCoords = row.get(startYCoordColumnIndex);
207 String endXCoords = row.get(endXCoordColumnIndex);
208 String endYCoords = row.get(endYCoordColumnIndex);
210 double startXCoord = Double.parseDouble(startXCoords); // make negative
211 double startYCoord = Double.parseDouble(startYCoords);
213 double endXCoord = Double.parseDouble(endXCoords); // make negative
214 double endYCoord = Double.parseDouble(endYCoords);
216 double[] startCoords;
218 if (actualDoTransform) {
219 DirectPosition2D startTargetPos = new DirectPosition2D();
220 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
221 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
222 startCoords = startRes.getCoordinate();
224 DirectPosition2D endTargetPos = new DirectPosition2D();
225 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
226 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
227 endCoords = endRes.getCoordinate();
229 startCoords = new double[] { startXCoord , startYCoord };
230 endCoords = new double[] { endXCoord , endYCoord };
233 Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding);
234 writeStringValue(graph, row, idColumn, edge, DN.HasId);
236 writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
237 writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
238 writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
239 writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
240 writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
241 writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
242 writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
244 } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
245 throw new DatabaseException(e);
251 } catch (Exception e) {
252 throw new InvocationTargetException(e);
257 } catch (InvocationTargetException e) {
258 Throwable t = e.getTargetException();
259 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
260 cp.setErrorMessage(t.getMessage());
261 ExceptionUtils.logAndShowError(t);
263 } catch (InterruptedException e) {
264 ExceptionUtils.logAndShowError(e);
269 private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
271 String stringValue = row.get(index);
272 if (!stringValue.isEmpty()) {
274 graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
275 } catch (NumberFormatException e) {
276 throw new DatabaseException(e);
282 private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
284 String stringValue = row.get(index);
285 if (!stringValue.isEmpty()) {
287 graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
288 } catch (NumberFormatException e) {
289 throw new DatabaseException(e);