svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / SimpleRangeTest.java @ 28375
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.feature.FeatureType; |
43 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
44 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
45 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
46 |
import org.gvsig.fmap.geom.GeometryLibrary; |
47 |
import org.gvsig.fmap.geom.primitive.Point; |
48 |
import org.gvsig.geocoding.address.Address; |
49 |
import org.gvsig.geocoding.address.Literal; |
50 |
import org.gvsig.geocoding.address.NumberAddress; |
51 |
import org.gvsig.geocoding.address.RelationsComponent; |
52 |
import org.gvsig.geocoding.address.impl.DefaultAddressComponent; |
53 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
54 |
import org.gvsig.geocoding.address.impl.DefaultNumberAddress; |
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.SimpleRange; |
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 SimpleRangeTest extends TestCase { |
78 |
|
79 |
protected DataManager dataManager = null; |
80 |
private File shape = new File("./test-data/geocoder/streets.shp"); |
81 |
private Logger log = LoggerFactory.getLogger(SimpleRangeTest.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 lib = new GeometryLibrary();
|
99 |
lib.initialize(); |
100 |
lib.postInitialize(); |
101 |
|
102 |
Library shpLib = new SHPLibrary();
|
103 |
shpLib.initialize(); |
104 |
shpLib.postInitialize(); |
105 |
|
106 |
Library geocoLib = new DefaultGeocodingLibrary();
|
107 |
geocoLib.initialize(); |
108 |
geocoLib.postInitialize(); |
109 |
|
110 |
dataManager = DALLocator.getDataManager(); |
111 |
} |
112 |
|
113 |
protected void tearDown() throws Exception { |
114 |
super.tearDown();
|
115 |
} |
116 |
|
117 |
/**
|
118 |
* Test
|
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 |
* @return
|
151 |
*/
|
152 |
private Patterngeocoding getPattern() {
|
153 |
|
154 |
assertNotNull(dataManager); |
155 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
156 |
|
157 |
Patterngeocoding pat = new DefaultPatterngeocoding();
|
158 |
|
159 |
try {
|
160 |
pat.setPatternName("SimpleRange");
|
161 |
|
162 |
GeocodingSource source = new DefaultGeocodingSource();
|
163 |
|
164 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
165 |
.createStoreParameters(SHPStoreProvider.NAME); |
166 |
assertNotNull(shape); |
167 |
params.setFile(shape); |
168 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
169 |
source.setLayerSource(store); |
170 |
|
171 |
AbstractGeocodingStyle style = new SimpleRange();
|
172 |
DefaultLiteral relations = new DefaultLiteral();
|
173 |
relations.add(new DefaultRelationsComponent("Calle","STREET_NAM")); |
174 |
relations.add(new DefaultRelationsComponent("TipoVia","STREET_TYP")); |
175 |
relations.add(new DefaultRelationsComponent("Municipio","MUNI")); |
176 |
relations.add(new DefaultRelationsComponent("Provincia","PROV")); |
177 |
relations.add(new DefaultRelationsComponent("Pais","PAIS")); |
178 |
|
179 |
style.setRelationsLiteral(relations); |
180 |
source.setStyle(style); |
181 |
|
182 |
RelationsComponent from = new DefaultRelationsComponent("NParFrom","RF_ADD"); |
183 |
; |
184 |
((SimpleRange) style).setFirstNumber(from); |
185 |
|
186 |
RelationsComponent to = new DefaultRelationsComponent("NParTo","RT_ADD"); |
187 |
((SimpleRange) style).setLastNumber(to); |
188 |
|
189 |
pat.setSource(source); |
190 |
|
191 |
} catch (Exception e) { |
192 |
log.error("Building a pattern", e);
|
193 |
} |
194 |
|
195 |
return pat;
|
196 |
} |
197 |
|
198 |
/**
|
199 |
* get Address
|
200 |
*
|
201 |
* @return
|
202 |
*/
|
203 |
private Address getAddress() {
|
204 |
Literal literal = new DefaultLiteral();
|
205 |
literal.add(new DefaultAddressComponent("Calle", "COLON")); |
206 |
literal.add(new DefaultAddressComponent("TipoVia", "C")); |
207 |
literal.add(new DefaultAddressComponent("Municipio", "Valencia")); |
208 |
literal.add(new DefaultAddressComponent("Provincia", "Valencia")); |
209 |
literal.add(new DefaultAddressComponent("Pais", "Espanya")); |
210 |
|
211 |
Address address = new DefaultNumberAddress(literal);
|
212 |
((NumberAddress)address).setNumber(56);
|
213 |
return address;
|
214 |
} |
215 |
|
216 |
} |