svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / SimpleRangeTest.java @ 29385
History | View | Annotate | Download (6.84 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.Iterator; |
32 |
import java.util.Set; |
33 |
|
34 |
import junit.framework.TestCase; |
35 |
|
36 |
import org.gvsig.fmap.dal.DALFileLibrary; |
37 |
import org.gvsig.fmap.dal.DALLibrary; |
38 |
import org.gvsig.fmap.dal.DALLocator; |
39 |
import org.gvsig.fmap.dal.DataManager; |
40 |
import org.gvsig.fmap.dal.exception.ReadException; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
43 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
44 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
45 |
import org.gvsig.fmap.geom.GeometryLibrary; |
46 |
import org.gvsig.fmap.geom.primitive.Point; |
47 |
import org.gvsig.geocoding.address.Address; |
48 |
import org.gvsig.geocoding.address.Literal; |
49 |
import org.gvsig.geocoding.address.NumberAddress; |
50 |
import org.gvsig.geocoding.address.RelationsComponent; |
51 |
import org.gvsig.geocoding.address.impl.DefaultAddressComponent; |
52 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
53 |
import org.gvsig.geocoding.address.impl.DefaultNumberAddress; |
54 |
import org.gvsig.geocoding.address.impl.DefaultRelationsComponent; |
55 |
import org.gvsig.geocoding.impl.DataGeocoderImpl; |
56 |
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary; |
57 |
import org.gvsig.geocoding.pattern.GeocodingSource; |
58 |
import org.gvsig.geocoding.pattern.Patterngeocoding; |
59 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
60 |
import org.gvsig.geocoding.pattern.impl.DefaultPatterngeocoding; |
61 |
import org.gvsig.geocoding.result.GeocodingResult; |
62 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
63 |
import org.gvsig.geocoding.styles.impl.SimpleRange; |
64 |
import org.gvsig.tools.ToolsLibrary; |
65 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
66 |
import org.gvsig.tools.locator.Library; |
67 |
import org.slf4j.Logger; |
68 |
import org.slf4j.LoggerFactory; |
69 |
|
70 |
/**
|
71 |
* Test
|
72 |
*
|
73 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
74 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
75 |
*/
|
76 |
public class SimpleRangeTest extends TestCase { |
77 |
|
78 |
protected DataManager dataManager = null; |
79 |
private File shape = new File("test-data/geocoder/streets.shp"); |
80 |
private Logger log = LoggerFactory.getLogger(SimpleRangeTest.class); |
81 |
|
82 |
protected void setUp() throws Exception { |
83 |
super.setUp();
|
84 |
|
85 |
Library tools = new ToolsLibrary();
|
86 |
tools.initialize(); |
87 |
tools.postInitialize(); |
88 |
|
89 |
Library dlib = new DALLibrary();
|
90 |
dlib.initialize(); |
91 |
dlib.postInitialize(); |
92 |
|
93 |
Library libFile = new DALFileLibrary();
|
94 |
libFile.initialize(); |
95 |
libFile.postInitialize(); |
96 |
|
97 |
Library lib = new GeometryLibrary();
|
98 |
lib.initialize(); |
99 |
lib.postInitialize(); |
100 |
|
101 |
Library shpLib = new SHPLibrary();
|
102 |
shpLib.initialize(); |
103 |
shpLib.postInitialize(); |
104 |
|
105 |
Library geocoLib = new DefaultGeocodingLibrary();
|
106 |
geocoLib.initialize(); |
107 |
geocoLib.postInitialize(); |
108 |
|
109 |
dataManager = DALLocator.getDataManager(); |
110 |
} |
111 |
|
112 |
protected void tearDown() throws Exception { |
113 |
super.tearDown();
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Test
|
118 |
*
|
119 |
* @throws ReadException
|
120 |
*/
|
121 |
public void testGeocode() throws ReadException { |
122 |
|
123 |
/* Set the pattern */
|
124 |
Patterngeocoding pat = getPattern(); |
125 |
|
126 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
127 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
128 |
|
129 |
Address address = getAddress(); |
130 |
|
131 |
Set<GeocodingResult> results = null; |
132 |
try {
|
133 |
results = geocoder.geocode(address); |
134 |
} catch (Exception e) { |
135 |
log.error("Geocoding", e);
|
136 |
} |
137 |
assertNotNull(results); |
138 |
assertEquals(2, results.size());
|
139 |
Iterator<GeocodingResult> it = results.iterator();
|
140 |
log.debug("SIMPLE RANGE 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 |
* get the pattern
|
150 |
*
|
151 |
* @return
|
152 |
*/
|
153 |
private Patterngeocoding getPattern() {
|
154 |
|
155 |
assertNotNull(dataManager); |
156 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
157 |
|
158 |
Patterngeocoding pat = new DefaultPatterngeocoding();
|
159 |
|
160 |
try {
|
161 |
pat.setPatternName("SimpleRange");
|
162 |
|
163 |
GeocodingSource source = new DefaultGeocodingSource();
|
164 |
|
165 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
166 |
.createStoreParameters(SHPStoreProvider.NAME); |
167 |
assertNotNull(shape); |
168 |
params.setFile(shape); |
169 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
170 |
source.setLayerSource(store); |
171 |
|
172 |
AbstractGeocodingStyle style = new SimpleRange();
|
173 |
DefaultLiteral relations = new DefaultLiteral();
|
174 |
relations.add(new DefaultRelationsComponent("Calle", "STREET_NAM")); |
175 |
relations |
176 |
.add(new DefaultRelationsComponent("TipoVia", "STREET_TYP")); |
177 |
relations.add(new DefaultRelationsComponent("Municipio", "MUNI")); |
178 |
relations.add(new DefaultRelationsComponent("Provincia", "PROV")); |
179 |
relations.add(new DefaultRelationsComponent("Pais", "PAIS")); |
180 |
|
181 |
style.setRelationsLiteral(relations); |
182 |
source.setStyle(style); |
183 |
|
184 |
RelationsComponent from = new DefaultRelationsComponent("NParFrom", |
185 |
"RF_ADD");
|
186 |
; |
187 |
((SimpleRange) style).setFirstNumber(from); |
188 |
|
189 |
RelationsComponent to = new DefaultRelationsComponent("NParTo", |
190 |
"RT_ADD");
|
191 |
((SimpleRange) style).setLastNumber(to); |
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 getAddress() {
|
208 |
Literal literal = new DefaultLiteral();
|
209 |
literal.add(new DefaultAddressComponent("Calle", "COLON")); |
210 |
literal.add(new DefaultAddressComponent("TipoVia", "C")); |
211 |
literal.add(new DefaultAddressComponent("Municipio", "Valencia")); |
212 |
literal.add(new DefaultAddressComponent("Provincia", "Valencia")); |
213 |
literal.add(new DefaultAddressComponent("Pais", "Espanya")); |
214 |
|
215 |
Address address = new DefaultNumberAddress(literal);
|
216 |
((NumberAddress) address).setNumber(56);
|
217 |
return address;
|
218 |
} |
219 |
|
220 |
} |