svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / DataGeocoderImpl.java @ 27215
History | View | Annotate | Download (8.02 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.ArrayList; |
31 |
import java.util.Iterator; |
32 |
import java.util.List; |
33 |
import java.util.Map; |
34 |
import java.util.Set; |
35 |
|
36 |
import org.gvsig.fmap.dal.DALLocator; |
37 |
import org.gvsig.fmap.dal.DataManager; |
38 |
import org.gvsig.fmap.dal.exception.DataException; |
39 |
import org.gvsig.fmap.dal.feature.Feature; |
40 |
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.FeatureQuery; |
42 |
import org.gvsig.fmap.dal.feature.FeatureSelection; |
43 |
import org.gvsig.fmap.dal.feature.FeatureSet; |
44 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
45 |
import org.gvsig.geocoding.pattern.Patterngeocoding; |
46 |
import org.gvsig.geocoding.result.GeocodingResult; |
47 |
import org.gvsig.geocoding.result.ScoredFeature; |
48 |
import org.gvsig.geocoding.styles.AbstractGeocodingStyle; |
49 |
import org.gvsig.geocoding.styles.Composed; |
50 |
import org.gvsig.geocoding.styles.DoubleRange; |
51 |
import org.gvsig.geocoding.styles.SimpleCentroid; |
52 |
import org.gvsig.geocoding.styles.SimpleRange; |
53 |
import org.gvsig.tools.evaluator.Evaluator; |
54 |
import org.gvsig.tools.locator.LocatorException; |
55 |
import org.slf4j.Logger; |
56 |
import org.slf4j.LoggerFactory; |
57 |
|
58 |
/**
|
59 |
* Data geocoder implemetacion
|
60 |
*
|
61 |
* @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
|
62 |
* @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
|
63 |
*/
|
64 |
|
65 |
public class DataGeocoderImpl implements DataGeocoder { |
66 |
|
67 |
private Logger log = LoggerFactory.getLogger(DataGeocoderImpl.class); |
68 |
private Patterngeocoding pattern;
|
69 |
private DataManager manager;
|
70 |
|
71 |
/**
|
72 |
* Constructor
|
73 |
*
|
74 |
* @param pat
|
75 |
*/
|
76 |
public DataGeocoderImpl(Patterngeocoding pat) {
|
77 |
this.pattern = pat;
|
78 |
manager = DALLocator.getDataManager(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* set the pattern
|
83 |
*
|
84 |
* @param pat
|
85 |
*/
|
86 |
public void setPattern(Patterngeocoding pat) { |
87 |
this.pattern = pat;
|
88 |
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* get the pattern
|
93 |
*
|
94 |
* @param pat
|
95 |
*/
|
96 |
public Patterngeocoding getPattern() {
|
97 |
return this.pattern; |
98 |
|
99 |
} |
100 |
|
101 |
/**
|
102 |
* Geocoding process
|
103 |
*/
|
104 |
public Set<GeocodingResult> geocode(Address address) |
105 |
throws LocatorException, DataException {
|
106 |
|
107 |
FeatureStore store = getPattern().getSource().getLayerSource(); |
108 |
AbstractGeocodingStyle astyle = getPattern().getSource().getStyle(); |
109 |
Set<GeocodingResult> geomResults = null; |
110 |
|
111 |
Literal literalRelations = astyle.getMainLiteral(); |
112 |
|
113 |
// SIMPLE RANGE
|
114 |
if (astyle instanceof SimpleRange) { |
115 |
SimpleRange style = (SimpleRange) astyle; |
116 |
Literal literalAddress = ((NumberAddress) address).getMainLiteral(); |
117 |
// literal search
|
118 |
List<ScoredFeature> litResults = literalSearch(literalRelations,
|
119 |
literalAddress, store); |
120 |
// geom search
|
121 |
List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>(); |
122 |
lists.add(litResults); |
123 |
geomResults = style.match(lists, address); |
124 |
} |
125 |
|
126 |
// DOUBLE RANGE
|
127 |
else if (astyle instanceof DoubleRange) { |
128 |
DoubleRange style = (DoubleRange) astyle; |
129 |
Literal literalAddress = ((NumberAddress) address).getMainLiteral(); |
130 |
// literal search
|
131 |
List<ScoredFeature> litResults = literalSearch(literalRelations,
|
132 |
literalAddress, store); |
133 |
// number search
|
134 |
List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>(); |
135 |
lists.add(litResults); |
136 |
geomResults = style.match(lists, address); |
137 |
} |
138 |
|
139 |
// SIMPLE CENTROID
|
140 |
else if (astyle instanceof SimpleCentroid) { |
141 |
SimpleCentroid style = (SimpleCentroid) astyle; |
142 |
Literal mainLiteral = address.getMainLiteral(); |
143 |
// literal search
|
144 |
List<ScoredFeature> litResults = literalSearch(literalRelations,
|
145 |
mainLiteral, store); |
146 |
// Geom search
|
147 |
List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>(); |
148 |
lists.add(litResults); |
149 |
geomResults = style.match(lists, address); |
150 |
} |
151 |
|
152 |
// STYLE COMPOSED
|
153 |
else if (astyle instanceof Composed) { |
154 |
|
155 |
List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>(); |
156 |
|
157 |
Composed style = (Composed) astyle; |
158 |
ComposeAddress cAddress = (ComposeAddress) address; |
159 |
|
160 |
// main literal search
|
161 |
Literal mainLiteralAddress = cAddress.getMainLiteral(); |
162 |
List<ScoredFeature> litResults = literalSearch(literalRelations,
|
163 |
mainLiteralAddress, store); |
164 |
lists.add(litResults); |
165 |
// search in others literals
|
166 |
List<Literal> intersectslist = cAddress.getIntersectionLiteral();
|
167 |
for (Literal addrLiteral : intersectslist) {
|
168 |
// literal search
|
169 |
List<ScoredFeature> secList = literalSearch(literalRelations,
|
170 |
addrLiteral, store); |
171 |
lists.add(secList); |
172 |
} |
173 |
// Match
|
174 |
geomResults = style.match(lists, address); |
175 |
} |
176 |
return geomResults;
|
177 |
} |
178 |
|
179 |
/**
|
180 |
* first scouting process, search literal elements in store
|
181 |
* @param literalRelations
|
182 |
* @param literalAddress
|
183 |
* @param store
|
184 |
* @return
|
185 |
* @throws DataException
|
186 |
*/
|
187 |
private List<ScoredFeature> literalSearch(Literal literalRelations, |
188 |
Literal literalAddress, FeatureStore store) throws DataException {
|
189 |
|
190 |
List<FeatureSelection> sels = new ArrayList<FeatureSelection>(); |
191 |
FeatureSet features = null;
|
192 |
|
193 |
String exp = ""; |
194 |
|
195 |
int i = 0; |
196 |
// search features related with the parameters
|
197 |
for (Map.Entry<String, Object> lit : literalAddress) { |
198 |
FeatureSelection selection = store.createFeatureSelection(); |
199 |
FeatureQuery query = store.createFeatureQuery(); |
200 |
String chain = ((String) lit.getValue()).trim(); |
201 |
|
202 |
FeatureAttributeDescriptor field = (FeatureAttributeDescriptor) literalRelations |
203 |
.get(i).getValue(); |
204 |
if (i == 0) { |
205 |
exp = field.getName() + " like '" + chain + "'"; |
206 |
} else {
|
207 |
exp = exp + " AND " + field.getName() + " like '" + chain + "'"; |
208 |
} |
209 |
|
210 |
Evaluator eval = manager.createExpresion(exp); |
211 |
|
212 |
query.setFilter(eval); |
213 |
features = store.getFeatureSet(query); |
214 |
selection.select(features); |
215 |
sels.add(selection); |
216 |
log.debug("Selection " + i + " : " + selection.getSelectedCount()); |
217 |
i++; |
218 |
} |
219 |
// Put scores
|
220 |
List<ScoredFeature> scores = createLiteralScores(store, sels);
|
221 |
log.debug("Results: " + scores.size());
|
222 |
return scores;
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* Create score of the feature to first search
|
227 |
* @param store
|
228 |
* @param sels
|
229 |
* @return
|
230 |
* @throws DataException
|
231 |
*/
|
232 |
private List<ScoredFeature> createLiteralScores(FeatureStore store, |
233 |
List<FeatureSelection> sels) throws DataException { |
234 |
|
235 |
List<ScoredFeature> scores = new ArrayList<ScoredFeature>(); |
236 |
|
237 |
FeatureSet features = store.getFeatureSet(); |
238 |
|
239 |
for (Iterator<Feature> it = features.iterator(); it.hasNext();) { |
240 |
Feature feature = it.next(); |
241 |
double num = 0; |
242 |
for (int i = 0; i < sels.size(); i++) { |
243 |
FeatureSelection sel = sels.get(i); |
244 |
if (sel.isSelected(feature)) {
|
245 |
num = num + scorePonderated(i, sels.size()); |
246 |
} |
247 |
} |
248 |
if (num > 0.0) { |
249 |
ScoredFeature scoFeat = new ScoredFeature();
|
250 |
scoFeat.setReference(feature.getReference()); |
251 |
scoFeat.setScore(num); |
252 |
scores.add(scoFeat); |
253 |
} |
254 |
} |
255 |
return scores;
|
256 |
} |
257 |
|
258 |
/**
|
259 |
* Get the score value ponderated
|
260 |
* @param i position
|
261 |
* @param total
|
262 |
* @return score
|
263 |
*/
|
264 |
private double scorePonderated(int i, int total) { |
265 |
return total - i;
|
266 |
} |
267 |
|
268 |
} |