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();
112 int mappingColumn = model.getComponentMappingIndex();
113 int idColumn = model.getIdIndex();
115 double padding = model.getEdgePadding();
117 String sourceEPSGCRS = model.getSourceCRS();
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);
128 final boolean actualDoTransform = doTransform;
129 final MathTransform actualTransform = transform;
130 Simantics.getSession().syncRequest(new Write() {
133 public void perform(WriteGraph graph) throws DatabaseException {
134 graph.markUndoPoint();
136 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
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));
145 for (int k = 1; k < rows.size(); k++) {
146 CSVRecord row = rows.get(k);
148 String mappingValue = row.get(mappingColumn);
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);
158 if (zCoordColumnIndex != -1) {
159 String zs = row.get(zCoordColumnIndex);
163 z = Double.parseDouble(zs);
164 } catch (NumberFormatException e) {
165 throw new DatabaseException(e);
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();
177 coords = new double[] { xCoord, yCoord };
179 Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, model.getComponentMappings().get(mappingValue));
181 writeStringValue(graph, row, idColumn, vertex, DN.HasId);
183 graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
185 writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
186 writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
187 writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
188 writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
189 writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
190 writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
191 writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
192 writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
193 writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
194 writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
195 writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
196 writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
197 writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
198 writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
199 writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
200 writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
201 writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
202 writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
203 writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
206 String startXCoords = row.get(startXCoordColumnIndex);
207 String startYCoords = row.get(startYCoordColumnIndex);
208 String endXCoords = row.get(endXCoordColumnIndex);
209 String endYCoords = row.get(endYCoordColumnIndex);
211 double startXCoord = Double.parseDouble(startXCoords); // make negative
212 double startYCoord = Double.parseDouble(startYCoords);
214 double endXCoord = Double.parseDouble(endXCoords); // make negative
215 double endYCoord = Double.parseDouble(endYCoords);
217 double[] startCoords;
219 if (actualDoTransform) {
220 DirectPosition2D startTargetPos = new DirectPosition2D();
221 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
222 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
223 startCoords = startRes.getCoordinate();
225 DirectPosition2D endTargetPos = new DirectPosition2D();
226 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
227 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
228 endCoords = endRes.getCoordinate();
230 startCoords = new double[] { startXCoord , startYCoord };
231 endCoords = new double[] { endXCoord , endYCoord };
234 Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding, true);
235 writeStringValue(graph, row, idColumn, edge, DN.HasId);
237 writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
238 writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
239 writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
240 writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
241 writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
242 writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
243 writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
244 writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
246 } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
247 throw new DatabaseException(e);
253 } catch (Exception e) {
254 throw new InvocationTargetException(e);
259 } catch (InvocationTargetException e) {
260 Throwable t = e.getTargetException();
261 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
262 cp.setErrorMessage(t.getMessage());
263 ExceptionUtils.logAndShowError(t);
265 } catch (InterruptedException e) {
266 ExceptionUtils.logAndShowError(e);
271 private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
273 String stringValue = row.get(index);
274 if (!stringValue.isEmpty()) {
276 graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
277 } catch (NumberFormatException e) {
278 throw new DatabaseException(e);
284 private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
286 String stringValue = row.get(index);
287 if (!stringValue.isEmpty()) {
289 graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
290 } catch (NumberFormatException e) {
291 throw new DatabaseException(e);