svn-gvsig-desktop / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src-test / org / gvsig / geocoding / SimpleRangeTest.java @ 32479
History | View | Annotate | Download (5.39 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 org.gvsig.LayerServer; |
35 |
import org.gvsig.fmap.dal.DALLocator; |
36 |
import org.gvsig.fmap.dal.DataManager; |
37 |
import org.gvsig.fmap.dal.exception.ReadException; |
38 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
39 |
import org.gvsig.fmap.dal.store.shp.SHPStoreParameters; |
40 |
import org.gvsig.fmap.dal.store.shp.SHPStoreProvider; |
41 |
import org.gvsig.fmap.geom.primitive.Point; |
42 |
import org.gvsig.geocoding.address.Address; |
43 |
import org.gvsig.geocoding.address.Literal; |
44 |
import org.gvsig.geocoding.address.NumberAddress; |
45 |
import org.gvsig.geocoding.address.impl.DefaultLiteral; |
46 |
import org.gvsig.geocoding.address.impl.DefaultNumberAddress; |
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.GeocodingSource; |
51 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingPattern; |
52 |
import org.gvsig.geocoding.pattern.impl.DefaultGeocodingSource; |
53 |
import org.gvsig.geocoding.result.GeocodingResult; |
54 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
55 |
import org.gvsig.geocoding.styles.impl.SimpleRange; |
56 |
import org.gvsig.tools.evaluator.sqljep.SQLJEPEvaluator; |
57 |
import org.gvsig.tools.library.Library; |
58 |
import org.slf4j.Logger; |
59 |
import org.slf4j.LoggerFactory; |
60 |
|
61 |
/**
|
62 |
* Test
|
63 |
*
|
64 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
65 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicent Sanjaime Calvet</a>
|
66 |
*/
|
67 |
public class SimpleRangeTest extends |
68 |
org.gvsig.tools.junit.AbstractLibraryAutoInitTestCase { |
69 |
|
70 |
private Logger log = LoggerFactory.getLogger(SimpleRangeTest.class); |
71 |
|
72 |
@Override
|
73 |
protected void doSetUp() throws Exception { |
74 |
Library geocoLib = new DefaultGeocodingLibrary();
|
75 |
geocoLib.initialize(); |
76 |
geocoLib.postInitialize(); |
77 |
} |
78 |
|
79 |
protected void tearDown() throws Exception { |
80 |
super.tearDown();
|
81 |
} |
82 |
|
83 |
/**
|
84 |
* Test
|
85 |
*
|
86 |
* @throws ReadException
|
87 |
*/
|
88 |
public void testGeocode() throws Exception { |
89 |
|
90 |
/* Set the pattern */
|
91 |
GeocodingPattern pat = getPattern(); |
92 |
|
93 |
Geocoder geocoder = GeocodingLocator.getInstance().getGeocoder(); |
94 |
((DataGeocoderImpl) geocoder).setPattern(pat); |
95 |
((DataGeocoderImpl) geocoder).setStore(LayerServer |
96 |
.getSHPStore(LayerServer.getStreetsFile())); |
97 |
|
98 |
Address address = getAddress(); |
99 |
|
100 |
Set<GeocodingResult> results = null; |
101 |
try {
|
102 |
results = geocoder.geocode(address); |
103 |
} catch (Exception e) { |
104 |
log.error("Geocoding", e);
|
105 |
} |
106 |
assertNotNull(results); |
107 |
assertEquals(2, results.size());
|
108 |
Iterator<GeocodingResult> it = results.iterator();
|
109 |
log.debug("SIMPLE RANGE GEOCODING");
|
110 |
while (it.hasNext()) {
|
111 |
GeocodingResult res = it.next(); |
112 |
Point pto = (Point) res.getGeometry(); |
113 |
log.debug("PTO: " + pto.getX() + "," + pto.getY()); |
114 |
} |
115 |
} |
116 |
|
117 |
/**
|
118 |
* get the pattern
|
119 |
*
|
120 |
* @return
|
121 |
*/
|
122 |
private GeocodingPattern getPattern() {
|
123 |
|
124 |
GeocodingPattern pat = new DefaultGeocodingPattern();
|
125 |
|
126 |
try {
|
127 |
pat.setPatternName("SimpleRange");
|
128 |
|
129 |
GeocodingSource source = new DefaultGeocodingSource();
|
130 |
|
131 |
AbstractGeocodingStyle style = new SimpleRange();
|
132 |
DefaultLiteral relations = new DefaultLiteral();
|
133 |
relations.put("Calle", "STREET_NAM"); |
134 |
relations.put("TipoVia", "STREET_TYP"); |
135 |
relations.put("Municipio", "MUNI"); |
136 |
relations.put("Provincia", "PROV"); |
137 |
relations.put("Pais", "PAIS"); |
138 |
relations.put(GeocodingTags.FIRSTNUMBER, "RF_ADD");
|
139 |
relations.put(GeocodingTags.LASTNUMBER, "RT_ADD");
|
140 |
|
141 |
style.setRelationsLiteral(relations); |
142 |
|
143 |
SimpleRange sstyle = (SimpleRange) style; |
144 |
sstyle.setFirstNumber(GeocodingTags.FIRSTNUMBER); |
145 |
sstyle.setLastNumber(GeocodingTags.LASTNUMBER); |
146 |
|
147 |
source.setStyle(sstyle); |
148 |
source.setLayerName("Streets.shp");
|
149 |
source.setLayerProvider("Shape file");
|
150 |
|
151 |
pat.setSource(source); |
152 |
|
153 |
} catch (Exception e) { |
154 |
log.error("Building a pattern", e);
|
155 |
} |
156 |
|
157 |
return pat;
|
158 |
} |
159 |
|
160 |
/**
|
161 |
* get Address
|
162 |
*
|
163 |
* @return
|
164 |
*/
|
165 |
private Address getAddress() {
|
166 |
Literal literal = new DefaultLiteral();
|
167 |
literal.put("Calle", "COLON"); |
168 |
literal.put("TipoVia", "C"); |
169 |
literal.put("Municipio", "Valencia"); |
170 |
literal.put("Provincia", "Valencia"); |
171 |
literal.put("Pais", "Espanya"); |
172 |
|
173 |
Address address = new DefaultNumberAddress(literal);
|
174 |
((NumberAddress) address).setNumber(56);
|
175 |
return address;
|
176 |
} |
177 |
|
178 |
} |