import java.lang.reflect.InvocationTargetException;
import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import org.apache.commons.csv.CSVRecord;
import org.simantics.databoard.Bindings;
import org.simantics.db.Resource;
import org.simantics.db.WriteGraph;
+import org.simantics.db.common.request.ObjectsWithType;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.request.Write;
+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.maps.MapScalingTransform;
+import org.simantics.district.network.ui.DNEdgeBuilder.ResourceVertex;
+import org.simantics.layer0.Layer0;
import org.simantics.utils.ui.ExceptionUtils;
public class CSVImportWizard extends Wizard implements IImportWizard {
public CSVImportWizard() {
setWindowTitle("Import CSV data");
+ setNeedsProgressMonitor(true);
}
@Override
public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
- monitor.beginTask("Importing CSV", 1000);
try {
Path csvFile = model.getSource();
char delim = model.getDelimiter();
List<CSVRecord> rows = DistrictImportUtils.readRows(csvFile, delim, -1);
+ monitor.beginTask("Importing CSV", rows.size());
// Path wktFile = model.getWKTFile();
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 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();
DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+ Collection<Resource> vertices = graph.syncRequest(new ObjectsWithType(model.getParentDiagram(), Layer0.getInstance(graph).ConsistsOf, DistrictNetworkResource.getInstance(graph).Vertex));
+ List<ResourceVertex> vv = new ArrayList<>(vertices.size());
+ for (Resource vertex : vertices) {
+ double[] existingCoords = graph.getRelatedValue2(vertex, DiagramResource.getInstance(graph).HasLocation, Bindings.DOUBLE_ARRAY);
+ vv.add(new ResourceVertex(vertex, existingCoords));
+ }
+
for (int k = 1; k < rows.size(); k++) {
CSVRecord row = rows.get(k);
if (model.isVertexImport()) {
String xCoords = row.get(xCoordColumnIndex);
String yCoords = row.get(yCoordColumnIndex);
- double xCoord = - Double.parseDouble(xCoords); // make negative for now
+ double xCoord = Double.parseDouble(xCoords);
double yCoord = Double.parseDouble(yCoords);
double z = 0;
DirectPosition res = actualTransform.transform(sourcePos, targetPos);
coords = res.getCoordinate();
} else {
- coords = new double[] { xCoord / MapScalingTransform.getScaleX(), yCoord / MapScalingTransform.getScaleY() };
+ coords = new double[] { xCoord, yCoord };
}
- Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), new double[] { coords[1], -coords[0]}, model.getComponentMappings().get(mappingValue));
+
+ // 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);
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 endXCoords = row.get(endXCoordColumnIndex);
String endYCoords = row.get(endYCoordColumnIndex);
- double startXCoord = - Double.parseDouble(startXCoords); // make negative
+ double startXCoord = Double.parseDouble(startXCoords); // make negative
double startYCoord = Double.parseDouble(startYCoords);
- double endXCoord = - Double.parseDouble(endXCoords); // make negative
+ double endXCoord = Double.parseDouble(endXCoords); // make negative
double endYCoord = Double.parseDouble(endYCoords);
double[] startCoords;
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() };
+ startCoords = new double[] { startXCoord , startYCoord };
+ endCoords = new double[] { endXCoord , endYCoord };
}
-
- Resource edge = DNEdgeBuilder.create(graph, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), new double[] { startCoords[1], -startCoords[0]}, new double[] { endCoords[1], -endCoords[0]}, padding);
+
+ // 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, 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 e) {
throw new DatabaseException(e);
}
+ monitor.worked(1);
}
}
});
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) {
+ if (index != -1) {
String stringValue = row.get(index);
if (!stringValue.isEmpty()) {
try {
}
}
}
+
+ 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()) {
+ 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++) {
+ 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);
+ }
+ }
+ }
+ }
}