Statistics
| Revision:

svn-gvsig-desktop / branches / org.gvsig.desktop-2018a / org.gvsig.desktop.library / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer.impl / src / test / java / org / gvsig / raster / lib / buffer / impl / DefaultVectorROITest.java @ 43803

History | View | Annotate | Download (12.5 KB)

1
package org.gvsig.raster.lib.buffer.impl;
2

    
3
import java.lang.reflect.Array;
4
import java.util.ArrayList;
5
import java.util.Arrays;
6
import java.util.List;
7

    
8
import org.gvsig.fmap.geom.Geometry;
9
import org.gvsig.fmap.geom.GeometryLocator;
10
import org.gvsig.fmap.geom.GeometryManager;
11
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
12
import org.gvsig.fmap.geom.exception.CreateGeometryException;
13
import org.gvsig.fmap.geom.operation.GeometryOperationException;
14
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
15
import org.gvsig.fmap.geom.primitive.Envelope;
16
import org.gvsig.fmap.geom.primitive.Line;
17
import org.gvsig.fmap.geom.primitive.Point;
18
import org.gvsig.fmap.geom.primitive.Polygon;
19
import org.gvsig.raster.lib.buffer.api.exceptions.GettingEnvelopeException;
20
import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase;
21
import org.gvsig.tools.locator.LocatorException;
22

    
23
public class DefaultVectorROITest extends AbstractLibraryAutoInitTestCase {
24

    
25
    DefaultVectorROI testVectorROI;
26

    
27
    @Override
28
    protected void doSetUp() throws Exception {
29
        testVectorROI = new DefaultVectorROI();
30
    }
31

    
32
    public void testDefaultVectorROI() {
33
        assertNotNull(testVectorROI);
34
    }
35

    
36
    public void testIsInsideIntInt() {
37
        try {
38
            // Returns false because is empty
39
            assertEquals(false, testVectorROI.isInside(10, 10));
40

    
41
            List<Point> vertices = new ArrayList<Point>();
42
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
43
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
44
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
45
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
46
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
47

    
48
            testVectorROI.addGeometry(this.createPolygon(vertices, SUBTYPES.GEOM2D));
49

    
50
            // Returns false because point is outside
51
            assertEquals(false, testVectorROI.isInside(30, 30));
52

    
53
            assertEquals(true, testVectorROI.isInside(5, 5));
54

    
55
        } catch (CreateGeometryException | GeometryOperationNotSupportedException
56
            | GeometryOperationException e) {
57
            fail("Can't create geometry");
58
        }
59
    }
60

    
61
    public void testIsInsideGeometry() {
62
        try {
63
            // Returns false because is empty
64
            Point point = this.createPoint(10, 10, SUBTYPES.GEOM2D);
65
            assertEquals(false, testVectorROI.isInside(point));
66

    
67
            List<Point> vertices = new ArrayList<Point>();
68
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
69
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
70
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
71
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
72
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
73

    
74
            testVectorROI.addGeometry(this.createPolygon(vertices, SUBTYPES.GEOM2D));
75

    
76
            // Returns false because point is outside
77
            point = this.createPoint(30, 40, SUBTYPES.GEOM2D);
78
            assertEquals(false, testVectorROI.isInside(point));
79

    
80
            point = this.createPoint(3, 3, SUBTYPES.GEOM2D);
81
            assertEquals(true, testVectorROI.isInside(point));
82

    
83
        } catch (CreateGeometryException | GeometryOperationNotSupportedException
84
            | GeometryOperationException e) {
85
            fail("Can't create geometry");
86
        }
87
    }
88

    
89
    public void testGetEnvelope() {
90

    
91
        try {
92
            assertNotNull(testVectorROI.getEnvelope());
93
        } catch (GettingEnvelopeException e) {
94
            fail("Can not get envelope of ROI");
95
        }
96

    
97
        Polygon polygon = null;
98
        try {
99
            List<Point> vertices = new ArrayList<Point>();
100
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
101
            vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
102
            vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
103
            vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
104
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
105
            polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
106
        } catch (CreateGeometryException | LocatorException e) {
107
            fail("Can not create geometry to test get envelope method");
108
        }
109

    
110
        testVectorROI.addGeometry(polygon);
111

    
112
        try {
113
            assertEquals(polygon.getEnvelope(), testVectorROI.getEnvelope());
114
        } catch (GettingEnvelopeException e) {
115
            fail("Can not get geometry manager to test get envelope method");
116
        }
117

    
118
        Line line = null;
119
        try {
120
            List<Point> vertices = new ArrayList<Point>();
121
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
122
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
123
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
124
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
125
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
126
            line = this.createLine(vertices, SUBTYPES.GEOM2D);
127
        } catch (CreateGeometryException | LocatorException e) {
128
            fail("Can not create geometry to test get envelope method");
129
        }
130

    
131
        testVectorROI.addGeometry(line);
132
        Envelope envelope = polygon.getEnvelope();
133
        envelope.add(line);
134
        try {
135
            assertEquals(envelope, testVectorROI.getEnvelope());
136
        } catch (GettingEnvelopeException e) {
137
            fail("Can not create geometry to test get envelope method");
138
        }
139
    }
140

    
141
    public void testAddGeometry() {
142

    
143
        assertTrue(testVectorROI.getGeometries().size() == 0);
144
        Polygon polygon = null;
145
        try {
146
            List<Point> vertices = new ArrayList<Point>();
147
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
148
            vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
149
            vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
150
            vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
151
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
152
            polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
153
        } catch (CreateGeometryException | LocatorException e) {
154
            fail("Can not create geometry to test add geometry method");
155
        }
156
        testVectorROI.addGeometry(polygon);
157

    
158
        assertTrue(testVectorROI.getGeometries().size() == 1);
159
        try {
160
            assertEquals(polygon.convertToWKT(), testVectorROI.getGeometries().get(0)
161
                .convertToWKT());
162
        } catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
163
            fail("Can not convert geometry to WKB");
164
        }
165

    
166
        // Test add geometry 3D
167
        Point point3D = null;
168
        try {
169
            point3D = this.createPoint(0, 0, SUBTYPES.GEOM3D);
170
        } catch (CreateGeometryException e) {
171
            fail("Can not create geometry to test add geometry method");
172
        }
173

    
174
        RuntimeException e = null;
175
        try {
176
            testVectorROI.addGeometry(point3D);
177
        } catch (RuntimeException runtimeException) {
178
            e = runtimeException;
179
        }
180
        assertNotNull(e);
181
    }
182

    
183
    public void testRemoveGeometry() {
184

    
185
        Polygon polygon = null;
186
        Polygon polygon2 = null;
187
        try {
188
            List<Point> vertices = new ArrayList<Point>();
189
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
190
            vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
191
            vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
192
            vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
193
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
194
            polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
195

    
196
            vertices = new ArrayList<Point>();
197
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
198
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
199
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
200
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
201
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
202
            polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
203
        } catch (CreateGeometryException | LocatorException e) {
204
            fail("Can not create geometry to test add geometry method");
205
        }
206
        testVectorROI.addGeometry(polygon);
207
        testVectorROI.addGeometry(polygon2);
208

    
209
        assertTrue(testVectorROI.getGeometryCount() == 2);
210
        testVectorROI.removeGeometry(0);
211
        assertTrue(testVectorROI.getGeometryCount() == 1);
212
        testVectorROI.removeGeometry(0);
213
        assertTrue(testVectorROI.getGeometryCount() == 0);
214
    }
215

    
216
    public void testGetGeometries() {
217

    
218
        Polygon polygon = null;
219
        Polygon polygon2 = null;
220
        try {
221
            List<Point> vertices = new ArrayList<Point>();
222
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
223
            vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
224
            vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
225
            vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
226
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
227
            polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
228

    
229
            vertices = new ArrayList<Point>();
230
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
231
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
232
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
233
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
234
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
235
            polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
236
        } catch (CreateGeometryException | LocatorException e) {
237
            fail("Can not create geometry to test add geometry method");
238
        }
239
        testVectorROI.addGeometry(polygon);
240
        testVectorROI.addGeometry(polygon2);
241

    
242
        List<Geometry> geometries = testVectorROI.getGeometries();
243
        try {
244
            assertEquals(polygon.convertToWKT(), geometries.get(0).convertToWKT());
245
            assertEquals(polygon2.convertToWKT(), geometries.get(1).convertToWKT());
246
        } catch (GeometryOperationNotSupportedException | GeometryOperationException e) {
247
            e.printStackTrace();
248
            fail("Can not get WKB from geometries to compare them");
249
        }
250
    }
251

    
252
    public void testGetGeometryCount() {
253
        
254
        Polygon polygon = null;
255
        Polygon polygon2 = null;
256
        try {
257
            List<Point> vertices = new ArrayList<Point>();
258
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
259
            vertices.add(this.createPoint(2, 0, SUBTYPES.GEOM2D));
260
            vertices.add(this.createPoint(2, 2, SUBTYPES.GEOM2D));
261
            vertices.add(this.createPoint(0, 2, SUBTYPES.GEOM2D));
262
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
263
            polygon = this.createPolygon(vertices, SUBTYPES.GEOM2D);
264

    
265
            vertices = new ArrayList<Point>();
266
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
267
            vertices.add(this.createPoint(10, 0, SUBTYPES.GEOM2D));
268
            vertices.add(this.createPoint(10, 10, SUBTYPES.GEOM2D));
269
            vertices.add(this.createPoint(0, 10, SUBTYPES.GEOM2D));
270
            vertices.add(this.createPoint(0, 0, SUBTYPES.GEOM2D));
271
            polygon2 = this.createPolygon(vertices, SUBTYPES.GEOM2D);
272
        } catch (CreateGeometryException | LocatorException e) {
273
            fail("Can not create geometry to test add geometry method");
274
        }
275
        testVectorROI.addGeometry(polygon);
276
        testVectorROI.addGeometry(polygon2);
277
        
278
        assertTrue(testVectorROI.getGeometryCount() == 2);
279
    }
280

    
281
    private Polygon createPolygon(List<Point> points, int subtype) throws CreateGeometryException {
282
        Polygon polygon = null;
283
        polygon = GeometryLocator.getGeometryManager().createPolygon(subtype);
284
        for (Point point : points) {
285
            polygon.addVertex(point.getX(), point.getY());
286
        }
287
        return polygon;
288
    }
289

    
290
    private Line createLine(List<Point> points, int subtype) throws CreateGeometryException {
291
        Line line = null;
292
        line = GeometryLocator.getGeometryManager().createLine(subtype);
293
        for (Point point : points) {
294
            line.addVertex(point.getX(), point.getY());
295
        }
296
        return line;
297
    }
298

    
299
    private Point createPoint(double x, double y, int subtype) throws CreateGeometryException {
300
        Point point = null;
301
        point = GeometryLocator.getGeometryManager().createPoint(x, y, subtype);
302
        return point;
303
    }
304
}