root / tags / v2_0_0_Build_2056 / libraries / libFMap_geometries / src-test / org / gvsig / fmap / geom / primitive / EnvelopeTest.java @ 39021
History | View | Annotate | Download (7.07 KB)
1 | 27313 | jmvivo | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | *
|
||
21 | */
|
||
22 | |||
23 | /*
|
||
24 | * AUTHORS (In addition to CIT):
|
||
25 | * 2009 {Iver T.I.} {Task}
|
||
26 | */
|
||
27 | |||
28 | package org.gvsig.fmap.geom.primitive; |
||
29 | |||
30 | import junit.framework.Assert; |
||
31 | |||
32 | import org.gvsig.fmap.geom.GeometryLocator; |
||
33 | import org.gvsig.fmap.geom.GeometryManager; |
||
34 | import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
||
35 | import org.gvsig.fmap.geom.Geometry.TYPES; |
||
36 | 27397 | jpiera | import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
37 | import org.gvsig.fmap.geom.exception.CreateGeometryException; |
||
38 | 33388 | jpiera | import org.gvsig.tools.ToolsLocator; |
39 | import org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase; |
||
40 | import org.gvsig.tools.locator.LocatorException; |
||
41 | import org.gvsig.tools.persistence.PersistentState; |
||
42 | import org.gvsig.tools.persistence.exception.PersistenceClassNotRegistered; |
||
43 | import org.gvsig.tools.persistence.exception.PersistenceException; |
||
44 | import org.gvsig.tools.persistence.exception.PersistenceTypeNotSupportedException; |
||
45 | import org.gvsig.tools.persistence.exception.PersistenceValidateExceptions; |
||
46 | 27313 | jmvivo | import org.slf4j.Logger; |
47 | import org.slf4j.LoggerFactory; |
||
48 | |||
49 | /**
|
||
50 | * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
|
||
51 | */
|
||
52 | 33388 | jpiera | public class EnvelopeTest extends AbstractLibraryAutoInitTestCase { |
53 | 27313 | jmvivo | private GeometryManager manager;
|
54 | |||
55 | final static private Logger logger = LoggerFactory.getLogger("org.gvsig"); |
||
56 | |||
57 | 33388 | jpiera | protected void doSetUp() throws Exception { |
58 | manager = GeometryLocator.getGeometryManager(); |
||
59 | 27313 | jmvivo | } |
60 | |||
61 | public void testEnvelope2D() throws InstantiationException, |
||
62 | 27397 | jpiera | IllegalAccessException, CreateEnvelopeException, CreateGeometryException {
|
63 | 27313 | jmvivo | |
64 | // Create
|
||
65 | Envelope env = createEnvelope2D(0, 1, 2, 3); |
||
66 | checkEnvelope2D(env, 0, 1, 2, 3, "Create"); |
||
67 | |||
68 | Envelope env2 = createEnvelope2D(1, 2, 3, 4); |
||
69 | checkEnvelope2D(env2, 1, 2, 3, 4, "Create 2"); |
||
70 | |||
71 | // Add
|
||
72 | env.add(env2); |
||
73 | checkEnvelope2D(env, 0, 1, 3, 4, "Add"); |
||
74 | |||
75 | // Contains
|
||
76 | assertTrue("Contains", env.contains(env2));
|
||
77 | env2 = createEnvelope2D(1, 1, 2, 2); |
||
78 | assertTrue("Contains 2", env.contains(env2));
|
||
79 | env2 = createEnvelope2D(1, 1, 6, 6); |
||
80 | assertFalse("Contains 3", env.contains(env2));
|
||
81 | env2 = createEnvelope2D(-1, -1, -6, -6); |
||
82 | assertFalse("Contains 4", env.contains(env2));
|
||
83 | |||
84 | // Intersets
|
||
85 | env2 = createEnvelope2D(1, 1, 2, 2); |
||
86 | assertTrue("Intersets", env.intersects(env2));
|
||
87 | env2 = createEnvelope2D(-1, -1, 2, 2); |
||
88 | assertTrue("Intersets 2", env.intersects(env2));
|
||
89 | env2 = createEnvelope2D(1, 1, 6, 6); |
||
90 | assertTrue("Intersets 3", env.intersects(env2));
|
||
91 | env2 = createEnvelope2D(-5, -5, 6, 6); |
||
92 | assertTrue("Intersets 3", env.intersects(env2));
|
||
93 | env2 = createEnvelope2D(-1, -1, -6, -6); |
||
94 | assertFalse("Intersets 4", env.intersects(env2));
|
||
95 | env2 = createEnvelope2D(5, 5, 9, 9); |
||
96 | assertFalse("Intersets 4", env.intersects(env2));
|
||
97 | } |
||
98 | |||
99 | private Envelope createEnvelope2D(double minx, double miny, double maxx, |
||
100 | 27397 | jpiera | double maxy) throws InstantiationException, IllegalAccessException, CreateEnvelopeException, CreateGeometryException { |
101 | 27313 | jmvivo | Envelope envelope = manager.createEnvelope(SUBTYPES.GEOM2D); |
102 | Point min = (Point) manager.create(TYPES.POINT,SUBTYPES.GEOM2D); |
||
103 | Point max = (Point) manager.create(TYPES.POINT,SUBTYPES.GEOM2D); |
||
104 | min.setX(minx); |
||
105 | min.setY(miny); |
||
106 | |||
107 | max.setX(maxx); |
||
108 | max.setY(maxy); |
||
109 | envelope.setLowerCorner(min); |
||
110 | envelope.setUpperCorner(max); |
||
111 | |||
112 | return envelope;
|
||
113 | } |
||
114 | |||
115 | private void checkEnvelope2D(Envelope env, double minx, double miny, |
||
116 | double maxx, double maxy, String info) { |
||
117 | |||
118 | Assert.assertTrue(info + ": minx : expected " + minx + " but has " |
||
119 | + env.getMinimum(0), env.getMinimum(0) - minx < 0.000000001); |
||
120 | Assert.assertTrue(info + ": miny : expected " + miny + " but has " |
||
121 | + env.getMinimum(1), env.getMinimum(1) - miny < 0.000000001); |
||
122 | Assert.assertTrue(info + ": maxx : expected " + maxx + " but has " |
||
123 | + env.getMaximum(0), env.getMaximum(0) - maxx < 0.000000001); |
||
124 | Assert.assertTrue(info + ": maxy : expected " + maxy + " but has " |
||
125 | + env.getMaximum(1), env.getMaximum(1) - maxy < 0.000000001); |
||
126 | |||
127 | Assert.assertTrue(info + ": lower conrner x : expected " + minx
|
||
128 | + "but has " + env.getLowerCorner().getX(),
|
||
129 | env.getLowerCorner().getX() - minx < 0.000000001);
|
||
130 | Assert.assertTrue(info + ": lower conrner y : expected " + miny
|
||
131 | + "but has " + env.getLowerCorner().getY(),
|
||
132 | env.getLowerCorner().getY() - miny < 0.000000001);
|
||
133 | Assert.assertTrue(info + ": upper conrner x : expected " + maxx
|
||
134 | + "but has " + env.getUpperCorner().getX(),
|
||
135 | env.getUpperCorner().getX() - maxx < 0.000000001);
|
||
136 | Assert.assertTrue(info + ": upper conrner y : expected " + maxy
|
||
137 | + "but has " + env.getUpperCorner().getY(),
|
||
138 | env.getUpperCorner().getY() - maxy < 0.000000001);
|
||
139 | |||
140 | double lengthx = 0, lengthy = 0; |
||
141 | double midx = 0, midy = 0; |
||
142 | try {
|
||
143 | lengthx = maxx - minx; |
||
144 | midx = ( lengthx/ 2) + minx;
|
||
145 | } catch (Exception e) { |
||
146 | // Nonthing to do
|
||
147 | } |
||
148 | try {
|
||
149 | lengthy = maxy - miny; |
||
150 | midy = (lengthy / 2) + miny;
|
||
151 | } catch (Exception e) { |
||
152 | // Nonthing to do
|
||
153 | } |
||
154 | |||
155 | Assert.assertTrue(info + ": center x : expected " + midx + "but has " |
||
156 | + env.getCenter(0),
|
||
157 | env.getCenter(0) - midx < 0.000000001); |
||
158 | Assert.assertTrue(info + ": center y : expected " + midy + "but has " |
||
159 | + env.getCenter(1), env.getCenter(1) - midy < 0.000000001); |
||
160 | |||
161 | Assert.assertTrue(info + ": length x : expected " + lengthx
|
||
162 | + "but has " + env.getLength(0), |
||
163 | env.getLength(0) - lengthx < 0.000000001); |
||
164 | Assert.assertTrue(info + ": length y : expected " + lengthy
|
||
165 | + "but has " + env.getLength(1), |
||
166 | env.getLength(1) - lengthy < 0.000000001); |
||
167 | 33388 | jpiera | } |
168 | |||
169 | public void testPersistenceEnvelope2D() throws CreateGeometryException, CreateEnvelopeException, InstantiationException, IllegalAccessException, PersistenceTypeNotSupportedException, PersistenceClassNotRegistered, PersistenceException, PersistenceValidateExceptions, LocatorException{ |
||
170 | Envelope envelope1 = createEnvelope2D(0, 1, 2, 3); |
||
171 | |||
172 | PersistentState persistentState = ToolsLocator.getPersistenceManager().getState(envelope1); |
||
173 | Envelope envelope2 = (Envelope) ToolsLocator.getPersistenceManager().create(persistentState); |
||
174 | |||
175 | Assert.assertEquals(envelope1.getLowerCorner().getX(), envelope2.getLowerCorner().getX(), 0);
|
||
176 | Assert.assertEquals(envelope1.getLowerCorner().getY(), envelope2.getLowerCorner().getY(), 0);
|
||
177 | Assert.assertEquals(envelope1.getUpperCorner().getX(), envelope2.getUpperCorner().getX(), 0);
|
||
178 | Assert.assertEquals(envelope1.getUpperCorner().getY(), envelope2.getUpperCorner().getY(), 0);
|
||
179 | 27313 | jmvivo | } |
180 | 33388 | jpiera | } |
181 | 27313 | jmvivo |