root / branches / Mobile_Compatible_Hito_1 / libFMap_mobile_shp_driver / src-test / org / gvsig / data / datastores / vectorial / file / shp / TestLevenshteinFilter.java @ 22037
History | View | Annotate | Download (7.35 KB)
1 |
/**
|
---|---|
2 |
*
|
3 |
*/
|
4 |
package org.gvsig.data.datastores.vectorial.file.shp; |
5 |
|
6 |
import java.io.File; |
7 |
import java.util.Date; |
8 |
import java.util.List; |
9 |
|
10 |
import junit.framework.TestCase; |
11 |
|
12 |
import org.apache.log4j.FileAppender; |
13 |
import org.apache.log4j.Level; |
14 |
import org.apache.log4j.Logger; |
15 |
import org.apache.log4j.PatternLayout; |
16 |
import org.apache.log4j.PropertyConfigurator; |
17 |
import org.gvsig.compatible.StringUtil; |
18 |
import org.gvsig.data.CloseException; |
19 |
import org.gvsig.data.DataManager; |
20 |
import org.gvsig.data.DataStoreParameters; |
21 |
import org.gvsig.data.InitializeException; |
22 |
import org.gvsig.data.OpenException; |
23 |
import org.gvsig.data.ReadException; |
24 |
import org.gvsig.data.datastores.vectorial.file.shp_util.ResourceReader; |
25 |
import org.gvsig.data.datastores.vectorial.file.shp_util.Utils; |
26 |
import org.gvsig.data.vectorial.AttributeDescriptor; |
27 |
import org.gvsig.data.vectorial.DefaultFeatureType; |
28 |
import org.gvsig.data.vectorial.FeatureCollection; |
29 |
import org.gvsig.data.vectorial.FeatureType; |
30 |
import org.gvsig.data.vectorial.filter.FilterFactory2Impl; |
31 |
import org.opengis.filter.Filter; |
32 |
import org.opengis.filter.FilterFactory2; |
33 |
import org.opengis.filter.expression.Expression; |
34 |
import org.opengis.filter.sort.SortBy; |
35 |
|
36 |
/**
|
37 |
* @author jsanz
|
38 |
*
|
39 |
*/
|
40 |
public class TestLevenshteinFilter extends TestCase { |
41 |
|
42 |
static Logger log = null; |
43 |
static File TEST_SHP = ResourceReader.getResourceFile("testdata", "albania.shp"); |
44 |
SHPStore store; |
45 |
|
46 |
|
47 |
public TestLevenshteinFilter() {
|
48 |
super();
|
49 |
} |
50 |
|
51 |
public TestLevenshteinFilter(String name) { |
52 |
super(name);
|
53 |
// TODO Auto-generated constructor stub
|
54 |
} |
55 |
|
56 |
/**
|
57 |
* @throws java.lang.Exception
|
58 |
*/
|
59 |
protected void setUp() throws Exception { |
60 |
|
61 |
|
62 |
if (log==null){ |
63 |
|
64 |
Date now = new Date(System.currentTimeMillis()); |
65 |
String nowstr = now.toString();
|
66 |
|
67 |
nowstr = StringUtil.replaceAllString(nowstr, " ", "_"); |
68 |
nowstr = StringUtil.replaceAllString(nowstr, ":", "_") + ".txt"; |
69 |
|
70 |
String outlogpath = ""; |
71 |
outlogpath = ResourceReader.getResourceFile("testdata", "filter_test_" + nowstr).getAbsolutePath(); |
72 |
|
73 |
System.out.println("Log file: " + outlogpath); |
74 |
|
75 |
try {
|
76 |
PatternLayout l = new PatternLayout("%5p [%t] - %m%n"); |
77 |
FileAppender fa = new FileAppender(l, outlogpath, false); |
78 |
// ConsoleAppender ca = new ConsoleAppender(l);
|
79 |
Logger.getRootLogger().setLevel(Level.DEBUG); |
80 |
Logger.getRootLogger().addAppender(fa);
|
81 |
// Logger.getRootLogger().addAppender(ca);
|
82 |
} catch (Exception ex) { |
83 |
System.err.println("Error while initializing log4j: " + ex.getMessage()); |
84 |
} |
85 |
log = Logger.getLogger(FilterTest.class);
|
86 |
} |
87 |
|
88 |
|
89 |
Register.selfRegister(); |
90 |
DataManager manager = DataManager.getManager(); |
91 |
SHPStoreParameters parameters = null;
|
92 |
parameters=(SHPStoreParameters)manager.createDataStoreParameters(SHPStore.DATASTORE_NAME); |
93 |
parameters.setFile(TEST_SHP); |
94 |
|
95 |
try {
|
96 |
store= (SHPStore)manager.createDataStore((DataStoreParameters) parameters); |
97 |
} catch (InitializeException e) {
|
98 |
// TODO Auto-generated catch block
|
99 |
e.printStackTrace(); |
100 |
} |
101 |
} |
102 |
|
103 |
/**
|
104 |
* @throws java.lang.Exception
|
105 |
*/
|
106 |
protected void tearDown() throws Exception { |
107 |
} |
108 |
|
109 |
public void testGetFeatureTypes(){ |
110 |
log.info("-----------testGetFeatureTypes-----------");
|
111 |
|
112 |
List types = store.getFeatureTypes();
|
113 |
assertEquals(1, types.size());
|
114 |
DefaultFeatureType ftype = (DefaultFeatureType) types.get(0);
|
115 |
log.info("Type with " + ftype.size() + " attributes"); |
116 |
Object att = ftype.get("ASCIINAME"); |
117 |
assertNotNull(att); |
118 |
assertTrue(att instanceof AttributeDescriptor);
|
119 |
AttributeDescriptor ad = (AttributeDescriptor) att; |
120 |
assertEquals(AttributeDescriptor.TYPE_STRING, ad.getDataType()); |
121 |
log.info("-----------testGetFeatureTypes-----------");
|
122 |
} |
123 |
|
124 |
|
125 |
public void testLevenshteinFilter(){ |
126 |
log.info("-----------testLevenshteinFilter-----------");
|
127 |
// long t1 = System.currentTimeMillis();
|
128 |
FilterFactory2 fact = new FilterFactory2Impl();
|
129 |
Expression exp1 = fact.property("ASCIINAME"); |
130 |
//Expression exp2 = fact.literal("Okshtunit");
|
131 |
|
132 |
|
133 |
Filter filter = fact.like(exp1, "%Okshtunit%"); |
134 |
|
135 |
//Filter filter = fact.like(exp2, "%Okshtunit%");
|
136 |
|
137 |
// Filter filter = fact.equals(exp1, exp2);
|
138 |
|
139 |
int results = testshp(filter, null); |
140 |
|
141 |
// log.info("Search retrieved in " + (System.currentTimeMillis() - t1) + " msecs");
|
142 |
|
143 |
assertEquals(2,results);
|
144 |
|
145 |
log.info("-----------testLevenshteinFilter-----------");
|
146 |
} |
147 |
|
148 |
private int testshp(Filter filter, SortBy[] order){ |
149 |
try {
|
150 |
store.open(); |
151 |
} catch (OpenException e2) {
|
152 |
// TODO Auto-generated catch block
|
153 |
e2.printStackTrace(); |
154 |
} |
155 |
|
156 |
FeatureType ft=store.getDefaultFeatureType(); |
157 |
FeatureCollection featureCollection=null;
|
158 |
|
159 |
// try{
|
160 |
// StringWriter out = new StringWriter();
|
161 |
// FilterToSQL converter = new FilterToSQL(out);
|
162 |
// converter.encode(filter);
|
163 |
// log.info(out.toString());
|
164 |
// } catch (FilterToSQLException e){
|
165 |
// e.printStackTrace();
|
166 |
// }
|
167 |
|
168 |
try {
|
169 |
long t1 = System.currentTimeMillis(); |
170 |
featureCollection = (FeatureCollection)store.getDataCollection(ft,filter,order); |
171 |
log.info("Search retrieved in " + (System.currentTimeMillis() - t1) + " msecs"); |
172 |
} catch (ReadException e1) {
|
173 |
// TODO Auto-generated catch block
|
174 |
e1.printStackTrace(); |
175 |
} |
176 |
// Iterator iter = featureCollection.iterator();
|
177 |
// Iterator titlesIt=ft.iterator();
|
178 |
// log.info("ID");
|
179 |
// while (titlesIt.hasNext()) {
|
180 |
// FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) titlesIt.next();
|
181 |
// log.info("\t"+ descriptor.getName() + "(" + descriptor.getDataType().substring(0,3)+")");
|
182 |
// }
|
183 |
// System.out.println("");
|
184 |
// while (iter.hasNext()) {
|
185 |
// Feature feature = (Feature)iter.next();
|
186 |
// log.info(feature.getID().toString());
|
187 |
// //System.out.println("SHP Feature ------------------- ");
|
188 |
// Iterator iterator=ft.iterator();
|
189 |
// while (iterator.hasNext()) {
|
190 |
// FeatureAttributeDescriptor descriptor = (FeatureAttributeDescriptor) iterator.next();
|
191 |
// int i=descriptor.ordinal();
|
192 |
// String type=descriptor.getDataType();
|
193 |
// //System.out.println("****** "+ descriptor.getName());
|
194 |
// if (type.equals(FeatureAttributeDescriptor.TYPE_BOOLEAN)){
|
195 |
// log.info("\t"+ feature.getBoolean(i));
|
196 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_BYTE)){
|
197 |
// log.info("\t"+ feature.getByte(i));
|
198 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_DATE)){
|
199 |
// try{
|
200 |
// log.info("\t"+ feature.getDate(i));
|
201 |
// } catch (Exception e){
|
202 |
//
|
203 |
// }
|
204 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_DOUBLE)){
|
205 |
// log.info("\t"+ feature.getDouble(i));
|
206 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_FLOAT)){
|
207 |
// log.info("\t"+ feature.getFloat(i));
|
208 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_INT)){
|
209 |
// log.info("\t"+ feature.getInt(i));
|
210 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_LONG)){
|
211 |
// log.info("\t"+ feature.getLong(i));
|
212 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_STRING)){
|
213 |
// log.info("\t"+ feature.getString(i));
|
214 |
// }else if (type.equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
|
215 |
//
|
216 |
// log.info("\tGeometry");
|
217 |
// }
|
218 |
// }
|
219 |
// log.info("");
|
220 |
// }
|
221 |
// iter=null;
|
222 |
long t1 = System.currentTimeMillis(); |
223 |
int numFeat = featureCollection.size();
|
224 |
log.info("Count retrieved in " + (System.currentTimeMillis() - t1) + " msecs"); |
225 |
featureCollection.dispose(); |
226 |
|
227 |
try {
|
228 |
store.close(); |
229 |
store.dispose(); |
230 |
} catch (CloseException e) {
|
231 |
// TODO Auto-generated catch block
|
232 |
e.printStackTrace(); |
233 |
} |
234 |
|
235 |
return numFeat;
|
236 |
} |
237 |
} |