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