Statistics
| Revision:

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

History | View | Annotate | Download (7.93 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.util.ArrayList;
31
import java.util.Iterator;
32
import java.util.List;
33
import java.util.Set;
34

    
35
import org.gvsig.LayerServer;
36
import org.gvsig.fmap.dal.DALLocator;
37
import org.gvsig.fmap.dal.DataManager;
38
import org.gvsig.fmap.dal.exception.InitializeException;
39
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException;
40
import org.gvsig.fmap.dal.exception.ReadException;
41
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.geocoding.address.Address;
44
import org.gvsig.geocoding.address.ComposedAddress;
45
import org.gvsig.geocoding.address.Literal;
46
import org.gvsig.geocoding.address.impl.DefaultComposedAddress;
47
import org.gvsig.geocoding.address.impl.DefaultLiteral;
48
import org.gvsig.geocoding.impl.DataGeocoderImpl;
49
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary;
50
import org.gvsig.geocoding.pattern.GeocodingPattern;
51
import org.gvsig.geocoding.pattern.GeocodingSource;
52
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
53
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource;
54
import org.gvsig.geocoding.result.GeocodingResult;
55
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
56
import org.gvsig.geocoding.styles.impl.Composed;
57
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator;
58
import org.gvsig.tools.library.Library;
59
import org.slf4j.Logger;
60
import org.slf4j.LoggerFactory;
61

    
62
/**
63
 * Test
64
 * 
65
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
66
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
67
 */
68
public class ComposedTest extends
69
                org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase {
70

    
71
        protected DataManager dataManager = null;
72

    
73
        private Logger log = LoggerFactory.getLogger(ComposedTest.class);
74

    
75
        @Override
76
        protected void doSetUp() throws Exception {
77
                dataManager = DALLocator.getDataManager();
78

    
79
                Library geocoLib = new DefaultGeocodingLibrary();
80
                geocoLib.initialize();
81
                geocoLib.postInitialize();
82
        }
83

    
84
        protected void tearDown() throws Exception {
85
                super.tearDown();
86
        }
87

    
88
        /**
89
         * 
90
         * @throws ReadException
91
         * @throws ProviderNotRegisteredException
92
         * @throws InitializeException
93
         * @throws ValidateDataParametersException
94
         */
95
        public void testCrossGeocode() throws Exception {
96

    
97
                /* Set the pattern */
98
                GeocodingPattern pat = getPattern();
99

    
100
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
101
                ((DataGeocoderImpl) geocoder).setPattern(pat);
102
                ((DataGeocoderImpl) geocoder).setStore(LayerServer
103
                                .getSHPStore(LayerServer.getStreetsFile()));
104

    
105
                /* Define a address search */
106
                Address address = getCrossAddress();
107

    
108
                Set<GeocodingResult> results = null;
109
                try {
110
                        results = geocoder.geocode(address);
111
                } catch (Exception e) {
112
                        log.error("Geocoding", e);
113
                }
114
                int si = results.size();
115
                assertNotNull(results);
116
                assertEquals(4, si);
117
                Iterator<GeocodingResult> it = results.iterator();
118
                log.debug("CROSS GEOCODING");
119
                while (it.hasNext()) {
120
                        GeocodingResult res = it.next();
121
                        Point pto = (Point) res.getGeometry();
122

    
123
                }
124
        }
125

    
126
        /**
127
         * 
128
         * @throws ReadException
129
         * @throws ProviderNotRegisteredException
130
         * @throws InitializeException
131
         * @throws ValidateDataParametersException
132
         */
133
        public void testThirdGeocode() throws Exception {
134

    
135
                /* Set the pattern */
136
                GeocodingPattern pat = getPattern();
137

    
138
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
139
                ((DataGeocoderImpl) geocoder).setPattern(pat);
140
                ((DataGeocoderImpl) geocoder).setStore(LayerServer
141
                                .getSHPStore(LayerServer.getStreetsFile()));
142

    
143
                /* Define a address search */
144
                Address address = getBetweenAddress();
145

    
146
                Set<GeocodingResult> results = null;
147
                try {
148
                        results = geocoder.geocode(address);
149
                } catch (Exception e) {
150
                        log.error("Geocoding", e);
151
                }
152
                assertNotNull(results);
153
                assertEquals(2, results.size());
154
                Iterator<GeocodingResult> it = results.iterator();
155
                log.debug("THIRD GEOCODING");
156
                while (it.hasNext()) {
157
                        GeocodingResult res = it.next();
158
                        Point pto = (Point) res.getGeometry();
159
                        log.debug("PTO: " + pto.getX() + "," + pto.getY());
160
                }
161
        }
162

    
163
        /**
164
         * get pattern
165
         * 
166
         * @return
167
         */
168
        private GeocodingPattern getPattern() {
169

    
170
                assertNotNull(dataManager);
171
                dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class);
172

    
173
                GeocodingPattern pat = new DefaultGeocodingPattern();
174

    
175
                try {
176
                        pat.setPatternName("ComposedCentroid");
177

    
178
                        GeocodingSource source = new DefaultGeocodingSource();
179

    
180
                        AbstractGeocodingStyle style = new Composed();
181
                        Literal relations = new DefaultLiteral();
182
                        relations.put("Calle", "STREET_NAM");
183
                        relations.put("TipoVia", "STREET_TYP");
184
                        relations.put("Municipio", "MUNI");
185
                        relations.put("Provincia", "PROV");
186
                        relations.put("Pais", "PAIS");
187

    
188
                        style.setRelationsLiteral(relations);
189
                        source.setStyle(style);
190
                        source.setLayerName("Streets.shp");
191
                        source.setLayerProvider("Shape file");
192

    
193
                        pat.setSource(source);
194

    
195
                } catch (Exception e) {
196
                        log.error("Building a pattern", e);
197
                }
198

    
199
                return pat;
200
        }
201

    
202
        /**
203
         * get Address
204
         * 
205
         * @return
206
         */
207
        private Address getCrossAddress() {
208

    
209
                Literal literal = new DefaultLiteral();
210
                literal.put("Calle", "DOCTOR ROMAGOSA");
211
                literal.put("TipoVia", "C");
212
                literal.put("Municipio", "Valencia");
213
                literal.put("Provincia", "Valencia");
214
                literal.put("Pais", "Espanya");
215

    
216
                Literal literal2 = new DefaultLiteral();
217
                literal2.put("Calle", "DON JUAN DE AUSTRIA");
218
                literal2.put("TipoVia", "C");
219
                literal2.put("Municipio", "Valencia");
220
                literal2.put("Provincia", "Valencia");
221
                literal2.put("Pais", "Espanya");
222

    
223
                Address address = new DefaultComposedAddress(literal);
224

    
225
                List<Literal> intersect = new ArrayList<Literal>();
226
                intersect.add(literal2);
227
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
228

    
229
                return address;
230
        }
231

    
232
        /**
233
         * get Address
234
         * 
235
         * @return
236
         */
237
        private Address getBetweenAddress() {
238

    
239
                DefaultLiteral literal = new DefaultLiteral();
240
                literal.put("Calle", "ROGER DE LAURIA");
241
                literal.put("TipoVia", "C");
242
                literal.put("Municipio", "Valencia");
243
                literal.put("Provincia", "Valencia");
244
                literal.put("Pais", "Espanya");
245

    
246
                Literal literal2 = new DefaultLiteral();
247
                literal2.put("Calle", "PASCUAL Y GENIS");
248
                literal2.put("TipoVia", "C");
249
                literal2.put("Municipio", "Valencia");
250
                literal2.put("Provincia", "Valencia");
251
                literal2.put("Pais", "Espanya");
252

    
253
                Literal literal3 = new DefaultLiteral();
254
                literal3.put("Calle", "PEREZ PUJOL");
255
                literal3.put("TipoVia", "C");
256
                literal3.put("Municipio", "Valencia");
257
                literal3.put("Provincia", "Valencia");
258
                literal3.put("Pais", "Espanya");
259

    
260
                Address address = new DefaultComposedAddress(literal);
261

    
262
                List<Literal> intersect = new ArrayList<Literal>();
263
                intersect.add(literal2);
264
                intersect.add(literal3);
265
                ((ComposedAddress) address).setIntersectionLiterals(intersect);
266

    
267
                return address;
268
        }
269

    
270
}