Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libGeocoding / src / org / gvsig / geocoding / geommatches / GeomMatcher.java @ 26883

History | View | Annotate | Download (8.81 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.geommatches;
29

    
30
import java.util.ArrayList;
31
import java.util.HashMap;
32
import java.util.Iterator;
33
import java.util.List;
34

    
35
import org.apache.log4j.Logger;
36
import org.gvsig.fmap.dal.feature.Feature;
37
import org.gvsig.fmap.geom.DefaultGeometryManager;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryManager;
40
import org.gvsig.fmap.geom.operation.GeometryOperation;
41
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
42
import org.gvsig.fmap.geom.operation.tojts.ToJTS;
43

    
44
import com.vividsolutions.jts.operation.overlay.OverlayOp;
45

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

    
55
public abstract class GeomMatcher {
56

    
57
        public Logger log = Logger.getLogger(GeomMatcher.class);
58

    
59
        @SuppressWarnings( { "unused", "unchecked" })
60
        private List listFeats;
61
        private MatchesParams params;
62
        private int fieldPosition;
63

    
64
        /**
65
         * Set features
66
         * 
67
         * @param _collectionsList
68
         */
69
        @SuppressWarnings("unchecked")
70
        public void setFeatures(List listFeatures) {
71
                this.listFeats = listFeatures;
72
        }
73

    
74
        /**
75
         * Get the features
76
         * 
77
         * @return
78
         */
79
        @SuppressWarnings("unchecked")
80
        public List getFeatures() {
81
                return this.listFeats;
82
        }
83

    
84
        /**
85
         * Set field name to group features
86
         * 
87
         * @param _collectionsList
88
         */
89
        public void setGroupField(int _fieldPosition) {
90
                this.fieldPosition = _fieldPosition;
91
        }
92

    
93
        /**
94
         * Get the group field name
95
         * 
96
         * @return
97
         */
98
        public int getGroupFieldPosition() {
99
                return this.fieldPosition;
100
        }
101

    
102
        /**
103
         * Get the parameters to make a match
104
         * 
105
         * @param context
106
         */
107
        public void setParams(MatchesParams context) {
108
                this.params = context;
109
        };
110

    
111
        /**
112
         * Set the parameters to make a match
113
         * 
114
         * @return
115
         */
116
        public MatchesParams getParams() {
117
                return params;
118
        }
119

    
120
        /**
121
         * This method parses geometries (FMap) to geometries (JTS)
122
         * 
123
         * @param geomClass
124
         *            type of geometry (Point2d.class, Curve2D.class, ...)
125
         * @param geoms
126
         * @return
127
         */
128
        @SuppressWarnings("unchecked")
129
        protected com.vividsolutions.jts.geom.Geometry[] parseGeomsToJTS(
130
                        Class geomClass, Geometry[] geoms) {
131

    
132
                com.vividsolutions.jts.geom.Geometry[] geomsjts = new com.vividsolutions.jts.geom.Geometry[geoms.length];
133

    
134
                GeometryManager gm = new DefaultGeometryManager();
135
                GeometryOperation geomOp = null;
136

    
137
                try {
138
                        geomOp = gm.getGeometryOperation(geomClass, ToJTS.CODE);
139

    
140
                        // Fill the operation context with required params
141
                        GeometryOperationContext ctx = new GeometryOperationContext();
142

    
143
                        // Here is the main loop where you call the operation
144
                        for (int i = 0; i < geoms.length; i++) {
145
                                geomsjts[i] = (com.vividsolutions.jts.geom.Geometry) geomOp
146
                                                .invoke(geoms[i], ctx);
147
                        }
148

    
149
                } catch (Exception e) {
150
                        log.debug("Error parsing geometries to JTS",e);
151
                }
152
                log.debug("Geometry parsed");
153
                return geomsjts;
154
        }
155

    
156
        /**
157
         * This method makes the union the geometries with one attribute common.
158
         * 
159
         * @param geomsJts
160
         * @return
161
         */
162
        protected com.vividsolutions.jts.geom.Geometry unionLinesJTS(
163
                        com.vividsolutions.jts.geom.Geometry[] geomsJts) {
164

    
165
                com.vividsolutions.jts.geom.Geometry geometJTS = null;
166

    
167
                // if there are more that one geometries
168
                if (geomsJts.length > 1) {
169

    
170
                        geometJTS = OverlayOp.overlayOp(geomsJts[0], geomsJts[1],
171
                                        OverlayOp.UNION);
172

    
173
                        for (int i = 2; i < geomsJts.length; i++) {
174
                                geometJTS = OverlayOp.overlayOp(geometJTS, geomsJts[i],
175
                                                OverlayOp.UNION);
176
                        }
177
                        log.debug("Union of geometries");
178
                        return geometJTS;
179

    
180
                }
181
                // Only there is one geometry
182
                else {
183
                        log.debug("Union of geometries");
184
                        return geomsJts[0];
185
                }
186
        }
187

    
188
        // /**
189
        // *
190
        // * @param geometriesJts
191
        // * @return
192
        // */
193
        // protected List intersectLinesJTS(
194
        // com.vividsolutions.jts.geom.Geometry[] geometriesJts) {
195
        //
196
        // List points = new ArrayList();
197
        // com.vividsolutions.jts.geom.Geometry geomPointsJTS = null;
198
        // com.vividsolutions.jts.geom.Geometry interBBoxJTS = null;
199
        // com.vividsolutions.jts.geom.Geometry colPointsJts[] = null;
200
        //
201
        // if (geometriesJts.length > 0 || geometriesJts != null) {
202
        //
203
        // for (int i = 0; i < geometriesJts.length; i++) {
204
        //
205
        // for (int j = i + 1; j < geometriesJts.length; j++) {
206
        //
207
        // interBBoxJTS = OverlayOp.overlayOp(geometriesJts[i]
208
        // .getEnvelope(), geometriesJts[j].getEnvelope(),
209
        // OverlayOp.INTERSECTION);
210
        // if (interBBoxJTS.getGeometryType().compareTo("LineString") == 0) {
211
        // geomPointsJTS = OverlayOp.overlayOp(geometriesJts[i],
212
        // geometriesJts[j], OverlayOp.INTERSECTION);
213
        // if (geomPointsJTS.getGeometryType().compareTo("Point") == 0) {
214
        //
215
        // boolean toca = false;
216
        // for (int k = 0; k < points.size(); k++) {
217
        // com.vividsolutions.jts.geom.Point po =
218
        // (com.vividsolutions.jts.geom.Point) points
219
        // .get(k);
220
        // if (po.getCoordinate().x == geomPointsJTS
221
        // .getCoordinate().x
222
        // && po.getCoordinate().y == geomPointsJTS
223
        // .getCoordinate().y) {
224
        // toca = true;
225
        // break;
226
        // }
227
        // }
228
        // if (!toca) {
229
        // points.add(geomPointsJTS);
230
        // }
231
        // }
232
        // }
233
        // }
234
        // }
235
        // return points;
236
        //
237
        // } else {
238
        // return points;
239
        // }
240
        // }
241

    
242
        /**
243
         * This method intersect two lines and return the intersection point. If
244
         * result is null, two lines doesn't intersect
245
         * 
246
         * @param geometriesJts
247
         * @return
248
         */
249
        protected com.vividsolutions.jts.geom.Point intersectTwoLinesJTS(
250
                        com.vividsolutions.jts.geom.Geometry geom1Jts,
251
                        com.vividsolutions.jts.geom.Geometry geom2Jts) {
252

    
253
                com.vividsolutions.jts.geom.Geometry interBBoxJTS = null;
254
                com.vividsolutions.jts.geom.Geometry geomPointJTS = null;
255

    
256
                if (geom1Jts != null || geom2Jts != null) {
257
                        log.debug("Intersect: Two geometries not nulls");
258

    
259
                        interBBoxJTS = OverlayOp.overlayOp(geom1Jts.getEnvelope(), geom2Jts
260
                                        .getEnvelope(), OverlayOp.INTERSECTION);
261
                        if (interBBoxJTS.getGeometryType().compareTo("LineString") == 0
262
                                        || interBBoxJTS.getGeometryType().compareTo("Polygon") == 0) {
263
                                log.debug("Intersect: Intersect two BBOX");
264
                                geomPointJTS = OverlayOp.overlayOp(geom1Jts, geom2Jts,
265
                                                OverlayOp.INTERSECTION);
266

    
267
                                if (geomPointJTS.getGeometryType().compareTo("Point") == 0) {
268
                                        log.debug("Intersect: Intersect in the point X= "
269
                                                        + geomPointJTS.getCoordinate().x + " Y= "
270
                                                        + geomPointJTS.getCoordinate().y);
271
                                        return (com.vividsolutions.jts.geom.Point) geomPointJTS;
272
                                } else {
273
                                        log.debug("Intersect: Two lines don't intersect");
274
                                        return null;
275
                                }
276
                        } else {
277
                                log.debug("Intersect: Two BBOX don't intersect");
278
                                return null;
279
                        }
280
                }
281
                return null;
282

    
283
        }
284

    
285
        /**
286
         * This method group geometries to one attribute
287
         * 
288
         * @param fieldPosition
289
         *            position of the field with the attribute
290
         * @param feats
291
         *            features collection
292
         * @return
293
         */
294
        @SuppressWarnings("unchecked")
295
        protected HashMap groupFeaturesByAttribute(int fieldPosition, List feats) {
296

    
297
                HashMap coleccion = new HashMap();
298
                Iterator it = feats.iterator();
299
                // Go for all geometries of the collection
300
                while (it.hasNext()) {
301
                        // Get feature
302
                        Feature featur = (Feature) it.next();
303
                        String key = featur.get(fieldPosition).toString();
304
                        // Get geometry
305
                        Geometry geomet = (Geometry) featur.getGeometry("GEOMETRY");
306
                        // Store the geometries for attribute in the List
307
                        boolean contiene = coleccion.containsKey(key);
308
                        if (!contiene) {
309
                                List geomss = new ArrayList();
310
                                geomss.add(geomet);
311
                                coleccion.put(key, geomss);
312
                        } else {
313
                                ((ArrayList) coleccion.get(key)).add(geomet);
314
                        }
315
                }
316
                return coleccion;
317
        }
318

    
319
        /**
320
         * This abstract method gets the position over the geometries
321
         * 
322
         * @return
323
         */
324
        public abstract Geometry[] match();
325

    
326
}