svn-gvsig-desktop / trunk / libraries / libGeocoding / src-filter / org / gvsig / data / vectorial / filter / testStreetsFilters.java @ 23020
History | View | Annotate | Download (3.96 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.data.vectorial.filter; |
5 |
|
6 |
import java.io.File; |
7 |
|
8 |
import junit.framework.TestCase; |
9 |
|
10 |
import org.apache.log4j.Logger; |
11 |
import org.apache.log4j.PropertyConfigurator; |
12 |
import org.gvsig.data.CloseException; |
13 |
import org.gvsig.data.DataManager; |
14 |
import org.gvsig.data.DataStoreParameters; |
15 |
import org.gvsig.data.InitializeException; |
16 |
import org.gvsig.data.OpenException; |
17 |
import org.gvsig.data.ReadException; |
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.vectorial.Feature; |
22 |
import org.gvsig.data.vectorial.FeatureCollection; |
23 |
import org.gvsig.data.vectorial.FeatureType; |
24 |
import org.opengis.filter.Filter; |
25 |
import org.opengis.filter.expression.Expression; |
26 |
import org.opengis.filter.sort.SortBy; |
27 |
|
28 |
/**
|
29 |
* @author jsanz
|
30 |
*
|
31 |
*/
|
32 |
public class testStreetsFilters extends TestCase { |
33 |
|
34 |
private static final float THRESHOLD = 0.78f; |
35 |
static Logger log = null; |
36 |
static String TEST_SHP = "test-data/geocoder/streets.shp"; |
37 |
static String SEARCH_TERM = "PECHINA"; |
38 |
|
39 |
SHPStore store; |
40 |
|
41 |
public testStreetsFilters() {
|
42 |
super();
|
43 |
} |
44 |
|
45 |
public testStreetsFilters(String name) { |
46 |
super(name);
|
47 |
// TODO Auto-generated constructor stub
|
48 |
} |
49 |
|
50 |
/**
|
51 |
* @throws java.lang.Exception
|
52 |
*/
|
53 |
protected void setUp() throws Exception { |
54 |
if (log == null) { |
55 |
// Configurar el log4j
|
56 |
PropertyConfigurator.configure("log4j.properties");
|
57 |
log = Logger.getLogger(testStreetsFilters.class);
|
58 |
log.info("Test initialized");
|
59 |
} |
60 |
|
61 |
Register.selfRegister(); |
62 |
DataManager manager = DataManager.getManager(); |
63 |
SHPStoreParameters parameters = null;
|
64 |
parameters = (SHPStoreParameters) manager |
65 |
.createDataStoreParameters(SHPStore.DATASTORE_NAME); |
66 |
parameters.setFile(new File(TEST_SHP)); |
67 |
|
68 |
try {
|
69 |
store = (SHPStore) manager |
70 |
.createDataStore((DataStoreParameters) parameters); |
71 |
} catch (InitializeException e) {
|
72 |
// TODO Auto-generated catch block
|
73 |
e.printStackTrace(); |
74 |
} |
75 |
} |
76 |
|
77 |
/**
|
78 |
* @throws java.lang.Exception
|
79 |
*/
|
80 |
protected void tearDown() throws Exception { |
81 |
} |
82 |
|
83 |
|
84 |
|
85 |
public void testTextDistanceFilter() { |
86 |
log.info("-----------testTextDistanceFilter-----------");
|
87 |
// long t1 = System.currentTimeMillis();
|
88 |
DistanceFilterFactory2Impl fact = new DistanceFilterFactory2Impl();
|
89 |
Expression exp1 = fact.property("STREET_NAM"); |
90 |
Filter filter = fact.distance(exp1, SEARCH_TERM, THRESHOLD);
|
91 |
// Filter filter = fact.equals(exp1, exp2);
|
92 |
int results = testshp(filter, null); |
93 |
assertEquals(5, results);
|
94 |
|
95 |
log.info("-----------testTextDistanceFilter-----------");
|
96 |
} |
97 |
|
98 |
/**
|
99 |
* Convenience method to test the filter
|
100 |
*
|
101 |
* @param filter
|
102 |
* @param order
|
103 |
* @return
|
104 |
*/
|
105 |
private int testshp(Filter filter, SortBy[] order) { |
106 |
try {
|
107 |
store.open(); |
108 |
} catch (OpenException e2) {
|
109 |
// TODO Auto-generated catch block
|
110 |
e2.printStackTrace(); |
111 |
} |
112 |
|
113 |
FeatureType ft = store.getDefaultFeatureType(); |
114 |
FeatureCollection featureCollection = null;
|
115 |
|
116 |
try {
|
117 |
long t1 = System.currentTimeMillis(); |
118 |
featureCollection = (FeatureCollection) store.getDataCollection(ft, |
119 |
filter, order); |
120 |
log.info("Search retrieved in " + (System.currentTimeMillis() - t1) |
121 |
+ " msecs");
|
122 |
log.info(featureCollection.size() + " results");
|
123 |
} catch (ReadException e1) {
|
124 |
// TODO Auto-generated catch block
|
125 |
e1.printStackTrace(); |
126 |
} |
127 |
|
128 |
for (Object obj : featureCollection) { |
129 |
if (obj instanceof Feature) { |
130 |
Feature feat = (Feature) obj; |
131 |
log.info("STREET NAME : " + feat.get("STREET_NAM")); |
132 |
} |
133 |
} |
134 |
|
135 |
int numFeat = featureCollection.size();
|
136 |
featureCollection.dispose(); |
137 |
|
138 |
try {
|
139 |
store.close(); |
140 |
store.dispose(); |
141 |
} catch (CloseException e) {
|
142 |
// TODO Auto-generated catch block
|
143 |
e.printStackTrace(); |
144 |
} |
145 |
|
146 |
return numFeat;
|
147 |
} |
148 |
} |