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