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