]> gerrit.simantics Code Review - simantics/district.git/blobdiff - 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
index 97fa990aed619334c5d080e04b74b0e4ec2fb830..703ade176923886f2691d9fe47d034d9f5ef33b7 100644 (file)
-package org.simantics.district.imports.ui;\r
-\r
-import java.io.IOException;\r
-import java.nio.file.Path;\r
-\r
-import org.eclipse.jface.viewers.IStructuredSelection;\r
-import org.eclipse.jface.wizard.Wizard;\r
-import org.eclipse.ui.IImportWizard;\r
-import org.eclipse.ui.IWorkbench;\r
-import org.simantics.district.imports.DistrictImportUtils;\r
-\r
-public class CSVImportWizard extends Wizard implements IImportWizard {\r
-\r
-    private CSVImportModel model;\r
-    \r
-    public CSVImportWizard() {\r
-        setWindowTitle("Import CSV data");\r
-    }\r
-    \r
-    \r
-    @Override\r
-    public void init(IWorkbench workbench, IStructuredSelection selection) {\r
-        model = new CSVImportModel();\r
-        addPage(new CSVImportWizardPage(model));\r
-    }\r
-    \r
-    @Override\r
-    public boolean performFinish() {\r
-        Path csvFile = model.getSource();\r
-        try {\r
-            DistrictImportUtils.importCSVAsLayer(csvFile);\r
-        } catch (IOException e) {\r
-            e.printStackTrace();\r
-        }\r
-        return false;\r
-    }\r
-\r
-}\r
+package org.simantics.district.imports.ui;
+
+import java.lang.reflect.InvocationTargetException;
+import java.nio.file.Path;
+import java.util.List;
+
+import org.apache.commons.csv.CSVRecord;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.jface.viewers.IStructuredSelection;
+import org.eclipse.jface.wizard.Wizard;
+import org.eclipse.jface.wizard.WizardPage;
+import org.eclipse.ui.IImportWizard;
+import org.eclipse.ui.IWorkbench;
+import org.geotools.geometry.DirectPosition2D;
+import org.geotools.referencing.CRS;
+import org.opengis.geometry.DirectPosition;
+import org.opengis.geometry.MismatchedDimensionException;
+import org.opengis.referencing.crs.CoordinateReferenceSystem;
+import org.opengis.referencing.operation.MathTransform;
+import org.opengis.referencing.operation.TransformException;
+import org.simantics.Simantics;
+import org.simantics.databoard.Bindings;
+import org.simantics.db.Resource;
+import org.simantics.db.WriteGraph;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.request.Write;
+import org.simantics.district.imports.DistrictImportUtils;
+import org.simantics.district.network.DistrictNetworkUtil;
+import org.simantics.district.network.ontology.DistrictNetworkResource;
+import org.simantics.district.network.ui.DNEdgeBuilder;
+import org.simantics.maps.MapScalingTransform;
+import org.simantics.utils.ui.ExceptionUtils;
+
+public class CSVImportWizard extends Wizard implements IImportWizard {
+
+    private CSVImportModel model;
+    
+    public CSVImportWizard() {
+        setWindowTitle("Import CSV data");
+    }
+    
+    
+    @Override
+    public void init(IWorkbench workbench, IStructuredSelection selection) {
+        model = new CSVImportModel();
+        addPage(new CSVImportWizardFirstPage(model));
+        addPage(new CSVImportWizardPage(model));
+        addPage(new ComponentMappingPage(model));
+    }
+    
+    @Override
+    public boolean performFinish() {
+        try {
+            getContainer().run(true, true, new IRunnableWithProgress() {
+
+                @Override
+                public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
+                    try {
+                        Path csvFile = model.getSource();
+                        char delim = model.getDelimiter();
+                        
+                        List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
+                        
+    //                    Path wktFile = model.getWKTFile();
+                        
+                        int xCoordColumnIndex = model.getXCoordIndex();
+                        int yCoordColumnIndex = model.getYCoordIndex();
+                        int zCoordColumnIndex = model.getZCoordIndex();
+                        int tempColumnIndex = model.getTempIndex();
+                        int pressureColumnIndex = model.getPressureIndex();
+                        
+                        int startXCoordColumnIndex = model.getStartXCoordIndex();
+                        int startYCoordColumnIndex = model.getStartYCoordIndex();
+                        int startZValueColumnIndex = model.getStartZCoordIndex();
+                        int endXCoordColumnIndex = model.getEndXCoordIndex();
+                        int endYCoordColumnIndex = model.getEndYCoordIndex();
+                        int endZValueColumnIndex = model.getEndZCoordIndex();
+                        int diameterColumnIndex= model.getDiameterIndex();
+                        int outerDiameterColumnIndex = model.getOuterDiamterIndex();
+                        int nominalMassFlowIndex = model.getNominalMassFlowIndex();
+                        
+                        int mappingColumn = model.getComponentMappingIndex();
+                        
+                        String sourceEPSGCRS = model.getSourceCRS();
+                        
+                        MathTransform transform = null;
+                        boolean doTransform = false;
+                        // if sourceEPSGCRS is empty || null then ignore transformation
+                        if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
+                            CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
+                            CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
+                            transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
+                            doTransform = true;
+                        }
+                        final boolean actualDoTransform = doTransform;
+                        final MathTransform actualTransform = transform;
+                        Simantics.getSession().syncRequest(new Write() {
+                            
+                            @Override
+                            public void perform(WriteGraph graph) throws DatabaseException {
+                                
+                                DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                                
+                                for (int k = 1; k < rows.size(); k++) {
+                                    CSVRecord row = rows.get(k);
+                                    
+                                    String mappingValue = row.get(mappingColumn);
+    
+                                    try {
+                                        if (model.isVertexImport()) {
+                                            String xCoords = row.get(xCoordColumnIndex);
+                                            String yCoords = row.get(yCoordColumnIndex);
+                                            double xCoord = Double.parseDouble(xCoords);
+                                            double yCoord = Double.parseDouble(yCoords);
+                                            
+                                            String zs = row.get(zCoordColumnIndex);
+                                            double z = 0;
+                                            if (!zs.isEmpty()) {
+                                                try {
+                                                    z = Double.parseDouble(zs);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+
+                                            double[] coords;
+                                            if (actualDoTransform) {
+                                                DirectPosition2D targetPos = new DirectPosition2D();
+                                                DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
+                                                DirectPosition res = actualTransform.transform(sourcePos, targetPos);
+                                                coords = res.getCoordinate();
+                                            } else {
+                                                coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() };
+                                            }
+                                            Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue));
+                                            
+                                            graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
+                                            
+                                            String tempS = row.get(tempColumnIndex);
+                                            if (!tempS.isEmpty()) {
+                                                try {
+                                                    double temp = Double.parseDouble(tempS);
+                                                    graph.claimLiteral(vertex, DN.Vertex_HasTemperature, temp, Bindings.DOUBLE);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+                                            String pressureS = row.get(pressureColumnIndex);
+                                            if (!pressureS.isEmpty()) {
+                                                try {
+                                                    double pressure = Double.parseDouble(pressureS);
+                                                    graph.claimLiteral(vertex, DN.Vertex_HasPressure, pressure, Bindings.DOUBLE);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+
+                                        } else {
+                                            String startXCoords = row.get(startXCoordColumnIndex);
+                                            String startYCoords = row.get(startYCoordColumnIndex);
+                                            String endXCoords = row.get(endXCoordColumnIndex);
+                                            String endYCoords = row.get(endYCoordColumnIndex);
+                                            
+                                            double startXCoord = Double.parseDouble(startXCoords);
+                                            double startYCoord = Double.parseDouble(startYCoords);
+                                            
+                                            double endXCoord = Double.parseDouble(endXCoords);
+                                            double endYCoord = Double.parseDouble(endYCoords);
+                                            
+                                            double[] startCoords;
+                                            double[] endCoords;
+                                            if (actualDoTransform) {
+                                                DirectPosition2D startTargetPos = new DirectPosition2D();
+                                                DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
+                                                DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
+                                                startCoords = startRes.getCoordinate();
+                                                
+                                                DirectPosition2D endTargetPos = new DirectPosition2D();
+                                                DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
+                                                DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
+                                                endCoords = endRes.getCoordinate();
+                                            } else {
+                                                startCoords = new double[] { startXCoord / MapScalingTransform.getScaleX(), startYCoord / MapScalingTransform.getScaleY() };
+                                                endCoords = new double[] { endXCoord / MapScalingTransform.getScaleX(), endYCoord / MapScalingTransform.getScaleY() };
+                                            }
+                                            
+                                            Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, 0.0000001);
+                                            graph.claim(edge, DistrictNetworkResource.getInstance(graph).HasMapping, model.getComponentMappings().get(mappingValue));
+                                            
+                                            String diameterS = row.get(diameterColumnIndex);
+                                            if (!diameterS.isEmpty()) {
+                                                try {
+                                                    double diameter = Double.parseDouble(diameterS);
+                                                    graph.claimLiteral(edge, DN.Edge_HasDiameter, diameter);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+                                            String outerDiameterS = row.get(outerDiameterColumnIndex);
+                                            if (!outerDiameterS.isEmpty()) {
+                                                try {
+                                                    double outerDiameter = Double.parseDouble(outerDiameterS);
+                                                    graph.claimLiteral(edge, DN.Edge_HasOuterDiameter, outerDiameter);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+                                            String nominalMassFlowS = row.get(nominalMassFlowIndex);
+                                            if (!nominalMassFlowS.isEmpty()) {
+                                                try {
+                                                    double nominalMassFlow = Double.parseDouble(nominalMassFlowS);
+                                                    graph.claimLiteral(edge, DN.Edge_HasNominalMassFlow, nominalMassFlow);
+                                                } catch (NumberFormatException e) {
+                                                    throw new DatabaseException(e);
+                                                }
+                                            }
+                                        }
+                                    } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
+                                        throw new DatabaseException(e);
+                                    }
+                                }
+                            }
+                        });
+                    } catch (Exception e) {
+                        throw new InvocationTargetException(e);
+                    }
+                }
+            });
+            return true;
+        } catch (InvocationTargetException e) {
+            Throwable t = e.getTargetException();
+            WizardPage cp = (WizardPage) getContainer().getCurrentPage();
+            cp.setErrorMessage(t.getMessage());
+            ExceptionUtils.logAndShowError(t);
+            return false;
+        } catch (InterruptedException e) {
+            ExceptionUtils.logAndShowError(e);
+            return false;
+        }
+    }
+}