]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java
Updates to Simantics district CSV import
[simantics/district.git] / org.simantics.district.imports.ui / src / org / simantics / district / imports / ui / CSVImportWizard.java
1 package org.simantics.district.imports.ui;
2
3 import java.lang.reflect.InvocationTargetException;
4 import java.nio.file.Path;
5 import java.util.List;
6
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;
34
35 public class CSVImportWizard extends Wizard implements IImportWizard {
36
37     private CSVImportModel model;
38     
39     public CSVImportWizard() {
40         setWindowTitle("Import CSV data");
41     }
42     
43     
44     @Override
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));
50     }
51     
52     @Override
53     public boolean performFinish() {
54         try {
55             getContainer().run(true, true, new IRunnableWithProgress() {
56
57                 @Override
58                 public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
59                     try {
60                         Path csvFile = model.getSource();
61                         char delim = model.getDelimiter();
62                         
63                         List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
64                         
65     //                    Path wktFile = model.getWKTFile();
66                         
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();
72                         
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();
82                         
83                         int mappingColumn = model.getComponentMappingIndex();
84                         
85                         String sourceEPSGCRS = model.getSourceCRS();
86                         
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);
94                             doTransform = true;
95                         }
96                         final boolean actualDoTransform = doTransform;
97                         final MathTransform actualTransform = transform;
98                         Simantics.getSession().syncRequest(new Write() {
99                             
100                             @Override
101                             public void perform(WriteGraph graph) throws DatabaseException {
102                                 
103                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
104                                 
105                                 for (int k = 1; k < rows.size(); k++) {
106                                     CSVRecord row = rows.get(k);
107                                     
108                                     String mappingValue = row.get(mappingColumn);
109     
110                                     try {
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);
116                                             
117                                             String zs = row.get(zCoordColumnIndex);
118                                             double z = 0;
119                                             if (!zs.isEmpty()) {
120                                                 try {
121                                                     z = Double.parseDouble(zs);
122                                                 } catch (NumberFormatException e) {
123                                                     throw new DatabaseException(e);
124                                                 }
125                                             }
126
127                                             double[] coords;
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();
133                                             } else {
134                                                 coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() };
135                                             }
136                                             Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue));
137                                             
138                                             graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
139                                             
140                                             String tempS = row.get(tempColumnIndex);
141                                             if (!tempS.isEmpty()) {
142                                                 try {
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);
147                                                 }
148                                             }
149                                             String pressureS = row.get(pressureColumnIndex);
150                                             if (!pressureS.isEmpty()) {
151                                                 try {
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);
156                                                 }
157                                             }
158
159                                         } else {
160                                             String startXCoords = row.get(startXCoordColumnIndex);
161                                             String startYCoords = row.get(startYCoordColumnIndex);
162                                             String endXCoords = row.get(endXCoordColumnIndex);
163                                             String endYCoords = row.get(endYCoordColumnIndex);
164                                             
165                                             double startXCoord = Double.parseDouble(startXCoords);
166                                             double startYCoord = Double.parseDouble(startYCoords);
167                                             
168                                             double endXCoord = Double.parseDouble(endXCoords);
169                                             double endYCoord = Double.parseDouble(endYCoords);
170                                             
171                                             double[] startCoords;
172                                             double[] endCoords;
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();
178                                                 
179                                                 DirectPosition2D endTargetPos = new DirectPosition2D();
180                                                 DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
181                                                 DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
182                                                 endCoords = endRes.getCoordinate();
183                                             } else {
184                                                 startCoords = new double[] { startXCoord / MapScalingTransform.getScaleX(), startYCoord / MapScalingTransform.getScaleY() };
185                                                 endCoords = new double[] { endXCoord / MapScalingTransform.getScaleX(), endYCoord / MapScalingTransform.getScaleY() };
186                                             }
187                                             
188                                             Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, 0.0000001);
189                                             graph.claim(edge, DistrictNetworkResource.getInstance(graph).HasMapping, model.getComponentMappings().get(mappingValue));
190                                             
191                                             String diameterS = row.get(diameterColumnIndex);
192                                             if (!diameterS.isEmpty()) {
193                                                 try {
194                                                     double diameter = Double.parseDouble(diameterS);
195                                                     graph.claimLiteral(edge, DN.Edge_HasDiameter, diameter);
196                                                 } catch (NumberFormatException e) {
197                                                     throw new DatabaseException(e);
198                                                 }
199                                             }
200                                             String outerDiameterS = row.get(outerDiameterColumnIndex);
201                                             if (!outerDiameterS.isEmpty()) {
202                                                 try {
203                                                     double outerDiameter = Double.parseDouble(outerDiameterS);
204                                                     graph.claimLiteral(edge, DN.Edge_HasOuterDiameter, outerDiameter);
205                                                 } catch (NumberFormatException e) {
206                                                     throw new DatabaseException(e);
207                                                 }
208                                             }
209                                             String nominalMassFlowS = row.get(nominalMassFlowIndex);
210                                             if (!nominalMassFlowS.isEmpty()) {
211                                                 try {
212                                                     double nominalMassFlow = Double.parseDouble(nominalMassFlowS);
213                                                     graph.claimLiteral(edge, DN.Edge_HasNominalMassFlow, nominalMassFlow);
214                                                 } catch (NumberFormatException e) {
215                                                     throw new DatabaseException(e);
216                                                 }
217                                             }
218                                         }
219                                     } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
220                                         throw new DatabaseException(e);
221                                     }
222                                 }
223                             }
224                         });
225                     } catch (Exception e) {
226                         throw new InvocationTargetException(e);
227                     }
228                 }
229             });
230             return true;
231         } catch (InvocationTargetException e) {
232             Throwable t = e.getTargetException();
233             WizardPage cp = (WizardPage) getContainer().getCurrentPage();
234             cp.setErrorMessage(t.getMessage());
235             ExceptionUtils.logAndShowError(t);
236             return false;
237         } catch (InterruptedException e) {
238             ExceptionUtils.logAndShowError(e);
239             return false;
240         }
241     }
242 }