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