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