Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / index / LucenGeocoderImplTest.java @ 32479

History | View | Annotate | Download (6.76 KB)

1
package org.gvsig.geocoding.index;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Set;
6

    
7
import org.gvsig.LayerServer;
8
import org.gvsig.fmap.dal.feature.FeatureStore;
9
import org.gvsig.geocoding.DataGeocoder;
10
import org.gvsig.geocoding.address.Address;
11
import org.gvsig.geocoding.address.ComposedAddress;
12
import org.gvsig.geocoding.address.Literal;
13
import org.gvsig.geocoding.address.impl.DefaultAddress;
14
import org.gvsig.geocoding.address.impl.DefaultComposedAddress;
15
import org.gvsig.geocoding.address.impl.DefaultLiteral;
16
import org.gvsig.geocoding.impl.LuceneGeocoderImpl;
17
import org.gvsig.geocoding.pattern.GeocodingPattern;
18
import org.gvsig.geocoding.pattern.GeocodingSource;
19
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
20
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource;
21
import org.gvsig.geocoding.result.GeocodingResult;
22
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
23
import org.gvsig.geocoding.styles.impl.Composed;
24
import org.gvsig.geocoding.styles.impl.SimpleCentroid;
25
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator;
26
import org.slf4j.Logger;
27
import org.slf4j.LoggerFactory;
28

    
29
public class LucenGeocoderImplTest extends
30
                org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase {
31

    
32
        private static final Logger log = LoggerFactory
33
                        .getLogger(LucenGeocoderImplTest.class);
34

    
35
        @Override
36
        protected void doSetUp() throws Exception {
37
                // TODO Auto-generated method stub
38

    
39
        }
40

    
41
        public void testStandardIndex() throws Exception {
42
                
43
                //get Pattern
44
                log.debug("Loading pattern ...");
45
                GeocodingPattern pattern = this.getPattern();
46
                
47
                //get Address
48
                log.debug("Loading address ...");
49
                Address address = this.getAddressLines();
50
                
51
                //get Pattern
52
                log.debug("Loading faeture store ...");
53
                FeatureStore store = LayerServer.getSHPStore(LayerServer
54
                                .getStreetsFile());
55
                
56
                // create geocoder
57
                log.debug("Builing geocoder ...");
58
                LuceneGeocoderImpl geocoder = new LuceneGeocoderImpl();                
59
                geocoder.setPattern(pattern);
60
                geocoder.setStore(store);
61
                
62
                //geocoding process
63
                log.debug("Run geocoding process ...");
64
                geocoder.indexer(store, pattern.getDataSource().getStyle().getRelationsLiteral());
65
                Set<GeocodingResult> results = geocoder.geocode(address);
66
                
67
                
68
                
69
        }
70
        
71
public void testComposedIndex() throws Exception {
72
                
73
                //get Pattern
74
                log.debug("Loading pattern ...");
75
                GeocodingPattern pattern = this.getComposedPattern();
76
                
77
                //get Address
78
                log.debug("Loading address ...");
79
                Address address = this.getCrossAddress();
80
                
81
                //get Pattern
82
                log.debug("Loading faeture store ...");
83
                FeatureStore store = LayerServer.getSHPStore(LayerServer
84
                                .getStreetsFile());
85
                
86
                // create geocoder
87
                log.debug("Builing geocoder ...");
88
                LuceneGeocoderImpl geocoder = new LuceneGeocoderImpl();                
89
                geocoder.setPattern(pattern);
90
                geocoder.setStore(store);
91
                
92
                //geocoding process
93
                log.debug("Run geocoding process ...");
94
                geocoder.indexer(store, pattern.getDataSource().getStyle().getRelationsLiteral());
95
                Set<GeocodingResult> results = geocoder.geocode(address);
96
                
97
                
98
                
99
        }
100
        
101

    
102
        
103
        
104
        
105
        /**
106
         * get pattern
107
         * 
108
         * @return
109
         */
110
        private GeocodingPattern getPattern() {
111

    
112
                GeocodingPattern pat = new DefaultGeocodingPattern();
113

    
114
                try {
115
                        pat.setPatternName("indexLucenePattern");
116

    
117
                        GeocodingSource source = new DefaultGeocodingSource();
118
                        source.setLayerName("Streets.shp");
119
                        source.setLayerProvider("Shape file");
120

    
121
                        AbstractGeocodingStyle style = new SimpleCentroid();
122
                        Literal relations = new DefaultLiteral();
123
                        relations.put("Calle", "STREET_NAM");
124
                        relations.put("TipoVia", "STREET_TYP");
125
                        relations.put("Municipio", "MUNI");
126
                        relations.put("Provincia", "PROV");
127
                        relations.put("Pais", "PAIS");
128
                        
129

    
130
                        style.setRelationsLiteral(relations);
131
                        source.setStyle(style);
132

    
133
                        pat.setSource(source);
134

    
135
                } catch (Exception e) {
136
                        log.error("Building a pattern", e);
137
                }
138

    
139
                return pat;
140
        }
141
        
142
        /**
143
         * get Address Lines
144
         * 
145
         * @return
146
         */
147
        private Address getAddressLines() {
148
                Literal literal = new DefaultLiteral();
149
                literal.put("Calle", "colon");
150
                literal.put("TipoVia", "c");
151
                literal.put("Municipio", "valencia");
152
                literal.put("Provincia", "valencia");
153
                literal.put("Pais", "espanya");
154

    
155
                Address address = new DefaultAddress(literal);
156
                return address;
157
        }
158
        
159
        /**
160
         * get pattern
161
         * 
162
         * @return
163
         */
164
        private GeocodingPattern getComposedPattern() {
165

    
166
                GeocodingPattern pat = new DefaultGeocodingPattern();
167

    
168
                try {
169
                        pat.setPatternName("ComposedCentroid");
170

    
171
                        GeocodingSource source = new DefaultGeocodingSource();
172

    
173
                        AbstractGeocodingStyle style = new Composed();
174
                        Literal relations = new DefaultLiteral();
175
                        relations.put("Calle", "STREET_NAM");
176
                        relations.put("TipoVia", "STREET_TYP");
177
                        relations.put("Municipio", "MUNI");
178
                        relations.put("Provincia", "PROV");
179
                        relations.put("Pais", "PAIS");
180

    
181
                        style.setRelationsLiteral(relations);
182
                        source.setStyle(style);
183
                        source.setLayerName("Streets.shp");
184
                        source.setLayerProvider("Shape file");
185

    
186
                        pat.setSource(source);
187

    
188
                } catch (Exception e) {
189
                        log.error("Building a pattern", e);
190
                }
191

    
192
                return pat;
193
        }
194

    
195
        /**
196
         * get Address
197
         * 
198
         * @return
199
         */
200
        private Address getCrossAddress() {
201

    
202
                Literal literal = new DefaultLiteral();
203
                literal.put("Calle", "DOCTOR ROMAGOSA");
204
                literal.put("TipoVia", "C");
205
                literal.put("Municipio", "Valencia");
206
                literal.put("Provincia", "Valencia");
207
                literal.put("Pais", "Espanya");
208

    
209
                Literal literal2 = new DefaultLiteral();
210
                literal2.put("Calle", "DON JUAN DE AUSTRIA");
211
                literal2.put("TipoVia", "C");
212
                literal2.put("Municipio", "Valencia");
213
                literal2.put("Provincia", "Valencia");
214
                literal2.put("Pais", "Espanya");
215

    
216
                Address address = new DefaultComposedAddress(literal);
217

    
218
                List<Literal> intersect = new ArrayList<Literal>();
219
                intersect.add(literal2);
220
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
221

    
222
                return address;
223
        }
224

    
225
        /**
226
         * get Address
227
         * 
228
         * @return
229
         */
230
        private Address getBetweenAddress() {
231

    
232
                DefaultLiteral literal = new DefaultLiteral();
233
                literal.put("Calle", "ROGER DE LAURIA");
234
                literal.put("TipoVia", "C");
235
                literal.put("Municipio", "Valencia");
236
                literal.put("Provincia", "Valencia");
237
                literal.put("Pais", "Espanya");
238

    
239
                Literal literal2 = new DefaultLiteral();
240
                literal2.put("Calle", "PASCUAL Y GENIS");
241
                literal2.put("TipoVia", "C");
242
                literal2.put("Municipio", "Valencia");
243
                literal2.put("Provincia", "Valencia");
244
                literal2.put("Pais", "Espanya");
245

    
246
                Literal literal3 = new DefaultLiteral();
247
                literal3.put("Calle", "PEREZ PUJOL");
248
                literal3.put("TipoVia", "C");
249
                literal3.put("Municipio", "Valencia");
250
                literal3.put("Provincia", "Valencia");
251
                literal3.put("Pais", "Espanya");
252

    
253
                Address address = new DefaultComposedAddress(literal);
254

    
255
                List<Literal> intersect = new ArrayList<Literal>();
256
                intersect.add(literal2);
257
                intersect.add(literal3);
258
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
259

    
260
                return address;
261
        }
262
}