1 package org.simantics.district.imports.ui;
3 import java.lang.reflect.InvocationTargetException;
4 import java.nio.file.Path;
7 import org.apache.commons.csv.CSVRecord;
8 import org.eclipse.core.runtime.IProgressMonitor;
9 import org.eclipse.jface.operation.IRunnableWithProgress;
10 import org.eclipse.jface.viewers.IStructuredSelection;
11 import org.eclipse.jface.wizard.Wizard;
12 import org.eclipse.jface.wizard.WizardPage;
13 import org.eclipse.ui.IImportWizard;
14 import org.eclipse.ui.IWorkbench;
15 import org.geotools.geometry.DirectPosition2D;
16 import org.geotools.referencing.CRS;
17 import org.opengis.geometry.DirectPosition;
18 import org.opengis.geometry.MismatchedDimensionException;
19 import org.opengis.referencing.crs.CoordinateReferenceSystem;
20 import org.opengis.referencing.operation.MathTransform;
21 import org.opengis.referencing.operation.TransformException;
22 import org.simantics.Simantics;
23 import org.simantics.databoard.Bindings;
24 import org.simantics.db.Resource;
25 import org.simantics.db.WriteGraph;
26 import org.simantics.db.exception.DatabaseException;
27 import org.simantics.db.request.Write;
28 import org.simantics.district.imports.DistrictImportUtils;
29 import org.simantics.district.network.DistrictNetworkUtil;
30 import org.simantics.district.network.ontology.DistrictNetworkResource;
31 import org.simantics.district.network.ui.DNEdgeBuilder;
32 import org.simantics.maps.MapScalingTransform;
33 import org.simantics.utils.ui.ExceptionUtils;
35 public class CSVImportWizard extends Wizard implements IImportWizard {
37 private CSVImportModel model;
39 public CSVImportWizard() {
40 setWindowTitle("Import CSV data");
45 public void init(IWorkbench workbench, IStructuredSelection selection) {
46 model = new CSVImportModel();
47 addPage(new CSVImportWizardFirstPage(model));
48 addPage(new CSVImportWizardPage(model));
49 addPage(new ComponentMappingPage(model));
53 public boolean performFinish() {
55 getContainer().run(true, true, new IRunnableWithProgress() {
58 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
60 Path csvFile = model.getSource();
61 char delim = model.getDelimiter();
63 List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
65 // Path wktFile = model.getWKTFile();
67 int xCoordColumnIndex = model.getXCoordIndex();
68 int yCoordColumnIndex = model.getYCoordIndex();
69 int zCoordColumnIndex = model.getZCoordIndex();
70 int tempColumnIndex = model.getTempIndex();
71 int pressureColumnIndex = model.getPressureIndex();
73 int startXCoordColumnIndex = model.getStartXCoordIndex();
74 int startYCoordColumnIndex = model.getStartYCoordIndex();
75 int startZValueColumnIndex = model.getStartZCoordIndex();
76 int endXCoordColumnIndex = model.getEndXCoordIndex();
77 int endYCoordColumnIndex = model.getEndYCoordIndex();
78 int endZValueColumnIndex = model.getEndZCoordIndex();
79 int diameterColumnIndex= model.getDiameterIndex();
80 int outerDiameterColumnIndex = model.getOuterDiamterIndex();
81 int nominalMassFlowIndex = model.getNominalMassFlowIndex();
83 int mappingColumn = model.getComponentMappingIndex();
85 String sourceEPSGCRS = model.getSourceCRS();
87 MathTransform transform = null;
88 boolean doTransform = false;
89 // if sourceEPSGCRS is empty || null then ignore transformation
90 if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
91 CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
92 CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
93 transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
96 final boolean actualDoTransform = doTransform;
97 final MathTransform actualTransform = transform;
98 Simantics.getSession().syncRequest(new Write() {
101 public void perform(WriteGraph graph) throws DatabaseException {
103 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
105 for (int k = 1; k < rows.size(); k++) {
106 CSVRecord row = rows.get(k);
108 String mappingValue = row.get(mappingColumn);
111 if (model.isVertexImport()) {
112 String xCoords = row.get(xCoordColumnIndex);
113 String yCoords = row.get(yCoordColumnIndex);
114 double xCoord = Double.parseDouble(xCoords);
115 double yCoord = Double.parseDouble(yCoords);
117 String zs = row.get(zCoordColumnIndex);
121 z = Double.parseDouble(zs);
122 } catch (NumberFormatException e) {
123 throw new DatabaseException(e);
128 if (actualDoTransform) {
129 DirectPosition2D targetPos = new DirectPosition2D();
130 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
131 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
132 coords = res.getCoordinate();
134 coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() };
136 Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue));
138 graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
140 String tempS = row.get(tempColumnIndex);
141 if (!tempS.isEmpty()) {
143 double temp = Double.parseDouble(tempS);
144 graph.claimLiteral(vertex, DN.Vertex_HasTemperature, temp, Bindings.DOUBLE);
145 } catch (NumberFormatException e) {
146 throw new DatabaseException(e);
149 String pressureS = row.get(pressureColumnIndex);
150 if (!pressureS.isEmpty()) {
152 double pressure = Double.parseDouble(pressureS);
153 graph.claimLiteral(vertex, DN.Vertex_HasPressure, pressure, Bindings.DOUBLE);
154 } catch (NumberFormatException e) {
155 throw new DatabaseException(e);
160 String startXCoords = row.get(startXCoordColumnIndex);
161 String startYCoords = row.get(startYCoordColumnIndex);
162 String endXCoords = row.get(endXCoordColumnIndex);
163 String endYCoords = row.get(endYCoordColumnIndex);
165 double startXCoord = Double.parseDouble(startXCoords);
166 double startYCoord = Double.parseDouble(startYCoords);
168 double endXCoord = Double.parseDouble(endXCoords);
169 double endYCoord = Double.parseDouble(endYCoords);
171 double[] startCoords;
173 if (actualDoTransform) {
174 DirectPosition2D startTargetPos = new DirectPosition2D();
175 DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
176 DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
177 startCoords = startRes.getCoordinate();
179 DirectPosition2D endTargetPos = new DirectPosition2D();
180 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
181 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
182 endCoords = endRes.getCoordinate();
184 startCoords = new double[] { startXCoord / MapScalingTransform.getScaleX(), startYCoord / MapScalingTransform.getScaleY() };
185 endCoords = new double[] { endXCoord / MapScalingTransform.getScaleX(), endYCoord / MapScalingTransform.getScaleY() };
188 Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, 0.0000001);
190 String diameterS = row.get(diameterColumnIndex);
191 if (!diameterS.isEmpty()) {
193 double diameter = Double.parseDouble(diameterS);
194 graph.claimLiteral(edge, DN.Edge_HasDiameter, diameter);
195 } catch (NumberFormatException e) {
196 throw new DatabaseException(e);
199 String outerDiameterS = row.get(outerDiameterColumnIndex);
200 if (!outerDiameterS.isEmpty()) {
202 double outerDiameter = Double.parseDouble(outerDiameterS);
203 graph.claimLiteral(edge, DN.Edge_HasOuterDiameter, outerDiameter);
204 } catch (NumberFormatException e) {
205 throw new DatabaseException(e);
208 String nominalMassFlowS = row.get(nominalMassFlowIndex);
209 if (!nominalMassFlowS.isEmpty()) {
211 double nominalMassFlow = Double.parseDouble(nominalMassFlowS);
212 graph.claimLiteral(edge, DN.Edge_HasNominalMassFlow, nominalMassFlow);
213 } catch (NumberFormatException e) {
214 throw new DatabaseException(e);
218 } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
219 throw new DatabaseException(e);
224 } catch (Exception e) {
225 throw new InvocationTargetException(e);
230 } catch (InvocationTargetException e) {
231 Throwable t = e.getTargetException();
232 WizardPage cp = (WizardPage) getContainer().getCurrentPage();
233 cp.setErrorMessage(t.getMessage());
234 ExceptionUtils.logAndShowError(t);
236 } catch (InterruptedException e) {
237 ExceptionUtils.logAndShowError(e);