Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / DataGeocoderImpl.java @ 27462

History | View | Annotate | Download (8.6 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.Set;
34

    
35
import org.gvsig.fmap.dal.DALLocator;
36
import org.gvsig.fmap.dal.DataManager;
37
import org.gvsig.fmap.dal.exception.DataException;
38
import org.gvsig.fmap.dal.feature.Feature;
39
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.FeatureQuery;
41
import org.gvsig.fmap.dal.feature.FeatureSelection;
42
import org.gvsig.fmap.dal.feature.FeatureSet;
43
import org.gvsig.fmap.dal.feature.FeatureStore;
44
import org.gvsig.geocoding.pattern.Patterngeocoding;
45
import org.gvsig.geocoding.result.GeocodingResult;
46
import org.gvsig.geocoding.result.ScoredFeature;
47
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
48
import org.gvsig.geocoding.styles.Composed;
49
import org.gvsig.geocoding.styles.DoubleRange;
50
import org.gvsig.geocoding.styles.SimpleCentroid;
51
import org.gvsig.geocoding.styles.SimpleRange;
52
import org.gvsig.tools.evaluator.Evaluator;
53
import org.gvsig.tools.locator.LocatorException;
54
import org.slf4j.Logger;
55
import org.slf4j.LoggerFactory;
56

    
57
/**
58
 * Data geocoder implementation
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 = LoggerFactory.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
         * get the pattern
92
         * 
93
         * @param pat
94
         */
95
        public Patterngeocoding getPattern() {
96
                return this.pattern;
97

    
98
        }
99

    
100
        /**
101
         * Geocoding process
102
         */
103
        public Set<GeocodingResult> geocode(Address address)
104
                        throws LocatorException, DataException {
105

    
106
                Set<GeocodingResult> geomResults = null;
107
                
108
                // get the FeatureStore registred in the pattern
109
                FeatureStore store = getPattern().getSource().getLayerSource();
110
                // get the style
111
                AbstractGeocodingStyle astyle = getPattern().getSource().getStyle();                
112

    
113
                Literal literalRelations = astyle.getRelationsLiteral();
114

    
115
                // SIMPLE RANGE
116
                if (astyle instanceof SimpleRange) {
117
                        SimpleRange style = (SimpleRange) astyle;
118
                        Literal literalAddress = ((NumberAddress) address).getMainLiteral();
119
                        // literal search
120
                        List<ScoredFeature> litResults = literalSearch(literalRelations,
121
                                        literalAddress, store);
122
                        // geom search
123
                        List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>();
124
                        lists.add(litResults);
125
                        geomResults = style.match(lists, address);
126
                }
127

    
128
                // DOUBLE RANGE
129
                else if (astyle instanceof DoubleRange) {
130
                        DoubleRange style = (DoubleRange) astyle;
131
                        Literal literalAddress = ((NumberAddress) address).getMainLiteral();
132
                        // literal search
133
                        List<ScoredFeature> litResults = literalSearch(literalRelations,
134
                                        literalAddress, store);
135
                        // number search
136
                        List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>();
137
                        lists.add(litResults);
138
                        geomResults = style.match(lists, address);
139
                }
140

    
141
                // SIMPLE CENTROID
142
                else if (astyle instanceof SimpleCentroid) {
143
                        SimpleCentroid style = (SimpleCentroid) astyle;
144
                        Literal mainLiteral = address.getMainLiteral();
145
                        // literal search
146
                        List<ScoredFeature> litResults = literalSearch(literalRelations,
147
                                        mainLiteral, store);
148
                        // Geom search
149
                        List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>();
150
                        lists.add(litResults);
151
                        geomResults = style.match(lists, address);
152
                }
153

    
154
                // STYLE COMPOSED
155
                else if (astyle instanceof Composed) {
156

    
157
                        List<List<ScoredFeature>> lists = new ArrayList<List<ScoredFeature>>();
158

    
159
                        Composed style = (Composed) astyle;
160
                        ComposeAddress cAddress = (ComposeAddress) address;
161

    
162
                        // main literal search
163
                        Literal mainLiteralAddress = cAddress.getMainLiteral();
164
                        List<ScoredFeature> litResults = literalSearch(literalRelations,
165
                                        mainLiteralAddress, store);
166
                        lists.add(litResults);
167
                        // search in others literals
168
                        List<Literal> intersectslist = cAddress.getIntersectionLiteral();
169
                        for (Literal addrLiteral : intersectslist) {
170
                                // literal search
171
                                List<ScoredFeature> secList = literalSearch(literalRelations,
172
                                                addrLiteral, store);
173
                                lists.add(secList);
174
                        }
175
                        // Match
176
                        geomResults = style.match(lists, address);
177
                }
178
                return geomResults;
179
        }
180

    
181
        /**
182
         * first scouting process, search literal elements in store
183
         * 
184
         * @param literalRelations
185
         * @param literalAddress
186
         * @param store
187
         * @return
188
         * @throws DataException
189
         */
190
        private List<ScoredFeature> literalSearch(Literal literalRelations,
191
                        Literal literalAddress, FeatureStore store) throws DataException {
192

    
193
                List<FeatureSelection> sels = new ArrayList<FeatureSelection>();
194
                FeatureSet features = null;
195

    
196
                String exp = "";
197

    
198
                int i = 0;
199
                // search features related with the parameters
200
                for (Object obj : literalRelations) {
201
                        RelationsComponent lit = (RelationsComponent) obj;
202
                        FeatureSelection selection = store.createFeatureSelection();
203
                        FeatureQuery query = store.createFeatureQuery();
204
                        String key = lit.getKeyElement().trim();
205
                        String chain = getValueAddress(key, literalAddress);
206

    
207
                        FeatureAttributeDescriptor field = lit.getValue();
208
                        if (i == 0) {
209
                                exp = field.getName() + " like '%" + chain + "%'";
210
                        } else {
211
                                exp = exp + " AND " + field.getName() + " like '%" + chain
212
                                                + "%'";
213
                        }
214

    
215
                        Evaluator eval = manager.createExpresion(exp);
216

    
217
                        query.setFilter(eval);
218
                        features = store.getFeatureSet(query);
219
                        selection.select(features);
220
                        sels.add(selection);
221
                        log.debug("Selection " + i + " : " + selection.getSelectedCount());
222
                        i++;
223
                }
224
                // Put scores
225
                List<ScoredFeature> scores = createLiteralScores(store, sels);
226
                
227
                return scores;
228
        }
229

    
230
        /**
231
         * Create score of the feature to first search
232
         * 
233
         * @param store
234
         * @param sels
235
         * @return
236
         * @throws DataException
237
         */
238
        @SuppressWarnings("unchecked")
239
        private List<ScoredFeature> createLiteralScores(FeatureStore store,
240
                        List<FeatureSelection> sels) throws DataException {
241

    
242
                List<ScoredFeature> scores = new ArrayList<ScoredFeature>();
243

    
244
                FeatureSet features = store.getFeatureSet();
245

    
246
                for (Iterator<Feature> it = features.iterator(); it.hasNext();) {
247
                        Feature feature = it.next();
248
                        double num = 0;
249
                        for (int i = 0; i < sels.size(); i++) {
250
                                FeatureSelection sel = sels.get(i);
251
                                if (sel.isSelected(feature)) {
252
                                        num = num + scorePonderated(i, sels.size());
253
                                }
254
                        }
255
                        if (num > 0.0) {
256
                                ScoredFeature scoFeat = new ScoredFeature();
257
                                scoFeat.setReference(feature.getReference());
258
                                scoFeat.setScore(num);
259
                                scores.add(scoFeat);
260
                        }
261
                }
262
                return scores;
263
        }
264

    
265
        /**
266
         * Get the score value ponderated
267
         * 
268
         * @param i
269
         *            position
270
         * @param total
271
         * @return score
272
         */
273
        private double scorePonderated(int i, int total) {
274
                double score = 100.0/(2.0*(i+1));
275
                return score;
276
        }
277

    
278
        /**
279
         * Get address value from key
280
         * 
281
         * @param key
282
         * @param litAddress
283
         * @return
284
         */
285
        private String getValueAddress(String key, Literal litAddress) {
286
                String value = "";
287
                for (Object obj : litAddress) {
288
                        AddressComponent comp = (AddressComponent) obj;
289
                        String key2 = comp.getKeyElement();
290
                        if (key.compareTo(key2) == 0) {
291
                                value = comp.getValue();
292
                                break;
293
                        }
294
                }
295
                return value;
296
        }
297

    
298
}