package org.simantics.district.imports.ui;
+import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Collection;
-import java.util.List;
+import java.util.Optional;
import org.apache.commons.csv.CSVRecord;
import org.eclipse.core.runtime.IProgressMonitor;
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.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 {
private CSVImportModel model;
Path csvFile = model.getSource();
char delim = model.getDelimiter();
- List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
- monitor.beginTask("Importing CSV", rows.size());
+ monitor.beginTask("Importing CSV", 1);
// Path wktFile = model.getWKTFile();
}
final boolean actualDoTransform = doTransform;
final MathTransform actualTransform = transform;
- Simantics.getSession().syncRequest(new Write() {
-
+
+ double halfPadding = padding / 2;
+
+ Quadtree vv = Simantics.getSession().syncRequest(new UniqueRead<Quadtree>() {
+
@Override
- public void perform(WriteGraph graph) throws DatabaseException {
- graph.markUndoPoint();
-
- DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
-
+ public Quadtree perform(ReadGraph graph) throws DatabaseException {
Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
- List<ResourceVertex> vv = new ArrayList<>(vertices.size());
+ Quadtree vv = new Quadtree();
for (Resource vertex : vertices) {
- double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
- vv.add(new ResourceVertex(vertex, existingCoords));
+ 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));
}
-
- for (int k = 1; k < rows.size(); k++) {
- CSVRecord row = rows.get(k);
+ return vv;
+ }
+ });
+
+ Simantics.getSession().syncRequest(new WriteRequest() {
+
+ @Override
+ public void perform(WriteGraph graph) throws DatabaseException {
+ try {
+ Layer0Utils.setDependenciesIndexingDisabled(graph, true);
+ graph.markUndoPoint();
- 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);
+ DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+
+ 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);
- if (!zs.isEmpty()) {
- try {
- z = Double.parseDouble(zs);
- } catch (NumberFormatException e) {
- throw new DatabaseException(e);
+ 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, model.getComponentMappings().get(mappingValue));
-
- writeStringValue(graph, row, idColumn, vertex, DN.HasId);
-
- graph.claimLiteral(vertex, DN.Vertex_HasElevation, z, Bindings.DOUBLE);
-
- 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 endXCoords = row.get(endXCoordColumnIndex);
- String endYCoords = row.get(endYCoordColumnIndex);
-
- double startXCoord = Double.parseDouble(startXCoords); // make negative
- double startYCoord = Double.parseDouble(startYCoords);
-
- double endXCoord = Double.parseDouble(endXCoords); // make negative
- 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();
+
+ 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 };
+ }
- DirectPosition2D endTargetPos = new DirectPosition2D();
- DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
- DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
- endCoords = endRes.getCoordinate();
+ // 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 {
- startCoords = new double[] { startXCoord , startYCoord };
- endCoords = new double[] { endXCoord , endYCoord };
+ 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<Resource> 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);
+ }
}
-
- // Switch to (longitude, latitude)
- flipAxes(startCoords);
- flipAxes(endCoords);
-
- Resource edge = DNEdgeBuilder.create(graph, vv, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, endCoords, padding, true);
- 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);
}
- } catch (MismatchedDimensionException | TransformException | DatabaseException e) {
- throw new DatabaseException(e);
- }
- monitor.worked(1);
+ monitor.worked(1);
+ return true;
+ });
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ Layer0Utils.setDependenciesIndexingDisabled(graph, false);
}
}
});
if (index != -1) {
String stringValue = row.get(index);
if (!stringValue.isEmpty()) {
- stringValue = stringValue.substring(1, stringValue.length() - 1);
+ if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) {
+ stringValue = stringValue.substring(1, stringValue.length() - 1);
+ }
String[] coordPairs = stringValue.split(";");
ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
for (int i = 0; i < coordPairs.length; i++) {