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.io.oracle;
13 
14 import java.sql.SQLException;
15 
16 import org.locationtech.jts.generator.*;
17 import org.locationtech.jts.geom.*;
18 import org.locationtech.jts.io.oracle.OraReader;
19 import org.locationtech.jts.io.oracle.OraWriter;
20 
21 import oracle.sql.STRUCT;
22 
23 
24 /**
25  *
26  * Does round trip testing by creating the oracle object, then decoding it.
27  *
28  * These tests do not include insert / delete / select operations.
29  *
30  * NOTE: This test does require a precision to be used during the comparison,
31  * as points are rounded somewhat when creating the oracle struct.
32  * (One less decimal than a java double).
33  *
34  * NOTE: The points may be re-ordered during these tests.
35  *
36  * @author David Zwiers, Vivid Solutions.
37  */
38 public class StaticPolygonTest extends ConnectedTestCase {
39 
40 	/**
41 	 * @param arg
42 	 */
StaticPolygonTest(String arg)43 	public StaticPolygonTest(String arg) {
44 		super(arg);
45 	}
46 
47 	/**
48 	 * Round Trip test for a single polygon
49 	 * @throws SQLException
50 	 */
testSinglePolygonNoHoleRoundTrip()51 	public void testSinglePolygonNoHoleRoundTrip() throws SQLException{
52 		PolygonGenerator pg = new PolygonGenerator();
53 		pg.setGeometryFactory(geometryFactory);
54 		pg.setBoundingBox(new Envelope(0,10,0,10));
55 		pg.setNumberPoints(10);
56 
57 		Polygon pt = (Polygon) pg.create();
58 
59 		OraWriter ow = new OraWriter();
60 		STRUCT st = ow.write(pt, getConnection());
61 
62 		OraReader or = new OraReader();
63 		Polygon pt2 = (Polygon) or.read(st);
64 
65 //		System.out.println((pt==null?"NULL":pt.toString()));
66 //		System.out.println((pt2==null?"NULL":pt2.toString()));
67 		assertTrue("The input polygon is not the same as the output polygon",pt.equals(pt2));
68 	}
69 
70 	/**
71 	 * Round Trip test for a 100 non overlapping polygon
72 	 * @throws SQLException
73 	 */
testGridPolygonsNoHoleRoundTrip()74 	public void testGridPolygonsNoHoleRoundTrip() throws SQLException{
75 		GridGenerator grid = new GridGenerator();
76 		grid.setGeometryFactory(geometryFactory);
77 		grid.setBoundingBox(new Envelope(0,10,0,10));
78 		grid.setNumberColumns(10);
79 		grid.setNumberRows(10);
80 
81 		Polygon[] pt = new Polygon[100];
82 		STRUCT[] st = new STRUCT[100];
83 
84 		PolygonGenerator pg = new PolygonGenerator();
85 		pg.setGeometryFactory(geometryFactory);
86 		pg.setNumberPoints(10);
87 		OraWriter ow = new OraWriter();
88 
89 		int i=0;
90 		while(grid.canCreate() && i<100){
91 			pg.setBoundingBox(grid.createEnv());
92 			pt[i] = (Polygon) pg.create();
93 			st[i] = ow.write(pt[i], getConnection());
94 			i++;
95 		}
96 
97 		OraReader or = new OraReader();
98 		i=0;
99 		while(i<100 && pt[i] != null){
100 			Polygon pt2 = (Polygon) or.read(st[i]);
101 //			System.out.println((pt[i]==null?"NULL":pt[i].toString()));
102 //			System.out.println((pt2==null?"NULL":pt2.toString()));
103 			assertTrue("The input polygon is not the same as the output polygon",pt[i].equals(pt2));
104 			i++;
105 		}
106 	}
107 
108 	/**
109 	 * Round Trip test for a 8 overlapping line polygons (4 distinct polygons)
110 	 * @throws SQLException
111 	 */
testOverlappingPolygonsNoHoleRoundTrip()112 	public void testOverlappingPolygonsNoHoleRoundTrip() throws SQLException{
113 		GridGenerator grid = new GridGenerator();
114 		grid.setGeometryFactory(geometryFactory);
115 		grid.setBoundingBox(new Envelope(0,10,0,10));
116 		grid.setNumberColumns(2);
117 		grid.setNumberRows(2);
118 
119 		Polygon[] pt = new Polygon[4];
120 		STRUCT[] st = new STRUCT[8];
121 
122 		PolygonGenerator pg = new PolygonGenerator();
123 		pg.setGeometryFactory(geometryFactory);
124 		pg.setNumberPoints(10);
125 		OraWriter ow = new OraWriter();
126 
127 		int i=0;
128 		while(grid.canCreate() && i<8){
129 			pg.setBoundingBox(grid.createEnv());
130 			pt[i] = (Polygon) pg.create();
131 			st[i] = ow.write(pt[i], getConnection());
132 			i++;
133 		}
134 		for(int j=0;j<4;j++){
135 			if(pt[j]!=null)
136 				st[i++] = ow.write(pt[j], getConnection());
137 		}
138 
139 		OraReader or = new OraReader();
140 		i=0;
141 		while(i<8 && pt[i%4] != null){
142 			Polygon pt2 = (Polygon) or.read(st[i]);
143 //			System.out.println((pt==null?"NULL":pt[i%4].toString()));
144 //			System.out.println((pt2==null?"NULL":pt2.toString()));
145 			assertTrue("The input polygon is not the same as the output polygon",pt[i%4].equals(pt2));
146 			i++;
147 		}
148 	}
149 
150 	/**
151 	 * Round Trip test for a single polygon with lotsa points
152 	 * @throws SQLException
153 	 */
testSinglePolygonManyPointsNoHoleRoundTrip()154 	public void testSinglePolygonManyPointsNoHoleRoundTrip() throws SQLException{
155 		PolygonGenerator pg = new PolygonGenerator();
156 		pg.setGeometryFactory(geometryFactory);
157 		pg.setBoundingBox(new Envelope(0,10,0,10));
158 		pg.setGenerationAlgorithm(PolygonGenerator.BOX);
159 		pg.setNumberPoints(1000);
160 
161 		Polygon pt = (Polygon) pg.create();
162 //		System.out.println((pt==null?"NULL":pt.toString()));
163 
164 		OraWriter ow = new OraWriter();
165 		STRUCT st = ow.write(pt, getConnection());
166 
167 		OraReader or = new OraReader();
168 		Polygon pt2 = (Polygon) or.read(st);
169 
170 //		System.out.println((pt==null?"NULL":pt.toString()));
171 //		System.out.println((pt2==null?"NULL":pt2.toString()));
172 		assertTrue("The input polygon is not the same as the output polygon",pt.equals(pt2));
173 	}
174 
175 	/**
176 	 * Round Trip test for a single polygon
177 	 * @throws SQLException
178 	 */
testSinglePolygonHolesRoundTrip()179 	public void testSinglePolygonHolesRoundTrip() throws SQLException{
180 		PolygonGenerator pg = new PolygonGenerator();
181 		pg.setGeometryFactory(geometryFactory);
182 		pg.setBoundingBox(new Envelope(0,10,0,10));
183 		pg.setNumberPoints(10);
184 		pg.setNumberHoles(4);
185 
186 		Polygon pt = (Polygon) pg.create();
187 
188 		OraWriter ow = new OraWriter();
189 		STRUCT st = ow.write(pt, getConnection());
190 
191 		OraReader or = new OraReader();
192 		Polygon pt2 = (Polygon) or.read(st);
193 
194 //		System.out.println((pt==null?"NULL":pt.toString()));
195 //		System.out.println((pt2==null?"NULL":pt2.toString()));
196 		assertTrue("The input polygon is not the same as the output polygon",pt.equals(pt2));
197 	}
198 
199 	/**
200 	 * Round Trip test for a 100 non overlapping polygon
201 	 * @throws SQLException
202 	 */
testGridPolygonsHolesRoundTrip()203 	public void testGridPolygonsHolesRoundTrip() throws SQLException{
204 		GridGenerator grid = new GridGenerator();
205 		grid.setGeometryFactory(geometryFactory);
206 		grid.setBoundingBox(new Envelope(0,10,0,10));
207 		grid.setNumberColumns(10);
208 		grid.setNumberRows(10);
209 
210 		Polygon[] pt = new Polygon[100];
211 		STRUCT[] st = new STRUCT[100];
212 
213 		PolygonGenerator pg = new PolygonGenerator();
214 		pg.setGeometryFactory(geometryFactory);
215 		pg.setNumberPoints(10);
216 		pg.setNumberHoles(4);
217 		OraWriter ow = new OraWriter();
218 
219 		int i=0;
220 		while(grid.canCreate() && i<100){
221 			pg.setBoundingBox(grid.createEnv());
222 			pt[i] = (Polygon) pg.create();
223 			st[i] = ow.write(pt[i], getConnection());
224 			i++;
225 		}
226 
227 		OraReader or = new OraReader();
228 		i=0;
229 		while(i<100 && pt[i] != null){
230 			Polygon pt2 = (Polygon) or.read(st[i]);
231 //			System.out.println((pt[i]==null?"NULL":pt[i].toString()));
232 //			System.out.println((pt2==null?"NULL":pt2.toString()));
233 			assertTrue("The input polygon is not the same as the output polygon",pt[i].equals(pt2));
234 			i++;
235 		}
236 	}
237 
238 	/**
239 	 * Round Trip test for a 8 overlapping line polygons (4 distinct polygons)
240 	 * @throws SQLException
241 	 */
testOverlappingPolygonsHolesRoundTrip()242 	public void testOverlappingPolygonsHolesRoundTrip() throws SQLException{
243 		GridGenerator grid = new GridGenerator();
244 		grid.setGeometryFactory(geometryFactory);
245 		grid.setBoundingBox(new Envelope(0,10,0,10));
246 		grid.setNumberColumns(2);
247 		grid.setNumberRows(2);
248 
249 		Polygon[] pt = new Polygon[4];
250 		STRUCT[] st = new STRUCT[8];
251 
252 		PolygonGenerator pg = new PolygonGenerator();
253 		pg.setGeometryFactory(geometryFactory);
254 		pg.setNumberPoints(10);
255 		pg.setNumberHoles(4);
256 		OraWriter ow = new OraWriter();
257 
258 		int i=0;
259 		while(grid.canCreate() && i<8){
260 			pg.setBoundingBox(grid.createEnv());
261 			pt[i] = (Polygon) pg.create();
262 			st[i] = ow.write(pt[i], getConnection());
263 			i++;
264 		}
265 		for(int j=0;j<4;j++){
266 			if(pt[j]!=null)
267 				st[i++] = ow.write(pt[j], getConnection());
268 		}
269 
270 		OraReader or = new OraReader();
271 		i=0;
272 		while(i<8 && pt[i%4] != null){
273 			Polygon pt2 = (Polygon) or.read(st[i]);
274 //			System.out.println((pt==null?"NULL":pt[i%4].toString()));
275 //			System.out.println((pt2==null?"NULL":pt2.toString()));
276 			assertTrue("The input polygon is not the same as the output polygon",pt[i%4].equals(pt2));
277 			i++;
278 		}
279 	}
280 
281 	/**
282 	 * Round Trip test for a single polygon with lotsa points
283 	 * @throws SQLException
284 	 */
testSinglePolygonManyPointsHolesRoundTrip()285 	public void testSinglePolygonManyPointsHolesRoundTrip() throws SQLException{
286 		PolygonGenerator pg = new PolygonGenerator();
287 		pg.setGeometryFactory(geometryFactory);
288 		pg.setBoundingBox(new Envelope(0,10,0,10));
289 		pg.setGenerationAlgorithm(PolygonGenerator.BOX);
290 		pg.setNumberPoints(1000);
291 		pg.setNumberHoles(4);
292 
293 		Polygon pt = (Polygon) pg.create();
294 //		System.out.println((pt==null?"NULL":pt.toString()));
295 
296 		OraWriter ow = new OraWriter();
297 		STRUCT st = ow.write(pt, getConnection());
298 
299 		OraReader or = new OraReader();
300 		Polygon pt2 = (Polygon) or.read(st);
301 
302 //		System.out.println((pt==null?"NULL":pt.toString()));
303 //		System.out.println((pt2==null?"NULL":pt2.toString()));
304 		assertTrue("The input polygon is not the same as the output polygon",pt.equals(pt2));
305 	}
306 
307 	/**
308 	 * Round Trip test for a single polygon with lotsa points
309 	 * @throws SQLException
310 	 */
testSinglePolygonManyPointsManyHolesRoundTrip()311 	public void testSinglePolygonManyPointsManyHolesRoundTrip() throws SQLException{
312 		PolygonGenerator pg = new PolygonGenerator();
313 		pg.setGeometryFactory(geometryFactory);
314 		pg.setBoundingBox(new Envelope(0,10,0,10));
315 		pg.setGenerationAlgorithm(PolygonGenerator.BOX);
316 		pg.setNumberPoints(100);
317 		pg.setNumberHoles(100);
318 
319 		Polygon pt = (Polygon) pg.create();
320 //		System.out.println((pt==null?"NULL":pt.toString()));
321 
322 		OraWriter ow = new OraWriter();
323 		STRUCT st = ow.write(pt, getConnection());
324 
325 		OraReader or = new OraReader();
326 		Polygon pt2 = (Polygon) or.read(st);
327 
328 //		System.out.println((pt==null?"NULL":pt.toString()));
329 //		System.out.println((pt2==null?"NULL":pt2.toString()));
330 		assertTrue("The input polygon is not the same as the output polygon",pt.equals(pt2));
331 	}
332 }
333