root / branches / v2_0_0_prep / libraries / libFMap_geometries / src-test / org / gvsig / fmap / geom / GeometryManagerTest.java @ 30323
History | View | Annotate | Download (10.3 KB)
1 |
package org.gvsig.fmap.geom; |
---|---|
2 |
|
3 |
import junit.framework.TestCase; |
4 |
|
5 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
6 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
7 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
8 |
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary; |
9 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
10 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
11 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
12 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
13 |
import org.gvsig.fmap.geom.operation.ensureOrientation.EnsureOrientation; |
14 |
import org.gvsig.fmap.geom.operation.flip.Flip; |
15 |
import org.gvsig.fmap.geom.operation.impl.DefaultGeometryOperationLibrary; |
16 |
import org.gvsig.fmap.geom.operation.isCCW.IsCCW; |
17 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
18 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
19 |
import org.gvsig.fmap.geom.primitive.Arc; |
20 |
import org.gvsig.fmap.geom.primitive.Point; |
21 |
import org.gvsig.fmap.geom.primitive.impl.Arc2D; |
22 |
import org.gvsig.fmap.geom.type.GeometryType; |
23 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
24 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
25 |
import org.slf4j.Logger; |
26 |
import org.slf4j.LoggerFactory; |
27 |
|
28 |
public class GeometryManagerTest extends TestCase { |
29 |
|
30 |
final static private Logger logger = LoggerFactory.getLogger("org.gvsig"); |
31 |
|
32 |
static {
|
33 |
//logger.addAppender(new ConsoleAppender(new SimpleLayout()));
|
34 |
} |
35 |
|
36 |
private GeometryManager manager;
|
37 |
|
38 |
protected void setUp() throws Exception { |
39 |
super.setUp();
|
40 |
|
41 |
DefaultGeometryLibrary lib = new DefaultGeometryLibrary();
|
42 |
lib.initialize(); |
43 |
lib.postInitialize(); |
44 |
|
45 |
DefaultGeometryOperationLibrary opLib = new DefaultGeometryOperationLibrary();
|
46 |
opLib.initialize(); |
47 |
opLib.postInitialize(); |
48 |
|
49 |
manager = GeometryLocator.getGeometryManager(); |
50 |
} |
51 |
|
52 |
public void testRegister() throws Exception { |
53 |
logger.debug("--------- testRegister() START ----------");
|
54 |
|
55 |
Point p2d = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
56 |
assertNotNull(p2d); |
57 |
p2d.setCoordinateAt(0, 0); |
58 |
p2d.setCoordinateAt(1, 0); |
59 |
|
60 |
java.awt.geom.Point2D pt1 = new java.awt.geom.Point2D.Float(0,1); |
61 |
java.awt.geom.Point2D pt2 = new java.awt.geom.Point2D.Float(1,1); |
62 |
java.awt.geom.Point2D pt3 = new java.awt.geom.Point2D.Float(1,0); |
63 |
Arc2D c2d = (Arc2D) manager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
64 |
assertNotNull(c2d); |
65 |
|
66 |
GeometryType geomType1 = manager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM2D); |
67 |
GeometryType geomType2 = manager.getGeometryType(TYPES.ARC, SUBTYPES.GEOM2D); |
68 |
|
69 |
assertEquals(p2d.getGeometryType(), geomType1); |
70 |
assertEquals(c2d.getGeometryType(), geomType2); |
71 |
|
72 |
// int op1 = manager.registerGeometryOperation("toJTS", new ToJTS(), geomType1);
|
73 |
// logger.debug("op1=" + op1);
|
74 |
//
|
75 |
// int op2 = -1;
|
76 |
// try {
|
77 |
// op2 = manager.registerGeometryOperation("toJTS", new ToJTS(), geomType1);
|
78 |
// logger.debug("op2=" + op2);
|
79 |
// } catch (Exception e) {
|
80 |
// logger.error("Error registrando operaci?n: ", e);
|
81 |
// }
|
82 |
|
83 |
int op3 = manager.registerGeometryOperation("toWKB", new ToWKB(),geomType1); |
84 |
int op4 = manager.registerGeometryOperation("toWKB", new ToWKB(),geomType2); |
85 |
|
86 |
logger.debug("op3=" + op3);
|
87 |
|
88 |
// assertTrue("FALLO: op1 != op2", op1 == op2);
|
89 |
// assertTrue("FALLO: op1 == op3", op1 != op3);
|
90 |
// assertFalse("FALLO: op4 == op", op4 == op1);
|
91 |
|
92 |
//Point p = (Point) factory.createPoint2D(1, 2);
|
93 |
Point p = (Point) manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
94 |
|
95 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
96 |
ctx.setAttribute("dummy", "DummyValue"); |
97 |
|
98 |
try {
|
99 |
p.invokeOperation(ToWKB.CODE, ctx); |
100 |
p.invokeOperation(ToJTS.CODE, ctx); |
101 |
p.invokeOperation(Flip.CODE, ctx); |
102 |
} catch (Exception e) { |
103 |
logger.error("Error, ", e);
|
104 |
} |
105 |
|
106 |
Geometry sol = manager.create(TYPES.SOLID, SUBTYPES.GEOM2DZ); |
107 |
assertNotNull(sol); |
108 |
//logger.debug(sol);
|
109 |
|
110 |
logger.debug("--------- testRegister() END ----------");
|
111 |
} |
112 |
|
113 |
public void testGeneralPathXOperations() throws InstantiationException, IllegalAccessException, CreateGeometryException{ |
114 |
logger.debug("--------- Test GeneralPathX Operation START ----------");
|
115 |
|
116 |
//Registering the operation Flip() to all the geometries...
|
117 |
manager.registerGeometryOperation("flip", new Flip()); |
118 |
|
119 |
//Registering the operation ensureOrientation() to all the geometries...
|
120 |
manager.registerGeometryOperation("ensureOrientation", new EnsureOrientation()); |
121 |
|
122 |
//Registering the operation isCCW() to all the geometries...
|
123 |
manager.registerGeometryOperation("isCCW", new IsCCW()); |
124 |
|
125 |
//Building the Points for a curve...
|
126 |
// java.awt.geom.Point2D p1 = new java.awt.geom.Point2D.Double(1,2);
|
127 |
// java.awt.geom.Point2D p2 = new java.awt.geom.Point2D.Double(2,1);
|
128 |
// java.awt.geom.Point2D p3 = new java.awt.geom.Point2D.Double(3,3);
|
129 |
|
130 |
Point p1 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
131 |
p1.setCoordinateAt(0, 1); |
132 |
p1.setCoordinateAt(1, 2); |
133 |
|
134 |
Point p2 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
135 |
p2.setCoordinateAt(0, 2); |
136 |
p2.setCoordinateAt(1, 1); |
137 |
|
138 |
Point p3 = (Point)manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
139 |
p3.setCoordinateAt(0, 3); |
140 |
p3.setCoordinateAt(1, 3); |
141 |
|
142 |
//Build a curve to get the operation invoked with the registered operation.
|
143 |
//Geometry curve = (Curve) factory.createArc(p1, p2, p3);
|
144 |
Arc arc = (Arc) manager.create(TYPES.ARC, SUBTYPES.GEOM2D); |
145 |
arc.setPoints(p1, p2, p3); |
146 |
|
147 |
try {
|
148 |
arc.invokeOperation(Flip.CODE, null);
|
149 |
} catch (GeometryOperationNotSupportedException e) {
|
150 |
logger.error("Operation doesn't be registered for this geometry. \n", e);
|
151 |
} catch (GeometryOperationException e) {
|
152 |
logger.error("An error produced while the Operation was running. \n", e);
|
153 |
} |
154 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
155 |
ctx.setAttribute("bCCW",new Boolean(true)); |
156 |
Boolean aux1 = null; |
157 |
try {
|
158 |
aux1 = (Boolean) arc.invokeOperation(EnsureOrientation.CODE, ctx);
|
159 |
} catch (GeometryOperationNotSupportedException e) {
|
160 |
logger.error("Operation doesn't be registered for this geometry. \n", e);
|
161 |
} catch (GeometryOperationException e) {
|
162 |
// TODO Auto-generated catch block
|
163 |
logger.error("An error produced while the Operation was running. \n", e);
|
164 |
} |
165 |
//True si es exterior, le ha dado la vuelta.
|
166 |
assertTrue(aux1.booleanValue()); |
167 |
|
168 |
Boolean aux2 = null; |
169 |
try {
|
170 |
aux2 = (Boolean) arc.invokeOperation(IsCCW.CODE, null); |
171 |
} catch (GeometryOperationNotSupportedException e) {
|
172 |
logger.error("Operation doesn't be registered for this geometry. \n", e);
|
173 |
} catch (GeometryOperationException e) {
|
174 |
// TODO Auto-generated catch block
|
175 |
logger.error("An error produced while the Operation was running. \n", e);
|
176 |
} |
177 |
//True si es CCW.
|
178 |
assertTrue(aux2.booleanValue()); |
179 |
|
180 |
logger.debug("--------- Test GeneralPathX Operation END ----------");
|
181 |
} |
182 |
|
183 |
public void testInvoke() throws InstantiationException, IllegalAccessException, CreateGeometryException { |
184 |
int size = 100000; |
185 |
|
186 |
// Fill the operation context with required params
|
187 |
GeometryOperationContext ctx = new GeometryOperationContext();
|
188 |
GeometryOperationContext var = new GeometryOperationContext();
|
189 |
var.setAttribute("bCCW",new Boolean(true)); |
190 |
|
191 |
logger.debug("ToJTS.-");
|
192 |
indirectInvoke(ToJTS.CODE, ctx, size); |
193 |
directInvoke(ToJTS.CODE, ctx, size); |
194 |
logger.debug("ToWKB.-");
|
195 |
indirectInvoke(ToWKB.CODE, null, size);
|
196 |
directInvoke(ToWKB.CODE, null, size);
|
197 |
logger.debug("Flip.-");
|
198 |
indirectInvoke(Flip.CODE, ctx, size); |
199 |
directInvoke(Flip.CODE, ctx, size); |
200 |
logger.debug("EnsureOrientation.-");
|
201 |
//indirectInvoke(EnsureOrientation.CODE, var, size);
|
202 |
directInvoke(EnsureOrientation.CODE, var, size); |
203 |
logger.debug("isCCW.-");
|
204 |
//indirectInvoke(IsCCW.CODE, ctx, size);
|
205 |
directInvoke(IsCCW.CODE, ctx, size); |
206 |
|
207 |
} |
208 |
|
209 |
private Geometry[] createGeometryArray(int size) throws InstantiationException, IllegalAccessException, CreateGeometryException { |
210 |
Geometry[] geom = new Geometry[size]; |
211 |
for (int i = 0; i < size; i++) { |
212 |
//geom[i] = factory.createPoint2D(Math.log(1 - i), Math.log(i + 1));
|
213 |
geom[i] = manager.create(TYPES.POINT, SUBTYPES.GEOM2D); |
214 |
} |
215 |
return geom;
|
216 |
} |
217 |
|
218 |
private long directInvoke(int opCode, GeometryOperationContext ctx, int size) throws InstantiationException, IllegalAccessException, CreateGeometryException { |
219 |
Geometry[] geoms = createGeometryArray(size);
|
220 |
|
221 |
long t0 = System.currentTimeMillis(); |
222 |
|
223 |
GeometryOperation geomOp = null;
|
224 |
try {
|
225 |
geomOp = manager.getGeometryOperation(ToJTS.CODE, TYPES.POINT, SUBTYPES.GEOM2D); |
226 |
} catch (GeometryTypeNotSupportedException gtnse) {
|
227 |
logger.error("Error:", gtnse);
|
228 |
} catch (GeometryOperationNotSupportedException gonse) {
|
229 |
logger.error("Error:", gonse);
|
230 |
} catch (GeometryTypeNotValidException e) {
|
231 |
logger.error("Error:", e);
|
232 |
} |
233 |
|
234 |
// Here is the main loop where you call the operation
|
235 |
try {
|
236 |
for (int i = 0; i < geoms.length; i++) { |
237 |
Object result = geomOp.invoke(geoms[i], ctx);
|
238 |
// if(i%100 == 99) logger.debug(result);
|
239 |
} |
240 |
|
241 |
} catch (GeometryOperationException goe) {
|
242 |
logger.error("Error:", goe);
|
243 |
} |
244 |
long t1 = System.currentTimeMillis(); |
245 |
long t = t1 - t0;
|
246 |
logger.debug("Milliseconds (direct): " + t);
|
247 |
return t;
|
248 |
} |
249 |
|
250 |
private long indirectInvoke(int opCode, GeometryOperationContext ctx, |
251 |
int size) throws InstantiationException, IllegalAccessException, CreateGeometryException { |
252 |
Geometry[] geoms = createGeometryArray(size);
|
253 |
|
254 |
long t0 = System.currentTimeMillis(); |
255 |
|
256 |
// Here is the main loop where you call the operation
|
257 |
try {
|
258 |
for (int i = 0; i < geoms.length; i++) { |
259 |
Object result = (geoms[i]).invokeOperation(opCode,
|
260 |
ctx); |
261 |
// if(i%100 == 99) logger.debug(result);
|
262 |
} |
263 |
} catch (GeometryOperationNotSupportedException gonse) {
|
264 |
logger.error("Error:", gonse);
|
265 |
} catch (GeometryOperationException goe) {
|
266 |
logger.error("Error:", goe);
|
267 |
} |
268 |
long t1 = System.currentTimeMillis(); |
269 |
long t = t1 - t0;
|
270 |
logger.debug("Milliseconds (indirect): " + t);
|
271 |
return t;
|
272 |
} |
273 |
|
274 |
// public void testRegisterBasicGeometryType() {
|
275 |
// GeometryType gt = GeometryLocator.getGeometryManager()
|
276 |
// .registerGeometryType(DummyPoint2D.class, "DummyPoint2D", TYPES.ELLIPSE, SUBTYPES.GEOM2DZ);
|
277 |
//
|
278 |
// logger.debug(String.valueOf(gt.getType()));
|
279 |
//
|
280 |
// GeometryLocator.getGeometryManager().unregisterGeometryType(DummyPoint2D.class);
|
281 |
//
|
282 |
// gt = GeometryLocator.getGeometryManager()
|
283 |
// .registerGeometryType(DummyPoint2D.class, "DummyPoint2D", TYPES.POINT, SUBTYPES.GEOM2D);
|
284 |
//
|
285 |
// logger.debug(String.valueOf(gt.getType()));
|
286 |
// GeometryLocator.getGeometryManager().unregisterGeometryType(
|
287 |
// DummyPoint2D.class);
|
288 |
//
|
289 |
// }
|
290 |
} |