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 / Point2DM.java @ 47432

History | View | Annotate | Download (9.72 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 com.vividsolutions.jts.geom.Coordinate;
26
import com.vividsolutions.jts.geom.CoordinateSequence;
27
import java.awt.geom.AffineTransform;
28
import java.awt.geom.PathIterator;
29
import org.cresques.cts.IProjection;
30
import org.gvsig.fmap.geom.Geometry;
31
import org.gvsig.fmap.geom.GeometryException;
32
import org.gvsig.fmap.geom.GeometryLocator;
33
import org.gvsig.fmap.geom.aggregate.MultiPoint;
34
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2DM;
35
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
36
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator;
37
import org.gvsig.fmap.geom.jts.mgeom.MCoordinate;
38
import org.gvsig.fmap.geom.jts.util.JTSUtils;
39
import org.gvsig.fmap.geom.operation.GeometryOperationException;
40
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException;
41
import org.gvsig.fmap.geom.primitive.GeneralPathX;
42
import org.gvsig.fmap.geom.primitive.Point;
43
import org.gvsig.fmap.geom.primitive.PointM;
44
import org.gvsig.fmap.geom.type.GeometryType;
45

    
46
/**
47
 * @author fdiaz
48
 *
49
 */
50
public class Point2DM extends AbstractPoint implements PointM{
51

    
52
    /**
53
     *
54
     */
55
    private static final long serialVersionUID = 7829655161985732518L;
56

    
57
    /**
58
   *
59
     * @param proj
60
     * @param coordinates
61
   */
62
    public Point2DM(IProjection proj, Coordinate coordinates) {
63
        super(Geometry.SUBTYPES.GEOM2DM, MCoordinate.convertCoordinate(coordinates));
64
        this.setProjection(proj);
65
    }
66

    
67
    /**
68
   *
69
   */
70
    public Point2DM() {
71
        this(null, JTSUtils.createMCoordinate(0, 0, 0));
72
    }
73

    
74
    /**
75
  *
76
     * @param x
77
     * @param y
78
     * @param m
79
  */
80
    public Point2DM(double x, double y, double m) {
81
        this(null, JTSUtils.createMCoordinate(x, y, m));
82
    }
83

    
84
    public Point2DM(IProjection proj, double x, double y, double m) {
85
        this(proj, JTSUtils.createMCoordinate(x, y, m));
86
    }
87

    
88
    @Override
89
    public double getM() {
90
        return ((MCoordinate)this.coordinate).m;
91
    }
92
    
93
    @Override
94
    public void setM(double m) {
95
        ((MCoordinate)this.coordinate).m = m;
96
    }
97

    
98
    /*
99
     * (non-Javadoc)
100
     *
101
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
102
     */
103
    @Override
104
    public int getDimension() {
105
        return 3;
106
    }
107

    
108
    /*
109
     * (non-Javadoc)
110
     *
111
     * @see org.gvsig.fmap.geom.Geometry#getGeometryType()
112
     */
113
    @Override
114
    public GeometryType getGeometryType() {
115
        try {
116
            return GeometryLocator.getGeometryManager()
117
                .getGeometryType(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM);
118
        } catch (Exception e) {
119
            return null;
120
        }
121
    }
122

    
123
    /*
124
     * (non-Javadoc)
125
     *
126
     * @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
127
     */
128
    @Override
129
    public Point cloneGeometry() {
130
        Point2DM clone = new Point2DM(this.getProjection(), MCoordinate.convertCoordinate((Coordinate) coordinate.clone()));
131
        clone.setProjection(this.getProjection());
132
        return clone;
133
        
134
    }
135

    
136
    /*
137
     * (non-Javadoc)
138
     *
139
     * @see org.gvsig.fmap.geom.primitive.Point#getCoordinateAt(int)
140
     */
141
    @Override
142
    public double getCoordinateAt(int dimension) {
143
        if (dimension == 2) {
144
            return this.getM();
145
        }
146
        return super.getCoordinateAt(dimension);
147
    }
148

    
149
    /*
150
     * (non-Javadoc)
151
     *
152
     * @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
153
     */
154
    @Override
155
    public void setCoordinateAt(int dimension, double value) {
156
        if (dimension == 2) {
157
            this.coordinate.setOrdinate(CoordinateSequence.M, value);
158
            return;
159
        }
160
        this.coordinate.setOrdinate(dimension, value);
161
    }
162

    
163
    /*
164
     * (non-Javadoc)
165
     *
166
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
167
     */
168
    @Override
169
    public boolean is3D() {
170
        return false;
171
    }
172

    
173
    /* (non-Javadoc)
174
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
175
     */
176
    @Override
177
    public void setJTSCoordinate(Coordinate coordinate) {
178
        this.coordinate = coordinate;
179
    }
180

    
181
    /*
182
     * (non-Javadoc)
183
     *
184
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
185
     */
186
    @Override
187
    public GeneralPathX getGeneralPath() {
188
        return new DefaultGeneralPathX(new PointIterator(null),false,0);
189
    }
190

    
191
    /*
192
     * (non-Javadoc)
193
     *
194
     * @see
195
     * org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
196
     * )
197
     */
198
    @Override
199
    public PathIterator getPathIterator(AffineTransform at) {
200
        PointIterator pi = new PointIterator(at);
201
        return pi;
202
    }
203

    
204
    @Override
205
    public int hashCode() {
206
        int hash = 3;
207
        return hash;
208
    }
209

    
210
    public class PointIterator extends GeneralPathXIterator {
211
        /** Transform applied on the coordinates during iteration */
212
        private final AffineTransform at;
213

    
214
        /** True when the point has been read once */
215
        private boolean done;
216

    
217
        /**
218
         * Creates a new PointIterator object.
219
         *
220
         * @param at The affine transform applied to coordinates during iteration
221
         */
222
        public PointIterator(AffineTransform at) {
223
            super(new GeneralPathX());
224
            if (at == null) {
225
                at = new AffineTransform();
226
            }
227

    
228
            this.at = at;
229
            done = false;
230
        }
231

    
232
        /**
233
         * Return the winding rule for determining the interior of the path.
234
         *
235
         * @return <code>WIND_EVEN_ODD</code> by default.
236
         */
237
        @Override
238
        public int getWindingRule() {
239
            return PathIterator.WIND_EVEN_ODD;
240
        }
241

    
242
        /**
243
         * @see java.awt.geom.PathIterator#next()
244
         */
245
        @Override
246
        public void next() {
247
            done = true;
248
        }
249

    
250
        /**
251
         * @see java.awt.geom.PathIterator#isDone()
252
         */
253
        @Override
254
        public boolean isDone() {
255
            return done;
256
        }
257

    
258
        /**
259
         * @param coords
260
         * @return 
261
         * @see java.awt.geom.PathIterator#currentSegment(double[])
262
         */
263
        @Override
264
        public int currentSegment(double[] coords) {
265
            coords[0] = getX();
266
            coords[1] = getY();
267
            at.transform(coords, 0, coords, 0, 1);
268

    
269
            return PathIterator.SEG_MOVETO;
270
        }
271

    
272
        /* (non-Javadoc)
273
         * @see java.awt.geom.PathIterator#currentSegment(float[])
274
         */
275
        @Override
276
        public int currentSegment(float[] coords) {
277
            coords[0] = (float) getX();
278
            coords[1] = (float) getY();
279
            at.transform(coords, 0, coords, 0, 1);
280

    
281
            return PathIterator.SEG_MOVETO;
282
        }
283
    }
284

    
285
    /* (non-Javadoc)
286
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
287
     */
288
    @Override
289
    public MultiPoint toPoints() throws GeometryException {
290
        MultiPoint multiPoint = new MultiPoint2DM();
291
        multiPoint.addPoint(this);
292
        return multiPoint;
293
    }
294

    
295
    @Override
296
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
297
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance));
298
    }
299

    
300
    @Override
301
    public String toString() {
302
        StringBuilder builder = new StringBuilder();
303
        builder.append("POINT M (");
304
        for( int i=0; i<this.getDimension()-1; i++) {
305
            builder.append(this.getCoordinateAt(i));
306
            builder.append(" ");
307
        }
308
        builder.append(this.getCoordinateAt(this.getDimension()-1));
309
        builder.append(")");
310
        return builder.toString();
311
    }
312

    
313
    @Override
314
    public boolean equals(Object obj) {
315
        if( obj == null ) {
316
            return false;
317
        }
318
        Point other;
319
        try {
320
            other = (Point) obj;
321
        } catch(ClassCastException e) {
322
            return false;
323
        }
324
        if( this.getGeometryType().getSubType() != other.getGeometryType().getSubType() ) {
325
            return false;
326
        }
327
        if( this.coordinate.x != other.getX() ) {
328
            return false;
329
        }
330
        if( this.coordinate.y != other.getY() ) {
331
            return false;
332
        }
333
        if( ((MCoordinate)this.coordinate).m != other.getCoordinateAt(2)) {
334
            return false;
335
        }
336
        return true;
337
    }
338

    
339
    @Override
340
    public Point force3D() {
341
        Point3D p = new Point3D(this.getProjection(), this.coordinate.x, this.coordinate.y, 0);
342
        return p;
343
    }
344

    
345
    @Override
346
    public Point force2DM() {
347
        Point2DM p = new Point2DM(this.getProjection(), this.coordinate.x, this.coordinate.y, ((MCoordinate)this.coordinate).m);
348
        return p;
349
    }
350

    
351
    @Override
352
    public Point force3DM() {
353
        Point3DM p = new Point3DM(this.getProjection(), this.coordinate.x, this.coordinate.y, 0, ((MCoordinate)this.coordinate).m);
354
        return p;
355
    }
356
}