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 @ 47346

History | View | Annotate | Download (9.62 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
    /*
94
     * (non-Javadoc)
95
     *
96
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
97
     */
98
    @Override
99
    public int getDimension() {
100
        return 3;
101
    }
102

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

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

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

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

    
158
    /*
159
     * (non-Javadoc)
160
     *
161
     * @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
162
     */
163
    @Override
164
    public boolean is3D() {
165
        return false;
166
    }
167

    
168
    /* (non-Javadoc)
169
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
170
     */
171
    @Override
172
    public void setJTSCoordinate(Coordinate coordinate) {
173
        this.coordinate = coordinate;
174
    }
175

    
176
    /*
177
     * (non-Javadoc)
178
     *
179
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
180
     */
181
    @Override
182
    public GeneralPathX getGeneralPath() {
183
        return new DefaultGeneralPathX(new PointIterator(null),false,0);
184
    }
185

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

    
199
    @Override
200
    public int hashCode() {
201
        int hash = 3;
202
        return hash;
203
    }
204

    
205
    public class PointIterator extends GeneralPathXIterator {
206
        /** Transform applied on the coordinates during iteration */
207
        private final AffineTransform at;
208

    
209
        /** True when the point has been read once */
210
        private boolean done;
211

    
212
        /**
213
         * Creates a new PointIterator object.
214
         *
215
         * @param at The affine transform applied to coordinates during iteration
216
         */
217
        public PointIterator(AffineTransform at) {
218
            super(new GeneralPathX());
219
            if (at == null) {
220
                at = new AffineTransform();
221
            }
222

    
223
            this.at = at;
224
            done = false;
225
        }
226

    
227
        /**
228
         * Return the winding rule for determining the interior of the path.
229
         *
230
         * @return <code>WIND_EVEN_ODD</code> by default.
231
         */
232
        @Override
233
        public int getWindingRule() {
234
            return PathIterator.WIND_EVEN_ODD;
235
        }
236

    
237
        /**
238
         * @see java.awt.geom.PathIterator#next()
239
         */
240
        @Override
241
        public void next() {
242
            done = true;
243
        }
244

    
245
        /**
246
         * @see java.awt.geom.PathIterator#isDone()
247
         */
248
        @Override
249
        public boolean isDone() {
250
            return done;
251
        }
252

    
253
        /**
254
         * @param coords
255
         * @return 
256
         * @see java.awt.geom.PathIterator#currentSegment(double[])
257
         */
258
        @Override
259
        public int currentSegment(double[] coords) {
260
            coords[0] = getX();
261
            coords[1] = getY();
262
            at.transform(coords, 0, coords, 0, 1);
263

    
264
            return PathIterator.SEG_MOVETO;
265
        }
266

    
267
        /* (non-Javadoc)
268
         * @see java.awt.geom.PathIterator#currentSegment(float[])
269
         */
270
        @Override
271
        public int currentSegment(float[] coords) {
272
            coords[0] = (float) getX();
273
            coords[1] = (float) getY();
274
            at.transform(coords, 0, coords, 0, 1);
275

    
276
            return PathIterator.SEG_MOVETO;
277
        }
278
    }
279

    
280
    /* (non-Javadoc)
281
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
282
     */
283
    @Override
284
    public MultiPoint toPoints() throws GeometryException {
285
        MultiPoint multiPoint = new MultiPoint2DM();
286
        multiPoint.addPoint(this);
287
        return multiPoint;
288
    }
289

    
290
    @Override
291
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
292
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance));
293
    }
294

    
295
    @Override
296
    public String toString() {
297
        StringBuilder builder = new StringBuilder();
298
        builder.append("POINT M (");
299
        for( int i=0; i<this.getDimension()-1; i++) {
300
            builder.append(this.getCoordinateAt(i));
301
            builder.append(" ");
302
        }
303
        builder.append(this.getCoordinateAt(this.getDimension()-1));
304
        builder.append(")");
305
        return builder.toString();
306
    }
307

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

    
334
    @Override
335
    public Point force3D() {
336
        Point3D p = new Point3D(this.getProjection(), this.coordinate.x, this.coordinate.y, 0);
337
        return p;
338
    }
339

    
340
    @Override
341
    public Point force2DM() {
342
        Point2DM p = new Point2DM(this.getProjection(), this.coordinate.x, this.coordinate.y, ((MCoordinate)this.coordinate).m);
343
        return p;
344
    }
345

    
346
    @Override
347
    public Point force3DM() {
348
        Point3DM p = new Point3DM(this.getProjection(), this.coordinate.x, this.coordinate.y, 0, ((MCoordinate)this.coordinate).m);
349
        return p;
350
    }
351
}