Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / org.gvsig.geocoding / src / org / gvsig / geocoding / styles / impl / Composed.java @ 32474

History | View | Annotate | Download (9.47 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.dal.feature.Feature;
37
import org.gvsig.fmap.geom.primitive.Point;
38
import org.gvsig.fmap.geom.util.Converter;
39
import org.gvsig.geocoding.GeocodingTags;
40
import org.gvsig.geocoding.address.Address;
41
import org.gvsig.geocoding.address.ComposedAddress;
42
import org.gvsig.geocoding.address.Literal;
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.utils.XMLEntity;
51
import org.gvsig.utils.XMLException;
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"> Vicent 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 List<Literal> intersectsLiterals = new ArrayList<Literal>();
69

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

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

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

    
100
                Set<GeocodingResult> results = new TreeSet<GeocodingResult>();
101

    
102
                // cross
103
                if (inLists.size() == 2) {
104

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

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

    
152
                        List<ScoredFeature> mainList = inLists.get(0);
153
                        List<ScoredFeature> secondList = inLists.get(1);
154
                        List<ScoredFeature> thirdList = inLists.get(2);
155

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

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

    
209
        /**
210
         * 
211
         * @param mainSources
212
         * @param secSources
213
         * @return
214
         * @throws DataException
215
         */
216
        private ComposedAddress getResultAddress(List<ScoredFeature> mainSources,
217
                        List<ScoredFeature> secSources) throws DataException {
218
                // get the first element
219
                Feature mainfeat = mainSources.get(0).getFeature();
220
                Feature secfeat = secSources.get(0).getFeature();
221

    
222
                Literal relLiteral = this.getRelationsLiteral();
223
                Literal mainliteral = createLiteralFromFeature(mainfeat, relLiteral);
224
                Literal secliteral = createLiteralFromFeature(secfeat, relLiteral);
225

    
226
                ComposedAddress address = (ComposedAddress) new DefaultComposedAddress();
227
                address.setMainLiteral(mainliteral);
228

    
229
                List<Literal> intersectionLiteralList = new ArrayList<Literal>();
230
                intersectionLiteralList.add(secliteral);
231
                address.setIntersectionLiterals(intersectionLiteralList);
232

    
233
                return address;
234
        }
235

    
236
        /**
237
         * 
238
         * @return
239
         * @throws DataException
240
         */
241
        private Address getResultAddress(List<ScoredFeature> mainSources,
242
                        List<ScoredFeature> secSources, List<ScoredFeature> thiSources)
243
                        throws DataException {
244

    
245
                ComposedAddress address = this
246
                                .getResultAddress(mainSources, secSources);
247

    
248
                // get the first element
249
                
250
                if (thiSources != null) {
251
                        Feature thifeat = thiSources.get(0).getFeature();
252
                        Literal relLiteral = this.getRelationsLiteral();
253
                        Literal thiliteral = createLiteralFromFeature(thifeat, relLiteral);
254

    
255
                        address.getIntersectionLiterals().add(thiliteral);
256
                }
257

    
258
                return address;
259
        }
260

    
261
        /**
262
         * Save
263
         * 
264
         * @return
265
         * @throws XMLException
266
         */
267
        public XMLEntity getXMLEntity() throws XMLException {
268
                XMLEntity xml = new XMLEntity();
269
                xml.setName(GeocodingTags.STYLETAG);
270
                xml.putProperty(GeocodingTags.STYLE, GeocodingTags.COMPOSED);
271
                xml.addChild(super.getRelationsLiteral().getXMLEntity());
272
                for (Literal lit : this.intersectsLiterals) {
273
                        xml.addChild(lit.getXMLEntity());
274
                }
275
                return xml;
276
        }
277

    
278
        /**
279
         * Load
280
         * 
281
         * @param xml
282
         * @throws XMLException
283
         */
284
        public void setXMLEntity(XMLEntity xml) throws XMLException {
285
                super.getRelationsLiteral().clear();
286
                XMLEntity xml2 = xml.getChild(0);
287
                super.getRelationsLiteral().setXMLEntity(xml2);
288

    
289
                this.intersectsLiterals.clear();
290
                for (int i = 1; i < xml.getChildrenCount(); i++) {
291
                        Literal lit = new DefaultLiteral();
292
                        lit.setXMLEntity(xml.getChild(i));
293
                        this.intersectsLiterals.add(lit);
294
                }
295
        }
296

    
297
        /**
298
         * Get this class name
299
         * 
300
         * @return
301
         */
302
        public String getClassName() {
303
                return this.getClass().getName();
304
        }
305

    
306
}