Skip to content

Commit de92a9f

Browse files
committed
Preserve Z/M when converting coordinate sequences
1 parent ed92c1c commit de92a9f

2 files changed

Lines changed: 96 additions & 1 deletion

File tree

jena-geosparql/src/main/java/org/apache/jena/geosparql/implementation/jts/CustomCoordinateSequenceFactory.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,21 @@ public CoordinateSequence create(CoordinateSequence coordSeq) {
4848
CustomCoordinateSequence customCoordSeq = (CustomCoordinateSequence) coordSeq;
4949
copyCoordSeq = customCoordSeq.copy();
5050
} else {
51-
copyCoordSeq = new CustomCoordinateSequence(coordSeq.toCoordinateArray());
51+
CoordinateSequenceDimensions dimensions;
52+
if (coordSeq.hasM()) {
53+
dimensions = coordSeq.hasZ() ? CoordinateSequenceDimensions.XYZM : CoordinateSequenceDimensions.XYM;
54+
} else {
55+
dimensions = coordSeq.hasZ() ? CoordinateSequenceDimensions.XYZ : CoordinateSequenceDimensions.XY;
56+
}
57+
copyCoordSeq = new CustomCoordinateSequence(coordSeq.size(), dimensions);
58+
for (int i = 0; i < coordSeq.size(); i++) {
59+
copyCoordSeq.setOrdinate(i, CoordinateSequence.X, coordSeq.getX(i));
60+
copyCoordSeq.setOrdinate(i, CoordinateSequence.Y, coordSeq.getY(i));
61+
if (coordSeq.hasZ())
62+
copyCoordSeq.setOrdinate(i, CoordinateSequence.Z, coordSeq.getZ(i));
63+
if (coordSeq.hasM())
64+
copyCoordSeq.setOrdinate(i, CoordinateSequence.M, coordSeq.getM(i));
65+
}
5266
}
5367

5468
return copyCoordSeq;
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* https://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
* SPDX-License-Identifier: Apache-2.0
20+
*/
21+
package org.apache.jena.geosparql.implementation.jts;
22+
23+
import org.junit.Test;
24+
import org.locationtech.jts.geom.CoordinateSequence;
25+
import org.locationtech.jts.geom.impl.CoordinateArraySequence;
26+
27+
import static org.junit.Assert.*;
28+
29+
public class CustomCoordinateSequenceFactoryTest {
30+
@Test
31+
public void conversionPreservesLayoutsValuesAndIndependence() {
32+
for (int[] layout : new int[][] { { 2, 0 }, { 3, 0 }, { 3, 1 }, { 4, 1 } }) {
33+
CoordinateSequence source = new CoordinateArraySequence(2, layout[0], layout[1]);
34+
for (int i = 0; i < source.size(); i++) {
35+
source.setOrdinate(i, 0, 100 + i);
36+
source.setOrdinate(i, 1, 10 + i);
37+
if (source.hasZ())
38+
source.setOrdinate(i, 2, i == 0 ? Double.NaN : 9);
39+
if (source.hasM())
40+
source.setOrdinate(i, source.getDimension() - 1, i == 0 ? Double.NaN : 8);
41+
}
42+
CoordinateSequence converted = new CustomCoordinateSequenceFactory().create(source);
43+
assertLayout(source, converted);
44+
for (int i = 0; i < source.size(); i++) {
45+
assertEquals(source.getX(i), converted.getX(i), 0);
46+
assertEquals(source.getY(i), converted.getY(i), 0);
47+
assertEquals(source.getZ(i), converted.getZ(i), 0);
48+
assertEquals(source.getM(i), converted.getM(i), 0);
49+
}
50+
converted.setOrdinate(1, CoordinateSequence.X, -1);
51+
converted.setOrdinate(1, CoordinateSequence.Y, -1);
52+
assertEquals(101, source.getX(1), 0);
53+
assertEquals(11, source.getY(1), 0);
54+
if (source.hasZ()) {
55+
converted.setOrdinate(1, CoordinateSequence.Z, -1);
56+
assertEquals(9, source.getZ(1), 0);
57+
}
58+
if (source.hasM()) {
59+
converted.setOrdinate(1, CoordinateSequence.M, -1);
60+
assertEquals(8, source.getM(1), 0);
61+
}
62+
}
63+
}
64+
65+
@Test
66+
public void conversionPreservesEmptyLayouts() {
67+
for (int[] layout : new int[][] { { 2, 0 }, { 3, 0 }, { 3, 1 }, { 4, 1 } }) {
68+
CoordinateSequence source = new CoordinateArraySequence(0, layout[0], layout[1]);
69+
assertLayout(source, new CustomCoordinateSequenceFactory().create(source));
70+
}
71+
}
72+
73+
private static void assertLayout(CoordinateSequence source, CoordinateSequence converted) {
74+
assertNotSame(source, converted);
75+
assertEquals(source.size(), converted.size());
76+
assertEquals(source.getDimension(), converted.getDimension());
77+
assertEquals(source.getMeasures(), converted.getMeasures());
78+
assertEquals(source.hasZ(), converted.hasZ());
79+
assertEquals(source.hasM(), converted.hasM());
80+
}
81+
}

0 commit comments

Comments
 (0)