]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.imports/src/org/simantics/district/imports/DistrictImportUtils.java
0b6edb5f51785773811c53b33153aff354af2e8c
[simantics/district.git] / org.simantics.district.imports / src / org / simantics / district / imports / DistrictImportUtils.java
1 package org.simantics.district.imports;
2
3 import java.io.IOException;
4 import java.io.Reader;
5 import java.nio.file.Files;
6 import java.nio.file.Path;
7 import java.util.ArrayList;
8 import java.util.Collection;
9 import java.util.HashMap;
10 import java.util.HashSet;
11 import java.util.Iterator;
12 import java.util.List;
13 import java.util.Map;
14 import java.util.Optional;
15 import java.util.Set;
16 import java.util.concurrent.atomic.AtomicInteger;
17 import java.util.function.Function;
18
19 import org.apache.commons.csv.CSVFormat;
20 import org.apache.commons.csv.CSVParser;
21 import org.apache.commons.csv.CSVRecord;
22 import org.geotools.geometry.DirectPosition2D;
23 import org.geotools.referencing.CRS;
24 import org.opengis.geometry.DirectPosition;
25 import org.opengis.geometry.MismatchedDimensionException;
26 import org.opengis.referencing.FactoryException;
27 import org.opengis.referencing.NoSuchAuthorityCodeException;
28 import org.opengis.referencing.crs.CoordinateReferenceSystem;
29 import org.opengis.referencing.operation.MathTransform;
30 import org.opengis.referencing.operation.TransformException;
31 import org.simantics.Simantics;
32 import org.simantics.databoard.Bindings;
33 import org.simantics.db.Resource;
34 import org.simantics.db.WriteGraph;
35 import org.simantics.db.common.request.WriteRequest;
36 import org.simantics.db.exception.DatabaseException;
37 import org.simantics.db.layer0.util.Layer0Utils;
38 import org.simantics.district.network.DNEdgeBuilder;
39 import org.simantics.district.network.DistrictNetworkUtil;
40 import org.simantics.district.network.ontology.DistrictNetworkResource;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 import com.vividsolutions.jts.index.quadtree.Quadtree;
45
46 public class DistrictImportUtils {
47
48     private DistrictImportUtils() { }
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(DistrictImportUtils.class);
51     
52     public static Resource importCSVAsLayer(Path csvFile) throws IOException {
53         
54         try (CSVParser parser = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(Files.newBufferedReader(csvFile))) {
55             Map<String, Integer> header = parser.getHeaderMap();
56             System.out.println(header);
57         }
58         return null;
59     }
60     
61     public static Map<String, Integer> readCSVHeader(Path source, char delimiter, boolean firstAsHeader) throws IOException {
62         return readCSVHeader(source, CSVFormat.newFormat(delimiter), firstAsHeader);
63     }
64     
65     public static Map<String, Integer> readCSVHeader(Path source, CSVFormat format, boolean firstAsHeader) throws IOException {
66         if (firstAsHeader)
67             format = format.withFirstRecordAsHeader();
68         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
69             return parser.getHeaderMap();
70         }
71     }
72
73     public static Map<String, Character> getSupportedCSVDelimiterFormats() {
74         Map<String, Character> delimiters = new HashMap<>();
75         delimiters.put("Comma", ',');
76         delimiters.put("Semicolon", ';');
77         delimiters.put("Tabulator", '\t');
78         return delimiters;
79     }
80
81     public static List<Map<String, String>> readRows(Path source, CSVFormat format, boolean firstAsHeader, int amount) throws IOException {
82         if (firstAsHeader)
83             format = format.withFirstRecordAsHeader();
84         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
85             int start = 0;
86             List<Map<String, String>> results = new ArrayList<>(amount);
87             Iterator<CSVRecord> iter = parser.iterator();
88             while (start < amount && iter.hasNext()) {
89                 CSVRecord record = iter.next();
90                 results.add(record.toMap());
91                 start++;
92             }
93             return results;
94         }
95     }
96
97     public static List<CSVRecord> readRows(Path source, char delim, boolean firstAsHeader, int rowAmount) throws IOException {
98         List<CSVRecord> results = new ArrayList<>();
99         AtomicInteger count = new AtomicInteger(0);
100         consumeCSV(source, delim, firstAsHeader, t -> {
101             results.add(t);
102             int current = count.incrementAndGet();
103             return current < rowAmount;
104         });
105         return results;
106     }
107     
108     public static void consumeCSV(Path source, char delim, boolean firstAsHeader, Function<CSVRecord, Boolean> consumer) throws IOException {
109         consumeCSV(Files.newBufferedReader(source), delim, firstAsHeader, consumer);
110     }
111
112     public static void consumeCSV(Reader reader, char delim, boolean firstAsHeader,
113             Function<CSVRecord, Boolean> consumer) throws IOException {
114         CSVFormat format = CSVFormat.newFormat(delim).withQuote('"');
115         if (firstAsHeader) {
116             format = format.withFirstRecordAsHeader();
117         }
118         try (CSVParser parser = format.parse(reader)) {
119             Iterator<CSVRecord> records = parser.iterator();
120             while (records.hasNext()) {
121                 Boolean cont = consumer.apply(records.next());
122                 if (!cont) {
123                     break;
124                 }
125             }
126         }
127     }
128
129     
130     public static Map<CSVHeader, List<String>> readCSVHeaderAndRows(Path source, char delimiter, boolean firstAsHeader, int amount) throws IOException {
131         Map<CSVHeader, List<String>> results = new HashMap<>();
132         CSVFormat format = CSVFormat.newFormat(delimiter);
133         if (firstAsHeader)
134             format = format.withFirstRecordAsHeader();
135         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
136             Map<String, Integer> headers = parser.getHeaderMap();
137             if (headers != null && !headers.isEmpty()) {
138                 for (int index = 0; index < headers.size(); index++) {
139                     for (String head : headers.keySet()) {
140                         results.put(new CSVHeader(head, index), new ArrayList<>());
141                     }
142                 }
143             }
144             
145             Iterator<CSVRecord> records = parser.iterator();
146             int rows = 0;
147             while (rows < amount && records.hasNext()) {
148                 CSVRecord record = records.next();
149                 for (int j = 0; j < record.size(); j++) {
150                     String value = record.get(j);
151                     String header = Integer.toString(j);
152                     CSVHeader csvHeader = new CSVHeader(header, j);
153                     List<String> vals = results.get(csvHeader);
154                     if (vals == null) {
155                         vals = new ArrayList<>();
156                         results.put(csvHeader, vals);
157                     }
158                     vals.add(value);
159                 }
160                 rows++;
161             }
162         }
163         return results;
164     }
165     
166     public static class CSVHeader {
167
168         private final String header;
169         private final int index;
170         
171         public CSVHeader(String header, int index) {
172             this.header = header;
173             this.index = index;
174         }
175
176         public String getHeader() {
177             return header;
178         }
179
180         public int getIndex() {
181             return index;
182         }
183         
184         @Override
185         public int hashCode() {
186             final int prime = 31;
187             int result = 1;
188             result = prime * result + ((header == null) ? 0 : header.hashCode());
189             result = prime * result + index;
190             return result;
191         }
192
193         @Override
194         public boolean equals(Object obj) {
195             if (this == obj)
196                 return true;
197             if (obj == null)
198                 return false;
199             if (getClass() != obj.getClass())
200                 return false;
201             CSVHeader other = (CSVHeader) obj;
202             if (header == null) {
203                 if (other.header != null)
204                     return false;
205             } else if (!header.equals(other.header))
206                 return false;
207             if (index != other.index)
208                 return false;
209             return true;
210         }
211     }
212
213     public static Collection<String> readDistinctValuesOfColumn(Path source, char delim, int mappingIndex) throws IOException {
214         Set<String> results = new HashSet<>();
215         CSVFormat format = CSVFormat.newFormat(delim);
216         try (CSVParser parser = format.parse(Files.newBufferedReader(source))) {
217             Iterator<CSVRecord> records = parser.iterator();
218             if (records.hasNext())
219                 records.next();
220             while (records.hasNext()) {
221                 CSVRecord row = records.next();
222                 String value = row.get(mappingIndex);
223                 results.add(value);
224             }
225         }
226         return results;
227     }
228
229     public static void importVertices(CSVImportModel model) throws NoSuchAuthorityCodeException, FactoryException, DatabaseException {
230
231         Path csvFile = model.getSource();
232         char delim = model.getDelimiter();
233
234         int xCoordColumnIndex = model.getXCoordIndex();
235         int yCoordColumnIndex = model.getYCoordIndex();
236         int zCoordColumnIndex = model.getZCoordIndex();
237         int altElevationIndex = model.getAlternativeElevationIndex();
238         int supplyTempColumnIndex = model.getSupplyTempIndex();
239         int returnTempColumnIndex = model.getReturnTempIndex();
240         int supplyPressureColumnIndex = model.getSupplyPressureIndex();
241         int returnPressureColumnIndex = model.getReturnPressureIndex();
242         int dpIndex = model.getDeltaPressureIndex();
243         int dtIndex = model.getDeltaTemperatureIndex();
244         int heatPowerIndex = model.getHeatPowerIndex();
245         int peakPowerIndex = model.getPeakPowerIndex();
246         int valvePositionIndex = model.getValvePositionIndx();
247         int nominalHeadMIndex = model.getNominalHeadMIndex();
248         int nominalHeadBIndex = model.getNominalHeadBIndex();
249         int nominalFlowIndex = model.getNominalFlowIndex();
250         int maximumHeadMIndex = model.getMaximumHeadMIndex();
251         int heatLoadDsIndex = model.getHeatLoadDsIndex();
252         int massFlowIndex = model.getMassFlowIndex();
253         int volFlowIndex = model.getVolFlowIndex();
254         int velocityIndex = model.getVelocityIndex();
255         int flowAreaIndex = model.getFlowAreaIndex();
256         int nominalPressureLossIndex = model.getNominalPressureLossIndex();
257         int addressIndex = model.getAddressIndex();
258         int regionIndex = model.getRegionIndex();
259         
260         int mappingColumn = model.getComponentMappingIndex();
261         int idColumn = model.getIdIndex();
262         
263         String sourceEPSGCRS = model.getSourceCRS();
264         
265         MathTransform transform = null;
266         boolean doTransform = false;
267         // if sourceEPSGCRS is empty || null then ignore transformation
268         if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
269             CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
270             CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
271             transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
272             doTransform = true;
273         }
274         final boolean actualDoTransform = doTransform;
275         final MathTransform actualTransform = transform;
276         
277         Simantics.getSession().syncRequest(new WriteRequest() {
278             
279             @Override
280             public void perform(WriteGraph graph) throws DatabaseException {
281                 try {
282                     Layer0Utils.setDependenciesIndexingDisabled(graph, true);
283                     graph.markUndoPoint();
284                     
285                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
286
287                     DistrictImportUtils.consumeCSV(csvFile, delim, true, (Function<CSVRecord, Boolean>) row -> {
288                         try {
289                             String mappingValue = row.get(mappingColumn);
290                             
291                             String xCoords = row.get(xCoordColumnIndex);
292                             String yCoords = row.get(yCoordColumnIndex);
293                             double xCoord = Double.parseDouble(xCoords);
294                             double yCoord = Double.parseDouble(yCoords);
295                             
296                             double z = 0;
297                             if (zCoordColumnIndex != -1) {
298                                 String zs = row.get(zCoordColumnIndex);
299                                 
300                                 if (!zs.isEmpty()) {
301                                     try {
302                                         z = Double.parseDouble(zs);
303                                     } catch (NumberFormatException e1) {
304                                         throw new DatabaseException(e1);
305                                     }
306                                 }
307                             }
308                             
309                             double[] coords;
310                             if (actualDoTransform) {
311                                 DirectPosition2D targetPos = new DirectPosition2D();
312                                 DirectPosition2D sourcePos = new DirectPosition2D(xCoord, yCoord);
313                                 DirectPosition res = actualTransform.transform(sourcePos, targetPos);
314                                 coords = res.getCoordinate();
315                             } else {
316                                 coords = new double[] { xCoord, yCoord };
317                             }
318                             
319                             // Switch to (longitude, latitude)
320                             flipAxes(coords);
321                             
322                             Resource vertex = DistrictNetworkUtil.createVertex(graph, model.getParentDiagram(), coords, z, model.getComponentMappings().get(mappingValue));
323                             
324                             writeStringValue(graph, row, idColumn, vertex, DN.HasId);
325                             
326                             writeValue(graph, row, altElevationIndex, vertex, DN.Vertex_HasAltElevation);
327                             
328                             writeValue(graph, row, supplyTempColumnIndex, vertex, DN.Vertex_HasSupplyTemperature);
329                             writeValue(graph, row, returnTempColumnIndex, vertex, DN.Vertex_HasReturnTemperature);
330                             writeValue(graph, row, supplyPressureColumnIndex, vertex, DN.Vertex_HasSupplyPressure);
331                             writeValue(graph, row, returnPressureColumnIndex, vertex, DN.Vertex_HasReturnPressure);
332                             writeValue(graph, row, dpIndex, vertex, DN.Vertex_HasDeltaPressure);
333                             writeValue(graph, row, dtIndex, vertex, DN.Vertex_HasDeltaTemperature);
334                             writeValue(graph, row, heatPowerIndex, vertex, DN.Vertex_HasHeatPower);
335                             writeValue(graph, row, peakPowerIndex, vertex, DN.Vertex_HasPeakPower);
336                             writeValue(graph, row, valvePositionIndex, vertex, DN.Vertex_HasValvePosition);
337                             writeValue(graph, row, nominalHeadMIndex, vertex, DN.Vertex_HasNominalHeadM);
338                             writeValue(graph, row, nominalHeadBIndex, vertex, DN.Vertex_HasNominalHeadB);
339                             writeValue(graph, row, nominalFlowIndex, vertex, DN.Vertex_HasNominalFlow);
340                             writeValue(graph, row, maximumHeadMIndex, vertex, DN.Vertex_HasMaximumHeadM);
341                             writeValue(graph, row, heatLoadDsIndex, vertex, DN.Vertex_HasHeatLoadDs);
342                             writeValue(graph, row, massFlowIndex, vertex, DN.Vertex_HasMassFlow);
343                             writeValue(graph, row, volFlowIndex, vertex, DN.Vertex_HasVolFlow);
344                             writeValue(graph, row, velocityIndex, vertex, DN.Vertex_HasVelocity);
345                             writeValue(graph, row, flowAreaIndex, vertex, DN.Vertex_HasFlowArea);
346                             writeValue(graph, row, nominalPressureLossIndex, vertex, DN.Vertex_HasNominalPressureLoss);
347                             writeStringValue(graph, row, addressIndex, vertex, DN.Vertex_HasAddress);
348                             writeStringValue(graph, row, regionIndex, vertex, DN.HasRegion);
349                         } catch (DatabaseException | MismatchedDimensionException | TransformException e) {
350                             throw new RuntimeException(e);
351                         }
352                         return true;
353                     });
354                     
355                 } catch (IOException e) {
356                     LOGGER.error("Could not import", e);
357                     throw new DatabaseException(e);
358                 } finally {
359                     Layer0Utils.setDependenciesIndexingDisabled(graph, false);
360                 }
361             }
362         });
363     }
364
365     public static void importEdges(CSVImportModel model) throws NoSuchAuthorityCodeException, FactoryException, DatabaseException {
366
367         Path csvFile = model.getSource();
368         char delim = model.getDelimiter();
369
370         Set<String> writtenIds = new HashSet<>();
371         
372         int startXCoordColumnIndex = model.getStartXCoordIndex();
373         int startYCoordColumnIndex = model.getStartYCoordIndex();
374         int startZValueColumnIndex = model.getStartZCoordIndex();
375         int endXCoordColumnIndex = model.getEndXCoordIndex();
376         int endYCoordColumnIndex = model.getEndYCoordIndex();
377         int endZValueColumnIndex = model.getEndZCoordIndex();
378         int diameterColumnIndex= model.getDiameterIndex();
379         int outerDiameterColumnIndex = model.getOuterDiamterIndex();
380         int nominalMassFlowIndex = model.getNominalMassFlowIndex();
381         int tGroundIndex = model.gettGroundIndex();
382         int edgeFlowAreaIndex = model.getEdgeFlowAreaIndex();
383         int kReturnIndex = model.getkReturnIndex();
384         int kSupplyIndex = model.getkSupplyIndex();
385         int lengthIndex = model.getLengthIndex();
386         int detailedGeometryIndex = model.getDetailedGeometryIndex();
387         int regionIndex = model.getRegionIndex();
388         int pipeTypeIndex = model.getPipeTypeIndex();
389         
390         int mappingColumn = model.getComponentMappingIndex();
391         int idColumn = model.getIdIndex();
392         
393         double padding = model.getEdgePadding();
394
395         String sourceEPSGCRS = model.getSourceCRS();
396         
397         MathTransform transform = null;
398         boolean doTransform = false;
399         // if sourceEPSGCRS is empty || null then ignore transformation
400         if (sourceEPSGCRS != null && !sourceEPSGCRS.isEmpty()) {
401             CoordinateReferenceSystem sourceCRS = CRS.decode(sourceEPSGCRS);
402             CoordinateReferenceSystem targetCRS = CRS.decode("EPSG:4326");
403             transform = CRS.findMathTransform(sourceCRS, targetCRS, true);
404             doTransform = true;
405         }
406         final boolean actualDoTransform = doTransform;
407         final MathTransform actualTransform = transform;
408         
409         double halfPadding = padding / 2;
410         
411         Quadtree existingVertices = DistrictNetworkUtil.existingVertices(model.getParentDiagram(), halfPadding);
412
413         Simantics.getSession().syncRequest(new WriteRequest() {
414             
415             @Override
416             public void perform(WriteGraph graph) throws DatabaseException {
417                 try {
418                     Layer0Utils.setDependenciesIndexingDisabled(graph, true);
419                     graph.markUndoPoint();
420
421                     DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
422                     
423                     DistrictImportUtils.consumeCSV(csvFile, delim, true, row -> {
424                         try {
425                             
426                             String idValue = row.get(idColumn);
427                             if (!writtenIds.contains(idValue)) {
428                                 writtenIds.add(idValue);
429                                 String mappingValue = row.get(mappingColumn);
430                                 
431                                 String startXCoords = row.get(startXCoordColumnIndex);
432                                 String startYCoords = row.get(startYCoordColumnIndex);
433                                 String startZCoords = row.get(startZValueColumnIndex);
434                                 String endXCoords = row.get(endXCoordColumnIndex);
435                                 String endYCoords = row.get(endYCoordColumnIndex);
436                                 String endZCoords = row.get(endZValueColumnIndex);
437                                 
438                                 double startXCoord = Double.parseDouble(startXCoords); // make negative
439                                 double startYCoord = Double.parseDouble(startYCoords);
440                                 double startZCoord = Double.parseDouble(startZCoords);
441                                 
442                                 double endXCoord = Double.parseDouble(endXCoords); // make negative
443                                 double endYCoord = Double.parseDouble(endYCoords);
444                                 double endZCoord = Double.parseDouble(endZCoords);
445                                 
446                                 double[] startCoords;
447                                 double[] endCoords;
448                                 if (actualDoTransform) {
449                                     DirectPosition2D startTargetPos = new DirectPosition2D();
450                                     DirectPosition2D startSourcePos = new DirectPosition2D(startXCoord, startYCoord);
451                                     DirectPosition startRes = actualTransform.transform(startSourcePos, startTargetPos);
452                                     startCoords = startRes.getCoordinate();
453                                     
454                                     DirectPosition2D endTargetPos = new DirectPosition2D();
455                                     DirectPosition2D endSourcePos = new DirectPosition2D(endXCoord, endYCoord);
456                                     DirectPosition endRes = actualTransform.transform(endSourcePos, endTargetPos);
457                                     endCoords = endRes.getCoordinate();
458                                 } else {
459                                     startCoords = new double[] { startXCoord , startYCoord };
460                                     endCoords = new double[] { endXCoord , endYCoord };
461                                 }
462                                 
463                                 // Switch to (longitude, latitude)
464                                 flipAxes(startCoords);
465                                 flipAxes(endCoords);
466
467                                 Optional<Resource> oedge = DNEdgeBuilder.create(graph, existingVertices, model.getParentDiagram(), model.getComponentMappings().get(mappingValue), startCoords, startZCoord, endCoords, endZCoord, new double[0], padding, true);
468                                 if (oedge.isPresent()) {
469                                     Resource edge = oedge.get();
470                                     
471                                     writeStringValue(graph, row, idColumn, edge, DN.HasId);
472                                     
473                                     writeValue(graph, row, diameterColumnIndex, edge, DN.Edge_HasDiameter);
474                                     writeValue(graph, row, outerDiameterColumnIndex, edge, DN.Edge_HasOuterDiameter);
475                                     writeValue(graph, row, nominalMassFlowIndex, edge, DN.Edge_HasNominalMassFlow);
476                                     writeValue(graph, row, tGroundIndex, edge, DN.Edge_HasTGround);
477                                     writeValue(graph, row, kReturnIndex, edge, DN.Edge_HasKReturn);
478                                     writeValue(graph, row, kSupplyIndex, edge, DN.Edge_HasKSupply);
479                                     writeValue(graph, row, edgeFlowAreaIndex, edge, DN.Edge_HasFlowArea);
480                                     writeValue(graph, row, lengthIndex, edge, DN.Edge_HasLength);
481                                     writeStringValue(graph, row, regionIndex, edge, DN.HasRegion);
482                                     writeStringValue(graph, row, pipeTypeIndex, edge, DN.Edge_HasType);
483                                     writeDoubleArrayFromString(graph, row, detailedGeometryIndex, edge, DN.Edge_HasGeometry, actualTransform);
484                                 }
485                             }
486                             return true;
487                         } catch (DatabaseException | MismatchedDimensionException | TransformException e) {
488                             throw new RuntimeException(e);
489                         }
490                     });
491                 } catch (IOException e) {
492                     LOGGER.error("Could not import edges {}", model.getSource(), e);
493                 } finally {
494                     Layer0Utils.setDependenciesIndexingDisabled(graph, false);
495                 }
496             }
497         });
498     }
499     
500     private static void flipAxes(double[] coords) {
501         double tmp = coords[0];
502         coords[0] = coords[1];
503         coords[1] = tmp;
504     }
505
506     private static void writeValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
507         if (index != -1) {
508             String stringValue = row.get(index);
509             if (!stringValue.isEmpty()) {
510                 try {
511                     if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) {
512                         stringValue = stringValue.substring(1, stringValue.length() - 1);
513                     }
514                     graph.claimLiteral(subject, relation, Double.parseDouble(stringValue), Bindings.DOUBLE);
515                 } catch (NumberFormatException e) {
516                     LOGGER.error("Could not parse {} {} {} {}", row, index, subject, relation, e);
517                     //throw new DatabaseException(e);
518                 }
519             }
520         }
521     }
522     
523     private static void writeStringValue(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation) throws DatabaseException {
524         if (index != -1) {
525             String stringValue = row.get(index);
526             if (!stringValue.isEmpty()) {
527                 try {
528                     graph.claimLiteral(subject, relation, stringValue, Bindings.STRING);
529                 } catch (NumberFormatException e) {
530                     throw new DatabaseException(e);
531                 }
532             }
533         }
534     }
535
536     private static void writeDoubleArrayFromString(WriteGraph graph, CSVRecord row, int index, Resource subject, Resource relation, MathTransform actualTransform) throws DatabaseException, MismatchedDimensionException, TransformException {
537         if (index != -1) {
538             String stringValue = row.get(index);
539             if (!stringValue.isEmpty()) {
540                 if (stringValue.startsWith("\"") && stringValue.endsWith("\"")) {
541                     stringValue = stringValue.substring(1, stringValue.length() - 1);
542                 }
543                 String[] coordPairs = stringValue.split(";");
544                 ArrayList<Double> dd = new ArrayList<>(coordPairs.length * 2);
545                 for (int i = 0; i < coordPairs.length; i++) {
546                     String coordPair = coordPairs[i];
547                     String[] p = coordPair.split(" ");
548                     double x = Double.parseDouble(p[0]);
549                     double y = Double.parseDouble(p[1]);
550                     if (actualTransform != null) {
551                         DirectPosition2D targetPos = new DirectPosition2D();
552                         DirectPosition2D sourcePos = new DirectPosition2D(y, x);
553                         DirectPosition res = actualTransform.transform(sourcePos, targetPos);
554                         double[] coords = res.getCoordinate();
555                         x = coords[1];
556                         y = coords[0];
557                     }
558                     dd.add(x);
559                     dd.add(y);
560                 }
561                 double[] detailedGeometryCoords = new double[dd.size()];
562                 for (int i = 0; i < dd.size(); i++) {
563                     double d = dd.get(i);
564                     detailedGeometryCoords[i] = d;
565                 }
566                 try {
567                     graph.claimLiteral(subject, relation, detailedGeometryCoords, Bindings.DOUBLE_ARRAY);
568                 } catch (NumberFormatException e) {
569                     throw new DatabaseException(e);
570                 }
571             }
572         }
573     }
574 }