svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / GeocoderTest.java @ 32479
History | View | Annotate | Download (5.68 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.util.Iterator; |
31 |
import java.util.Set; |
32 |
|
33 |
import org.gvsig.LayerServer; |
34 |
import org.gvsig.fmap.dal.DALLocator; |
35 |
import org.gvsig.fmap.dal.DataManager; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.exception.ProviderNotRegisteredException; |
38 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
39 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
40 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
41 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
42 |
import org.gvsig.fmap.geom.primitive.Point; |
43 |
import org.gvsig.geocoding.address.Address; |
44 |
import org.gvsig.geocoding.address.Literal; |
45 |
import org.gvsig.geocoding.address.impl.DefaultAddress; |
46 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
47 |
import org.gvsig.geocoding.impl.DataGeocoderImpl; |
48 |
import org.gvsig.geocoding.impl.DefaultGeocodingLibrary; |
49 |
import org.gvsig.geocoding.pattern.GeocodingPattern; |
50 |
import org.gvsig.geocoding.pattern.GeocodingSettings; |
51 |
import org.gvsig.geocoding.pattern.GeocodingSource; |
52 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern; |
53 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSettings; |
54 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
55 |
import org.gvsig.geocoding.result.GeocodingResult; |
56 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
57 |
import org.gvsig.geocoding.styles.impl.SimpleCentroid; |
58 |
import org.gvsig.tools.ToolsLocator; |
59 |
import org.gvsig.tools.evaluator.Evaluator; |
60 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
61 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPLibrary; |
62 |
import org.gvsig.tools.library.Library; |
63 |
import org.jfree.util.Log; |
64 |
import org.slf4j.Logger; |
65 |
import org.slf4j.LoggerFactory; |
66 |
|
67 |
/**
|
68 |
* Test
|
69 |
*
|
70 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
71 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
|
72 |
*/
|
73 |
public class GeocoderTest extends |
74 |
org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase { |
75 |
|
76 |
protected Logger logger = LoggerFactory.getLogger(getClass());; |
77 |
|
78 |
@Override
|
79 |
protected void doSetUp() throws Exception { |
80 |
Library geocoLib = new DefaultGeocodingLibrary();
|
81 |
geocoLib.initialize(); |
82 |
geocoLib.postInitialize(); |
83 |
|
84 |
Library sql = new SQLJEPLibrary();
|
85 |
sql.initialize(); |
86 |
sql.postInitialize(); |
87 |
|
88 |
DataManager manager = DALLocator.getDataManager(); |
89 |
manager.registerDefaultEvaluator(SQLJEPEvaluator.class); |
90 |
} |
91 |
|
92 |
protected void tearDown() throws Exception { |
93 |
super.tearDown();
|
94 |
} |
95 |
|
96 |
public void testGeocode() throws Exception { |
97 |
|
98 |
/* Set the pattern */
|
99 |
GeocodingPattern pat = getAPattern(); |
100 |
|
101 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
102 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
103 |
((DataGeocoderImpl) geocoder).setStore(LayerServer |
104 |
.getSHPStore(LayerServer.getStreetsFile())); |
105 |
|
106 |
/* Define a address search */
|
107 |
Literal literal = new DefaultLiteral();
|
108 |
literal.put("via", "C"); |
109 |
literal.put("calle", "LIRIA"); |
110 |
|
111 |
Address address = new DefaultAddress(literal);
|
112 |
|
113 |
Set<GeocodingResult> results = geocoder.geocode(address);
|
114 |
assertEquals(554, results.size());
|
115 |
|
116 |
Iterator<GeocodingResult> it = results.iterator();
|
117 |
while (it.hasNext()) {
|
118 |
GeocodingResult res = it.next(); |
119 |
Point pto = (Point) res.getGeometry(); |
120 |
logger.debug("PTO: " + pto.getX() + "," + pto.getY()); |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
public void testFilter() throws Exception { |
126 |
String expression = "STREET_NAM = 'LIRIA'"; |
127 |
FeatureStore store = (FeatureStore) LayerServer.getSHPStore(LayerServer |
128 |
.getStreetsFile()); |
129 |
|
130 |
FeatureQuery query = store.createFeatureQuery(); |
131 |
DataManager manager = DALLocator.getDataManager(); |
132 |
|
133 |
Evaluator filter = manager.createExpresion(expression); |
134 |
assertNotNull(filter); |
135 |
query.setFilter(filter); |
136 |
|
137 |
FeatureSet set = store.getFeatureSet(query); |
138 |
assertNotNull(set); |
139 |
logger.info("SELECTED FEATURES:"+set.getSize());
|
140 |
assertTrue(set.getSize()==2);
|
141 |
} |
142 |
|
143 |
|
144 |
|
145 |
|
146 |
private GeocodingPattern getAPattern() throws InitializeException, |
147 |
ProviderNotRegisteredException, ValidateDataParametersException { |
148 |
|
149 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
150 |
|
151 |
pat.setPatternName("Test pattern");
|
152 |
|
153 |
GeocodingSource source = new DefaultGeocodingSource();
|
154 |
|
155 |
AbstractGeocodingStyle style = new SimpleCentroid();
|
156 |
Literal relations = new DefaultLiteral();
|
157 |
relations.put("via", "STREET_TYP"); |
158 |
relations.put("calle", "STREET_NAM"); |
159 |
|
160 |
style.setRelationsLiteral(relations); |
161 |
source.setStyle(style); |
162 |
source.setLayerName("Streets.shp");
|
163 |
source.setLayerProvider("Shape file");
|
164 |
|
165 |
pat.setSource(source); |
166 |
|
167 |
GeocodingSettings settings = new DefaultGeocodingSettings();
|
168 |
pat.setSettings(settings); |
169 |
|
170 |
return pat;
|
171 |
|
172 |
} |
173 |
} |