1 /* 2 * Copyright (c) 2016 Vivid Solutions. 3 * 4 * All rights reserved. This program and the accompanying materials 5 * are made available under the terms of the Eclipse Public License 2.0 6 * and Eclipse Distribution License v. 1.0 which accompanies this distribution. 7 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v20.html 8 * and the Eclipse Distribution License is available at 9 * 10 * http://www.eclipse.org/org/documents/edl-v10.php. 11 */ 12 13 package org.locationtech.jts.geom.impl; 14 15 import org.locationtech.jts.geom.Coordinate; 16 import org.locationtech.jts.geom.CoordinateSequence; 17 import org.locationtech.jts.geom.CoordinateSequenceFactory; 18 import org.locationtech.jts.geom.CoordinateXY; 19 import org.locationtech.jts.geom.CoordinateXYM; 20 import org.locationtech.jts.geom.CoordinateXYZM; 21 import org.locationtech.jts.geom.GeometryFactory; 22 import org.locationtech.jts.geom.Polygon; 23 24 import junit.textui.TestRunner; 25 26 /** 27 * Test {@link PackedCoordinateSequence} 28 * @version 1.7 29 */ 30 public class PackedCoordinateSequenceTest 31 extends CoordinateSequenceTestBase 32 { main(String args[])33 public static void main(String args[]) { 34 TestRunner.run(PackedCoordinateSequenceTest.class); 35 } 36 PackedCoordinateSequenceTest(String name)37 public PackedCoordinateSequenceTest(String name) 38 { 39 super(name); 40 } 41 42 @Override getCSFactory()43 CoordinateSequenceFactory getCSFactory() { 44 return new PackedCoordinateSequenceFactory(); 45 } 46 testDouble()47 public void testDouble() { 48 checkAll( PackedCoordinateSequenceFactory.DOUBLE_FACTORY ); 49 } 50 testFloat()51 public void testFloat() { 52 checkAll( PackedCoordinateSequenceFactory.FLOAT_FACTORY) ; 53 } 54 checkAll(CoordinateSequenceFactory factory)55 public void checkAll(CoordinateSequenceFactory factory) 56 { 57 checkDim2(1, factory); 58 checkDim2(5, factory); 59 checkDim3(factory); 60 checkDim3_M1(factory); 61 checkDim4_M1(factory); 62 checkDim4(factory); 63 checkDimInvalid(factory); 64 } 65 checkDim2(int size, CoordinateSequenceFactory factory)66 public void checkDim2(int size, CoordinateSequenceFactory factory) 67 { 68 CoordinateSequence seq = factory.create(size, 2); 69 initProgression(seq); 70 71 assertEquals("Dimension should be 2", 2, seq.getDimension()); 72 assertTrue("Z should not be present", !seq.hasZ()); 73 assertTrue("M should not be present", !seq.hasM()); 74 75 int indexLast = size - 1; 76 double valLast = indexLast; 77 78 Coordinate coord = seq.getCoordinate(indexLast); 79 assertTrue( coord instanceof CoordinateXY); 80 assertEquals( valLast, coord.getX()); 81 assertEquals( valLast, coord.getY()); 82 83 Coordinate[] array = seq.toCoordinateArray(); 84 assertEquals(coord, array[indexLast]); 85 assertTrue(coord != array[indexLast]); 86 assertTrue(isEqual(seq,array)); 87 88 CoordinateSequence copy = factory.create(array); 89 assertTrue(isEqual(copy,array)); 90 91 CoordinateSequence copy2 = factory.create(seq); 92 assertTrue(isEqual(copy2,array)); 93 } 94 checkDim3(CoordinateSequenceFactory factory)95 public void checkDim3(CoordinateSequenceFactory factory) 96 { 97 CoordinateSequence seq = factory.create(5, 3); 98 initProgression(seq); 99 100 assertEquals("Dimension should be 3", 3, seq.getDimension()); 101 assertTrue("Z should be present", seq.hasZ()); 102 assertTrue("M should not be present", !seq.hasM()); 103 104 Coordinate coord = seq.getCoordinate(4); 105 assertTrue( coord.getClass() == Coordinate.class); 106 assertEquals( 4.0, coord.getX()); 107 assertEquals( 4.0, coord.getY()); 108 assertEquals( 4.0, coord.getZ()); 109 110 Coordinate[] array = seq.toCoordinateArray(); 111 assertEquals(coord, array[4]); 112 assertTrue(coord != array[4]); 113 assertTrue(isEqual(seq, array)); 114 115 CoordinateSequence copy = factory.create(array); 116 assertTrue(isEqual(copy, array)); 117 118 CoordinateSequence copy2 = factory.create(seq); 119 assertTrue(isEqual(copy2, array)); 120 } 121 checkDim3_M1(CoordinateSequenceFactory factory)122 public void checkDim3_M1(CoordinateSequenceFactory factory) 123 { 124 CoordinateSequence seq = factory.create(5, 3, 1); 125 initProgression(seq); 126 127 assertEquals("Dimension should be 3", 3, seq.getDimension()); 128 assertTrue("Z should not be present", !seq.hasZ()); 129 assertTrue("M should be present", seq.hasM()); 130 131 Coordinate coord = seq.getCoordinate(4); 132 assertTrue( coord instanceof CoordinateXYM); 133 assertEquals( 4.0, coord.getX()); 134 assertEquals( 4.0, coord.getY()); 135 assertEquals( 4.0, coord.getM()); 136 137 Coordinate[] array = seq.toCoordinateArray(); 138 assertEquals(coord, array[4]); 139 assertTrue(coord != array[4]); 140 assertTrue(isEqual(seq,array)); 141 142 CoordinateSequence copy = factory.create(array); 143 assertTrue(isEqual(copy,array)); 144 145 CoordinateSequence copy2 = factory.create(seq); 146 assertTrue(isEqual(copy2,array)); 147 } 148 checkDim4_M1(CoordinateSequenceFactory factory)149 public void checkDim4_M1(CoordinateSequenceFactory factory) 150 { 151 CoordinateSequence seq = factory.create(5, 4, 1); 152 initProgression(seq); 153 154 assertEquals("Dimension should be 4", 4, seq.getDimension()); 155 assertTrue("Z should be present", seq.hasZ()); 156 assertTrue("M should be present", seq.hasM()); 157 158 Coordinate coord = seq.getCoordinate(4); 159 assertTrue( coord instanceof CoordinateXYZM); 160 assertEquals( 4.0, coord.getX()); 161 assertEquals( 4.0, coord.getY()); 162 assertEquals( 4.0, coord.getZ()); 163 assertEquals( 4.0, coord.getM()); 164 165 Coordinate[] array = seq.toCoordinateArray(); 166 assertEquals(coord, array[4]); 167 assertTrue(coord != array[4]); 168 assertTrue(isEqual(seq,array)); 169 170 CoordinateSequence copy = factory.create(array); 171 assertTrue(isEqual(copy,array)); 172 173 CoordinateSequence copy2 = factory.create(seq); 174 assertTrue(isEqual(copy2, array)); 175 } 176 checkDim4(CoordinateSequenceFactory factory)177 public void checkDim4(CoordinateSequenceFactory factory) 178 { 179 CoordinateSequence seq = factory.create(5, 4); 180 initProgression(seq); 181 182 assertEquals("Dimension should be 4", 4, seq.getDimension()); 183 assertTrue("Z should be present", seq.hasZ()); 184 assertTrue("M should be present", seq.hasM()); 185 186 Coordinate coord = seq.getCoordinate(4); 187 assertTrue( coord instanceof CoordinateXYZM); 188 assertEquals( 4.0, coord.getX()); 189 assertEquals( 4.0, coord.getY()); 190 assertEquals( 4.0, coord.getZ()); 191 assertEquals( 4.0, coord.getM()); 192 193 Coordinate[] array = seq.toCoordinateArray(); 194 assertEquals(coord, array[4]); 195 assertTrue(coord != array[4]); 196 assertTrue(isEqual(seq,array)); 197 198 CoordinateSequence copy = factory.create(array); 199 assertTrue(isEqual(copy,array)); 200 201 CoordinateSequence copy2 = factory.create(seq); 202 assertTrue(isEqual(copy2, array)); 203 } 204 205 /** 206 * Disable for now until solution can be found. 207 * See Issue 434. 208 */ XtestMixedFactoryWithXY()209 public void XtestMixedFactoryWithXY() { 210 GeometryFactory factoryPacked = new GeometryFactory(new PackedCoordinateSequenceFactory()); 211 Polygon polygonPacked = factoryPacked 212 .createPolygon( 213 new Coordinate[] { 214 new CoordinateXY(0, 0), new CoordinateXY(10, 0), new CoordinateXY(10, 10), 215 new CoordinateXY(0, 10), new CoordinateXY(0, 0) }); 216 GeometryFactory factoryDefault = new GeometryFactory(); 217 Polygon polygonArray = factoryDefault.createPolygon( 218 new Coordinate[] { new CoordinateXY(5, 5), 219 new CoordinateXY(15, 5), new CoordinateXY(15, 15), new CoordinateXY(5, 15), new CoordinateXY(5, 5) }); 220 221 polygonArray.intersection(polygonPacked); 222 223 // this fails as of 2019-June-7 224 polygonPacked.intersection(polygonArray); 225 } 226 checkDimInvalid(CoordinateSequenceFactory factory)227 public void checkDimInvalid(CoordinateSequenceFactory factory) 228 { 229 try { 230 CoordinateSequence seq = factory.create(5, 2, 1); 231 fail("Dimension=2/Measure=1 (XM) not supported"); 232 } catch (IllegalArgumentException expected) { 233 } 234 } 235 initProgression(CoordinateSequence seq)236 private void initProgression(CoordinateSequence seq) { 237 for (int index = 0; index < seq.size(); index++) { 238 for( int ordinateIndex = 0; ordinateIndex < seq.getDimension(); ordinateIndex++) { 239 seq.setOrdinate(index, ordinateIndex, (double) index); 240 } 241 } 242 } 243 244 } 245