Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / styles / impl / Composed.java @ 28375

History | View | Annotate | Download (9.79 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 S.L. main development
26
 */
27

    
28
package org.gvsig.geocoding.styles.impl;
29

    
30
import java.util.ArrayList;
31
import java.util.List;
32
import java.util.Set;
33
import java.util.TreeSet;
34

    
35
import org.gvsig.fmap.dal.exception.DataException;
36
import org.gvsig.fmap.geom.primitive.Point;
37
import org.gvsig.fmap.geom.util.Converter;
38
import org.gvsig.geocoding.address.Address;
39
import org.gvsig.geocoding.address.ComposedAddress;
40
import org.gvsig.geocoding.address.Literal;
41
import org.gvsig.geocoding.address.RelationsComponent;
42
import org.gvsig.geocoding.address.impl.DefaultAddressComponent;
43
import org.gvsig.geocoding.address.impl.DefaultComposedAddress;
44
import org.gvsig.geocoding.address.impl.DefaultLiteral;
45
import org.gvsig.geocoding.geommatches.MatcherUtils;
46
import org.gvsig.geocoding.result.GeocodingResult;
47
import org.gvsig.geocoding.result.ScoredFeature;
48
import org.gvsig.geocoding.result.impl.GeomMatchResult;
49
import org.gvsig.geocoding.styles.AbstractGeocodingStyle;
50
import org.gvsig.tools.persistence.PersistenceException;
51
import org.gvsig.tools.persistence.PersistentState;
52
import org.slf4j.Logger;
53
import org.slf4j.LoggerFactory;
54

    
55
/**
56
 * Implemented Geocoding style with a address composed. This style can geocode a
57
 * cross of streets and the street result of the intersection between two
58
 * parallel streets.
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 Composed extends AbstractGeocodingStyle {
65

    
66
        private static final Logger log = LoggerFactory.getLogger(Composed.class);
67

    
68
        private static final String INTERSECTLITERALS = "intersectliterals";
69

    
70
        private List<Literal> intersectsLiterals;
71

    
72
        /**
73
         * Get list of the secondaries literals
74
         * 
75
         * @return
76
         */
77
        public List<Literal> getIntersectsLiterals() {
78
                return intersectsLiterals;
79
        }
80

    
81
        /**
82
         * Set list of the secondaries literals
83
         * 
84
         * @param intersectName
85
         */
86
        public void setIntersectsLiterals(List<Literal> intersects) {
87
                this.intersectsLiterals = intersects;
88
        }
89

    
90
        /**
91
         * spatial search over the geometries
92
         * 
93
         * @param lists
94
         *            list of lists with ScoredFeatures
95
         * @param address
96
         * @return
97
         */
98
        @Override
99
        public Set<GeocodingResult> match(List<List<ScoredFeature>> inLists,
100
                        Address address) {
101

    
102
                Set<GeocodingResult> results = new TreeSet<GeocodingResult>();
103

    
104
                // cross
105
                if (inLists.size() == 2) {
106

    
107
                        List<ScoredFeature> mainList = inLists.get(0);
108
                        List<ScoredFeature> secondList = inLists.get(1);
109

    
110
                        // this list store elements of the first intersection (cross)
111
                        if (mainList.size() > 0 && secondList.size() > 0) {
112
                                try {
113
                                        for (ScoredFeature mainFeat : mainList) {
114
                                                com.vividsolutions.jts.geom.Geometry mainGeomJTS = Converter
115
                                                                .geometryToJts(mainFeat.getFeature()
116
                                                                                .getDefaultGeometry());
117
                                                for (ScoredFeature secFeat : secondList) {
118
                                                        com.vividsolutions.jts.geom.Geometry secGeomJTS = Converter
119
                                                                        .geometryToJts(secFeat.getFeature()
120
                                                                                        .getDefaultGeometry());
121
                                                        if (mainGeomJTS.touches(secGeomJTS)) {
122
                                                                Point pto = MatcherUtils.intersectTwoLinesJTS(
123
                                                                                mainGeomJTS, secGeomJTS);
124
                                                                GeomMatchResult res = new GeomMatchResult();
125
                                                                // geom
126
                                                                res.setGeom(pto);
127
                                                                // features scored
128
                                                                List<ScoredFeature> mainSources = new ArrayList<ScoredFeature>();
129
                                                                mainSources.add(mainFeat);
130
                                                                res.setMainSources(mainSources);
131
                                                                List<ScoredFeature> secSources = new ArrayList<ScoredFeature>();
132
                                                                secSources.add(secFeat);
133
                                                                res.setSecondSources(secSources);
134
                                                                // address
135
                                                                res.setAddress(getResultAddress(mainSources,
136
                                                                                secSources, null));
137
                                                                results.add(res);
138
                                                        }
139
                                                }
140
                                        }
141
                                } catch (Exception e) {
142
                                        log.error("Error getting the features", e);
143
                                }
144
                        }
145
                }
146
                // between
147
                if (inLists.size() == 3) {
148

    
149
                        List<ScoredFeature> mainList = inLists.get(0);
150
                        List<ScoredFeature> secondList = inLists.get(1);
151
                        List<ScoredFeature> thirdList = inLists.get(2);
152

    
153
                        // this list store elements of the first intersection (cross)
154
                        if (mainList.size() > 0 && secondList.size() > 0) {
155
                                try {
156
                                        for (ScoredFeature mainFeat : mainList) {
157
                                                com.vividsolutions.jts.geom.Geometry mainGeomJTS = Converter
158
                                                                .geometryToJts(mainFeat.getFeature()
159
                                                                                .getDefaultGeometry());
160
                                                for (ScoredFeature secFeat : secondList) {
161
                                                        com.vividsolutions.jts.geom.Geometry secGeomJTS = Converter
162
                                                                        .geometryToJts(secFeat.getFeature()
163
                                                                                        .getDefaultGeometry());
164
                                                        if (mainGeomJTS.touches(secGeomJTS)) {
165
                                                                for (ScoredFeature thiFeat : thirdList) {
166
                                                                        com.vividsolutions.jts.geom.Geometry thiGeomJTS = Converter
167
                                                                                        .geometryToJts(thiFeat.getFeature()
168
                                                                                                        .getDefaultGeometry());
169
                                                                        if (mainGeomJTS.touches(thiGeomJTS)) {
170
                                                                                Point pto = MatcherUtils
171
                                                                                                .getLinePositionFromRelativeDistance(
172
                                                                                                                mainGeomJTS, 50);
173
                                                                                GeomMatchResult res = new GeomMatchResult();
174
                                                                                // geom
175
                                                                                res.setGeom(pto);
176
                                                                                // features scored
177
                                                                                List<ScoredFeature> mainSources = new ArrayList<ScoredFeature>();
178
                                                                                mainSources.add(mainFeat);
179
                                                                                res.setMainSources(mainSources);
180
                                                                                List<ScoredFeature> secSources = new ArrayList<ScoredFeature>();
181
                                                                                secSources.add(secFeat);
182
                                                                                res.setSecondSources(secSources);
183
                                                                                List<ScoredFeature> thiSources = new ArrayList<ScoredFeature>();
184
                                                                                thiSources.add(thiFeat);
185
                                                                                res.setThirdSources(thiSources);
186
                                                                                // address
187
                                                                                res.setAddress(getResultAddress(
188
                                                                                                mainSources, secSources,
189
                                                                                                thiSources));
190
                                                                                results.add(res);
191
                                                                        }
192
                                                                }
193
                                                        }
194
                                                }
195
                                        }
196
                                }
197

    
198
                                catch (DataException e) {
199
                                        log.error("Error getting the features", e);
200
                                }
201
                        }
202
                }
203
                return results;
204
        }
205

    
206
        /**
207
         * 
208
         * @return
209
         */
210
        private Address getResultAddress(List<ScoredFeature> mainSources,
211
                        List<ScoredFeature> secSources, List<ScoredFeature> thiSources) {
212

    
213
                // get the first element
214
                ScoredFeature mainfeat = mainSources.get(0);
215
                ScoredFeature secfeat = secSources.get(0);
216

    
217
                Literal relLiteral = this.getRelationsLiteral();
218
                Literal mainliteral = new DefaultLiteral();
219
                Literal secliteral = new DefaultLiteral();
220
                Literal thiliteral = new DefaultLiteral();
221
                // main literal
222
                for (Object obj : relLiteral) {
223
                        RelationsComponent rel = (RelationsComponent) obj;
224
                        String key = rel.getKeyElement();
225
                        String fieldName = rel.getValue();
226
                        Object object = null;
227
                        String value = "";
228
                        try {
229
                                object = mainfeat.getFeature().get(fieldName);
230
                                value = object.toString();
231
                        } catch (DataException e) {
232
                                log.error("Getting attributes ot the feature", e);
233
                        }
234
                        mainliteral.add(new DefaultAddressComponent(key, value));
235
                }
236
                // second literal
237
                for (Object obj : relLiteral) {
238
                        RelationsComponent rel = (RelationsComponent) obj;
239
                        String key = rel.getKeyElement();
240
                        String fieldName = rel.getValue();
241
                        Object object = null;
242
                        String value = "";
243
                        try {
244
                                object = secfeat.getFeature().get(fieldName);
245
                                value = object.toString();
246
                        } catch (DataException e) {
247
                                log.error("Getting attributes ot the feature", e);
248
                        }
249
                        secliteral.add(new DefaultAddressComponent(key, value));
250
                }
251

    
252
                ComposedAddress address = (ComposedAddress) new DefaultComposedAddress();
253
                address.setMainLiteral(mainliteral);
254
                List<Literal> intersect = new ArrayList<Literal>();
255
                intersect.add(secliteral);
256

    
257
                if (thiSources != null) {
258
                        ScoredFeature thifeat = thiSources.get(0);
259
                        // third literal
260
                        for (Object obj : relLiteral) {
261
                                RelationsComponent rel = (RelationsComponent) obj;
262
                                String key = rel.getKeyElement();
263
                                String fieldName = rel.getValue();
264
                                Object object = null;
265
                                String value = "";
266
                                try {
267
                                        object = thifeat.getFeature().get(fieldName);
268
                                        value = object.toString();
269
                                } catch (DataException e) {
270
                                        log.error("Getting attributes ot the feature", e);
271
                                }
272
                                thiliteral.add(new DefaultAddressComponent(key, value));
273
                        }
274
                        intersect.add(thiliteral);
275
                }
276

    
277
                address.setIntersectionLiterals(intersect);
278
                return address;
279
        }
280

    
281
        /**
282
         * Saves the internal state of the object on the provided PersistentState
283
         * object.
284
         * 
285
         * @param state
286
         */
287
        public void saveToState(PersistentState state) throws PersistenceException {
288
                super.saveToState(state);
289
                state.set(INTERSECTLITERALS, this.intersectsLiterals.iterator());
290
        }
291

    
292
        /**
293
         * Set the state of the object from the state passed as parameter.
294
         * 
295
         * @param state
296
         */
297
        public void setState(PersistentState state) throws PersistenceException {
298
                super.setState(state);
299
                this.intersectsLiterals = (List<Literal>) state.get(INTERSECTLITERALS);
300
        }
301

    
302
}