svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src-test / org / gvsig / geocoding / GeocoderTest.java @ 29385
History | View | Annotate | Download (6.49 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.DataException; |
41 |
import org.gvsig.fmap.dal.exception.InitializeException; |
42 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
43 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.fmap.dal.store.shp.SHPLibrary; |
46 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
47 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
48 |
import org.gvsig.fmap.geom.impl.DefaultGeometryLibrary; |
49 |
import org.gvsig.fmap.geom.primitive.Point; |
50 |
import org.gvsig.geocoding.address.Address; |
51 |
import org.gvsig.geocoding.address.Literal; |
52 |
import org.gvsig.geocoding.address.impl.DefaultAddress; |
53 |
import org.gvsig.geocoding.address.impl.DefaultAddressComponent; |
54 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
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.GeocodingSettings; |
59 |
import org.gvsig.geocoding.pattern.GeocodingSource; |
60 |
import org.gvsig.geocoding.pattern.Patterngeocoding; |
61 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSettings; |
62 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
63 |
import org.gvsig.geocoding.pattern.impl.DefaultPatterngeocoding; |
64 |
import org.gvsig.geocoding.result.GeocodingResult; |
65 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
66 |
import org.gvsig.geocoding.styles.impl.SimpleCentroid; |
67 |
import org.gvsig.tools.ToolsLibrary; |
68 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
69 |
import org.gvsig.tools.locator.Library; |
70 |
import org.gvsig.tools.locator.LocatorException; |
71 |
import org.slf4j.Logger; |
72 |
import org.slf4j.LoggerFactory; |
73 |
|
74 |
/**
|
75 |
* Test
|
76 |
*
|
77 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
78 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
79 |
*/
|
80 |
public class GeocoderTest extends TestCase { |
81 |
|
82 |
protected DataManager dataManager = null; |
83 |
private File file = new File("test-data/geocoder/streets.shp"); |
84 |
private Logger log = LoggerFactory.getLogger(GeocoderTest.class); |
85 |
|
86 |
protected void setUp() throws Exception { |
87 |
super.setUp();
|
88 |
|
89 |
Library tools = new ToolsLibrary();
|
90 |
tools.initialize(); |
91 |
tools.postInitialize(); |
92 |
|
93 |
Library dlib = new DALLibrary();
|
94 |
dlib.initialize(); |
95 |
dlib.postInitialize(); |
96 |
|
97 |
Library libFile = new DALFileLibrary();
|
98 |
libFile.initialize(); |
99 |
libFile.postInitialize(); |
100 |
|
101 |
Library lib = new DefaultGeometryLibrary();
|
102 |
lib.initialize(); |
103 |
lib.postInitialize(); |
104 |
|
105 |
Library shpLib = new SHPLibrary();
|
106 |
shpLib.initialize(); |
107 |
shpLib.postInitialize(); |
108 |
|
109 |
Library geocoLib = new DefaultGeocodingLibrary();
|
110 |
geocoLib.initialize(); |
111 |
geocoLib.postInitialize(); |
112 |
|
113 |
dataManager = DALLocator.getDataManager(); |
114 |
} |
115 |
|
116 |
protected void tearDown() throws Exception { |
117 |
super.tearDown();
|
118 |
} |
119 |
|
120 |
public void testGeocode() throws InitializeException, LocatorException, |
121 |
DataException, ValidateDataParametersException { |
122 |
|
123 |
/* Set the pattern */
|
124 |
Patterngeocoding pat = getAPattern(); |
125 |
|
126 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
127 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
128 |
|
129 |
/* Define a address search */
|
130 |
Literal literal = new DefaultLiteral();
|
131 |
literal.add(new DefaultAddressComponent("via", "C")); |
132 |
literal.add(new DefaultAddressComponent("calle", "LIRIA")); |
133 |
|
134 |
Address address = new DefaultAddress(literal);
|
135 |
|
136 |
Set<GeocodingResult> results = geocoder.geocode(address);
|
137 |
assertEquals(2, results.size());
|
138 |
|
139 |
Iterator<GeocodingResult> it = results.iterator();
|
140 |
while (it.hasNext()) {
|
141 |
GeocodingResult res = it.next(); |
142 |
Point pto = (Point) res.getGeometry(); |
143 |
log.debug("PTO: " + pto.getX() + "," + pto.getY()); |
144 |
} |
145 |
|
146 |
} |
147 |
|
148 |
private Patterngeocoding getAPattern() throws InitializeException, |
149 |
ProviderNotRegisteredException, ValidateDataParametersException { |
150 |
|
151 |
assertNotNull(dataManager); |
152 |
dataManager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
153 |
|
154 |
Patterngeocoding pat = new DefaultPatterngeocoding();
|
155 |
|
156 |
pat.setPatternName("Test pattern");
|
157 |
|
158 |
GeocodingSource source = new DefaultGeocodingSource();
|
159 |
|
160 |
SHPStoreParameters params = (SHPStoreParameters) dataManager |
161 |
.createStoreParameters(SHPStoreProvider.NAME); |
162 |
System.out.println(file.getAbsolutePath());
|
163 |
assertNotNull(file); |
164 |
params.setFile(file); |
165 |
FeatureStore store = (FeatureStore) dataManager.createStore(params); |
166 |
source.setLayerSource(store); |
167 |
|
168 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
169 |
Literal relations = new DefaultLiteral();
|
170 |
relations.add(new DefaultRelationsComponent("via", "STREET_TYP")); |
171 |
relations.add(new DefaultRelationsComponent("calle", "STREET_NAM")); |
172 |
relations.add(new DefaultRelationsComponent("nparfrom", "RF_ADD")); |
173 |
relations.add(new DefaultRelationsComponent("nparto", "RT_ADD")); |
174 |
relations.add(new DefaultRelationsComponent("nimparfrom", "LF_ADD")); |
175 |
relations.add(new DefaultRelationsComponent("nimparto", "LT_ADD")); |
176 |
|
177 |
style.setRelationsLiteral(relations); |
178 |
source.setStyle(style); |
179 |
|
180 |
pat.setSource(source); |
181 |
|
182 |
GeocodingSettings settings = new DefaultGeocodingSettings();
|
183 |
pat.setSettings(settings); |
184 |
|
185 |
return pat;
|
186 |
|
187 |
} |
188 |
|
189 |
} |