1 |
1 |
package org.gvsig.raster.lib.buffer.impl;
|
2 |
2 |
|
|
3 |
import java.util.ArrayList;
|
|
4 |
import java.util.Arrays;
|
|
5 |
import java.util.List;
|
|
6 |
|
3 |
7 |
import org.gvsig.fmap.geom.Geometry;
|
4 |
8 |
import org.gvsig.fmap.geom.GeometryLocator;
|
5 |
9 |
import org.gvsig.fmap.geom.GeometryManager;
|
|
10 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
|
6 |
11 |
import org.gvsig.fmap.geom.exception.CreateGeometryException;
|
7 |
12 |
import org.gvsig.fmap.geom.operation.GeometryOperationException;
|
8 |
13 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
|
|
14 |
import org.gvsig.fmap.geom.primitive.Envelope;
|
|
15 |
import org.gvsig.fmap.geom.primitive.Line;
|
9 |
16 |
import org.gvsig.fmap.geom.primitive.Point;
|
10 |
17 |
import org.gvsig.fmap.geom.primitive.Polygon;
|
|
18 |
import org.gvsig.raster.lib.buffer.api.exceptions.GettingEnvelopeException;
|
|
19 |
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
|
|
20 |
import org.gvsig.tools.locator.LocatorException;
|
11 |
21 |
|
12 |
|
import junit.framework.TestCase;
|
|
22 |
public class DefaultVectorROITest extends AbstractLibraryAutoInitTestCase {
|
13 |
23 |
|
14 |
|
public class DefaultVectorROITest extends TestCase {
|
15 |
|
|
16 |
24 |
DefaultVectorROI testVectorROI;
|
17 |
25 |
|
18 |
|
private void init() {
|
|
26 |
@Override
|
|
27 |
protected void doSetUp() throws Exception {
|
19 |
28 |
testVectorROI = new DefaultVectorROI();
|
20 |
29 |
}
|
21 |
30 |
|
22 |
31 |
public void testDefaultVectorROI() {
|
23 |
|
init();
|
24 |
32 |
assertNotNull(testVectorROI);
|
25 |
33 |
}
|
26 |
34 |
|
27 |
35 |
public void testIsInsideIntInt() {
|
28 |
|
init();
|
29 |
|
GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
30 |
36 |
try {
|
31 |
37 |
// Returns false because is empty
|
32 |
|
assertEquals(false, testVectorROI.isInside(0, 0));
|
|
38 |
assertEquals(false, testVectorROI.isInside(10, 10));
|
33 |
39 |
|
34 |
|
Polygon polygon =
|
35 |
|
geomManager.createPolygon(Geometry.SUBTYPES.GEOM2D);
|
36 |
|
polygon.addVertex(0.0, 0.0);
|
37 |
|
polygon.addVertex(2.0, 0.0);
|
38 |
|
polygon.addVertex(2.0, 2.0);
|
39 |
|
polygon.addVertex(0.0, 2.0);
|
40 |
|
polygon.addVertex(0.0, 0.0);
|
41 |
|
testVectorROI.addGeometry(polygon);
|
|
40 |
List<Point> vertices = new ArrayList<Point>();
|
|
41 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
42 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
43 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
44 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
45 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
42 |
46 |
|
|
47 |
testVectorROI.addGeometry(this.createPolygon(vertices, SUBTYPES.GEOM2D));
|
|
48 |
|
43 |
49 |
// Returns false because point is outside
|
44 |
|
assertEquals(false, testVectorROI.isInside(5, 5));
|
|
50 |
assertEquals(false, testVectorROI.isInside(30, 30));
|
45 |
51 |
|
46 |
|
assertEquals(true, testVectorROI.isInside(1,1));
|
|
52 |
assertEquals(true, testVectorROI.isInside(5, 5));
|
47 |
53 |
|
48 |
|
} catch (CreateGeometryException
|
49 |
|
| GeometryOperationNotSupportedException
|
|
54 |
} catch (CreateGeometryException | GeometryOperationNotSupportedException
|
50 |
55 |
| GeometryOperationException e) {
|
51 |
56 |
fail("Can't create geometry");
|
52 |
57 |
}
|
53 |
58 |
}
|
54 |
59 |
|
55 |
60 |
public void testIsInsideGeometry() {
|
56 |
|
init();
|
57 |
|
GeometryManager geomManager = GeometryLocator.getGeometryManager();
|
58 |
61 |
try {
|
59 |
62 |
// Returns false because is empty
|
60 |
|
Point point=geomManager.createPoint(0.0, 0.0, Geometry.SUBTYPES.GEOM2D);
|
|
63 |
Point point = this.createPoint(10, 10, SUBTYPES.GEOM2D);
|
61 |
64 |
assertEquals(false, testVectorROI.isInside(point));
|
62 |
65 |
|
63 |
|
Polygon polygon =
|
64 |
|
geomManager.createPolygon(Geometry.SUBTYPES.GEOM2D);
|
65 |
|
polygon.addVertex(0.0, 0.0);
|
66 |
|
polygon.addVertex(2.0, 0.0);
|
67 |
|
polygon.addVertex(2.0, 2.0);
|
68 |
|
polygon.addVertex(0.0, 2.0);
|
69 |
|
polygon.addVertex(0.0, 0.0);
|
70 |
|
testVectorROI.addGeometry(polygon);
|
|
66 |
List<Point> vertices = new ArrayList<Point>();
|
|
67 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
68 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
69 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
70 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
71 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
71 |
72 |
|
|
73 |
testVectorROI.addGeometry(this.createPolygon(vertices, SUBTYPES.GEOM2D));
|
|
74 |
|
72 |
75 |
// Returns false because point is outside
|
73 |
|
point.setX(5.0);
|
74 |
|
point.setY(5.0);
|
|
76 |
point = this.createPoint(30, 40, SUBTYPES.GEOM2D);
|
75 |
77 |
assertEquals(false, testVectorROI.isInside(point));
|
76 |
78 |
|
77 |
|
point.setX(1.0);
|
78 |
|
point.setY(1.0);
|
|
79 |
point = this.createPoint(3, 3, SUBTYPES.GEOM2D);
|
79 |
80 |
assertEquals(true, testVectorROI.isInside(point));
|
80 |
81 |
|
81 |
|
} catch (CreateGeometryException
|
82 |
|
| GeometryOperationNotSupportedException
|
|
82 |
} catch (CreateGeometryException | GeometryOperationNotSupportedException
|
83 |
83 |
| GeometryOperationException e) {
|
84 |
84 |
fail("Can't create geometry");
|
85 |
85 |
}
|
86 |
86 |
}
|
87 |
87 |
|
88 |
88 |
public void testGetEnvelope() {
|
89 |
|
fail("Not yet implemented");
|
|
89 |
|
|
90 |
try {
|
|
91 |
assertNotNull(testVectorROI.getEnvelope());
|
|
92 |
} catch (GettingEnvelopeException e) {
|
|
93 |
fail("Can not get envelope of ROI");
|
|
94 |
}
|
|
95 |
|
|
96 |
Polygon polygon = null;
|
|
97 |
try {
|
|
98 |
List<Point> vertices = new ArrayList<Point>();
|
|
99 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
100 |
vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
|
|
101 |
vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
|
|
102 |
vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
|
|
103 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
104 |
polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
105 |
} catch (CreateGeometryException | LocatorException e) {
|
|
106 |
fail("Can not create geometry to test get envelope method");
|
|
107 |
}
|
|
108 |
|
|
109 |
testVectorROI.addGeometry(polygon);
|
|
110 |
|
|
111 |
try {
|
|
112 |
assertEquals(polygon.getEnvelope(), testVectorROI.getEnvelope());
|
|
113 |
} catch (GettingEnvelopeException e) {
|
|
114 |
fail("Can not get geometry manager to test get envelope method");
|
|
115 |
}
|
|
116 |
|
|
117 |
Line line = null;
|
|
118 |
try {
|
|
119 |
List<Point> vertices = new ArrayList<Point>();
|
|
120 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
121 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
122 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
123 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
124 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
125 |
line = this.createLine(vertices, SUBTYPES.GEOM2D);
|
|
126 |
} catch (CreateGeometryException | LocatorException e) {
|
|
127 |
fail("Can not create geometry to test get envelope method");
|
|
128 |
}
|
|
129 |
|
|
130 |
testVectorROI.addGeometry(line);
|
|
131 |
Envelope envelope = polygon.getEnvelope();
|
|
132 |
envelope.add(line);
|
|
133 |
try {
|
|
134 |
assertEquals(envelope, testVectorROI.getEnvelope());
|
|
135 |
} catch (GettingEnvelopeException e) {
|
|
136 |
fail("Can not create geometry to test get envelope method");
|
|
137 |
}
|
90 |
138 |
}
|
91 |
139 |
|
92 |
140 |
public void testAddGeometry() {
|
93 |
|
fail("Not yet implemented");
|
|
141 |
|
|
142 |
assertTrue(testVectorROI.getGeometries().size() == 0);
|
|
143 |
Polygon polygon = null;
|
|
144 |
try {
|
|
145 |
List<Point> vertices = new ArrayList<Point>();
|
|
146 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
147 |
vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
|
|
148 |
vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
|
|
149 |
vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
|
|
150 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
151 |
polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
152 |
} catch (CreateGeometryException | LocatorException e) {
|
|
153 |
fail("Can not create geometry to test add geometry method");
|
|
154 |
}
|
|
155 |
testVectorROI.addGeometry(polygon);
|
|
156 |
|
|
157 |
assertTrue(testVectorROI.getGeometries().size() == 1);
|
|
158 |
try {
|
|
159 |
assertTrue(Arrays.equals(polygon.convertToWKB(), testVectorROI.getGeometries().get(0)
|
|
160 |
.convertToWKB()));
|
|
161 |
} catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
|
|
162 |
fail("Can not convert geometry to WKB");
|
|
163 |
}
|
|
164 |
|
|
165 |
// Test add geometry 3D
|
|
166 |
Point point3D = null;
|
|
167 |
try {
|
|
168 |
point3D = this.createPoint(0, 0, SUBTYPES.GEOM3D);
|
|
169 |
} catch (CreateGeometryException e) {
|
|
170 |
fail("Can not create geometry to test add geometry method");
|
|
171 |
}
|
|
172 |
|
|
173 |
RuntimeException e = null;
|
|
174 |
try {
|
|
175 |
testVectorROI.addGeometry(point3D);
|
|
176 |
} catch (RuntimeException runtimeException) {
|
|
177 |
e = runtimeException;
|
|
178 |
}
|
|
179 |
assertNotNull(e);
|
94 |
180 |
}
|
95 |
181 |
|
96 |
182 |
public void testRemoveGeometry() {
|
97 |
|
fail("Not yet implemented");
|
|
183 |
|
|
184 |
Polygon polygon = null;
|
|
185 |
Polygon polygon2 = null;
|
|
186 |
try {
|
|
187 |
List<Point> vertices = new ArrayList<Point>();
|
|
188 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
189 |
vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
|
|
190 |
vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
|
|
191 |
vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
|
|
192 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
193 |
polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
194 |
|
|
195 |
vertices = new ArrayList<Point>();
|
|
196 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
197 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
198 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
199 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
200 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
201 |
polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
202 |
} catch (CreateGeometryException | LocatorException e) {
|
|
203 |
fail("Can not create geometry to test add geometry method");
|
|
204 |
}
|
|
205 |
testVectorROI.addGeometry(polygon);
|
|
206 |
testVectorROI.addGeometry(polygon2);
|
|
207 |
|
|
208 |
assertTrue(testVectorROI.getGeometryCount() == 2);
|
|
209 |
testVectorROI.removeGeometry(0);
|
|
210 |
assertTrue(testVectorROI.getGeometryCount() == 1);
|
|
211 |
testVectorROI.removeGeometry(0);
|
|
212 |
assertTrue(testVectorROI.getGeometryCount() == 0);
|
98 |
213 |
}
|
99 |
214 |
|
100 |
215 |
public void testGetGeometries() {
|
101 |
|
fail("Not yet implemented");
|
|
216 |
|
|
217 |
Polygon polygon = null;
|
|
218 |
Polygon polygon2 = null;
|
|
219 |
try {
|
|
220 |
List<Point> vertices = new ArrayList<Point>();
|
|
221 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
222 |
vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
|
|
223 |
vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
|
|
224 |
vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
|
|
225 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
226 |
polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
227 |
|
|
228 |
vertices = new ArrayList<Point>();
|
|
229 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
230 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
231 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
232 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
233 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
234 |
polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
235 |
} catch (CreateGeometryException | LocatorException e) {
|
|
236 |
fail("Can not create geometry to test add geometry method");
|
|
237 |
}
|
|
238 |
testVectorROI.addGeometry(polygon);
|
|
239 |
testVectorROI.addGeometry(polygon2);
|
|
240 |
|
|
241 |
List<Geometry> geometries = testVectorROI.getGeometries();
|
|
242 |
try {
|
|
243 |
assertTrue(Arrays.equals(polygon.convertToWKB(), geometries.get(0).convertToWKB()));
|
|
244 |
assertTrue(Arrays.equals(polygon2.convertToWKB(), geometries.get(1).convertToWKB()));
|
|
245 |
} catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
|
|
246 |
fail("Can not get WKB from geometries to compare them");
|
|
247 |
}
|
102 |
248 |
}
|
103 |
249 |
|
104 |
250 |
public void testGetGeometryCount() {
|
105 |
|
fail("Not yet implemented");
|
|
251 |
|
|
252 |
Polygon polygon = null;
|
|
253 |
Polygon polygon2 = null;
|
|
254 |
try {
|
|
255 |
List<Point> vertices = new ArrayList<Point>();
|
|
256 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
257 |
vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
|
|
258 |
vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
|
|
259 |
vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
|
|
260 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
261 |
polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
262 |
|
|
263 |
vertices = new ArrayList<Point>();
|
|
264 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
265 |
vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
|
|
266 |
vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
|
|
267 |
vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
|
|
268 |
vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
|
|
269 |
polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
|
|
270 |
} catch (CreateGeometryException | LocatorException e) {
|
|
271 |
fail("Can not create geometry to test add geometry method");
|
|
272 |
}
|
|
273 |
testVectorROI.addGeometry(polygon);
|
|
274 |
testVectorROI.addGeometry(polygon2);
|
|
275 |
|
|
276 |
assertTrue(testVectorROI.getGeometryCount() == 2);
|
106 |
277 |
}
|
107 |
278 |
|
|
279 |
private Polygon createPolygon(List<Point> points, int subtype) throws CreateGeometryException {
|
|
280 |
Polygon polygon = null;
|
|
281 |
polygon = GeometryLocator.getGeometryManager().createPolygon(subtype);
|
|
282 |
for (Point point : points) {
|
|
283 |
polygon.addVertex(point.getX(), point.getY());
|
|
284 |
}
|
|
285 |
return polygon;
|
|
286 |
}
|
|
287 |
|
|
288 |
private Line createLine(List<Point> points, int subtype) throws CreateGeometryException {
|
|
289 |
Line line = null;
|
|
290 |
line = GeometryLocator.getGeometryManager().createLine(subtype);
|
|
291 |
for (Point point : points) {
|
|
292 |
line.addVertex(point.getX(), point.getY());
|
|
293 |
}
|
|
294 |
return line;
|
|
295 |
}
|
|
296 |
|
|
297 |
private Point createPoint(double x, double y, int subtype) throws CreateGeometryException {
|
|
298 |
Point point = null;
|
|
299 |
point = GeometryLocator.getGeometryManager().createPoint(x, y, subtype);
|
|
300 |
return point;
|
|
301 |
}
|
108 |
302 |
}
|