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 package org.locationtech.jts.geom; 13 14 import org.locationtech.jts.io.ParseException; 15 import org.locationtech.jts.io.WKTReader; 16 17 import junit.framework.TestCase; 18 import junit.textui.TestRunner; 19 20 21 /** 22 * @version 1.7 23 */ 24 public class EnvelopeTest extends TestCase { 25 private PrecisionModel precisionModel = new PrecisionModel(1); 26 27 private GeometryFactory geometryFactory = new GeometryFactory(precisionModel, 28 0); 29 30 WKTReader reader = new WKTReader(geometryFactory); 31 EnvelopeTest(String name)32 public EnvelopeTest(String name) { 33 super(name); 34 } 35 main(String[] args)36 public static void main(String[] args) { 37 TestRunner.run(EnvelopeTest.class); 38 } 39 testEverything()40 public void testEverything() throws Exception { 41 Envelope e1 = new Envelope(); 42 assertTrue(e1.isNull()); 43 assertEquals(0, e1.getWidth(), 1E-3); 44 assertEquals(0, e1.getHeight(), 1E-3); 45 e1.expandToInclude(100, 101); 46 e1.expandToInclude(200, 202); 47 e1.expandToInclude(150, 151); 48 assertEquals(200, e1.getMaxX(), 1E-3); 49 assertEquals(202, e1.getMaxY(), 1E-3); 50 assertEquals(100, e1.getMinX(), 1E-3); 51 assertEquals(101, e1.getMinY(), 1E-3); 52 assertTrue(e1.contains(120, 120)); 53 assertTrue(e1.contains(120, 101)); 54 assertTrue(!e1.contains(120, 100)); 55 assertEquals(101, e1.getHeight(), 1E-3); 56 assertEquals(100, e1.getWidth(), 1E-3); 57 assertTrue(!e1.isNull()); 58 59 Envelope e2 = new Envelope(499, 500, 500, 501); 60 assertTrue(!e1.contains(e2)); 61 assertTrue(!e1.intersects(e2)); 62 e1.expandToInclude(e2); 63 assertTrue(e1.contains(e2)); 64 assertTrue(e1.intersects(e2)); 65 assertEquals(500, e1.getMaxX(), 1E-3); 66 assertEquals(501, e1.getMaxY(), 1E-3); 67 assertEquals(100, e1.getMinX(), 1E-3); 68 assertEquals(101, e1.getMinY(), 1E-3); 69 70 Envelope e3 = new Envelope(300, 700, 300, 700); 71 assertTrue(!e1.contains(e3)); 72 assertTrue(e1.intersects(e3)); 73 74 Envelope e4 = new Envelope(300, 301, 300, 301); 75 assertTrue(e1.contains(e4)); 76 assertTrue(e1.intersects(e4)); 77 } 78 testIntersects()79 public void testIntersects() { 80 checkIntersectsPermuted(1,1, 2,2, 2,2, 3,3, true); 81 checkIntersectsPermuted(1,1, 2,2, 3,3, 4,4, false); 82 } 83 testIntersectsEmpty()84 public void testIntersectsEmpty() { 85 assertTrue(!new Envelope(-5, 5, -5, 5).intersects(new Envelope())); 86 assertTrue(!new Envelope().intersects(new Envelope(-5, 5, -5, 5))); 87 assertTrue(!new Envelope().intersects(new Envelope(100, 101, 100, 101))); 88 assertTrue(!new Envelope(100, 101, 100, 101).intersects(new Envelope())); 89 } 90 testDisjointEmpty()91 public void testDisjointEmpty() { 92 assertTrue(new Envelope(-5, 5, -5, 5).disjoint(new Envelope())); 93 assertTrue(new Envelope().disjoint(new Envelope(-5, 5, -5, 5))); 94 assertTrue(new Envelope().disjoint(new Envelope(100, 101, 100, 101))); 95 assertTrue(new Envelope(100, 101, 100, 101).disjoint(new Envelope())); 96 } 97 testContainsEmpty()98 public void testContainsEmpty() { 99 assertTrue(!new Envelope(-5, 5, -5, 5).contains(new Envelope())); 100 assertTrue(!new Envelope().contains(new Envelope(-5, 5, -5, 5))); 101 assertTrue(!new Envelope().contains(new Envelope(100, 101, 100, 101))); 102 assertTrue(!new Envelope(100, 101, 100, 101).contains(new Envelope())); 103 } 104 testExpandToIncludeEmpty()105 public void testExpandToIncludeEmpty() { 106 assertEquals(new Envelope(-5, 5, -5, 5), expandToInclude(new Envelope(-5, 107 5, -5, 5), new Envelope())); 108 assertEquals(new Envelope(-5, 5, -5, 5), expandToInclude(new Envelope(), 109 new Envelope(-5, 5, -5, 5))); 110 assertEquals(new Envelope(100, 101, 100, 101), expandToInclude( 111 new Envelope(), new Envelope(100, 101, 100, 101))); 112 assertEquals(new Envelope(100, 101, 100, 101), expandToInclude( 113 new Envelope(100, 101, 100, 101), new Envelope())); 114 } 115 expandToInclude(Envelope a, Envelope b)116 private Envelope expandToInclude(Envelope a, Envelope b) { 117 a.expandToInclude(b); 118 return a; 119 } 120 testEmpty()121 public void testEmpty() { 122 assertEquals(0, new Envelope().getHeight(), 0); 123 assertEquals(0, new Envelope().getWidth(), 0); 124 assertEquals(new Envelope(), new Envelope()); 125 Envelope e = new Envelope(100, 101, 100, 101); 126 e.init(new Envelope()); 127 assertEquals(new Envelope(), e); 128 } 129 testAsGeometry()130 public void testAsGeometry() throws Exception { 131 assertTrue(geometryFactory.createPoint((Coordinate) null).getEnvelope() 132 .isEmpty()); 133 134 Geometry g = geometryFactory.createPoint(new Coordinate(5, 6)) 135 .getEnvelope(); 136 assertTrue(!g.isEmpty()); 137 assertTrue(g instanceof Point); 138 139 Point p = (Point) g; 140 assertEquals(5, p.getX(), 1E-1); 141 assertEquals(6, p.getY(), 1E-1); 142 143 LineString l = (LineString) reader.read("LINESTRING(10 10, 20 20, 30 40)"); 144 Geometry g2 = l.getEnvelope(); 145 assertTrue(!g2.isEmpty()); 146 assertTrue(g2 instanceof Polygon); 147 148 Polygon poly = (Polygon) g2; 149 poly.normalize(); 150 assertEquals(5, poly.getExteriorRing().getNumPoints()); 151 assertEquals(new Coordinate(10, 10), poly.getExteriorRing().getCoordinateN( 152 0)); 153 assertEquals(new Coordinate(10, 40), poly.getExteriorRing().getCoordinateN( 154 1)); 155 assertEquals(new Coordinate(30, 40), poly.getExteriorRing().getCoordinateN( 156 2)); 157 assertEquals(new Coordinate(30, 10), poly.getExteriorRing().getCoordinateN( 158 3)); 159 assertEquals(new Coordinate(10, 10), poly.getExteriorRing().getCoordinateN( 160 4)); 161 } 162 testSetToNull()163 public void testSetToNull() throws Exception { 164 Envelope e1 = new Envelope(); 165 assertTrue(e1.isNull()); 166 e1.expandToInclude(5, 5); 167 assertTrue(!e1.isNull()); 168 e1.setToNull(); 169 assertTrue(e1.isNull()); 170 } 171 testEquals()172 public void testEquals() throws Exception { 173 Envelope e1 = new Envelope(1, 2, 3, 4); 174 Envelope e2 = new Envelope(1, 2, 3, 4); 175 assertEquals(e1, e2); 176 assertEquals(e1.hashCode(), e2.hashCode()); 177 178 Envelope e3 = new Envelope(1, 2, 3, 5); 179 assertTrue(!e1.equals(e3)); 180 assertTrue(e1.hashCode() != e3.hashCode()); 181 e1.setToNull(); 182 assertTrue(!e1.equals(e2)); 183 assertTrue(e1.hashCode() != e2.hashCode()); 184 e2.setToNull(); 185 assertEquals(e1, e2); 186 assertEquals(e1.hashCode(), e2.hashCode()); 187 } 188 testEquals2()189 public void testEquals2() { 190 assertTrue(new Envelope().equals(new Envelope())); 191 assertTrue(new Envelope(1, 2, 1, 2).equals(new Envelope(1, 2, 1, 2))); 192 assertTrue(!new Envelope(1, 2, 1.5, 2).equals(new Envelope(1, 2, 1, 2))); 193 } 194 testCopyConstructor()195 public void testCopyConstructor() throws Exception { 196 Envelope e1 = new Envelope(1, 2, 3, 4); 197 Envelope e2 = new Envelope(e1); 198 assertEquals(1, e2.getMinX(), 1E-5); 199 assertEquals(2, e2.getMaxX(), 1E-5); 200 assertEquals(3, e2.getMinY(), 1E-5); 201 assertEquals(4, e2.getMaxY(), 1E-5); 202 } 203 testCopy()204 public void testCopy() throws Exception { 205 Envelope e1 = new Envelope(1, 2, 3, 4); 206 Envelope e2 = e1.copy(); 207 assertEquals(1, e2.getMinX(), 1E-5); 208 assertEquals(2, e2.getMaxX(), 1E-5); 209 assertEquals(3, e2.getMinY(), 1E-5); 210 assertEquals(4, e2.getMaxY(), 1E-5); 211 212 Envelope eNull = new Envelope(); 213 Envelope eNullCopy = eNull.copy(); 214 assertTrue(eNullCopy.isNull()); 215 } 216 testGeometryFactoryCreateEnvelope()217 public void testGeometryFactoryCreateEnvelope() 218 throws Exception 219 { 220 checkExpectedEnvelopeGeometry("POINT (0 0)"); 221 checkExpectedEnvelopeGeometry("POINT (100 13)"); 222 checkExpectedEnvelopeGeometry("LINESTRING (0 0, 0 10)"); 223 checkExpectedEnvelopeGeometry("LINESTRING (0 0, 10 0)"); 224 225 String poly10 = "POLYGON ((0 10, 10 10, 10 0, 0 0, 0 10))"; 226 checkExpectedEnvelopeGeometry(poly10); 227 228 checkExpectedEnvelopeGeometry("LINESTRING (0 0, 10 10)", 229 poly10); 230 checkExpectedEnvelopeGeometry("POLYGON ((5 10, 10 6, 5 0, 0 6, 5 10))", 231 poly10); 232 } 233 testMetrics()234 public void testMetrics() { 235 Envelope env = new Envelope(0, 4, 0, 3); 236 assertEquals(env.getWidth(), 4.0); 237 assertEquals(env.getHeight(), 3.0); 238 assertEquals(env.getDiameter(), 5.0); 239 } 240 testEmptyMetrics()241 public void testEmptyMetrics() { 242 Envelope env = new Envelope(); 243 assertEquals(env.getWidth(), 0.0); 244 assertEquals(env.getHeight(), 0.0); 245 assertEquals(env.getDiameter(), 0.0); 246 } 247 checkIntersectsPermuted(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, boolean expected)248 private void checkIntersectsPermuted(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, boolean expected) { 249 checkIntersects(a1x, a1y, a2x, a2y, b1x, b1y, b2x, b2y, expected); 250 checkIntersects(a1x, a2y, a2x, a1y, b1x, b1y, b2x, b2y, expected); 251 checkIntersects(a1x, a1y, a2x, a2y, b1x, b2y, b2x, b1y, expected); 252 checkIntersects(a1x, a2y, a2x, a1y, b1x, b2y, b2x, b1y, expected); 253 } checkIntersects(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, boolean expected)254 private void checkIntersects(double a1x, double a1y, double a2x, double a2y, double b1x, double b1y, double b2x, double b2y, boolean expected) { 255 Envelope a = new Envelope(a1x, a2x, a1y, a2y); 256 Envelope b = new Envelope(b1x, b2x, b1y, b2y); 257 assertEquals(expected, a.intersects(b)); 258 assertEquals(expected, ! a.disjoint(b)); 259 260 Coordinate a1 = new Coordinate(a1x, a1y); 261 Coordinate a2 = new Coordinate(a2x, a2y); 262 Coordinate b1 = new Coordinate(b1x, b1y); 263 Coordinate b2 = new Coordinate(b2x, b2y); 264 assertEquals(expected, Envelope.intersects(a1, a2, b1, b2)); 265 266 assertEquals(expected, a.intersects(b1, b2)); 267 } 268 checkExpectedEnvelopeGeometry(String wktInput)269 void checkExpectedEnvelopeGeometry(String wktInput) 270 throws ParseException 271 { 272 checkExpectedEnvelopeGeometry(wktInput, wktInput); 273 } 274 checkExpectedEnvelopeGeometry(String wktInput, String wktEnvGeomExpected)275 void checkExpectedEnvelopeGeometry(String wktInput, String wktEnvGeomExpected) 276 throws ParseException 277 { 278 Geometry input = reader.read(wktInput); 279 Geometry envGeomExpected = reader.read(wktEnvGeomExpected); 280 281 Envelope env = input.getEnvelopeInternal(); 282 Geometry envGeomActual = geometryFactory.toGeometry(env); 283 boolean isEqual = envGeomActual.equalsNorm(envGeomExpected); 284 assertTrue(isEqual); 285 } 286 testCompareTo()287 public void testCompareTo() 288 { 289 checkCompareTo(0, new Envelope(), new Envelope()); 290 checkCompareTo(0, new Envelope(1,2, 1,2), new Envelope(1,2, 1,2)); 291 checkCompareTo(1, new Envelope(2,3, 1,2), new Envelope(1,2, 1,2)); 292 checkCompareTo(-1, new Envelope(1,2, 1,2), new Envelope(2,3, 1,2)); 293 checkCompareTo(1, new Envelope(1,2, 1,3), new Envelope(1,2, 1,2)); 294 checkCompareTo(1, new Envelope(2,3, 1,3), new Envelope(1,3, 1,2)); 295 } 296 checkCompareTo(int expected, Envelope env1, Envelope env2)297 public void checkCompareTo(int expected, Envelope env1, Envelope env2) 298 { 299 assertTrue(expected == env1.compareTo(env2)); 300 assertTrue(-expected == env2.compareTo(env1)); 301 } 302 } 303