root / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / SimpleCentroidTest.java @ 31082
History | View | Annotate | Download (11.4 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.impl.DefaultAddress; |
51 |
import org.gvsig.geocoding.address.impl.DefaultAddressComponent; |
52 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
53 |
import org.gvsig.geocoding.address.impl.DefaultRelationsComponent; |
54 |
import org.gvsig.geocoding.impl.DataGeocoderImpl; |
55 |
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary; |
56 |
import org.gvsig.geocoding.pattern.GeocodingSource; |
57 |
import org.gvsig.geocoding.pattern.GeocodingPattern; |
58 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
59 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern; |
60 |
import org.gvsig.geocoding.result.GeocodingResult; |
61 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
62 |
import org.gvsig.geocoding.styles.impl.SimpleCentroid; |
63 |
import org.gvsig.tools.ToolsLibrary; |
64 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
65 |
import org.gvsig.tools.library.Library; |
66 |
import org.slf4j.Logger; |
67 |
import org.slf4j.LoggerFactory; |
68 |
|
69 |
/**
|
70 |
* Test
|
71 |
*
|
72 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
73 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
|
74 |
*/
|
75 |
public class SimpleCentroidTest extends TestCase { |
76 |
|
77 |
protected DataManager dataManager = null; |
78 |
private File streets = new File("test-data/geocoder/streets.shp"); |
79 |
private File prov = new File("test-data/geocoder/prov.shp"); |
80 |
private File points = new File("test-data/geocoder/pointss.shp"); |
81 |
private Logger log = LoggerFactory.getLogger(SimpleCentroidTest.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 |
/**
|
119 |
* Test geocoding by centroid in a polygons layer
|
120 |
* @throws ReadException
|
121 |
*/
|
122 |
public void testPointsGeocode() throws ReadException { |
123 |
|
124 |
/* Set the pattern */
|
125 |
GeocodingPattern pat = getPatternPoints(); |
126 |
|
127 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
128 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
129 |
|
130 |
/* Define a address search */
|
131 |
Address address = getAddressPoints(); |
132 |
|
133 |
Set<GeocodingResult> results = null; |
134 |
try {
|
135 |
results = geocoder.geocode(address); |
136 |
} catch (Exception e) { |
137 |
log.error("Geocoding", e);
|
138 |
} |
139 |
assertNotNull(results); |
140 |
assertEquals(5, results.size());
|
141 |
Iterator<GeocodingResult> it = results.iterator();
|
142 |
log.debug("CENTROID POINTS GEOCODING");
|
143 |
while (it.hasNext()) {
|
144 |
GeocodingResult res = it.next(); |
145 |
Point pto = (Point) res.getGeometry(); |
146 |
log.debug("PTO: " + pto.getX() + "," + pto.getY()); |
147 |
} |
148 |
} |
149 |
|
150 |
|
151 |
/**
|
152 |
* Test geocoding by centroid in a lines layer
|
153 |
* @throws ReadException
|
154 |
*/
|
155 |
public void testLinesGeocode() throws ReadException { |
156 |
|
157 |
/* Set the pattern */
|
158 |
GeocodingPattern pat = getPatternLines(); |
159 |
|
160 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
161 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
162 |
|
163 |
/* Define a address search */
|
164 |
Address address = getAddressLines(); |
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 |
int si = results.size();
|
174 |
assertEquals(2, si);
|
175 |
Iterator<GeocodingResult> it = results.iterator();
|
176 |
log.debug("CENTROID LINES GEOCODING");
|
177 |
while (it.hasNext()) {
|
178 |
GeocodingResult res = it.next(); |
179 |
Point pto = (Point) res.getGeometry(); |
180 |
log.debug("PTO: " + pto.getX() + "," + pto.getY()); |
181 |
} |
182 |
} |
183 |
|
184 |
/**
|
185 |
* Test geocoding by centroid in a polygons layer
|
186 |
* @throws ReadException
|
187 |
*/
|
188 |
public void testPolysGeocode() throws ReadException { |
189 |
|
190 |
/* Set the pattern */
|
191 |
GeocodingPattern pat = getPatternPolys(); |
192 |
|
193 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
194 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
195 |
|
196 |
/* Define a address search */
|
197 |
Address address = getAddressPolys(); |
198 |
|
199 |
Set<GeocodingResult> results = null; |
200 |
try {
|
201 |
results = geocoder.geocode(address); |
202 |
} catch (Exception e) { |
203 |
log.error("Geocoding", e);
|
204 |
} |
205 |
assertNotNull(results); |
206 |
assertEquals(7, results.size());
|
207 |
Iterator<GeocodingResult> it = results.iterator();
|
208 |
log.debug("CENTROID POLYS GEOCODING");
|
209 |
while (it.hasNext()) {
|
210 |
GeocodingResult res = it.next(); |
211 |
Point pto = (Point) res.getGeometry(); |
212 |
log.debug("PTO: " + pto.getX() + "," + pto.getY()); |
213 |
} |
214 |
} |
215 |
|
216 |
/**
|
217 |
* get pattern
|
218 |
*
|
219 |
* @return
|
220 |
*/
|
221 |
private GeocodingPattern getPatternPoints() {
|
222 |
|
223 |
assertNotNull(dataManager); |
224 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
225 |
|
226 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
227 |
|
228 |
try {
|
229 |
pat.setPatternName("SimpleCentroidPoints");
|
230 |
|
231 |
GeocodingSource source = new DefaultGeocodingSource();
|
232 |
|
233 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
234 |
.createStoreParameters(SHPStoreProvider.NAME); |
235 |
assertNotNull(points); |
236 |
params.setFile(points); |
237 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
238 |
source.setStore(store); |
239 |
|
240 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
241 |
Literal relations = new DefaultLiteral();
|
242 |
relations.add(new DefaultRelationsComponent("Provincia","NOMBRE99")); |
243 |
relations.add(new DefaultRelationsComponent("Pais","PAIS")); |
244 |
|
245 |
style.setRelationsLiteral(relations); |
246 |
source.setStyle(style); |
247 |
|
248 |
pat.setSource(source); |
249 |
|
250 |
} catch (Exception e) { |
251 |
log.error("Building a pattern", e);
|
252 |
} |
253 |
|
254 |
return pat;
|
255 |
} |
256 |
|
257 |
/**
|
258 |
* get pattern
|
259 |
*
|
260 |
* @return
|
261 |
*/
|
262 |
private GeocodingPattern getPatternLines() {
|
263 |
|
264 |
assertNotNull(dataManager); |
265 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
266 |
|
267 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
268 |
|
269 |
try {
|
270 |
pat.setPatternName("SimpleCentroidLines");
|
271 |
|
272 |
GeocodingSource source = new DefaultGeocodingSource();
|
273 |
|
274 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
275 |
.createStoreParameters(SHPStoreProvider.NAME); |
276 |
assertNotNull(streets); |
277 |
params.setFile(streets); |
278 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
279 |
source.setStore(store); |
280 |
|
281 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
282 |
Literal relations = new DefaultLiteral();
|
283 |
relations.add(new DefaultRelationsComponent("Calle","STREET_NAM")); |
284 |
relations.add(new DefaultRelationsComponent("TipoVia","STREET_TYP")); |
285 |
relations.add(new DefaultRelationsComponent("Municipio","MUNI")); |
286 |
relations.add(new DefaultRelationsComponent("Provincia","PROV")); |
287 |
relations.add(new DefaultRelationsComponent("Pais","PAIS")); |
288 |
|
289 |
style.setRelationsLiteral(relations); |
290 |
source.setStyle(style); |
291 |
|
292 |
pat.setSource(source); |
293 |
|
294 |
} catch (Exception e) { |
295 |
log.error("Building a pattern", e);
|
296 |
} |
297 |
|
298 |
return pat;
|
299 |
} |
300 |
|
301 |
/**
|
302 |
* get pattern
|
303 |
*
|
304 |
* @return
|
305 |
*/
|
306 |
private GeocodingPattern getPatternPolys() {
|
307 |
|
308 |
assertNotNull(dataManager); |
309 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
310 |
|
311 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
312 |
|
313 |
try {
|
314 |
pat.setPatternName("SimpleCentroidPolys");
|
315 |
|
316 |
GeocodingSource source = new DefaultGeocodingSource();
|
317 |
|
318 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
319 |
.createStoreParameters(SHPStoreProvider.NAME); |
320 |
assertNotNull(prov); |
321 |
params.setFile(prov); |
322 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
323 |
source.setStore(store); |
324 |
|
325 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
326 |
FeatureType ftype = store.getDefaultFeatureType(); |
327 |
DefaultLiteral relations = new DefaultLiteral();
|
328 |
relations.add(new DefaultRelationsComponent("Provincia", "NOMBRE99")); |
329 |
relations.add(new DefaultRelationsComponent("Pais","PAIS")); |
330 |
|
331 |
style.setRelationsLiteral(relations); |
332 |
source.setStyle(style); |
333 |
|
334 |
pat.setSource(source); |
335 |
|
336 |
} catch (Exception e) { |
337 |
log.error("Building a pattern", e);
|
338 |
} |
339 |
|
340 |
return pat;
|
341 |
} |
342 |
|
343 |
/**
|
344 |
* get Address Lines
|
345 |
*
|
346 |
* @return
|
347 |
*/
|
348 |
private Address getAddressLines() {
|
349 |
Literal literal = new DefaultLiteral();
|
350 |
literal.add(new DefaultAddressComponent("Calle", "COLON")); |
351 |
literal.add(new DefaultAddressComponent("TipoVia", "c")); |
352 |
literal.add(new DefaultAddressComponent("Municipio", "Valencia")); |
353 |
literal.add(new DefaultAddressComponent("Provincia", "Valencia")); |
354 |
literal.add(new DefaultAddressComponent("Pais", "Espanya")); |
355 |
|
356 |
Address address = new DefaultAddress(literal);
|
357 |
return address;
|
358 |
} |
359 |
|
360 |
/**
|
361 |
* get Address Polys
|
362 |
*
|
363 |
* @return
|
364 |
*/
|
365 |
private DefaultAddress getAddressPolys() {
|
366 |
DefaultLiteral literal = new DefaultLiteral();
|
367 |
literal.add(new DefaultAddressComponent("Provincia", "Pontevedra")); |
368 |
literal.add(new DefaultAddressComponent("Pais", "ESP")); |
369 |
|
370 |
DefaultAddress address = new DefaultAddress(literal);
|
371 |
return address;
|
372 |
} |
373 |
|
374 |
/**
|
375 |
* get Address Polys
|
376 |
*
|
377 |
* @return
|
378 |
*/
|
379 |
private DefaultAddress getAddressPoints() {
|
380 |
DefaultLiteral literal = new DefaultLiteral();
|
381 |
literal.add(new DefaultAddressComponent("Provincia", "Ourense")); |
382 |
literal.add(new DefaultAddressComponent("Pais", "ESP")); |
383 |
|
384 |
DefaultAddress address = new DefaultAddress(literal);
|
385 |
return address;
|
386 |
} |
387 |
|
388 |
} |