Revision 21308 branches/v2_0_0_prep/libraries/libFMap_geometries/src-test/org/gvsig/fmap/geom/GeometryManagerTest.java
GeometryManagerTest.java | ||
---|---|---|
1 | 1 |
package org.gvsig.fmap.geom; |
2 | 2 |
|
3 |
|
|
4 | 3 |
import junit.framework.TestCase; |
5 | 4 |
|
6 | 5 |
import org.apache.log4j.ConsoleAppender; |
7 | 6 |
import org.apache.log4j.Logger; |
8 | 7 |
import org.apache.log4j.SimpleLayout; |
8 |
import org.gvsig.fmap.geom.operation.GeometryOperation; |
|
9 | 9 |
import org.gvsig.fmap.geom.operation.GeometryOperationContext; |
10 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
|
11 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
|
10 | 12 |
import org.gvsig.fmap.geom.operation.tojts.ToJTS; |
11 | 13 |
import org.gvsig.fmap.geom.operation.towkb.ToWKB; |
12 | 14 |
import org.gvsig.fmap.geom.primitive.Curve2D; |
... | ... | |
14 | 16 |
import org.gvsig.fmap.geom.primitive.Point2D; |
15 | 17 |
import org.gvsig.fmap.geom.primitive.Solid; |
16 | 18 |
import org.gvsig.fmap.geom.type.GeometryType; |
19 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
|
17 | 20 |
|
18 |
|
|
19 | 21 |
public class GeometryManagerTest extends TestCase { |
20 | 22 |
|
21 | 23 |
private static Logger logger = Logger.getLogger("org.gvsig"); |
22 |
|
|
24 |
|
|
23 | 25 |
static { |
24 |
|
|
26 |
|
|
25 | 27 |
logger.addAppender(new ConsoleAppender(new SimpleLayout())); |
26 |
|
|
28 |
|
|
27 | 29 |
} |
28 |
|
|
29 |
private GeometryManager gm; |
|
30 |
|
|
30 |
|
|
31 |
private GeometryManager manager; |
|
32 |
private GeometryFactory factory; |
|
33 |
|
|
31 | 34 |
protected void setUp() throws Exception { |
32 | 35 |
super.setUp(); |
33 |
|
|
34 |
gm = GeometryManager.getInstance(); |
|
36 |
|
|
37 |
manager = GeometryManager.getInstance(); |
|
38 |
factory = manager.getGeometryFactory(); |
|
35 | 39 |
} |
36 |
|
|
37 |
public void testRegister() { |
|
38 |
|
|
39 |
logger.debug("--------- testRegister() START ----------");
|
|
40 |
|
|
41 |
GeometryType geomType1 = gm.registerGeometryType(Point2D.class);
|
|
42 |
GeometryType geomType2 = gm.registerGeometryType(Curve2D.class);
|
|
43 |
|
|
44 |
|
|
45 |
int op = gm.registerGeometryOperation("toJTS", new ToJTS(), geomType1);
|
|
46 |
logger.debug("op="+op);
|
|
47 |
|
|
40 |
|
|
41 |
public void testRegister() throws Exception {
|
|
42 |
|
|
43 |
logger.debug("--------- testRegister() START ----------"); |
|
44 |
|
|
45 |
GeometryType geomType1 = manager.registerGeometryType(Point2D.class);
|
|
46 |
GeometryType geomType2 = manager.registerGeometryType(Curve2D.class);
|
|
47 |
|
|
48 |
int op = manager.registerGeometryOperation("toJTS", new ToJTS(), |
|
49 |
geomType1);
|
|
50 |
logger.debug("op=" + op);
|
|
51 |
|
|
48 | 52 |
int op2 = -1; |
49 |
try { |
|
50 |
op2 = gm.registerGeometryOperation("toJTS", ToJTS.class, geomType1); |
|
51 |
logger.debug("op2="+op2); |
|
53 |
try { |
|
54 |
op2 = manager.registerGeometryOperation("toJTS", ToJTS.class, |
|
55 |
geomType1); |
|
56 |
logger.debug("op2=" + op2); |
|
52 | 57 |
} catch (Exception e) { |
53 |
logger.error("Error registrando operaciรณn: ", e);
|
|
58 |
logger.error("Error registrando operaci?n: ", e);
|
|
54 | 59 |
} |
55 |
|
|
56 |
int op3 = gm.registerGeometryOperation("toWKB", new ToWKB(), geomType1);
|
|
57 |
|
|
58 |
int op4 = gm.registerGeometryOperation("toWKB", new ToWKB(), geomType2); |
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
logger.debug("op3="+op3);
|
|
63 |
|
|
60 |
|
|
61 |
int op3 = manager.registerGeometryOperation("toWKB", new ToWKB(),
|
|
62 |
geomType1); |
|
63 |
|
|
64 |
int op4 = manager.registerGeometryOperation("toWKB", new ToWKB(), |
|
65 |
geomType2); |
|
66 |
|
|
67 |
logger.debug("op3=" + op3);
|
|
68 |
|
|
64 | 69 |
assertTrue("FALLO: op != op2", op == op2); |
65 | 70 |
assertTrue("FALLO: op == op3", op != op3); |
66 | 71 |
assertFalse("FALLO: op4 == op", op4 == op); |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
Point p = (Point) gm.getGeometryFactory().createPoint2D(1, 2); |
|
71 | 72 |
|
72 |
|
|
73 |
Point p = (Point) factory.createPoint2D(1, 2); |
|
74 |
|
|
73 | 75 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
74 | 76 |
ctx.setAttribute("dummy", "DummyValue"); |
75 |
|
|
77 |
|
|
76 | 78 |
try { |
77 | 79 |
p.invokeOperation(ToWKB.CODE, ctx); |
78 | 80 |
p.invokeOperation(ToJTS.CODE, ctx); |
79 | 81 |
} catch (Exception e) { |
80 | 82 |
logger.error("Error, ", e); |
81 | 83 |
} |
82 |
|
|
83 |
|
|
84 |
|
|
84 |
|
|
85 | 85 |
// Registramos una factory |
86 |
|
|
87 |
gm.registerGeometryFactory(new MyFactory());
|
|
88 |
MyFactory fact = (MyFactory) gm.getGeometryFactory();
|
|
89 |
Solid sol = fact.createTorus3D(0,1); |
|
86 |
|
|
87 |
manager.registerGeometryFactory(new MyFactory());
|
|
88 |
MyFactory fact = (MyFactory) manager.getGeometryFactory();
|
|
89 |
Solid sol = fact.createTorus3D(0, 1);
|
|
90 | 90 |
assertNotNull(sol); |
91 | 91 |
logger.debug(sol); |
92 |
manager.registerGeometryFactory(factory); |
|
92 | 93 |
logger.debug("--------- testRegister() END ----------"); |
93 | 94 |
} |
94 | 95 |
|
96 |
public void testInvoke() { |
|
97 |
int size = 1000000; |
|
98 |
|
|
99 |
// Fill the operation context with required params |
|
100 |
GeometryOperationContext ctx = new GeometryOperationContext(); |
|
101 |
|
|
102 |
logger.debug("ToJTS.-"); |
|
103 |
indirectInvoke(ToJTS.CODE, ctx, size); |
|
104 |
directInvoke(ToJTS.CODE, ctx, size); |
|
105 |
logger.debug("ToWKB.-"); |
|
106 |
indirectInvoke(ToWKB.CODE, ctx, size); |
|
107 |
directInvoke(ToWKB.CODE, ctx, size); |
|
108 |
|
|
109 |
} |
|
110 |
|
|
111 |
private Geometry[] createGeometryArray(int size) { |
|
112 |
Geometry[] geom = new Geometry[size]; |
|
113 |
for (int i = 0; i < size; i++) { |
|
114 |
geom[i] = factory.createPoint2D(Math.log(1 - i), Math.log(i + 1)); |
|
115 |
} |
|
116 |
return geom; |
|
117 |
} |
|
118 |
|
|
119 |
private long directInvoke(int opCode, GeometryOperationContext ctx, int size) { |
|
120 |
Geometry[] geoms = createGeometryArray(size); |
|
121 |
|
|
122 |
long t0 = System.currentTimeMillis(); |
|
123 |
|
|
124 |
GeometryOperation geomOp = null; |
|
125 |
try { |
|
126 |
geomOp = manager.getGeometryOperation(Point2D.class, ToJTS.CODE); |
|
127 |
} catch (GeometryTypeNotSupportedException gtnse) { |
|
128 |
logger.error("Error:", gtnse); |
|
129 |
} catch (GeometryOperationNotSupportedException gonse) { |
|
130 |
logger.error("Error:", gonse); |
|
131 |
} |
|
132 |
|
|
133 |
// Here is the main loop where you call the operation |
|
134 |
try { |
|
135 |
for (int i = 0; i < geoms.length; i++) { |
|
136 |
Object result = geomOp.invoke(geoms[i], ctx); |
|
137 |
// if(i%100 == 99) logger.debug(result); |
|
138 |
} |
|
139 |
|
|
140 |
} catch (GeometryOperationException goe) { |
|
141 |
logger.error("Error:", goe); |
|
142 |
} |
|
143 |
long t1 = System.currentTimeMillis(); |
|
144 |
long t = t1 - t0; |
|
145 |
logger.debug("Milliseconds (direct): " + t); |
|
146 |
return t; |
|
147 |
} |
|
148 |
|
|
149 |
private long indirectInvoke(int opCode, GeometryOperationContext ctx, |
|
150 |
int size) { |
|
151 |
Geometry[] geoms = createGeometryArray(size); |
|
152 |
|
|
153 |
long t0 = System.currentTimeMillis(); |
|
154 |
|
|
155 |
// Here is the main loop where you call the operation |
|
156 |
try { |
|
157 |
for (int i = 0; i < geoms.length; i++) { |
|
158 |
Object result = ((Geometry) geoms[i]).invokeOperation(opCode, |
|
159 |
ctx); |
|
160 |
// if(i%100 == 99) logger.debug(result); |
|
161 |
} |
|
162 |
} catch (GeometryOperationNotSupportedException gonse) { |
|
163 |
logger.error("Error:", gonse); |
|
164 |
} catch (GeometryOperationException goe) { |
|
165 |
logger.error("Error:", goe); |
|
166 |
} |
|
167 |
long t1 = System.currentTimeMillis(); |
|
168 |
long t = t1 - t0; |
|
169 |
logger.debug("Milliseconds (indirect): " + t); |
|
170 |
return t; |
|
171 |
} |
|
95 | 172 |
} |
Also available in: Unified diff