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 / point / AbstractPoint.java @ 42464

History | View | Annotate | Download (9.39 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.point;
24

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

    
29
import org.cresques.cts.ICoordTrans;
30

    
31
import org.gvsig.fmap.geom.DirectPosition;
32
import org.gvsig.fmap.geom.Geometry;
33
import org.gvsig.fmap.geom.GeometryException;
34
import org.gvsig.fmap.geom.aggregate.MultiLine;
35
import org.gvsig.fmap.geom.aggregate.MultiPolygon;
36
import org.gvsig.fmap.geom.exception.ReprojectionRuntimeException;
37
import org.gvsig.fmap.geom.handler.AbstractHandler;
38
import org.gvsig.fmap.geom.handler.FinalHandler;
39
import org.gvsig.fmap.geom.handler.Handler;
40
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
41
import org.gvsig.fmap.geom.jts.primitive.AbstractPrimitive;
42
import org.gvsig.fmap.geom.jts.primitive.Envelope2D;
43
import org.gvsig.fmap.geom.jts.util.JTSUtils;
44
import org.gvsig.fmap.geom.operation.GeometryOperationException;
45
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
46
import org.gvsig.fmap.geom.primitive.Envelope;
47

    
48
/**
49
 * @author fdiaz
50
 *
51
 */
52
public abstract class AbstractPoint extends AbstractPrimitive implements PointJTS {
53

    
54
    /**
55
     *
56
     */
57
    private static final long serialVersionUID = -8378193151810866724L;
58
    protected com.vividsolutions.jts.geom.Coordinate coordinate;
59

    
60
    /**
61
    *
62
    */
63
    protected AbstractPoint(int subtype) {
64
        super(Geometry.TYPES.POINT, subtype);
65
    }
66

    
67
    /**
68
     *
69
     */
70
    public AbstractPoint(int subtype, com.vividsolutions.jts.geom.Coordinate coordinate) {
71
        this(subtype);
72
        this.coordinate = coordinate;
73
    }
74

    
75
    /*
76
     * (non-Javadoc)
77
     *
78
     * @see org.gvsig.fmap.geom.primitive.Point#getDirectPosition()
79
     */
80
    public DirectPosition getDirectPosition() {
81
        return new PointDirectPosition();
82
    }
83

    
84
    class PointDirectPosition implements DirectPosition {
85

    
86
        /*
87
         * (non-Javadoc)
88
         *
89
         * @see org.gvsig.fmap.geom.DirectPosition#getDimension()
90
         */
91
        public int getDimension() {
92
            return AbstractPoint.this.getDimension();
93
        }
94

    
95
        /*
96
         * (non-Javadoc)
97
         *
98
         * @see org.gvsig.fmap.geom.DirectPosition#getOrdinate(int)
99
         */
100
        public double getOrdinate(int dimension) {
101
            return AbstractPoint.this.getCoordinateAt(dimension);
102
        }
103

    
104
    }
105

    
106
    /*
107
     * (non-Javadoc)
108
     *
109
     * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
110
     */
111
    public void setCoordinateAt(int dimension, double value) {
112
        this.coordinate.setOrdinate(dimension, value);
113
    }
114

    
115
    /*
116
     * (non-Javadoc)
117
     *
118
     * @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
119
     */
120
    public void setCoordinates(double[] values) {
121
        for (int i = 0; i < values.length; i++) {
122
            this.coordinate.setOrdinate(i, values[i]);
123
        }
124
    }
125

    
126
  /*
127
     * (non-Javadoc)
128
     *
129
     * @see org.gvsig.fmap.geom.primitive.Point#setX(double)
130
     */
131
    public void setX(double x) {
132
        this.coordinate.x = x;
133
    }
134

    
135
    /*
136
     * (non-Javadoc)
137
     *
138
     * @see org.gvsig.fmap.geom.primitive.Point#setY(double)
139
     */
140
    public void setY(double y) {
141
        this.coordinate.y = y;
142
    }
143

    
144
    /*
145
     * (non-Javadoc)
146
     *
147
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinateAt(int)
148
     */
149
    public double getCoordinateAt(int dimension) {
150
        return this.coordinate.getOrdinate(dimension);
151
    }
152

    
153
    /*
154
     * (non-Javadoc)
155
     *
156
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
157
     */
158
    public double[] getCoordinates() {
159
        double[] coords = new double[this.getDimension()];
160
        for (int i = 0; i < this.getDimension(); i++) {
161
            coords[i] = this.coordinate.getOrdinate(i);
162
        }
163
        return coords;
164
    }
165

    
166
    /*
167
     * (non-Javadoc)
168
     *
169
     * @see org.gvsig.fmap.geom.primitive.Point#getX()
170
     */
171
    public double getX() {
172
        return this.coordinate.x;
173
    }
174

    
175
    /*
176
     * (non-Javadoc)
177
     *
178
     * @see org.gvsig.fmap.geom.primitive.Point#getY()
179
     */
180
    public double getY() {
181
        return this.coordinate.y;
182
    }
183

    
184
    /*
185
     * (non-Javadoc)
186
     *
187
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#getJTS()
188
     */
189
    public com.vividsolutions.jts.geom.Geometry getJTS() {
190
        return JTSUtils.createJTSPoint(this.coordinate);
191
    }
192

    
193
    /*
194
     * (non-Javadoc)
195
     *
196
     * @see org.gvsig.fmap.geom.Geometry#reProject(org.cresques.cts.ICoordTrans)
197
     */
198
    public void reProject(ICoordTrans ct) {
199
        if (ct == null) {
200
            return;
201
        }
202
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
203
        try {
204
            p = ct.convert(p, p);
205
            this.setX(p.getX());
206
            this.setY(p.getY());
207
        } catch (Exception exc) {
208
            /*
209
             * This can happen when the reprojection lib is unable
210
             * to reproject (for example the source point
211
             * is out of the valid range and some computing
212
             * problem happens)
213
             */
214
            this.setX(0);
215
            this.setY(0);
216
        }
217
    }
218

    
219
    /*
220
     * (non-Javadoc)
221
     *
222
     * @see
223
     * org.gvsig.fmap.geom.Geometry#transform(java.awt.geom.AffineTransform)
224
     */
225
    public void transform(AffineTransform at) {
226
        if (at == null) {
227
            return;
228
        }
229

    
230
        java.awt.geom.Point2D p = new java.awt.geom.Point2D.Double(this.getX(), this.getY());
231
        at.transform(p, p);
232
        setX(p.getX());
233
        setY(p.getY());
234
    }
235

    
236
    /*
237
     * (non-Javadoc)
238
     *
239
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#getJTSCoordinates()
240
     */
241
    public com.vividsolutions.jts.geom.Coordinate getJTSCoordinate() {
242
        return this.coordinate;
243
    }
244

    
245
    /*
246
     * (non-Javadoc)
247
     *
248
     * @see org.gvsig.fmap.geom.Geometry#getShape(java.awt.geom.AffineTransform)
249
     */
250
    public Shape getShape(AffineTransform affineTransform) {
251
        return new DefaultGeneralPathX(getPathIterator(affineTransform), false, 0);
252
    }
253

    
254
    /*
255
     * (non-Javadoc)
256
     *
257
     * @see org.gvsig.fmap.geom.Geometry#getShape()
258
     */
259
    public Shape getShape() {
260
        return new DefaultGeneralPathX(getPathIterator(null), false, 0);
261
    }
262

    
263
    /*
264
     * (non-Javadoc)
265
     *
266
     * @see
267
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
268
     * , double)
269
     */
270
    public PathIterator getPathIterator(AffineTransform at, double flatness) {
271
        return getPathIterator(at);
272
    }
273

    
274
    /*
275
     * (non-Javadoc)
276
     *
277
     * @see org.gvsig.fmap.geom.Geometry#getHandlers(int)
278
     */
279
    public Handler[] getHandlers(int type) {
280
        return new Handler[] { new PointHandler() };
281
    }
282

    
283
    class PointHandler extends AbstractHandler implements FinalHandler {
284

    
285
        public PointHandler() {
286
            point = new java.awt.geom.Point2D.Double(AbstractPoint.this.getX(), AbstractPoint.this.getY());
287
            index = 0;
288
        }
289

    
290
        public void move(double movex, double movey) {
291
            AbstractPoint.this.setX(AbstractPoint.this.getX() + movex);
292
            AbstractPoint.this.setY(AbstractPoint.this.getY() + movey);
293
        }
294

    
295
        public void set(double setx, double sety) {
296
            AbstractPoint.this.setX(setx);
297
            AbstractPoint.this.setY(sety);
298
        }
299
    }
300

    
301
    public Envelope getEnvelope() {
302
        return new Envelope2D(this.getX(), this.getY(), this.getX(), this.getY());
303
    }
304

    
305

    
306
    /* (non-Javadoc)
307
     * @see org.gvsig.fmap.geom.Geometry#toLines()
308
     */
309
    public MultiLine toLines() throws GeometryException {
310
        String message = "Can't get lines from a point";
311
        notifyDeprecated(message);
312
        throw new UnsupportedOperationException(message);
313
    }
314

    
315
    /* (non-Javadoc)
316
     * @see org.gvsig.fmap.geom.Geometry#toPolygons()
317
     */
318
    public MultiPolygon toPolygons() throws GeometryException {
319
        String message = "Can't get polygons from a point";
320
        notifyDeprecated(message);
321
        throw new UnsupportedOperationException(message);
322
    }
323

    
324

    
325
    /* (non-Javadoc)
326
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#flip()
327
     */
328
    public void flip() throws GeometryOperationNotSupportedException, GeometryOperationException {
329
        //Do nothing
330
    }
331

    
332
    public String toString() {
333
        StringBuilder builder = new StringBuilder();
334
        builder.append("POINT (");
335
        for( int i=0; i<this.getDimension(); i++) {
336
            builder.append(this.getCoordinateAt(i));
337
            builder.append(" ");
338
        }
339
        builder.append(")");
340
        return builder.toString();
341
    }
342
}