Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_geometries / src / org / gvsig / fmap / geom / primitive / impl / AbstractPrimitive.java @ 29097

History | View | Annotate | Download (10.5 KB)

1
package org.gvsig.fmap.geom.primitive.impl;
2

    
3
import java.awt.Shape;
4
import java.awt.geom.AffineTransform;
5
import java.awt.geom.PathIterator;
6
import java.awt.geom.Point2D;
7
import java.io.Serializable;
8

    
9
import org.cresques.cts.ICoordTrans;
10
import org.cresques.cts.IProjection;
11
import org.gvsig.fmap.geom.Geometry;
12
import org.gvsig.fmap.geom.GeometryLocator;
13
import org.gvsig.fmap.geom.GeometryManager;
14
import org.gvsig.fmap.geom.handler.Handler;
15
import org.gvsig.fmap.geom.operation.GeometryOperationContext;
16
import org.gvsig.fmap.geom.operation.GeometryOperationException;
17
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
18
import org.gvsig.fmap.geom.primitive.FShape;
19
import org.gvsig.fmap.geom.primitive.GeneralPathX;
20
import org.gvsig.fmap.geom.primitive.Primitive;
21
import org.gvsig.fmap.geom.type.GeometryType;
22

    
23

    
24
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
25
 *
26
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
27
 *
28
 * This program is free software; you can redistribute it and/or
29
 * modify it under the terms of the GNU General Public License
30
 * as published by the Free Software Foundation; either version 2
31
 * of the License, or (at your option) any later version.
32
 *
33
 * This program is distributed in the hope that it will be useful,
34
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
35
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
36
 * GNU General Public License for more details.
37
 *
38
 * You should have received a copy of the GNU General Public License
39
 * along with this program; if not, write to the Free Software
40
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
41
 *
42
 * For more information, contact:
43
 *
44
 *  Generalitat Valenciana
45
 *   Conselleria d'Infraestructures i Transport
46
 *   Av. Blasco Ib??ez, 50
47
 *   46010 VALENCIA
48
 *   SPAIN
49
 *
50
 *      +34 963862235
51
 *   gvsig@gva.es
52
 *      www.gvsig.gva.es
53
 *
54
 *    or
55
 *
56
 *   IVER T.I. S.A
57
 *   Salamanca 50
58
 *   46005 Valencia
59
 *   Spain
60
 *
61
 *   +34 963163400
62
 *   dac@iver.es
63
 */
64
/* CVS MESSAGES:
65
 *
66
 * $Id: AbstractGeometry.java,v 1.2 2008/03/25 08:47:41 cvs Exp $
67
 * $Log: AbstractGeometry.java,v $
68
 * Revision 1.2  2008/03/25 08:47:41  cvs
69
 * Visitors removed
70
 *
71
 * Revision 1.1  2008/03/12 08:46:20  cvs
72
 * *** empty log message ***
73
 *
74
 *
75
 */
76
/**
77
 * @author Jorge Piera Llodr? (jorge.piera@iver.es)
78
 */
79
public abstract class AbstractPrimitive implements Primitive, FShape, Serializable {
80
        private static final long serialVersionUID = -4334977368955260872L;
81
        protected String id = null;
82
        protected IProjection projection = null;
83
        protected GeometryType geometryType = null;
84
        private static GeometryManager geomManager = GeometryLocator.getGeometryManager();
85

    
86
        /**
87
         * The constructor with the GeometryType like and argument 
88
         * is used by the {@link GeometryType}{@link #create()}
89
         * to create the geometry
90
         * @param type
91
         * The geometry type
92
         */
93
        public AbstractPrimitive(GeometryType geometryType) {
94
                this(geometryType, null, null);                
95
        }
96
        
97
        AbstractPrimitive(int type, int subtype) {
98
                try {
99
                        geometryType = geomManager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM2D);
100
                } catch (Exception e) {
101
                        //Type not registered
102
                }
103
        }        
104
        
105
        public AbstractPrimitive(GeometryType geometryType, String id, IProjection projection) {
106
                super();
107
                this.id = id;
108
                this.projection = projection;
109
                this.geometryType = geometryType;
110
        }
111

    
112
        public AbstractPrimitive(GeometryType geometryType, IProjection projection) {
113
                this(geometryType, null, projection);
114
        }
115

    
116
        /* (non-Javadoc)
117
         * @see org.gvsig.fmap.geom.Geometry#getGeometryType()
118
         */
119
        public GeometryType getGeometryType() {
120
                return geometryType;
121
        }
122

    
123
        /* (non-Javadoc)
124
         * @see org.gvsig.fmap.geom.Geometry#getType()
125
         */
126
        public int getType() {
127
                return geometryType.getType();
128
        }
129

    
130
        /**
131
         * (non-Javadoc)
132
         * @see com.iver.cit.gvsig.fmap.core.Geometry#getInternalShape()
133
         * @deprecated this Geometry is a Shape.
134
         */
135
        public Shape getInternalShape() {
136
                return this;
137
        }
138

    
139
        /* (non-Javadoc)
140
         * @see org.gvsig.geometries.iso.AbstractGeometry#getId()
141
         */
142
        public String getId() {
143
                return id;
144
        }
145

    
146
        /* (non-Javadoc)
147
         * @see org.gvsig.geometries.iso.AbstractGeometry#getSRS()
148
         */
149
        public IProjection getSRS() {
150
                return projection;
151
        }
152

    
153
        /* (non-Javadoc)
154
         * @see org.gvsig.geometries.iso.AbstractGeometry#transform(org.cresques.cts.IProjection)
155
         */
156
        public AbstractPrimitive transform(IProjection newProjection) {
157
                Geometry newGeom = cloneGeometry();
158
                ICoordTrans coordTrans = projection.getCT(newProjection);
159
                newGeom.reProject(coordTrans);
160
                return (AbstractPrimitive)newGeom;
161
        }
162

    
163

    
164
        /*
165
         * TODO adaptar metodo procedente de UtilFunctions
166
         */
167
        public static void rotateGeom(Geometry geometry, double radAngle, double basex, double basey) {
168
                AffineTransform at = new AffineTransform();
169
                at.rotate(radAngle,basex,basey);
170
                geometry.transform(at);
171

    
172
        }
173

    
174
        /*
175
         * TODO adaptar metodo procedente de UtilFunctions
176
         */
177
        public static void moveGeom(Geometry geometry, double dx, double dy) {
178
        AffineTransform at = new AffineTransform();
179
        at.translate(dx, dy);
180
        geometry.transform(at);
181
        }
182

    
183
        /*
184
         * TODO adaptar metodo procedente de UtilFunctions
185
         */
186
        public static void scaleGeom(Geometry geometry, Point2D basePoint, double sx, double sy) {
187
                AffineTransform at = new AffineTransform();
188
                at.setToTranslation(basePoint.getX(),basePoint.getY());
189
                at.scale(sx,sy);
190
                at.translate(-basePoint.getX(),-basePoint.getY());
191
                geometry.transform(at);
192
        }
193

    
194
        /*
195
         * (non-Javadoc)
196
         * @see org.gvsig.fmap.geom.Geometry#fastIntersects(double, double, double, double)
197
         */
198
        public boolean fastIntersects(double x, double y, double w, double h) {
199
                return intersects(x,y,w,h);
200
        }
201

    
202
        /*
203
         * (non-Javadoc)
204
         * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
205
         */
206
        public Geometry cloneGeometry() {
207
                return (Geometry)cloneFShape();
208
        }
209

    
210
        /*
211
         * (non-Javadoc)
212
         * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
213
         */
214
        public Handler[] getHandlers(int type) {
215
                if (type==STRETCHINGHANDLER){
216
                        return getStretchingHandlers();
217
                }else if (type==SELECTHANDLER){
218
                        return getSelectHandlers();
219
                }
220
                return null;
221
        }
222

    
223
        /*
224
         * (non-Javadoc)
225
         * @see org.gvsig.fmap.geom.Geometry#isSimple()
226
         */
227
        public boolean isSimple() {
228
                return true;
229
        }
230

    
231
        /*
232
         * (non-Javadoc)
233
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(int, org.gvsig.fmap.geom.operation.GeometryOperationContext)
234
         */
235
        public Object invokeOperation(int index, GeometryOperationContext ctx) throws GeometryOperationNotSupportedException, GeometryOperationException {
236
                return geomManager.invokeOperation(index, this, ctx);
237
        }
238

    
239
        /*
240
         * (non-Javadoc)
241
         * @see org.gvsig.fmap.geom.Geometry#invokeOperation(java.lang.String, org.gvsig.fmap.geom.operation.GeometryOperationContext)
242
         */
243
        public Object invokeOperation(String oppName, GeometryOperationContext ctx) throws GeometryOperationNotSupportedException, GeometryOperationException {
244
                return geomManager.invokeOperation(oppName, this, ctx);
245
        }
246

    
247
        /*
248
         * (non-Javadoc)
249
         * @see java.lang.Comparable#compareTo(T)
250
         */
251
        public int compareTo(Object arg0) {
252
                // TODO Auto-generated method stub
253
                return -1;
254
        }
255

    
256
        /*
257
         * (non-Javadoc)
258
         * @see java.lang.Object#toString()
259
         */
260
        public String toString() {
261
                String name=getGeometryType().getName();
262
                return name.substring(name.lastIndexOf(".")+1);
263
        }
264

    
265
        /*
266
         * (non-Javadoc)
267
         * @see java.lang.Object#equals(java.lang.Object)
268
         */
269
        public boolean equals(Object obj) {
270
                if (obj == null) {
271
                        return false;
272
                }
273
                if (this.getClass() != obj.getClass()) {
274
                        return false;
275
                }
276

    
277
                AbstractPrimitive other = (AbstractPrimitive) obj;
278
                if (this.getGeometryType().getType() != other.getGeometryType()
279
                                .getType()) {
280
                        return false;
281

    
282
                }
283
                if (this.getGeometryType().getSubType() != other.getGeometryType()
284
                                .getSubType()) {
285
                        return false;
286

    
287
                }
288
                if (this.projection != other.projection) {
289
                        if (this.projection == null) {
290
                                return false;
291
                        }
292
                        if (this.projection.getAbrev() != other.projection.getAbrev()) { //FIXME this must be false
293
                                return false;
294
                        }
295
                }
296
                if (!this.getBounds().equals(other.getBounds())) {
297
                        return false;
298
                }
299

    
300

    
301
                GeneralPathX myPath = this.getGeneralPath();
302
                GeneralPathX otherPath = other.getGeneralPath();
303
                if (myPath == null) {
304
                        if (otherPath != null) {
305
                                return false;
306
                        } else {
307
                                // TODO checkThis
308
                                return true;
309
                        }
310

    
311
                }
312
                if (myPath.getNumTypes() != otherPath.getNumTypes()) {
313
                        return false;
314
                }
315
                if (Math.abs(myPath.getNumCoords() - otherPath.getNumCoords()) > this
316
                                .getDimension()) {
317
                        return false;
318
                }
319
                PathIterator myIter = myPath.getPathIterator(null);
320
                PathIterator otherIter = otherPath.getPathIterator(null);
321
                int myType,otherType;
322
                // FIXME when 3D, 2DM and 3DM
323
                double[] myData = new double[6];
324
                double[] otherData = new double[6];
325
                double[] myFirst = new double[] { myPath.getPointCoords()[0],myPath.getPointCoords()[1]};
326
                double[] otherFirst = new double[] { otherPath.getPointCoords()[0],otherPath.getPointCoords()[1]};
327

    
328
                while (!myIter.isDone()) {
329
                        if (otherIter.isDone()) {
330
                                return false;
331
                        }
332
                        for (int i = 0; i < myData.length; i++) {
333
                                myData[i] = 0.0;
334
                                otherData[i] = 0.0;
335
                        }
336

    
337
                        myType = myIter.currentSegment(myData);
338
                        otherType = otherIter.currentSegment(otherData);
339

    
340
                        switch (myType) {
341
                        case PathIterator.SEG_LINETO:
342
                                if (otherType != myType) {
343
                                        if (otherType == PathIterator.SEG_CLOSE){
344
                                                if (!comparePathIteratorData(otherFirst, myData)) {
345
                                                        return false;
346
                                                }
347
                                        } else {
348
                                                return false;
349
                                        }
350
                                } else {
351
                                        if (!comparePathIteratorData(myData, otherData)) {
352
                                                return false;
353
                                        }
354
                                }
355
                                break;
356

    
357

    
358
                        case PathIterator.SEG_CLOSE:
359
                                if (otherType != myType) {
360
                                        if (otherType == PathIterator.SEG_LINETO) {
361
                                                if (!comparePathIteratorData(myFirst, otherData)) {
362
                                                        return false;
363
                                                }
364
                                        } else {
365
                                                return false;
366
                                        }
367
                                } else {
368
                                        if (!comparePathIteratorData(myData, otherData)) {
369
                                                return false;
370
                                        }
371
                                }
372
                                break;
373

    
374

    
375

    
376
                        case PathIterator.SEG_MOVETO:
377
                        case PathIterator.SEG_QUADTO:
378
                        case PathIterator.SEG_CUBICTO:
379
                                if (otherType != myType) {
380
                                        return false;
381
                                }
382
                                if (!comparePathIteratorData(myData, otherData)) {
383
                                        return false;
384
                                }
385
                                break;
386

    
387
                        } // end switch
388

    
389

    
390
                        myIter.next();
391
                        otherIter.next();
392
                }
393
                if (!otherIter.isDone()) {
394
                        return false;
395
                }
396
                return true;
397
        }
398

    
399
        private boolean comparePathIteratorData(double[] org, double[] other) {
400
                for (int i = 0; i < org.length; i++) {
401
                        if (Math.abs(org[i] - other[i]) > 0.0000001) {
402
                                return false;
403
                        }
404
                }
405
                return true;
406
        }
407

    
408
        /* (non-Javadoc)
409
         * @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
410
         */
411
        public int getShapeType() {
412
                return getType();
413
        }
414
        
415
        
416
}