Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libGeocoding / src / org / gvsig / geocoding / geommatches / GeomMatcher.java @ 22684

History | View | Annotate | Download (8.85 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.data.vectorial.Feature;
37
import org.gvsig.data.vectorial.FeatureCollection;
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 = GeometryManager.getInstance();
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");
151
                        e.printStackTrace();
152
                }
153
                log.debug("Geometry parsed");
154
                return geomsjts;
155
        }
156

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

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

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

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

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

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

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

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

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

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

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

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

    
284
        }
285

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

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

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

    
328
}