X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports.ui%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2Fui%2FCSVImportWizard.java;h=c0d756d3c86cfef87ef9eaf0ebca232e464d2725;hb=refs%2Fchanges%2F42%2F2842%2F2;hp=58fd168c8db9f94517a8a2770e1b2f161ea560a7;hpb=02ecca5e61d2eb17de40cc058be678b414aaad00;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java index 58fd168c..c0d756d3 100644 --- a/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java +++ b/org.simantics.district.imports.ui/src/org/simantics/district/imports/ui/CSVImportWizard.java @@ -1,32 +1,48 @@ package org.simantics.district.imports.ui; import java.io.IOException; +import java.lang.reflect.InvocationTargetException; import java.nio.file.Path; -import java.util.List; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Optional; 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.FactoryException; 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.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.UniqueRead; +import org.simantics.db.common.request.WriteRequest; import org.simantics.db.exception.DatabaseException; -import org.simantics.db.request.Write; +import org.simantics.db.layer0.util.Layer0Utils; +import org.simantics.diagram.stubs.DiagramResource; 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.district.network.ui.DNEdgeBuilder.ResourceVertex; +import org.simantics.layer0.Layer0; +import org.simantics.utils.ui.ExceptionUtils; + +import com.vividsolutions.jts.geom.Envelope; +import com.vividsolutions.jts.index.quadtree.Quadtree; public class CSVImportWizard extends Wizard implements IImportWizard { @@ -34,6 +50,7 @@ public class CSVImportWizard extends Wizard implements IImportWizard { public CSVImportWizard() { setWindowTitle("Import CSV data"); + setNeedsProgressMonitor(true); } @@ -48,100 +65,319 @@ public class CSVImportWizard extends Wizard implements IImportWizard { @Override public boolean performFinish() { try { - - - Path csvFile = model.getSource(); - char delim = model.getDelimiter(); - - List rows = DistrictImportUtils.readRows(csvFile, delim, -1); - - Path wktFile = model.getWKTFile(); - - int xCoordColumnIndex = model.getXCoordIndex(); - int yCoordColumnIndex = model.getYCoordIndex(); - int zCoordColumnIndex = model.getZCoordIndex(); - - 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 mappingColumn = model.getComponentMappingIndex(); - - try { - CoordinateReferenceSystem sourceCRS = CRS.decode("EPSG:3879"); - CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326"); - MathTransform transform = CRS.findMathTransform(sourceCRS, targetCRS, true); - - Simantics.getSession().syncRequest(new Write() { - - @Override - public void perform(WriteGraph graph) throws DatabaseException { - for (int k = 1; k < rows.size(); k++) { - CSVRecord row = rows.get(k); - - String mappingValue = row.get(mappingColumn); + getContainer().run(true, true, new IRunnableWithProgress() { - 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 = Double.parseDouble(zs); - - DirectPosition2D targetPos = new DirectPosition2D(); - DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord); - DirectPosition res = transform.transform(sourcePos, targetPos); - double[] coords = res.getCoordinate(); - Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue)); - graph.claimLiteral(vertex, DistrictNetworkResource.getInstance(graph).Vertex_HasElevation, z, Bindings.DOUBLE); - } 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); - - DirectPosition2D startTargetPos = new DirectPosition2D(); - DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord); - DirectPosition startRes = transform.transform(startSourcePos, startTargetPos); - double[] startCoords = startRes.getCoordinate(); + @Override + public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException { + try { + Path csvFile = model.getSource(); + char delim = model.getDelimiter(); + + monitor.beginTask("Importing CSV", 1); + + // Path wktFile = model.getWKTFile(); + + int xCoordColumnIndex = model.getXCoordIndex(); + int yCoordColumnIndex = model.getYCoordIndex(); + int zCoordColumnIndex = model.getZCoordIndex(); + int supplyTempColumnIndex = model.getSupplyTempIndex(); + int returnTempColumnIndex = model.getReturnTempIndex(); + int supplyPressureColumnIndex = model.getSupplyPressureIndex(); + int returnPressureColumnIndex = model.getReturnPressureIndex(); + int dpIndex = model.getDeltaPressureIndex(); + int dtIndex = model.getDeltaTemperatureIndex(); + int heatPowerIndex = model.getHeatPowerIndex(); + int valvePositionIndex = model.getValvePositionIndx(); + int nominalHeadMIndex = model.getNominalHeadMIndex(); + int nominalHeadBIndex = model.getNominalHeadBIndex(); + int nominalFlowIndex = model.getNominalFlowIndex(); + int maximumHeadMIndex = model.getMaximumHeadMIndex(); + int heatLoadDsIndex = model.getHeatLoadDsIndex(); + int massFlowIndex = model.getMassFlowIndex(); + int volFlowIndex = model.getVolFlowIndex(); + int velocityIndex = model.getVelocityIndex(); + int flowAreaIndex = model.getFlowAreaIndex(); + int nominalPressureLossIndex = model.getNominalPressureLossIndex(); + int addressIndex = model.getAddressIndex(); + + 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 tGroundIndex = model.gettGroundIndex(); + int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex(); + int kReturnIndex = model.getkReturnIndex(); + int kSupplyIndex = model.getkSupplyIndex(); + int lengthIndex = model.getLengthIndex(); + int detailedGeometryIndex = model.getDetailedGeometryIndex(); + + int mappingColumn = model.getComponentMappingIndex(); + int idColumn = model.getIdIndex(); + + double padding = model.getEdgePadding(); + + 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; + + double halfPadding = padding / 2; + + Quadtree vv = Simantics.getSession().syncRequest(new UniqueRead() { + + @Override + public Quadtree perform(ReadGraph graph) throws DatabaseException { + Collection vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex)); + Quadtree vv = new Quadtree(); + for (Resource vertex : vertices) { + double[] coords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY); + double x1 = coords[0] - halfPadding; + double y1= coords[1] - halfPadding; + double x2 = coords[0] + halfPadding; + double y2= coords[1] + halfPadding; + Envelope e = new Envelope(x1, x2, y1, y2); + vv.insert(e, new ResourceVertex(vertex, coords, true)); + } + return vv; + } + }); + + Simantics.getSession().syncRequest(new WriteRequest() { + + @Override + public void perform(WriteGraph graph) throws DatabaseException { + try { + Layer0Utils.setDependenciesIndexingDisabled(graph, true); + graph.markUndoPoint(); - DirectPosition2D endTargetPos = new DirectPosition2D(); - DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord); - DirectPosition endRes = transform.transform(endSourcePos, endTargetPos); - double[] endCoords = endRes.getCoordinate(); + DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph); - 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)); -// graph.claimLiteral(edge, DistrictNetworkResource.getInstance(graph).Edge_HasDiameter, value); + DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> { + 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); + + double z = 0; + if (zCoordColumnIndex != -1) { + String zs = row.get(zCoordColumnIndex); + + if (!zs.isEmpty()) { + try { + z = Double.parseDouble(zs); + } catch (NumberFormatException e1) { + throw new DatabaseException(e1); + } + } + } + + 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, yCoord }; + } + + // Switch to (longitude, latitude) + flipAxes(coords); + + Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, z, model.getComponentMappings().get(mappingValue)); + + writeStringValue(graph, row, idColumn, vertex, DN.HasId); + + writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature); + writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature); + writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure); + writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure); + writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure); + writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature); + writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower); + writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition); + writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM); + writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB); + writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow); + writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM); + writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs); + writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow); + writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow); + writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity); + writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea); + writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss); + writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress); + + } else { + String startXCoords = row.get(startXCoordColumnIndex); + String startYCoords = row.get(startYCoordColumnIndex); + String startZCoords = row.get(startZValueColumnIndex); + String endXCoords = row.get(endXCoordColumnIndex); + String endYCoords = row.get(endYCoordColumnIndex); + String endZCoords = row.get(endZValueColumnIndex); + + double startXCoord = Double.parseDouble(startXCoords); // make negative + double startYCoord = Double.parseDouble(startYCoords); + double startZCoord = Double.parseDouble(startZCoords); + + double endXCoord = Double.parseDouble(endXCoords); // make negative + double endYCoord = Double.parseDouble(endYCoords); + double endZCoord = Double.parseDouble(endZCoords); + + 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 , startYCoord }; + endCoords = new double[] { endXCoord , endYCoord }; + } + + // Switch to (longitude, latitude) + flipAxes(startCoords); + flipAxes(endCoords); + + Optional oedge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, padding, true); + if (oedge.isPresent()) { + Resource edge = oedge.get(); + writeStringValue(graph, row, idColumn, edge, DN.HasId); + + writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter); + writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter); + writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow); + writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround); + writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn); + writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply); + writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea); + writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength); + writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform); + } + } + } catch (MismatchedDimensionException | TransformException | DatabaseException e2) { + throw new RuntimeException(e2); + } + monitor.worked(1); + return true; + }); + } catch (IOException e) { + e.printStackTrace(); + } finally { + Layer0Utils.setDependenciesIndexingDisabled(graph, false); } - } catch (MismatchedDimensionException | TransformException | DatabaseException e) { - e.printStackTrace(); } - - } + }); + } catch (Exception e) { + throw new InvocationTargetException(e); } - }); - return true; - } catch (FactoryException | DatabaseException e) { - e.printStackTrace(); + } + }); + 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; + } + } + + private static void flipAxes(double[] coords) { + double tmp = coords[0]; + coords[0] = coords[1]; + coords[1] = tmp; + } + + private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + try { + graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + } + } + + private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + try { + graph.claimLiteral(subject, relation, stringValue, Bindings.STRING); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } } - - } catch (IOException e) { - e.printStackTrace(); } - return false; } + private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) { + stringValue = stringValue.substring(1, stringValue.length() - 1); + } + String[] coordPairs = stringValue.split(";"); + ArrayList dd = new ArrayList<>(coordPairs.length * 2); + for (int i = 0; i < coordPairs.length; i++) { + String coordPair = coordPairs[i]; + String[] p = coordPair.split(" "); + double x = Double.parseDouble(p[0]); + double y = Double.parseDouble(p[1]); + if (actualTransform != null) { + DirectPosition2D targetPos = new DirectPosition2D(); + DirectPosition2D sourcePos = new DirectPosition2D(y, x); + DirectPosition res = actualTransform.transform(sourcePos, targetPos); + double[] coords = res.getCoordinate(); + x = coords[1]; + y = coords[0]; + } + dd.add(x); + dd.add(y); + } + double[] detailedGeometryCoords = new double[dd.size()]; + for (int i = 0; i < dd.size(); i++) { + double d = dd.get(i); + detailedGeometryCoords[i] = d; + } + try { + graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY); + } catch (NumberFormatException e) { + throw new DatabaseException(e); + } + } + } + } }