Revision 21573
branches/Mobile_Compatible_Hito_1/libFMap_dataFile/src-test/org/gvsig/data/datastores/vectorial/FilterTest.java | ||
---|---|---|
1 |
/** |
|
2 |
* |
|
3 |
*/ |
|
4 |
package org.gvsig.data.datastores.vectorial; |
|
5 |
|
|
6 |
import java.io.File; |
|
7 |
import java.util.HashSet; |
|
8 |
import java.util.Iterator; |
|
9 |
import java.util.Set; |
|
10 |
|
|
11 |
import org.gvsig.data.CloseException; |
|
12 |
import org.gvsig.data.DataManager; |
|
13 |
import org.gvsig.data.DataStoreParameters; |
|
14 |
import org.gvsig.data.InitializeException; |
|
15 |
import org.gvsig.data.OpenException; |
|
16 |
import org.gvsig.data.ReadException; |
|
17 |
import org.gvsig.data.datastores.vectorial.file.DataStoreTest; |
|
18 |
import org.gvsig.data.datastores.vectorial.file.shp.Register; |
|
19 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStore; |
|
20 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPStoreParameters; |
|
21 |
import org.gvsig.data.datastores.vectorial.file.shp.SHPTest; |
|
22 |
import org.gvsig.data.datastores.vectorial.file.shp.ShpFeatureID; |
|
23 |
import org.gvsig.data.vectorial.Feature; |
|
24 |
import org.gvsig.data.vectorial.FeatureAttributeDescriptor; |
|
25 |
import org.gvsig.data.vectorial.FeatureCollection; |
|
26 |
import org.gvsig.data.vectorial.FeatureType; |
|
27 |
import org.gvsig.data.vectorial.filter.FilterFactory2Impl; |
|
28 |
import org.opengis.filter.Filter; |
|
29 |
import org.opengis.filter.FilterFactory2; |
|
30 |
import org.opengis.filter.expression.Expression; |
|
31 |
import org.opengis.filter.sort.SortBy; |
|
32 |
import org.opengis.filter.sort.SortOrder; |
|
33 |
|
|
34 |
import junit.framework.TestCase; |
|
35 |
|
|
36 |
/** |
|
37 |
* @author jcarras |
|
38 |
* |
|
39 |
*/ |
|
40 |
public class FilterTest extends TestCase { |
|
41 |
|
|
42 |
private File fileNormal = new File(DataStoreTest.class.getResource("data/pruebaFiltros.shp").getFile()); |
|
43 |
SHPStore store; |
|
44 |
|
|
45 |
public static void main(String[] args) { |
|
46 |
junit.textui.TestRunner.run(FilterTest.class); |
|
47 |
} |
|
48 |
|
|
49 |
protected void setUp() throws Exception { |
|
50 |
super.setUp(); |
|
51 |
} |
|
52 |
|
|
53 |
public void initialize() { |
|
54 |
Register.selfRegister(); |
|
55 |
DataManager manager = DataManager.getManager(); |
|
56 |
SHPStoreParameters shpParameters=null; |
|
57 |
shpParameters=(SHPStoreParameters)manager.createDataStoreParameters(SHPStore.DATASTORE_NAME); |
|
58 |
shpParameters.setFile(fileNormal); |
|
59 |
// shpParameters.setDBFFile(SHP.getDbfFile(file)); |
|
60 |
// shpParameters.setSHXFile(SHP.getShxFile(file)); |
|
61 |
|
|
62 |
try { |
|
63 |
store= (SHPStore)manager.createDataStore((DataStoreParameters)shpParameters); |
|
64 |
} catch (InitializeException e) { |
|
65 |
// TODO Auto-generated catch block |
|
66 |
e.printStackTrace(); |
|
67 |
} |
|
68 |
} |
|
69 |
|
|
70 |
public void testOrder(){ |
|
71 |
System.out.println("testOrder"); |
|
72 |
initialize(); |
|
73 |
SortBy[] order = new SortBy[2]; |
|
74 |
order[0]=new FilterFactory2Impl().sort("TIPO", SortOrder.ASCENDING); |
|
75 |
order[1]=new FilterFactory2Impl().sort("AREA", SortOrder.ASCENDING); |
|
76 |
assertEquals(9,testshp(null, order)); |
|
77 |
} |
|
78 |
|
|
79 |
public void testGT(){ |
|
80 |
System.out.println("testGT"); |
|
81 |
initialize(); |
|
82 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
83 |
Expression exp1 = fact.property("AREA"); |
|
84 |
Expression exp2 = fact.literal(30000000); |
|
85 |
Filter filter = fact.greater(exp1, exp2); |
|
86 |
assertEquals(4,testshp(filter, null)); |
|
87 |
} |
|
88 |
|
|
89 |
public void testGT_string(){ |
|
90 |
System.out.println("testGT_string"); |
|
91 |
initialize(); |
|
92 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
93 |
Expression exp1 = fact.property("NOMBRE"); |
|
94 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
95 |
Filter filter = fact.greater(exp1, exp2); |
|
96 |
assertEquals(2,testshp(filter, null)); |
|
97 |
} |
|
98 |
|
|
99 |
public void testGTE(){ |
|
100 |
System.out.println("testGTE"); |
|
101 |
initialize(); |
|
102 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
103 |
Expression exp1 = fact.property("NOMBRE"); |
|
104 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
105 |
Filter filter = fact.greaterOrEqual(exp1, exp2); |
|
106 |
assertEquals(3,testshp(filter, null)); |
|
107 |
} |
|
108 |
|
|
109 |
public void testLT(){ |
|
110 |
System.out.println("testLT"); |
|
111 |
initialize(); |
|
112 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
113 |
Expression exp1 = fact.property("AREA"); |
|
114 |
Expression exp2 = fact.literal(30000000); |
|
115 |
Filter filter = fact.less(exp1, exp2); |
|
116 |
assertEquals(5,testshp(filter, null)); |
|
117 |
} |
|
118 |
|
|
119 |
public void testLTE(){ |
|
120 |
System.out.println("testLTE"); |
|
121 |
initialize(); |
|
122 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
123 |
Expression exp1 = fact.property("NOMBRE"); |
|
124 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
125 |
Filter filter = fact.lessOrEqual(exp1, exp2); |
|
126 |
assertEquals(7,testshp(filter, null)); |
|
127 |
} |
|
128 |
|
|
129 |
public void testEquals(){ |
|
130 |
System.out.println("testEquals"); |
|
131 |
initialize(); |
|
132 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
133 |
Expression exp1 = fact.property("INE"); |
|
134 |
Expression exp2 = fact.literal(3125); |
|
135 |
Filter filter = fact.equals(exp1, exp2); |
|
136 |
assertEquals(1,testshp(filter, null)); |
|
137 |
} |
|
138 |
|
|
139 |
public void testEquals_String(){ |
|
140 |
System.out.println("testEquals"); |
|
141 |
initialize(); |
|
142 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
143 |
Expression exp1 = fact.property("NOMBRE"); |
|
144 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
145 |
Filter filter = fact.equals(exp1, exp2); |
|
146 |
assertEquals(1,testshp(filter, null)); |
|
147 |
} |
|
148 |
|
|
149 |
public void testNotEquals(){ |
|
150 |
System.out.println("testNotEquals"); |
|
151 |
initialize(); |
|
152 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
153 |
Expression exp1 = fact.property("NOMBRE"); |
|
154 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
155 |
Filter filter = fact.notEqual(exp1, exp2,true); |
|
156 |
assertEquals(8,testshp(filter, null)); |
|
157 |
} |
|
158 |
|
|
159 |
public void testNot(){ |
|
160 |
System.out.println("testNot"); |
|
161 |
initialize(); |
|
162 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
163 |
Expression exp1 = fact.property("NOMBRE"); |
|
164 |
Expression exp2 = fact.literal("PEDREGUER"); |
|
165 |
Filter filter = fact.equals(exp1, exp2); |
|
166 |
filter = fact.not(filter); |
|
167 |
assertEquals(8,testshp(filter, null)); |
|
168 |
} |
|
169 |
|
|
170 |
/** |
|
171 |
* Current shp driver fails when using a shapefile |
|
172 |
* with null values |
|
173 |
*/ |
|
174 |
public void testIsNull(){ |
|
175 |
System.out.println("testIsNull"); |
|
176 |
initialize(); |
|
177 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
178 |
Expression exp1 = fact.property("OTRO_IDIOM"); |
|
179 |
Filter filter = fact.not(fact.isNull(exp1)); |
|
180 |
assertEquals(7,testshp(filter, null)); |
|
181 |
} |
|
182 |
|
|
183 |
public void testBetween(){ |
|
184 |
System.out.println("Between"); |
|
185 |
initialize(); |
|
186 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
187 |
Expression exp1 = fact.literal(25000000.0); |
|
188 |
Expression exp3 = fact.literal(40000000); |
|
189 |
Expression exp2 = fact.property("AREA"); |
|
190 |
Filter filter = fact.between(exp1, exp2, exp3); |
|
191 |
assertEquals(2,testshp(filter, null)); |
|
192 |
} |
|
193 |
|
|
194 |
public void testLike(){ |
|
195 |
System.out.println("testLike"); |
|
196 |
initialize(); |
|
197 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
198 |
Expression exp1 = fact.property("NOMBRE"); |
|
199 |
Filter filter = fact.like(exp1, "%I_A%"); |
|
200 |
assertEquals(2,testshp(filter, null)); |
|
201 |
} |
|
202 |
|
|
203 |
public void testFId(){ |
|
204 |
System.out.println("testId"); |
|
205 |
initialize(); |
|
206 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
207 |
HashSet ids = new HashSet(); |
|
208 |
ids.add(new ShpFeatureID(store,3)); |
|
209 |
ids.add(new ShpFeatureID(store,6)); |
|
210 |
Filter filter = fact.id(ids); |
|
211 |
assertEquals(2,testshp(filter, null)); |
|
212 |
} |
|
213 |
|
|
214 |
public void testBBOX(){ |
|
215 |
System.out.println("testBBOX"); |
|
216 |
initialize(); |
|
217 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
218 |
Expression exp1 = fact.property("GEOMETRY"); |
|
219 |
Filter filter = fact.bbox(exp1, 769009, 4291393, 772960, 4293701, null); |
|
220 |
assertEquals(4,testshp(filter, null)); |
|
221 |
} |
|
222 |
|
|
223 |
public void testAnd(){ |
|
224 |
System.out.println("testAnd"); |
|
225 |
initialize(); |
|
226 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
227 |
Expression exp1 = fact.property("GEOMETRY"); |
|
228 |
Filter filter1 = fact.bbox(exp1, 769009, 4291393, 772960, 4293701, null); |
|
229 |
Expression exp2 = fact.property("NOMBRE"); |
|
230 |
Filter filter2 = fact.like(exp2, "B%"); |
|
231 |
Filter filter = fact.and(filter1, filter2); |
|
232 |
assertEquals(1,testshp(filter, null)); |
|
233 |
} |
|
234 |
|
|
235 |
public void testOr(){ |
|
236 |
System.out.println("testOr"); |
|
237 |
initialize(); |
|
238 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
239 |
|
|
240 |
Expression exp1 = fact.property("GEOMETRY"); |
|
241 |
Filter filter1 = fact.bbox(exp1, 769009, 4291393, 772960, 4293701, null); |
|
242 |
|
|
243 |
Expression exp2 = fact.property("NOMBRE"); |
|
244 |
Filter filter2 = fact.like(exp2, "B%"); |
|
245 |
|
|
246 |
Filter filter = fact.or(filter1, filter2); |
|
247 |
|
|
248 |
assertEquals(5,testshp(filter, null)); |
|
249 |
} |
|
250 |
|
|
251 |
/** |
|
252 |
* area + 3e7 > 6e7 --> area > 3e7 |
|
253 |
*/ |
|
254 |
public void testAdd(){ |
|
255 |
System.out.println("testAdd"); |
|
256 |
initialize(); |
|
257 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
258 |
Expression exp1 = fact.property("AREA"); |
|
259 |
Expression exp2 = fact.literal(30000000); |
|
260 |
Expression exp3 = fact.literal(60000000); |
|
261 |
Filter filter = fact.greater(fact.add(exp1, exp2),exp3); |
|
262 |
assertEquals(4,testshp(filter, null)); |
|
263 |
} |
|
264 |
|
|
265 |
/** |
|
266 |
* area - 3e7 > 0 --> area > 3e7 |
|
267 |
*/ |
|
268 |
public void testSubstract(){ |
|
269 |
System.out.println("testSubstract"); |
|
270 |
initialize(); |
|
271 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
272 |
Expression exp1 = fact.property("AREA"); |
|
273 |
Expression exp2 = fact.literal(30000000); |
|
274 |
Expression exp3 = fact.literal(0); |
|
275 |
Filter filter = fact.greater(fact.subtract(exp1, exp2),exp3); |
|
276 |
assertEquals(4,testshp(filter, null)); |
|
277 |
} |
|
278 |
|
|
279 |
/** |
|
280 |
* area * 2 > 6e7 --> area > 3e7 |
|
281 |
*/ |
|
282 |
public void testMultiply(){ |
|
283 |
System.out.println("testMultiply"); |
|
284 |
initialize(); |
|
285 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
286 |
Expression exp1 = fact.property("AREA"); |
|
287 |
Expression exp2 = fact.literal(2); |
|
288 |
Expression exp3 = fact.literal(60000000); |
|
289 |
Filter filter = fact.greater(fact.multiply(exp1, exp2),exp3); |
|
290 |
assertEquals(4,testshp(filter, null)); |
|
291 |
} |
|
292 |
|
|
293 |
/** |
|
294 |
* area / 2 > 1.5e7 --> area > 3e7 |
|
295 |
*/ |
|
296 |
public void testDivide(){ |
|
297 |
System.out.println("testDivide"); |
|
298 |
initialize(); |
|
299 |
FilterFactory2 fact = new FilterFactory2Impl(); |
|
300 |
Expression exp1 = fact.property("AREA"); |
|
301 |
Expression exp2 = fact.literal(2); |
|
302 |
Expression exp3 = fact.literal(15000000); |
|
303 |
Filter filter = fact.greater(fact.divide(exp1, exp2),exp3); |
|
304 |
assertEquals(4,testshp(filter, null)); |
|
305 |
} |
|
306 |
|
|
307 |
private int testshp(Filter filter, SortBy[] order){ |
|
308 |
|
|
309 |
try { |
|
310 |
store.open(); |
|
311 |
} catch (OpenException e2) { |
|
312 |
// TODO Auto-generated catch block |
|
313 |
e2.printStackTrace(); |
|
314 |
} |
|
315 |
|
|
316 |
FeatureType ft=store.getDefaultFeatureType(); |
|
317 |
FeatureCollection featureCollection=null; |
|
318 |
|
|
319 |
try { |
|
320 |
featureCollection = (FeatureCollection)store.getDataCollection(ft,filter,order); |
|
321 |
} catch (ReadException e1) { |
|
322 |
// TODO Auto-generated catch block |
|
323 |
e1.printStackTrace(); |
|
324 |
} |
|
325 |
Iterator iter = featureCollection.iterator(); |
|
326 |
Iterator titlesIt=ft.iterator(); |
|
327 |
System.out.print("ID"); |
|
328 |
while (titlesIt.hasNext()) { |
|
329 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) titlesIt.next(); |
|
330 |
System.out.print("\t"+ descriptor.getName() + "(" + descriptor.getDataType().substring(0,3)+")"); |
|
331 |
} |
|
332 |
System.out.println(""); |
|
333 |
while (iter.hasNext()) { |
|
334 |
Feature feature = (Feature)iter.next(); |
|
335 |
System.out.print(feature.getID().toString()); |
|
336 |
//System.out.println("SHP Feature ------------------- "); |
|
337 |
Iterator iterator=ft.iterator(); |
|
338 |
while (iterator.hasNext()) { |
|
339 |
FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next(); |
|
340 |
int i=descriptor.ordinal(); |
|
341 |
String type=descriptor.getDataType(); |
|
342 |
//System.out.println("****** "+ descriptor.getName()); |
|
343 |
if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)){ |
|
344 |
System.out.print("\t"+ feature.getBoolean(i)); |
|
345 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)){ |
|
346 |
System.out.print("\t"+ feature.getByte(i)); |
|
347 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)){ |
|
348 |
try{ |
|
349 |
System.out.print("\t"+ feature.getDate(i)); |
|
350 |
} catch (Exception e){ |
|
351 |
|
|
352 |
} |
|
353 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)){ |
|
354 |
System.out.print("\t"+ feature.getDouble(i)); |
|
355 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)){ |
|
356 |
System.out.print("\t"+ feature.getFloat(i)); |
|
357 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)){ |
|
358 |
System.out.print("\t"+ feature.getInt(i)); |
|
359 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)){ |
|
360 |
System.out.print("\t"+ feature.getLong(i)); |
|
361 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)){ |
|
362 |
System.out.print("\t"+ feature.getString(i)); |
|
363 |
}else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){ |
|
364 |
|
|
365 |
System.out.print("\tGeometry"); |
|
366 |
} |
|
367 |
} |
|
368 |
System.out.println(""); |
|
369 |
} |
|
370 |
iter=null; |
|
371 |
int numFeat = featureCollection.size(); |
|
372 |
featureCollection.dispose(); |
|
373 |
|
|
374 |
try { |
|
375 |
store.close(); |
|
376 |
store.dispose(); |
|
377 |
} catch (CloseException e) { |
|
378 |
// TODO Auto-generated catch block |
|
379 |
e.printStackTrace(); |
|
380 |
} |
|
381 |
|
|
382 |
return numFeat; |
|
383 |
|
|
384 |
} |
|
385 |
|
|
386 |
} |
Also available in: Unified diff