Statistics
| Revision:

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

History | View | Annotate | Download (8.86 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.Iterator;
31
import java.util.Set;
32

    
33
import org.gvsig.LayerServer;
34
import org.gvsig.fmap.dal.DataStore;
35
import org.gvsig.fmap.dal.exception.ReadException;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.geocoding.address.Address;
38
import org.gvsig.geocoding.address.Literal;
39
import org.gvsig.geocoding.address.impl.DefaultAddress;
40
import org.gvsig.geocoding.address.impl.DefaultLiteral;
41
import org.gvsig.geocoding.impl.DataGeocoderImpl;
42
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary;
43
import org.gvsig.geocoding.pattern.GeocodingPattern;
44
import org.gvsig.geocoding.pattern.GeocodingSource;
45
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
46
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource;
47
import org.gvsig.geocoding.result.GeocodingResult;
48
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
49
import org.gvsig.geocoding.styles.impl.SimpleCentroid;
50
import org.gvsig.tools.library.Library;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 * Test
56
 * 
57
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
58
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
59
 */
60
public class SimpleCentroidTest extends
61
                org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase {
62

    
63
        private Logger log = LoggerFactory.getLogger(SimpleCentroidTest.class);
64

    
65
        @Override
66
        protected void doSetUp() throws Exception {
67
                Library geocoLib = new DefaultGeocodingLibrary();
68
                geocoLib.initialize();
69
                geocoLib.postInitialize();
70
        }
71

    
72
        protected void tearDown() throws Exception {
73
                super.tearDown();
74
        }
75

    
76
        /**
77
         * Test geocoding by centroid in a points layer
78
         * 
79
         * @throws ReadException
80
         */
81
        public void testPointsGeocode() throws Exception {
82

    
83
                /* Set the pattern */
84
                GeocodingPattern pat = getPatternPoints();
85

    
86
                DataStore store = LayerServer.getSHPStore(LayerServer.getPointsFile());
87

    
88
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
89
                geocoder.setPattern(pat);
90
                 geocoder.setStore(store);
91

    
92
                /* Define a address search */
93
                Address address = getAddressPoints();
94

    
95
                Set<GeocodingResult> results = null;
96
                try {
97
                        results = geocoder.geocode(address);
98
                } catch (Exception e) {
99
                        log.error("Geocoding", e);
100
                }
101
                assertNotNull(results);
102
                assertEquals(5, results.size());
103
                Iterator<GeocodingResult> it = results.iterator();
104
                log.debug("CENTROID POINTS GEOCODING");
105
                while (it.hasNext()) {
106
                        GeocodingResult res = it.next();
107
                        Point pto = (Point) res.getGeometry();
108
                        log.debug("PTO: " + pto.getX() + "," + pto.getY());
109
                }
110
        }
111

    
112
        /**
113
         * Test geocoding by centroid in a lines layer
114
         * 
115
         * @throws ReadException
116
         */
117
        public void testLinesGeocode() throws Exception {
118

    
119
                /* Set the pattern */
120
                GeocodingPattern pat = getPatternLines();
121

    
122
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
123
                ((DataGeocoderImpl) geocoder).setPattern(pat);
124
                ((DataGeocoderImpl) geocoder).setStore(LayerServer
125
                                .getSHPStore(LayerServer.getStreetsFile()));
126

    
127
                /* Define a address search */
128
                Address address = getAddressLines();
129

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

    
148
        /**
149
         * Test geocoding by centroid in a polygons layer
150
         * 
151
         * @throws ReadException
152
         */
153
        public void testPolysGeocode() throws Exception {
154

    
155
                /* Set the pattern */
156
                GeocodingPattern pat = getPatternPolys();
157

    
158
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
159
                ((DataGeocoderImpl) geocoder).setPattern(pat);
160
                ((DataGeocoderImpl) geocoder).setStore(LayerServer
161
                                .getSHPStore(LayerServer.getRegionsFile()));
162

    
163
                /* Define a address search */
164
                Address address = getAddressPolys();
165

    
166
                Set<GeocodingResult> results = null;
167
                try {
168
                        results = geocoder.geocode(address);
169
                } catch (Exception e) {
170
                        log.error("Geocoding", e);
171
                }
172
                assertNotNull(results);
173
                assertEquals(7, results.size());
174
                Iterator<GeocodingResult> it = results.iterator();
175
                log.debug("CENTROID POLYS GEOCODING");
176
                while (it.hasNext()) {
177
                        GeocodingResult res = it.next();
178
                        Point pto = (Point) res.getGeometry();
179
                        log.debug("PTO: " + pto.getX() + "," + pto.getY());
180
                }
181
        }
182

    
183
        /**
184
         * get pattern
185
         * 
186
         * @return
187
         */
188
        private GeocodingPattern getPatternPoints() {
189

    
190
                GeocodingPattern pat = new DefaultGeocodingPattern();
191

    
192
                try {
193
                        pat.setPatternName("SimpleCentroidPoints");
194

    
195
                        GeocodingSource source = new DefaultGeocodingSource();
196

    
197
                        AbstractGeocodingStyle style = new SimpleCentroid();
198
                        Literal relations = new DefaultLiteral();
199
                        relations.put("Provincia", "NOMBRE99");
200
                        relations.put("Pais", "PAIS");
201

    
202
                        style.setRelationsLiteral(relations);
203
                        source.setStyle(style);
204
                        source.setLayerName("Pointss.shp");
205
                        source.setLayerProvider("Shape file");
206

    
207
                        pat.setSource(source);
208

    
209
                } catch (Exception e) {
210
                        log.error("Building a pattern", e);
211
                }
212

    
213
                return pat;
214
        }
215

    
216
        /**
217
         * get pattern
218
         * 
219
         * @return
220
         */
221
        private GeocodingPattern getPatternLines() {
222

    
223
                GeocodingPattern pat = new DefaultGeocodingPattern();
224

    
225
                try {
226
                        pat.setPatternName("SimpleCentroidLines");
227

    
228
                        GeocodingSource source = new DefaultGeocodingSource();
229

    
230
                        AbstractGeocodingStyle style = new SimpleCentroid();
231
                        Literal relations = new DefaultLiteral();
232
                        relations.put("Calle", "STREET_NAM");
233
                        relations.put("TipoVia", "STREET_TYP");
234
                        relations.put("Municipio", "MUNI");
235
                        relations.put("Provincia", "PROV");
236
                        relations.put("Pais", "PAIS");
237

    
238
                        style.setRelationsLiteral(relations);
239
                        source.setStyle(style);
240
                        source.setLayerName("Streets.shp");
241
                        source.setLayerProvider("Shape file");
242

    
243
                        pat.setSource(source);
244

    
245
                } catch (Exception e) {
246
                        log.error("Building a pattern", e);
247
                }
248

    
249
                return pat;
250
        }
251

    
252
        /**
253
         * get pattern
254
         * 
255
         * @return
256
         */
257
        private GeocodingPattern getPatternPolys() {
258

    
259
                GeocodingPattern pat = new DefaultGeocodingPattern();
260

    
261
                try {
262
                        pat.setPatternName("SimpleCentroidPolys");
263

    
264
                        GeocodingSource source = new DefaultGeocodingSource();
265

    
266
                        AbstractGeocodingStyle style = new SimpleCentroid();
267
                        DefaultLiteral relations = new DefaultLiteral();
268
                        relations.put("Provincia", "NOMBRE99");
269
                        relations.put("Pais", "PAIS");
270

    
271
                        style.setRelationsLiteral(relations);
272
                        source.setStyle(style);
273
                        source.setLayerName("prov.shp");
274
                        source.setLayerProvider("Shape file");
275

    
276
                        pat.setSource(source);
277

    
278
                } catch (Exception e) {
279
                        log.error("Building a pattern", e);
280
                }
281

    
282
                return pat;
283
        }
284

    
285
        /**
286
         * get Address Lines
287
         * 
288
         * @return
289
         */
290
        private Address getAddressLines() {
291
                Literal literal = new DefaultLiteral();
292
                literal.put("Calle", "COLON");
293
                literal.put("TipoVia", "c");
294
                literal.put("Municipio", "Valencia");
295
                literal.put("Provincia", "Valencia");
296
                literal.put("Pais", "Espanya");
297

    
298
                Address address = new DefaultAddress(literal);
299
                return address;
300
        }
301

    
302
        /**
303
         * get Address Polys
304
         * 
305
         * @return
306
         */
307
        private DefaultAddress getAddressPolys() {
308
                DefaultLiteral literal = new DefaultLiteral();
309
                literal.put("Provincia", "Pontevedra");
310
                literal.put("Pais", "ESP");
311

    
312
                DefaultAddress address = new DefaultAddress(literal);
313
                return address;
314
        }
315

    
316
        /**
317
         * get Address Polys
318
         * 
319
         * @return
320
         */
321
        private DefaultAddress getAddressPoints() {
322
                DefaultLiteral literal = new DefaultLiteral();
323
                literal.put("Provincia", "Ourense");
324
                literal.put("Pais", "ESP");
325

    
326
                DefaultAddress address = new DefaultAddress(literal);
327
                return address;
328
        }
329

    
330
}