1 package org.simantics.district.imports.ui;
3 import java.io.IOException;
4 import java.lang.reflect.InvocationTargetException;
5 import java.nio.file.Path;
6 import java.util.ArrayList;
7 import java.util.Collection;
8 import java.util.Optional;
10 import org.apache.commons.csv.CSVRecord;
11 import org.eclipse.core.runtime.IProgressMonitor;
12 import org.eclipse.jface.operation.IRunnableWithProgress;
13 import org.eclipse.jface.viewers.IStructuredSelection;
14 import org.eclipse.jface.wizard.Wizard;
15 import org.eclipse.jface.wizard.WizardPage;
16 import org.eclipse.ui.IImportWizard;
17 import org.eclipse.ui.IWorkbench;
18 import org.geotools.geometry.DirectPosition2D;
19 import org.geotools.referencing.CRS;
20 import org.opengis.geometry.DirectPosition;
21 import org.opengis.geometry.MismatchedDimensionException;
22 import org.opengis.referencing.crs.CoordinateReferenceSystem;
23 import org.opengis.referencing.operation.MathTransform;
24 import org.opengis.referencing.operation.TransformException;
25 import org.simantics.Simantics;
26 import org.simantics.databoard.Bindings;
27 import org.simantics.db.ReadGraph;
28 import org.simantics.db.Resource;
29 import org.simantics.db.WriteGraph;
30 import org.simantics.db.common.request.ObjectsWithType;
31 import org.simantics.db.common.request.UniqueRead;
32 import org.simantics.db.common.request.WriteRequest;
33 import org.simantics.db.exception.DatabaseException;
34 import org.simantics.db.layer0.util.Layer0Utils;
35 import org.simantics.diagram.stubs.DiagramResource;
36 import org.simantics.district.imports.DistrictImportUtils;
37 import org.simantics.district.network.DistrictNetworkUtil;
38 import org.simantics.district.network.ontology.DistrictNetworkResource;
39 import org.simantics.district.network.ui.DNEdgeBuilder;
40 import org.simantics.district.network.ui.DNEdgeBuilder.ResourceVertex;
41 import org.simantics.layer0.Layer0;
42 import org.simantics.utils.ui.ExceptionUtils;
44 import com.vividsolutions.jts.geom.Envelope;
45 import com.vividsolutions.jts.index.quadtree.Quadtree;
47 public class CSVImportWizard extends Wizard implements IImportWizard {
49 private CSVImportModel model;
51 public CSVImportWizard() {
52 setWindowTitle("Import CSV data");
53 setNeedsProgressMonitor(true);
58 public void init(IWorkbench workbench, IStructuredSelection selection) {
59 model = new CSVImportModel();
60 addPage(new CSVImportWizardFirstPage(model));
61 addPage(new CSVImportWizardPage(model));
62 addPage(new ComponentMappingPage(model));
66 public boolean performFinish() {
68 getContainer().run(true, true, new IRunnableWithProgress() {
71 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
73 Path csvFile = model.getSource();
74 char delim = model.getDelimiter();
76 monitor.beginTask("Importing CSV", 1);
78 // Path wktFile = model.getWKTFile();
80 int xCoordColumnIndex = model.getXCoordIndex();
81 int yCoordColumnIndex = model.getYCoordIndex();
82 int zCoordColumnIndex = model.getZCoordIndex();
83 int supplyTempColumnIndex = model.getSupplyTempIndex();
84 int returnTempColumnIndex = model.getReturnTempIndex();
85 int supplyPressureColumnIndex = model.getSupplyPressureIndex();
86 int returnPressureColumnIndex = model.getReturnPressureIndex();
87 int dpIndex = model.getDeltaPressureIndex();
88 int dtIndex = model.getDeltaTemperatureIndex();
89 int heatPowerIndex = model.getHeatPowerIndex();
90 int valvePositionIndex = model.getValvePositionIndx();
91 int nominalHeadMIndex = model.getNominalHeadMIndex();
92 int nominalHeadBIndex = model.getNominalHeadBIndex();
93 int nominalFlowIndex = model.getNominalFlowIndex();
94 int maximumHeadMIndex = model.getMaximumHeadMIndex();
95 int heatLoadDsIndex = model.getHeatLoadDsIndex();
96 int massFlowIndex = model.getMassFlowIndex();
97 int volFlowIndex = model.getVolFlowIndex();
98 int velocityIndex = model.getVelocityIndex();
99 int flowAreaIndex = model.getFlowAreaIndex();
100 int nominalPressureLossIndex = model.getNominalPressureLossIndex();
101 int addressIndex = model.getAddressIndex();
103 int startXCoordColumnIndex = model.getStartXCoordIndex();
104 int startYCoordColumnIndex = model.getStartYCoordIndex();
105 int startZValueColumnIndex = model.getStartZCoordIndex();
106 int endXCoordColumnIndex = model.getEndXCoordIndex();
107 int endYCoordColumnIndex = model.getEndYCoordIndex();
108 int endZValueColumnIndex = model.getEndZCoordIndex();
109 int diameterColumnIndex= model.getDiameterIndex();
110 int outerDiameterColumnIndex = model.getOuterDiamterIndex();
111 int nominalMassFlowIndex = model.getNominalMassFlowIndex();
112 int tGroundIndex = model.gettGroundIndex();
113 int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex();
114 int kReturnIndex = model.getkReturnIndex();
115 int kSupplyIndex = model.getkSupplyIndex();
116 int lengthIndex = model.getLengthIndex();
117 int detailedGeometryIndex = model.getDetailedGeometryIndex();
119 int mappingColumn = model.getComponentMappingIndex();
120 int idColumn = model.getIdIndex();
122 double padding = model.getEdgePadding();
124 String sourceEPSGCRS = model.getSourceCRS();
126 MathTransform transform = null;
127 boolean doTransform = false;
128 // if sourceEPSGCRS is empty || null then ignore transformation
129 if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
130 CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
131 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
132 transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
135 final boolean actualDoTransform = doTransform;
136 final MathTransform actualTransform = transform;
138 double halfPadding = padding / 2;
140 Quadtree vv = Simantics.getSession().syncRequest(new UniqueRead<Quadtree>() {
143 public Quadtree perform(ReadGraph graph) throws DatabaseException {
144 Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
145 Quadtree vv = new Quadtree();
146 for (Resource vertex : vertices) {
147 double[] coords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
148 double x1 = coords[0] - halfPadding;
149 double y1= coords[1] - halfPadding;
150 double x2 = coords[0] + halfPadding;
151 double y2= coords[1] + halfPadding;
152 Envelope e = new Envelope(x1, x2, y1, y2);
153 vv.insert(e, new ResourceVertex(vertex, coords, true));
159 Simantics.getSession().syncRequest(new WriteRequest() {
162 public void perform(WriteGraph graph) throws DatabaseException {
164 Layer0Utils.setDependenciesIndexingDisabled(graph, true);
165 graph.markUndoPoint();
167 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
169 DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> {
170 String mappingValue = row.get(mappingColumn);
173 if (model.isVertexImport()) {
174 String xCoords = row.get(xCoordColumnIndex);
175 String yCoords = row.get(yCoordColumnIndex);
176 double xCoord = Double.parseDouble(xCoords);
177 double yCoord = Double.parseDouble(yCoords);
180 if (zCoordColumnIndex != -1) {
181 String zs = row.get(zCoordColumnIndex);
185 z = Double.parseDouble(zs);
186 } catch (NumberFormatException e1) {
187 throw new DatabaseException(e1);
193 if (actualDoTransform) {
194 DirectPosition2D targetPos = new DirectPosition2D();
195 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
196 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
197 coords = res.getCoordinate();
199 coords = new double[] { xCoord, yCoord };
202 // Switch to (longitude, latitude)
205 Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, z, model.getComponentMappings().get(mappingValue));
207 writeStringValue(graph, row, idColumn, vertex, DN.HasId);
209 writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
210 writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
211 writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
212 writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
213 writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
214 writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
215 writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
216 writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
217 writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
218 writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
219 writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
220 writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
221 writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
222 writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
223 writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
224 writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
225 writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
226 writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
227 writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
230 String startXCoords = row.get(startXCoordColumnIndex);
231 String startYCoords = row.get(startYCoordColumnIndex);
232 String startZCoords = row.get(startZValueColumnIndex);
233 String endXCoords = row.get(endXCoordColumnIndex);
234 String endYCoords = row.get(endYCoordColumnIndex);
235 String endZCoords = row.get(endZValueColumnIndex);
237 double startXCoord = Double.parseDouble(startXCoords); // make negative
238 double startYCoord = Double.parseDouble(startYCoords);
239 double startZCoord = Double.parseDouble(startZCoords);
241 double endXCoord = Double.parseDouble(endXCoords); // make negative
242 double endYCoord = Double.parseDouble(endYCoords);
243 double endZCoord = Double.parseDouble(endZCoords);
245 double[] startCoords;
247 if (actualDoTransform) {
248 DirectPosition2D startTargetPos = new DirectPosition2D();
249 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
250 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
251 startCoords = startRes.getCoordinate();
253 DirectPosition2D endTargetPos = new DirectPosition2D();
254 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
255 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
256 endCoords = endRes.getCoordinate();
258 startCoords = new double[] { startXCoord , startYCoord };
259 endCoords = new double[] { endXCoord , endYCoord };
262 // Switch to (longitude, latitude)
263 flipAxes(startCoords);
266 Optional<Resource> oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, padding, true);
267 if (oedge.isPresent()) {
268 Resource edge = oedge.get();
269 writeStringValue(graph, row, idColumn, edge, DN.HasId);
271 writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
272 writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
273 writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
274 writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
275 writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
276 writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
277 writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
278 writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
279 writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
282 } catch (MismatchedDimensionException | TransformException | DatabaseException e2) {
283 throw new RuntimeException(e2);
288 } catch (IOException e) {
291 Layer0Utils.setDependenciesIndexingDisabled(graph, false);
295 } catch (Exception e) {
296 throw new InvocationTargetException(e);
301 } catch (InvocationTargetException e) {
302 Throwable t = e.getTargetException();
303 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
304 cp.setErrorMessage(t.getMessage());
305 ExceptionUtils.logAndShowError(t);
307 } catch (InterruptedException e) {
308 ExceptionUtils.logAndShowError(e);
313 private static void flipAxes(double[] coords) {
314 double tmp = coords[0];
315 coords[0] = coords[1];
319 private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
321 String stringValue = row.get(index);
322 if (!stringValue.isEmpty()) {
324 graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
325 } catch (NumberFormatException e) {
326 throw new DatabaseException(e);
332 private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
334 String stringValue = row.get(index);
335 if (!stringValue.isEmpty()) {
337 graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
338 } catch (NumberFormatException e) {
339 throw new DatabaseException(e);
345 private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
347 String stringValue = row.get(index);
348 if (!stringValue.isEmpty()) {
349 if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) {
350 stringValue = stringValue.substring(1, stringValue.length() - 1);
352 String[] coordPairs = stringValue.split(";");
353 ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
354 for (int i = 0; i < coordPairs.length; i++) {
355 String coordPair = coordPairs[i];
356 String[] p = coordPair.split(" ");
357 double x = Double.parseDouble(p[0]);
358 double y = Double.parseDouble(p[1]);
359 if (actualTransform != null) {
360 DirectPosition2D targetPos = new DirectPosition2D();
361 DirectPosition2D sourcePos = new DirectPosition2D(y, x);
362 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
363 double[] coords = res.getCoordinate();
370 double[] detailedGeometryCoords = new double[dd.size()];
371 for (int i = 0; i < dd.size(); i++) {
372 double d = dd.get(i);
373 detailedGeometryCoords[i] = d;
376 graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
377 } catch (NumberFormatException e) {
378 throw new DatabaseException(e);