Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / ComposedTest.java @ 28375

History | View | Annotate | Download (9.37 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
/*
24
 * AUTHORS (In addition to CIT):
25
 * 2008 PRODEVELOP                Main development
26
 */
27

    
28
package org.gvsig.geocoding;
29

    
30
import java.io.File;
31
import java.util.ArrayList;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Set;
35

    
36
import junit.framework.TestCase;
37

    
38
import org.gvsig.fmap.dal.DALFileLibrary;
39
import org.gvsig.fmap.dal.DALLibrary;
40
import org.gvsig.fmap.dal.DALLocator;
41
import org.gvsig.fmap.dal.DataManager;
42
import org.gvsig.fmap.dal.exception.ReadException;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.fmap.dal.feature.FeatureType;
45
import org.gvsig.fmap.dal.store.shp.SHPLibrary;
46
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters;
47
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider;
48
import org.gvsig.fmap.geom.primitive.Point;
49
import org.gvsig.geocoding.address.Address;
50
import org.gvsig.geocoding.address.ComposedAddress;
51
import org.gvsig.geocoding.address.Literal;
52
import org.gvsig.geocoding.address.impl.DefaultAddressComponent;
53
import org.gvsig.geocoding.address.impl.DefaultComposedAddress;
54
import org.gvsig.geocoding.address.impl.DefaultLiteral;
55
import org.gvsig.geocoding.address.impl.DefaultRelationsComponent;
56
import org.gvsig.geocoding.impl.DataGeocoderImpl;
57
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary;
58
import org.gvsig.geocoding.pattern.GeocodingSource;
59
import org.gvsig.geocoding.pattern.Patterngeocoding;
60
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource;
61
import org.gvsig.geocoding.pattern.impl.DefaultPatterngeocoding;
62
import org.gvsig.geocoding.result.GeocodingResult;
63
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
64
import org.gvsig.geocoding.styles.impl.Composed;
65
import org.gvsig.tools.ToolsLibrary;
66
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator;
67
import org.gvsig.tools.locator.Library;
68
import org.slf4j.Logger;
69
import org.slf4j.LoggerFactory;
70

    
71
/**
72
 * Test
73
 * 
74
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
75
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
76
 */
77
public class ComposedTest extends TestCase {
78

    
79
        protected DataManager dataManager = null;
80
        private File file = new File("./test-data/geocoder/streets.shp");
81
        private Logger log = LoggerFactory.getLogger(ComposedTest.class);
82

    
83
        protected void setUp() throws Exception {
84
                super.setUp();
85

    
86
                Library tools = new ToolsLibrary();
87
                tools.initialize();
88
                tools.postInitialize();
89

    
90
                Library dlib = new DALLibrary();
91
                dlib.initialize();
92
                dlib.postInitialize();
93

    
94
                Library libFile = new DALFileLibrary();
95
                libFile.initialize();
96
                libFile.postInitialize();
97

    
98
                Library shpLib = new SHPLibrary();
99
                shpLib.initialize();
100
                shpLib.postInitialize();
101

    
102
                Library geocoLib = new DefaultGeocodingLibrary();
103
                geocoLib.initialize();
104
                geocoLib.postInitialize();
105

    
106
                dataManager = DALLocator.getDataManager();
107
        }
108

    
109
        protected void tearDown() throws Exception {
110
                super.tearDown();
111
        }
112

    
113
        /**
114
         * 
115
         * @throws ReadException
116
         */
117
        public void testCrossGeocode() throws ReadException {
118

    
119
                /* Set the pattern */
120
                Patterngeocoding pat = getPattern();
121

    
122
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
123
                ((DataGeocoderImpl) geocoder).setPattern(pat);
124

    
125
                /* Define a address search */
126
                Address address = getCrossAddress();
127

    
128
                Set<GeocodingResult> results = null;
129
                try {
130
                        results = geocoder.geocode(address);
131
                } catch (Exception e) {
132
                        log.error("Geocoding", e);
133
                }
134
                int si = results.size();
135
                assertNotNull(results);
136
                assertEquals(4, si);
137
                Iterator<GeocodingResult> it = results.iterator();
138
                log.debug("CROSS GEOCODING");
139
                while (it.hasNext()) {
140
                        GeocodingResult res = it.next();
141
                        Point pto = (Point) res.getGeometry();
142
                        // log.debug("PTO: "+pto.getX()+","+pto.getY());
143
                }
144
        }
145

    
146
        /**
147
         * 
148
         * @throws ReadException
149
         */
150
        public void testThirdGeocode() throws ReadException {
151

    
152
                /* Set the pattern */
153
                Patterngeocoding pat = getPattern();
154
                
155
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
156
                ((DataGeocoderImpl) geocoder).setPattern(pat);
157

    
158
                /* Define a address search */
159
                Address address = getBetweenAddress();
160

    
161
                Set<GeocodingResult> results = null;
162
                try {
163
                        results = geocoder.geocode(address);
164
                } catch (Exception e) {
165
                        log.error("Geocoding", e);
166
                }
167
                assertNotNull(results);
168
                assertEquals(2, results.size());
169
                Iterator<GeocodingResult> it = results.iterator();
170
                log.debug("THIRD GEOCODING");
171
                while (it.hasNext()) {
172
                        GeocodingResult res = it.next();
173
                        Point pto = (Point) res.getGeometry();
174
                        log.debug("PTO: " + pto.getX() + "," + pto.getY());
175
                }
176
        }
177

    
178
        /**
179
         * get pattern
180
         * 
181
         * @return
182
         */
183
        private Patterngeocoding getPattern() {
184

    
185
                assertNotNull(dataManager);
186
                dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class);
187

    
188
                Patterngeocoding pat = new DefaultPatterngeocoding();
189

    
190
                try {
191
                        pat.setPatternName("ComposedCentroid");
192

    
193
                        GeocodingSource source = new DefaultGeocodingSource();
194

    
195
                        SHPStoreParameters params = (SHPStoreParameters) dataManager
196
                                        .createStoreParameters(SHPStoreProvider.NAME);
197
                        assertNotNull(file);
198
                        params.setFile(file);
199
                        FeatureStore store = (FeatureStore) dataManager.createStore(params);
200
                        source.setLayerSource(store);
201

    
202
                        AbstractGeocodingStyle style = new Composed();
203
                        FeatureType ftype = store.getDefaultFeatureType();
204
                        Literal relations = new DefaultLiteral();
205
                        relations.add(new DefaultRelationsComponent("Calle", "STREET_NAM"));
206
                        relations.add(new DefaultRelationsComponent("TipoVia","STREET_TYP"));
207
                        relations.add(new DefaultRelationsComponent("Municipio","MUNI"));
208
                        relations.add(new DefaultRelationsComponent("Provincia","PROV"));
209
                        relations.add(new DefaultRelationsComponent("Pais","PAIS"));
210

    
211
                        style.setRelationsLiteral(relations);
212
                        source.setStyle(style);
213

    
214
                        pat.setSource(source);
215

    
216
                } catch (Exception e) {
217
                        log.error("Building a pattern", e);
218
                }
219

    
220
                return pat;
221
        }
222

    
223
        /**
224
         * get Address
225
         * 
226
         * @return
227
         */
228
        private Address getCrossAddress() {
229

    
230
                Literal literal = new DefaultLiteral();
231
                literal.add(new DefaultAddressComponent("Calle", "DOCTOR ROMAGOSA"));
232
                literal.add(new DefaultAddressComponent("TipoVia", "C"));
233
                literal.add(new DefaultAddressComponent("Municipio", "Valencia"));
234
                literal.add(new DefaultAddressComponent("Provincia", "Valencia"));
235
                literal.add(new DefaultAddressComponent("Pais", "Espanya"));
236

    
237
                Literal literal2 = new DefaultLiteral();
238
                literal2
239
                                .add(new DefaultAddressComponent("Calle", "DON JUAN DE AUSTRIA"));
240
                literal2.add(new DefaultAddressComponent("TipoVia", "C"));
241
                literal2.add(new DefaultAddressComponent("Municipio", "Valencia"));
242
                literal2.add(new DefaultAddressComponent("Provincia", "Valencia"));
243
                literal2.add(new DefaultAddressComponent("Pais", "Espanya"));
244

    
245
                Address address = new DefaultComposedAddress(literal);
246

    
247
                List<Literal> intersect = new ArrayList<Literal>();
248
                intersect.add(literal2);
249
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
250

    
251
                return address;
252
        }
253

    
254
        /**
255
         * get Address
256
         * 
257
         * @return
258
         */
259
        private Address getBetweenAddress() {
260

    
261
                DefaultLiteral literal = new DefaultLiteral();
262
                literal.add(new DefaultAddressComponent("Calle", "ROGER DE LAURIA"));
263
                literal.add(new DefaultAddressComponent("TipoVia", "C"));
264
                literal.add(new DefaultAddressComponent("Municipio", "Valencia"));
265
                literal.add(new DefaultAddressComponent("Provincia", "Valencia"));
266
                literal.add(new DefaultAddressComponent("Pais", "Espanya"));
267

    
268
                Literal literal2 = new DefaultLiteral();
269
                literal2.add(new DefaultAddressComponent("Calle", "PASCUAL Y GENIS"));
270
                literal2.add(new DefaultAddressComponent("TipoVia", "C"));
271
                literal2.add(new DefaultAddressComponent("Municipio", "Valencia"));
272
                literal2.add(new DefaultAddressComponent("Provincia", "Valencia"));
273
                literal2.add(new DefaultAddressComponent("Pais", "Espanya"));
274

    
275
                Literal literal3 = new DefaultLiteral();
276
                literal3.add(new DefaultAddressComponent("Calle", "PEREZ PUJOL"));
277
                literal3.add(new DefaultAddressComponent("TipoVia", "C"));
278
                literal3.add(new DefaultAddressComponent("Municipio", "Valencia"));
279
                literal3.add(new DefaultAddressComponent("Provincia", "Valencia"));
280
                literal3.add(new DefaultAddressComponent("Pais", "Espanya"));
281

    
282
                Address address = new DefaultComposedAddress(literal);
283

    
284
                List<Literal> intersect = new ArrayList<Literal>();
285
                intersect.add(literal2);
286
                intersect.add(literal3);
287
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
288

    
289
                return address;
290
        }
291

    
292
}