X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=org.simantics.district.imports%2Fsrc%2Forg%2Fsimantics%2Fdistrict%2Fimports%2FDistrictImportUtils.java;h=67e07b0cbd792ec3cfdc9a978ee8f14950382745;hb=refs%2Fchanges%2F72%2F4472%2F1;hp=139b63e8d0bfe9b6eb2e4a99358ef7d2c20e9092;hpb=797e65f69a2bd674f8fc4dcc16bcd11e2cf49ca3;p=simantics%2Fdistrict.git diff --git a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java index 139b63e8..67e07b0c 100644 --- a/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java +++ b/org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java @@ -1,6 +1,8 @@ package org.simantics.district.imports; import java.io.IOException; +import java.io.Reader; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; @@ -29,24 +31,17 @@ 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.layer0.util.Layer0Utils; -import org.simantics.diagram.stubs.DiagramResource; import org.simantics.district.network.DNEdgeBuilder; import org.simantics.district.network.DistrictNetworkUtil; import org.simantics.district.network.ontology.DistrictNetworkResource; -import org.simantics.layer0.Layer0; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.simantics.district.network.DistrictNetworkUtil.ResourceVertex; -import com.vividsolutions.jts.geom.Envelope; import com.vividsolutions.jts.index.quadtree.Quadtree; public class DistrictImportUtils { @@ -71,7 +66,7 @@ public class DistrictImportUtils { public static Map readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException { if (firstAsHeader) format = format.withFirstRecordAsHeader(); - try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + try (CSVParser parser = format.parse(Files.newBufferedReader(source, Charset.defaultCharset()))) { return parser.getHeaderMap(); } } @@ -112,11 +107,16 @@ public class DistrictImportUtils { } public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function consumer) throws IOException { - CSVFormat format = CSVFormat.newFormat(delim); + consumeCSV(Files.newBufferedReader(source), delim, firstAsHeader, consumer); + } + + public static void consumeCSV(Reader reader, char delim, boolean firstAsHeader, + Function consumer) throws IOException { + CSVFormat format = CSVFormat.newFormat(delim).withQuote('"'); if (firstAsHeader) { format = format.withFirstRecordAsHeader(); } - try (CSVParser parser = format.parse(Files.newBufferedReader(source))) { + try (CSVParser parser = format.parse(reader)) { Iterator records = parser.iterator(); while (records.hasNext()) { Boolean cont = consumer.apply(records.next()); @@ -256,6 +256,7 @@ public class DistrictImportUtils { int flowAreaIndex = model.getFlowAreaIndex(); int nominalPressureLossIndex = model.getNominalPressureLossIndex(); int addressIndex = model.getAddressIndex(); + int regionIndex = model.getRegionIndex(); int mappingColumn = model.getComponentMappingIndex(); int idColumn = model.getIdIndex(); @@ -345,6 +346,7 @@ public class DistrictImportUtils { writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea); writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss); writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress); + writeStringValue(graph, row, regionIndex, vertex, DN.HasRegion); } catch (DatabaseException | MismatchedDimensionException | TransformException e) { throw new RuntimeException(e); } @@ -366,6 +368,7 @@ public class DistrictImportUtils { Path csvFile = model.getSource(); char delim = model.getDelimiter(); + Set writtenIds = new HashSet<>(); int startXCoordColumnIndex = model.getStartXCoordIndex(); int startYCoordColumnIndex = model.getStartYCoordIndex(); @@ -382,6 +385,15 @@ public class DistrictImportUtils { int kSupplyIndex = model.getkSupplyIndex(); int lengthIndex = model.getLengthIndex(); int detailedGeometryIndex = model.getDetailedGeometryIndex(); + int regionIndex = model.getRegionIndex(); + int pipeTypeIndex = model.getPipeTypeIndex(); + int pipeCodeIndex = model.getPipeCodeIndex(); + int installationYearIndex = model.getInstallationYearIndex(); + int wallThicknessIndex = model.getWallThicknessIndex(); + int insulationConductivityIndex = model.getInsulationConductivityIndex(); + int pipeSizeDNIndex = model.getPipeSizeDNIndex(); + int roughnessIndex = model.getRoughnessIndex(); + int structureIndex = model.getStructureIndex(); int mappingColumn = model.getComponentMappingIndex(); int idColumn = model.getIdIndex(); @@ -404,24 +416,7 @@ public class DistrictImportUtils { 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; - } - }); + Quadtree existingVertices = DistrictNetworkUtil.existingVertices(model.getParentDiagram(), halfPadding); Simantics.getSession().syncRequest(new WriteRequest() { @@ -435,60 +430,74 @@ public class DistrictImportUtils { DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> { try { - String mappingValue = row.get(mappingColumn); - 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(); + String idValue = row.get(idColumn); + if (!writtenIds.contains(idValue)) { + writtenIds.add(idValue); + String mappingValue = row.get(mappingColumn); - 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, new double[0], padding, true); - if (oedge.isPresent()) { - Resource edge = oedge.get(); - writeStringValue(graph, row, idColumn, edge, DN.HasId); + 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); - 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); + 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, existingVertices, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], 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); + writeStringValue(graph, row, regionIndex, edge, DN.HasRegion); + writeStringValue(graph, row, pipeTypeIndex, edge, DN.Edge_HasType); + writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform); + writeStringValue(graph, row, pipeCodeIndex, edge, DN.Edge_HasPipeCode); + writeIntegerValue(graph, row, installationYearIndex, edge, DN.Edge_HasInstallationYear); + writeValue(graph, row, wallThicknessIndex, edge, DN.Edge_HasWallThickness); + writeValue(graph, row, insulationConductivityIndex, edge, DN.Edge_HasInsulationConductivity); + writeIntegerValue(graph, row, pipeSizeDNIndex, edge, DN.Edge_HasPipeSizeDN); + writeValue(graph, row, roughnessIndex, edge, DN.Edge_HasRoughness); + writeStringValue(graph, row, structureIndex, edge, DN.Edge_HasStructure); + } } - return true; } catch (DatabaseException | MismatchedDimensionException | TransformException e) { throw new RuntimeException(e); @@ -519,7 +528,25 @@ public class DistrictImportUtils { } graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE); } catch (NumberFormatException e) { - throw new DatabaseException(e); + LOGGER.error("Could not parse {} {} {} {}", row, index, subject, relation, e); + //throw new DatabaseException(e); + } + } + } + } + + private static void writeIntegerValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException { + if (index != -1) { + String stringValue = row.get(index); + if (!stringValue.isEmpty()) { + try { + if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) { + stringValue = stringValue.substring(1, stringValue.length() - 1); + } + graph.claimLiteral(subject, relation, Integer.parseInt(stringValue), Bindings.INTEGER); + } catch (NumberFormatException e) { + LOGGER.error("Could not parse {} {} {} {}", row, index, subject, relation, e); + //throw new DatabaseException(e); } } } @@ -554,7 +581,7 @@ public class DistrictImportUtils { double y = Double.parseDouble(p[1]); if (actualTransform != null) { DirectPosition2D targetPos = new DirectPosition2D(); - DirectPosition2D sourcePos = new DirectPosition2D(x, y); + DirectPosition2D sourcePos = new DirectPosition2D(y, x); DirectPosition res = actualTransform.transform(sourcePos, targetPos); double[] coords = res.getCoordinate(); x = coords[1];