Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / impl / Geometry2D.java @ 29972

History | View | Annotate | Download (11.6 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
* 2009 {Iver T.I.}   {Task}
26
*/
27
 
28
package org.gvsig.fmap.geom.impl;
29

    
30
import java.awt.Rectangle;
31
import java.awt.Shape;
32
import java.awt.geom.AffineTransform;
33
import java.awt.geom.PathIterator;
34
import java.awt.geom.Point2D;
35
import java.awt.geom.Rectangle2D;
36

    
37
import org.cresques.cts.ICoordTrans;
38
import org.gvsig.fmap.geom.Geometry;
39
import org.gvsig.fmap.geom.GeometryLocator;
40
import org.gvsig.fmap.geom.GeometryManager;
41
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
42
import org.gvsig.fmap.geom.handler.Handler;
43
import org.gvsig.fmap.geom.operation.GeometryOperation;
44
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
45
import org.gvsig.fmap.geom.operation.GeometryOperationException;
46
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
47
import org.gvsig.fmap.geom.operation.relationship.DefaultRelationshipGeometryOperationContext;
48
import org.gvsig.fmap.geom.primitive.Envelope;
49
import org.gvsig.fmap.geom.primitive.GeneralPathX;
50
import org.gvsig.fmap.geom.type.GeometryType;
51
import org.slf4j.Logger;
52
import org.slf4j.LoggerFactory;
53

    
54
/**
55
 * @author <a href="mailto:jpiera@gvsig.org">Jorge Piera</a>
56
 */
57
public class Geometry2D implements Geometry{
58
        
59
        private static final Logger logger = LoggerFactory.getLogger(GeometryManager.class);
60
        
61
        private Geometry geometry = null;
62
                
63
        /**
64
         * @param primitive
65
         */
66
        public Geometry2D(Geometry geometry) {
67
                super();
68
                this.geometry = geometry;
69
        }
70

    
71
        /* (non-Javadoc)
72
         * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
73
         */
74
        public Geometry cloneGeometry() {
75
                return new Geometry2D((Geometry)geometry.cloneGeometry());
76
        }
77

    
78
        /* (non-Javadoc)
79
         * @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double, double)
80
         */
81
        public boolean fastIntersects(double x, double y, double w, double h) {
82
                return geometry.fastIntersects(x, y, w, h);
83
        }
84

    
85
        /* (non-Javadoc)
86
         * @see org.gvsig.fmap.geom.Geometry#getBounds2D()
87
         */
88
        public Rectangle2D getBounds2D() {
89
                return geometry.getBounds2D();
90
        }
91

    
92
        /* (non-Javadoc)
93
         * @see org.gvsig.fmap.geom.Geometry#getDimension()
94
         */
95
        public int getDimension() {
96
                return geometry.getDimension();
97
        }
98

    
99
        /* (non-Javadoc)
100
         * @see org.gvsig.fmap.geom.Geometry#getEnvelope()
101
         */
102
        public Envelope getEnvelope() {
103
                return geometry.getEnvelope();
104
        }
105

    
106
        /* (non-Javadoc)
107
         * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
108
         */
109
        public GeneralPathX getGeneralPath() {
110
                return geometry.getGeneralPath();
111
        }
112

    
113
        /* (non-Javadoc)
114
         * @see org.gvsig.fmap.geom.Geometry#getGeometryType()
115
         */
116
        public GeometryType getGeometryType() {
117
                return geometry.getGeometryType();
118
        }
119

    
120
        /* (non-Javadoc)
121
         * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
122
         */
123
        public Handler[] getHandlers(int type) {
124
                return geometry.getHandlers(type);
125
        }
126

    
127
        /* (non-Javadoc)
128
         * @see org.gvsig.fmap.geom.Geometry#getInternalShape()
129
         */
130
        public Shape getInternalShape() {
131
                return geometry.getInternalShape();
132
        }
133

    
134
        /* (non-Javadoc)
135
         * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
136
         */
137
        public PathIterator getPathIterator(AffineTransform at) {
138
                return geometry.getPathIterator(at);
139
        }
140

    
141
        /* (non-Javadoc)
142
         * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
143
         */
144
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
145
                return geometry.getPathIterator(at, flatness);
146
        }
147

    
148
        /* (non-Javadoc)
149
         * @see org.gvsig.fmap.geom.Geometry#getType()
150
         */
151
        public int getType() {
152
                return geometry.getType();
153
        }
154

    
155
        /* (non-Javadoc)
156
         * @see org.gvsig.fmap.geom.Geometry#intersects(java.awt.geom.Rectangle2D)
157
         */
158
        public boolean intersects(Rectangle2D r) {
159
                return geometry.intersects(r);
160
        }
161

    
162
        /* (non-Javadoc)
163
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(int, org.gvsig.fmap.geom.operation.GeometryOperationContext)
164
         */
165
        public Object invokeOperation(int index, GeometryOperationContext ctx)
166
                        throws GeometryOperationNotSupportedException,
167
                        GeometryOperationException {
168
                return geometry.invokeOperation(index, ctx);
169
        }
170

    
171
        /* (non-Javadoc)
172
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String, org.gvsig.fmap.geom.operation.GeometryOperationContext)
173
         */
174
        public Object invokeOperation(String opName, GeometryOperationContext ctx)
175
                        throws GeometryOperationNotSupportedException,
176
                        GeometryOperationException {
177
                return geometry.invokeOperation(opName, ctx);
178
        }
179

    
180
        /* (non-Javadoc)
181
         * @see org.gvsig.fmap.geom.Geometry#isSimple()
182
         */
183
        public boolean isSimple() {
184
                return geometry.isSimple();
185
        }
186

    
187
        /* (non-Javadoc)
188
         * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
189
         */
190
        public void reProject(ICoordTrans ct) {
191
                geometry.reProject(ct);
192
        }
193

    
194
        /* (non-Javadoc)
195
         * @see org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
196
         */
197
        public void transform(AffineTransform at) {
198
                geometry.transform(at);
199
        }
200

    
201
        /* (non-Javadoc)
202
         * @see java.awt.Shape#contains(java.awt.geom.Point2D)
203
         */
204
        public boolean contains(Point2D p) {
205
                
206
                
207
                
208
                try {
209
                        return containsPoint(geometry, p.getX(), p.getY());
210
                } catch (GeometryOperationException e) {
211
                        logger.error("While doing contains: " + e.getMessage(), e);
212
                        return true;
213
                }
214
        }
215

    
216
        /* (non-Javadoc)
217
         * @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
218
         */
219
        public boolean contains(Rectangle2D r) {
220
                try {
221
                        return containsRectangle(geometry,
222
                                        r.getMinX(),
223
                                        r.getMinY(),
224
                                        r.getWidth(),
225
                                        r.getHeight());
226
                } catch (GeometryOperationException e) {
227
                        logger.error("While doing contains: " + e.getMessage(), e);
228
                        return true;
229
                }
230
        }
231

    
232
        /* (non-Javadoc)
233
         * @see java.awt.Shape#contains(double, double)
234
         */
235
        public boolean contains(double x, double y) {
236
                try {
237
                        return containsPoint(geometry, x, y);
238
                } catch (GeometryOperationException e) {
239
                        logger.error("While doing contains: " + e.getMessage(), e);
240
                        return true;
241
                }
242
        }
243

    
244
        /* (non-Javadoc)
245
         * @see java.awt.Shape#contains(double, double, double, double)
246
         */
247
        public boolean contains(double x, double y, double w, double h) {
248
                try {
249
                        return containsRectangle(geometry, x, y, w, h);
250
                } catch (GeometryOperationException e) {
251
                        logger.error("While doing contains: " + e.getMessage(), e);
252
                        return true;
253
                }
254
        }
255

    
256
        /* (non-Javadoc)
257
         * @see java.awt.Shape#getBounds()
258
         */
259
        public Rectangle getBounds() {
260
                return geometry.getBounds();
261
        }
262

    
263
        /* (non-Javadoc)
264
         * @see java.awt.Shape#intersects(double, double, double, double)
265
         */
266
        public boolean intersects(double x, double y, double w, double h) {
267
                try {
268
                        return intersectsRectangle(geometry, x, y, w, h);
269
                } catch (GeometryOperationException e) {
270
                        logger.error("While doing intersects: " + e.getMessage(), e);
271
                        return true;
272
                }
273
        }
274

    
275
        /* (non-Javadoc)
276
         * @see java.lang.Comparable#compareTo(java.lang.Object)
277
         */
278
        public int compareTo(Object arg0) {
279
                return geometry.compareTo(arg0);
280
        }
281
        
282
        
283
        
284
        
285
        
286
        
287
        
288
        
289
        
290
        /**
291
         * Utility method
292
         * @param geometry
293
         * @param x
294
         * @param y
295
         * @return
296
         * @throws GeometryOperationException
297
         */
298
        protected boolean containsPoint(Geometry geom, double x, double y) throws GeometryOperationException {
299
                
300
                // exclude disjoint 
301
                Envelope env = geom.getEnvelope();
302
                if (x > env.getMaximum(0)) return false; 
303
                if (y > env.getMaximum(1)) return false; 
304
                if (x < env.getMinimum(0)) return false; 
305
                if (y < env.getMinimum(1)) return false; 
306
                
307
                // boxes overlap, need long version
308
                
309
                Geometry geompoint = null;
310
                try {
311
                        geompoint = GeometryLocator.getGeometryManager().createPoint(x, y, SUBTYPES.GEOM2D);
312
                } catch (Exception e1) {
313
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1);
314
                }
315
                
316
                DefaultRelationshipGeometryOperationContext drgoc =
317
                        new DefaultRelationshipGeometryOperationContext(geompoint);
318
                
319
                Object resp = null;
320
                boolean respboolean = true;
321
                try {
322
                        resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc);
323
                        respboolean = ((Boolean) resp).booleanValue();
324
                } catch (Exception e) {
325
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e);
326
                }
327

    
328
                return respboolean;
329
        }
330

    
331
        /**
332
         * 
333
         * @param geometry
334
         * @param x
335
         * @param y
336
         * @param w
337
         * @param h
338
         * @return
339
         */
340
        protected boolean containsRectangle(Geometry geom, double x, double y,
341
                        double w, double h) throws GeometryOperationException {
342
                
343
                
344
                // exclude disjoint boxes
345
                Envelope env = geom.getEnvelope();
346
                if (x > env.getMaximum(0)) return false; 
347
                if ((x+w) < env.getMinimum(0)) return false; 
348
                if (y > env.getMaximum(1)) return false; 
349
                if ((y+h) < env.getMinimum(1)) return false; 
350
                
351
                if (w == 0 && h == 0) {
352
                        return  containsPoint(geom, x, y); 
353
                }
354
                
355
                // boxes overlap, need long version
356
                Geometry rectgeom = null;
357
                GeneralPathX gpx = new GeneralPathX();
358
                gpx.moveTo(x, y);
359
                gpx.lineTo(x+w, y);
360
                gpx.lineTo(x+w, y+h);
361
                gpx.lineTo(x, y+h);
362
                gpx.lineTo(x, y);
363
                
364
                try {
365
                        rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D);
366
                } catch (Exception e1) {
367
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e1);
368
                }
369
                
370
                DefaultRelationshipGeometryOperationContext drgoc =
371
                        new DefaultRelationshipGeometryOperationContext(rectgeom);
372
                
373
                Object resp = null;
374
                boolean respboolean = true;
375
                try {
376
                        resp = geom.invokeOperation(GeometryOperation.OPERATION_CONTAINS_CODE, drgoc);
377
                        respboolean = ((Boolean) resp).booleanValue();
378
                } catch (Exception e) {
379
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_CONTAINS_CODE, e);
380
                }
381

    
382
                return respboolean;
383
        }
384
        
385

    
386
        /**
387
         * 
388
         * @param geom
389
         * @param x
390
         * @param y
391
         * @param w
392
         * @param h
393
         * @return
394
         */
395
        protected boolean intersectsRectangle(Geometry geom, double x, double y,
396
                        double w, double h) throws GeometryOperationException {
397
                
398
                // exclude disjoint boxes
399
                Envelope env = geom.getEnvelope();
400
                if (x > env.getMaximum(0)) return false; 
401
                if ((x+w) < env.getMinimum(0)) return false; 
402
                if (y > env.getMaximum(1)) return false; 
403
                if ((y+h) < env.getMinimum(1)) return false; 
404
                
405
                // boxes overlap, need long version
406
                Geometry rectgeom = null;
407
                GeneralPathX gpx = new GeneralPathX();
408
                gpx.moveTo(x, y);
409
                gpx.lineTo(x+w, y);
410
                gpx.lineTo(x+w, y+h);
411
                gpx.lineTo(x, y+h);
412
                gpx.lineTo(x, y);
413
                
414
                try {
415
                        rectgeom = GeometryLocator.getGeometryManager().createSurface(gpx, SUBTYPES.GEOM2D);
416
                } catch (Exception e1) {
417
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e1);
418
                }
419
                
420
                DefaultRelationshipGeometryOperationContext drgoc =
421
                        new DefaultRelationshipGeometryOperationContext(rectgeom);
422
                
423
                Object resp = null;
424
                boolean respboolean = true;
425
                try {
426
                        resp = geom.invokeOperation(GeometryOperation.OPERATION_INTERSECTS_CODE, drgoc);
427
                        respboolean = ((Boolean) resp).booleanValue();
428
                } catch (Exception e) {
429
                        throw new GeometryOperationException(geom.getType(), GeometryOperation.OPERATION_INTERSECTS_CODE, e);
430
                }
431

    
432
                return respboolean;
433

    
434

    
435
        }
436

    
437
}
438