Statistics
| Revision:

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

History | View | Annotate | Download (5.05 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.geom.primitive.Point;
35
import org.gvsig.geocoding.address.Address;
36
import org.gvsig.geocoding.address.Literal;
37
import org.gvsig.geocoding.address.NumberAddress;
38
import org.gvsig.geocoding.address.impl.DefaultLiteral;
39
import org.gvsig.geocoding.address.impl.DefaultNumberAddress;
40
import org.gvsig.geocoding.impl.DataGeocoderImpl;
41
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary;
42
import org.gvsig.geocoding.pattern.GeocodingPattern;
43
import org.gvsig.geocoding.pattern.GeocodingSource;
44
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern;
45
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource;
46
import org.gvsig.geocoding.result.GeocodingResult;
47
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
48
import org.gvsig.geocoding.styles.impl.DoubleRange;
49
import org.gvsig.tools.library.Library;
50
import org.slf4j.Logger;
51
import org.slf4j.LoggerFactory;
52

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

    
62
        private Logger log = LoggerFactory.getLogger(DoubleRangeTest.class);
63

    
64
        @Override
65
        protected void doSetUp() throws Exception {
66

    
67
                Library geocoLib = new DefaultGeocodingLibrary();
68
                geocoLib.initialize();
69
                geocoLib.postInitialize();
70

    
71
        }
72

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

    
77
        public void testGeocode() throws Exception {
78

    
79
                /* Set the pattern */
80
                GeocodingPattern pat = getPattern();
81

    
82
                Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder();
83
                ((DataGeocoderImpl) geocoder).setPattern(pat);
84
                ((DataGeocoderImpl) geocoder).setStore(LayerServer
85
                                .getSHPStore(LayerServer.getStreetsFile()));
86

    
87
                /* Define a address search */
88
                Address address = getAddress();
89

    
90
                Set<GeocodingResult> results = null;
91
                try {
92
                        results = geocoder.geocode(address);
93
                } catch (Exception e) {
94
                        log.error("Geocoding", e);
95
                }
96
                assertNotNull(results);
97
                Iterator<GeocodingResult> it = results.iterator();
98
                log.debug("DOUBLE RANGE GEOCODING");
99
                while (it.hasNext()) {
100
                        GeocodingResult res = it.next();
101
                        Point pto = (Point) res.getGeometry();
102
                        log.debug("PTO: " + pto.getX() + "," + pto.getY());
103
                }
104
        }
105

    
106
        /**
107
         * Get pattern
108
         */
109
        private GeocodingPattern getPattern() {
110

    
111
                GeocodingPattern pat = new DefaultGeocodingPattern();
112

    
113
                try {
114
                        pat.setPatternName("DoubleRange");
115

    
116
                        GeocodingSource source = new DefaultGeocodingSource();
117

    
118
                        AbstractGeocodingStyle style = new DoubleRange();
119
                        Literal relations = new DefaultLiteral();
120
                        relations.put("Calle", "STREET_NAM");
121
                        relations.put("TipoVia", "STREET_TYP");
122
                        relations.put("Municipio", "MUNI");
123
                        relations.put("Provincia", "PROV");
124
                        relations.put("Pais", "PAIS");
125
                        relations.put("NParFrom", "RF_ADD");
126
                        relations.put("NParTo", "RT_ADD");
127
                        relations.put("NImparFrom", "LF_ADD");
128
                        relations.put("NImparTo", "LT_ADD");
129

    
130
                        style.setRelationsLiteral(relations);
131

    
132
                        DoubleRange dstyle = (DoubleRange) style;
133
                        dstyle.setFirstRightNumber("NParFrom");
134
                        dstyle.setLastRightNumber("NParTo");
135
                        dstyle.setFirstLeftNumber("NImparFrom");
136
                        dstyle.setLastLeftNumber("NImparTo");
137

    
138
                        source.setStyle(style);
139

    
140
                        source.setLayerName("Streets.shp");
141
                        source.setLayerProvider("Shape file");
142
                        pat.setSource(source);
143

    
144
                } catch (Exception e) {
145
                        log.error("Building a pattern", e);
146
                }
147

    
148
                return pat;
149
        }
150

    
151
        /**
152
         * get Address
153
         * 
154
         * @return
155
         */
156
        private Address getAddress() {
157
                Literal literal = new DefaultLiteral();
158
                literal.put("Calle", "COLON");
159
                literal.put("TipoVia", "C");
160
                literal.put("Municipio", "Valencia");
161
                literal.put("Provincia", "Valencia");
162
                literal.put("Pais", "Espanya");
163

    
164
                Address address = new DefaultNumberAddress(literal);
165
                ((NumberAddress) address).setNumber(35);
166
                return address;
167
        }
168

    
169
}