Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / styles / SimpleCentroid.java @ 27057

History | View | Annotate | Download (5.26 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;
29

    
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34
import java.util.Map;
35
import java.util.Set;
36
import java.util.TreeSet;
37

    
38
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
39
import org.gvsig.fmap.geom.Geometry;
40
import org.gvsig.fmap.geom.primitive.Point2D;
41
import org.gvsig.geocoding.Address;
42
import org.gvsig.geocoding.geommatches.MatcherUtils;
43
import org.gvsig.geocoding.result.DissolveResult;
44
import org.gvsig.geocoding.result.GeocodingResult;
45
import org.gvsig.geocoding.result.GeomMatchResult;
46
import org.gvsig.geocoding.result.ScoredFeature;
47

    
48
/**
49
 * 
50
 * 
51
 * @author <a href="mailto:jsanz@prodevelop.es"> Jorge Gaspar Sanz Salinas</a>
52
 * @author <a href="mailto:vsanjaime@prodevelop.es"> Vicente Sanjaime Calvet</a>
53
 */
54

    
55
public class SimpleCentroid extends AbstractGeocodingStyle {
56

    
57
        @Override
58
        public Set<GeocodingResult> match(List inFeats, Address address) {
59

    
60
                Set<GeocodingResult> geocoRes = new TreeSet<GeocodingResult>();
61

    
62
                // catch the first element of the main Literal to get the field
63
                // descriptor
64
                // to make the dissolve
65
                FeatureAttributeDescriptor desc = (FeatureAttributeDescriptor) this
66
                                .getMainLiteral().get(0).getValue();
67

    
68
                // POINTs FEATURES
69
                if (((ScoredFeature) inFeats.get(0)).getDefaultGeometry()
70
                                .getDimension() == 0) {
71
                        geocoRes = createPointsFeaturesResult(inFeats);
72
                }
73
                // OTHER GEOMETRIES TYPES
74
                else {
75
                        // dissolve features selected
76
                        List<DissolveResult> disRes = this.dissolveScoredFeatures(desc,
77
                                        inFeats);
78

    
79
                        for (DissolveResult dis : disRes) {
80
                                GeomMatchResult matchres = new GeomMatchResult();
81
                                Point2D position = null;
82

    
83
                                Geometry geom = dis.getGeom();
84
                                // Points
85
                                if (geom.getDimension() == 0) {
86
                                        // TODO faltan los puntos
87
                                }
88
                                // Lines
89
                                if (geom.getDimension() == 1) {
90
                                        com.vividsolutions.jts.geom.Geometry geoJTS = MatcherUtils
91
                                                        .parseGeomGVToJTS(geom);
92
                                        position = MatcherUtils
93
                                                        .getLinePositionFromRelativeDistance(geoJTS, 50);
94
                                }
95
                                // Polys
96
                                if (geom.getDimension() == 2) {
97
                                        position = MatcherUtils.internalPointGeometry(geom);
98
                                }
99
                                matchres.setGeom(position);
100
                                matchres.setSources(dis.getScoredFeatures());
101
                                geocoRes.add(matchres);
102
                        }
103
                }
104

    
105
                return geocoRes;
106
        }
107

    
108
        /**
109
         * 
110
         * @param inFeats
111
         * @return
112
         */
113
        private Set<GeocodingResult> createPointsFeaturesResult(
114
                        List<ScoredFeature> inFeats) {
115

    
116
                Set<GeocodingResult> results = new TreeSet<GeocodingResult>();
117

    
118
                for (ScoredFeature feat : inFeats) {
119
                        GeomMatchResult res = new GeomMatchResult();
120
                        res.setGeom(feat.getDefaultGeometry());
121
                        List<ScoredFeature> sources = new ArrayList<ScoredFeature>();
122
                        sources.add(feat);
123
                        res.setSources(sources);
124
                        results.add(res);
125
                }
126
                return results;
127
        }
128

    
129
        /**
130
         * Dissolve features geometry behind one folder
131
         * 
132
         * @param field
133
         * @param features
134
         */
135
        public List<DissolveResult> dissolveScoredFeatures(
136
                        FeatureAttributeDescriptor field, List<ScoredFeature> features) {
137

    
138
                List<DissolveResult> listResults = new ArrayList<DissolveResult>();
139

    
140
                // Group geometries by main attribute
141
                HashMap<String, List<ScoredFeature>> dissolAttributes = MatcherUtils
142
                                .groupScoredFeaturesByAttribute(field, features);
143

    
144
                // For each group of features, to do geometries dissolve process getting
145
                // at the end one o more geometries
146
                for (Iterator<Map.Entry<String, List<ScoredFeature>>> iterator = dissolAttributes
147
                                .entrySet().iterator(); iterator.hasNext();) {
148
                        Map.Entry<String, List<ScoredFeature>> e = iterator.next();
149
                        List<ScoredFeature> feats = e.getValue();
150

    
151
                        // dissolve Lines (if first geometry of array is Line)
152
                        List<DissolveResult> geomsDissol = null;
153
                        if (feats.get(0).getDefaultGeometry().getDimension() == 1) {
154
                                geomsDissol = MatcherUtils.dissolveLinesJTS(feats);
155
                        }
156
                        // dissolve Polys (if first geometry of array is Poly)
157
                        if (feats.get(0).getDefaultGeometry().getDimension() == 2) {
158
                                geomsDissol = MatcherUtils.dissolvePolysJTS(feats);
159
                        }
160
                        // insert each dissolve result in the general array
161
                        for (DissolveResult dissolveResult : geomsDissol) {
162
                                listResults.add(dissolveResult);
163
                        }
164
                }
165
                return listResults;
166
        }
167

    
168
}