Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.geometry / org.gvsig.fmap.geometry.jts / src / main / java / org / gvsig / fmap / geom / jts / primitive / surface / circle / AbstractCircle.java @ 44612

History | View | Annotate | Download (15 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2015 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.fmap.geom.jts.primitive.surface.circle;
24

    
25
import java.awt.Shape;
26
import java.awt.geom.AffineTransform;
27
import java.awt.geom.PathIterator;
28

    
29
import org.cresques.cts.CoordTransRuntimeException;
30
import org.cresques.cts.ICoordTrans;
31
import org.gvsig.fmap.geom.Geometry;
32

    
33
import org.gvsig.fmap.geom.GeometryLocator;
34
import org.gvsig.fmap.geom.exception.CreateGeometryException;
35
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
36
import org.gvsig.fmap.geom.jts.primitive.point.Point2D;
37
import org.gvsig.fmap.geom.jts.primitive.point.PointJTS;
38
import org.gvsig.fmap.geom.jts.primitive.surface.AbstractSurface;
39
import org.gvsig.fmap.geom.jts.util.UtilFunctions;
40
import org.gvsig.fmap.geom.operation.GeometryOperationException;
41
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
42
import org.gvsig.fmap.geom.primitive.GeneralPathX;
43
import org.gvsig.fmap.geom.primitive.Line;
44
import org.gvsig.fmap.geom.primitive.Point;
45
import org.gvsig.fmap.geom.primitive.Polygon;
46
import org.gvsig.fmap.geom.primitive.Ring;
47
import org.gvsig.tools.exception.BaseException;
48
import org.gvsig.tools.locator.LocatorException;
49

    
50

    
51
/**
52
 * @author fdiaz
53
 *
54
 */
55
public abstract class AbstractCircle extends AbstractSurface {
56

    
57
    /**
58
     *
59
     */
60
    private static final long serialVersionUID = -5509291843865895995L;
61

    
62
    protected Point center;
63
    protected double radius;
64

    
65
    /**
66
     * @param type
67
     * @param subtype
68
     */
69
    public AbstractCircle(int type, int subtype) {
70
        super(type, subtype);
71
    }
72

    
73
    /**
74
     * @param type
75
     * @param subtype
76
     */
77
    protected AbstractCircle(int type, int subtype, Point center, double radius) {
78
        this(type, subtype);
79
        this.setCenter(center);
80
        this.setRadius(radius);
81
    }
82

    
83
    @Override
84
    public Geometry force2D() throws GeometryOperationNotSupportedException, GeometryOperationException {
85
        Circle2D other = new Circle2D((Point) center.force2D(), radius);
86
        other.setProjection(this.getProjection());
87
        return other;
88
    }
89

    
90
    /**
91
     * @param initialPoint
92
     * @return
93
     */
94
    protected abstract Point fixPoint(Point point);
95

    
96
    /**
97
     * @param center the center to set
98
     */
99
    public void setCenter(Point center) {
100
        this.center = fixPoint(center);
101
    }
102

    
103
    /* (non-Javadoc)
104
     * @see org.gvsig.fmap.geom.primitive.Circle#getCenter()
105
     */
106
    public Point getCenter() {
107
        return this.center;
108
    }
109

    
110
    /**
111
     * @param radius the radius to set
112
     */
113
    public void setRadius(double radius) {
114
        this.radius = radius;
115
    }
116

    
117
    /* (non-Javadoc)
118
     * @see org.gvsig.fmap.geom.primitive.Circle#getRadious()
119
     */
120
    public double getRadious() {
121
        return this.radius;
122
    }
123

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

    
131
    /* (non-Javadoc)
132
     * @see org.gvsig.fmap.geom.Geometry#isSimple()
133
     */
134
    public boolean isSimple() {
135
        return true;
136
    }
137

    
138
    /* (non-Javadoc)
139
     * @see org.gvsig.fmap.geom.primitive.Circle#setPoints(org.gvsig.fmap.geom.primitive.Point, org.gvsig.fmap.geom.primitive.Point)
140
     */
141
    public void setPoints(Point center, Point radius) {
142
        setCenter(center);
143
        this.radius = ((PointJTS)radius).getJTSCoordinate().distance(((PointJTS)this.center).getJTSCoordinate());
144
    }
145

    
146
    /* (non-Javadoc)
147
     * @see org.gvsig.fmap.geom.primitive.Circle#setPoints(org.gvsig.fmap.geom.primitive.Point, double)
148
     */
149
    public void setPoints(Point center, double radius) {
150
        setCenter(center);
151
        this.radius = radius;
152
    }
153

    
154
    /* (non-Javadoc)
155
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
156
     */
157
    public boolean is3D() {
158
        return ((PointJTS)center).is3D();
159
    }
160

    
161
    /* (non-Javadoc)
162
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getCoordinateAt(int, int)
163
     */
164
    public double getCoordinateAt(int index, int dimension) {
165
        String message = "Calling deprecated method setPoints of a circle";
166
        notifyDeprecated(message);
167
        throw new UnsupportedOperationException(message);
168
    }
169

    
170
    /* (non-Javadoc)
171
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setCoordinateAt(int, int, double)
172
     */
173
    public void setCoordinateAt(int index, int dimension, double value) {
174
        String message = "Calling deprecated method setPoints of a circle";
175
        notifyDeprecated(message);
176
        throw new UnsupportedOperationException(message);
177
    }
178

    
179
    /* (non-Javadoc)
180
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(org.gvsig.fmap.geom.primitive.Point)
181
     */
182
    public void addVertex(Point point) {
183
        String message = "Calling deprecated method setPoints of a circle";
184
        notifyDeprecated(message);
185
        throw new UnsupportedOperationException(message);
186
    }
187

    
188
    /* (non-Javadoc)
189
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double)
190
     */
191
    public void addVertex(double x, double y) {
192
        String message = "Calling deprecated method setPoints of a circle";
193
        notifyDeprecated(message);
194
        throw new UnsupportedOperationException(message);
195
    }
196

    
197
    /* (non-Javadoc)
198
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addVertex(double, double, double)
199
     */
200
    public void addVertex(double x, double y, double z) {
201
        String message = "Calling deprecated method setPoints of a circle";
202
        notifyDeprecated(message);
203
        throw new UnsupportedOperationException(message);
204
    }
205

    
206
    /* (non-Javadoc)
207
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#removeVertex(int)
208
     */
209
    public void removeVertex(int index) {
210
        String message = "Calling deprecated method setPoints of a circle";
211
        notifyDeprecated(message);
212
        throw new UnsupportedOperationException(message);
213
    }
214

    
215
    /* (non-Javadoc)
216
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getVertex(int)
217
     */
218
    public Point getVertex(int index) {
219
        String message = "Calling deprecated method setPoints of a circle";
220
        notifyDeprecated(message);
221
        throw new UnsupportedOperationException(message);
222
    }
223

    
224
    /* (non-Javadoc)
225
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#getNumVertices()
226
     */
227
    public int getNumVertices() {
228
        String message = "Calling deprecated method getNumVertices of a circle";
229
        notifyDeprecated(message);
230
        throw new UnsupportedOperationException(message);
231
    }
232

    
233
    /* (non-Javadoc)
234
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#insertVertex(int, org.gvsig.fmap.geom.primitive.Point)
235
     */
236
    public void insertVertex(int index, Point p) {
237
        String message = "Calling deprecated method setPoints of a circle";
238
        notifyDeprecated(message);
239
        throw new UnsupportedOperationException(message);
240
    }
241

    
242
    /* (non-Javadoc)
243
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setVertex(int, org.gvsig.fmap.geom.primitive.Point)
244
     */
245
    public void setVertex(int index, Point p) {
246
        String message = "Calling deprecated method setPoints of a circle";
247
        notifyDeprecated(message);
248
        throw new UnsupportedOperationException(message);
249
    }
250

    
251
    /* (non-Javadoc)
252
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#setGeneralPath(org.gvsig.fmap.geom.primitive.GeneralPathX)
253
     */
254
    public void setGeneralPath(GeneralPathX generalPathX) {
255
        String message = "Calling deprecated method setPoints of a circle";
256
        notifyDeprecated(message);
257
        throw new UnsupportedOperationException(message);
258
    }
259

    
260
    /* (non-Javadoc)
261
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#addMoveToVertex(org.gvsig.fmap.geom.primitive.Point)
262
     */
263
    public void addMoveToVertex(Point point) {
264
        String message = "Calling deprecated method setPoints of a circle";
265
        notifyDeprecated(message);
266
        throw new UnsupportedOperationException(message);
267
    }
268

    
269
    /* (non-Javadoc)
270
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#closePrimitive()
271
     */
272
    public void closePrimitive() {
273
        String message = "Calling deprecated method setPoints of a circle";
274
        notifyDeprecated(message);
275
        throw new UnsupportedOperationException(message);
276
    }
277

    
278
    /* (non-Javadoc)
279
     * @see org.gvsig.fmap.geom.primitive.OrientablePrimitive#ensureCapacity(int)
280
     */
281
    public void ensureCapacity(int capacity) {
282
        String message = "Calling deprecated method ensureCapacity of a circle";
283
        notifyDeprecated(message);
284
        throw new UnsupportedOperationException(message);
285
    }
286

    
287
    /* (non-Javadoc)
288
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
289
     */
290
    public Shape getShape(AffineTransform affineTransform) {
291
        return new DefaultGeneralPathX(getPathIterator(affineTransform),false,0);
292
    }
293

    
294
    /* (non-Javadoc)
295
     * @see org.gvsig.fmap.geom.Geometry#getShape()
296
     */
297
    public Shape getShape() {
298
        return getShape(null);
299
    }
300

    
301
    /* (non-Javadoc)
302
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform)
303
     */
304
    public PathIterator getPathIterator(AffineTransform at) {
305
        return this.getPathIterator(at, getManager().getFlatness());
306
    }
307

    
308
    /* (non-Javadoc)
309
     * @see org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform, double)
310
     */
311
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
312

    
313
        java.awt.geom.Point2D.Double center = new java.awt.geom.Point2D.Double(this.center.getX(), this.center.getY());
314
        java.awt.geom.Arc2D arco = UtilFunctions.createCircle(center,  radius);
315

    
316
        return arco.getPathIterator(at, flatness);
317
    }
318

    
319
    /* (non-Javadoc)
320
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
321
     */
322
    public GeneralPathX getGeneralPath() {
323
        GeneralPathX gp = new DefaultGeneralPathX(getPathIterator(null, getManager().getFlatness()), is3D(), 0.0);
324
         return gp;
325
     }
326

    
327
    /* (non-Javadoc)
328
     * @see org.gvsig.fmap.geom.primitive.Surface#getNumInteriorRings()
329
     */
330
    public int getNumInteriorRings() {
331
        String message = "Calling deprecated method getNumInteriorRings of a circle";
332
        notifyDeprecated(message);
333
        throw new UnsupportedOperationException(message);
334
    }
335

    
336
    /* (non-Javadoc)
337
     * @see org.gvsig.fmap.geom.primitive.Surface#getInteriorRing(int)
338
     */
339
    public Ring getInteriorRing(int index) {
340
        String message = "Calling deprecated method getInteriorRing of a circle";
341
        notifyDeprecated(message);
342
        throw new UnsupportedOperationException(message);
343
    }
344

    
345
    /* (non-Javadoc)
346
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Ring)
347
     */
348
    public void addInteriorRing(Ring ring) {
349
        String message = "Calling deprecated method addInteriorRing of a circle";
350
        notifyDeprecated(message);
351
        throw new UnsupportedOperationException(message);
352
    }
353

    
354
    /* (non-Javadoc)
355
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Line)
356
     */
357
    public void addInteriorRing(Line ring) {
358
        String message = "Calling deprecated method addInteriorRing of a circle";
359
        notifyDeprecated(message);
360
        throw new UnsupportedOperationException(message);
361
    }
362

    
363
    /* (non-Javadoc)
364
     * @see org.gvsig.fmap.geom.primitive.Surface#addInteriorRing(org.gvsig.fmap.geom.primitive.Polygon)
365
     */
366
    public void addInteriorRing(Polygon polygon) {
367
        String message = "Calling unsupported method addInteriorRing of a circle";
368
        notifyDeprecated(message);
369
        throw new UnsupportedOperationException(message);
370
    }
371
    /* (non-Javadoc)
372
     * @see org.gvsig.fmap.geom.primitive.Surface#removeInteriorRing(int)
373
     */
374
    public void removeInteriorRing(int index) {
375
        String message = "Calling deprecated method removeInteriorRing of a circle";
376
        notifyDeprecated(message);
377
        throw new UnsupportedOperationException(message);
378
    }
379

    
380
    /* (non-Javadoc)
381
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
382
     */
383
    public void reProject(ICoordTrans ct) {
384
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
385
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
386

    
387
        Point2D aux = new Point2D(center.getX(), center.getY()-radius);
388
        try {
389
            center.reProject(ct);
390
            aux.reProject(ct);
391
            this.setProjection(ct.getPDest());
392
        } catch (CoordTransRuntimeException e){
393
            center.setX(0);
394
            center.setY(0);
395
            radius = 0;
396
            return;
397
        }
398
        try {
399
            radius = center.distance(aux);
400
        } catch (BaseException e) {
401
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
402
        }
403
    }
404

    
405
    /* (non-Javadoc)
406
     * @see org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
407
     */
408
    public void transform(AffineTransform at) {
409
        //FIXME: Esto solo ser?a correcto para transformaciones de traslaci?n, rotaci?n y escala
410
        // Ser?a incorrecto para las de deformaci?n en cizallamiento
411

    
412
        Point2D aux = new Point2D(center.getX(), center.getY()-radius);
413
        center.transform(at);
414
        aux.transform(at);
415
        try {
416
            radius = center.distance(aux);
417
        } catch (BaseException e) {
418
            throw new UnsupportedOperationException("Error calculating the radius of the transformed circle.", e);
419
        }
420

    
421
    }
422

    
423
    /* (non-Javadoc)
424
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
425
     */
426
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
427
        //FIXME: throw UnssupportedOperationException or do nothing?
428
//        String message = "Can't flip a circle";
429
//        notifyDeprecated(message);
430
//        throw new UnsupportedOperationException(message);
431
    }
432

    
433

    
434
    @Override
435
    public boolean canBeTransformed(AffineTransform at) {
436
        return false;
437
    }
438

    
439
    @Override
440
    public boolean canBeReprojected(ICoordTrans ct) {
441
        return false;
442
    }
443

    
444
}