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

History | View | Annotate | Download (9.48 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.MCoordinate;
35
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2DM;
36
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX;
37
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator;
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.type.GeometryType;
44

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

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

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

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

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

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

    
87
    public double getM() {
88
        return ((org.hibernate.spatial.jts.mgeom.MCoordinate)this.coordinate).m;
89
    }
90

    
91
    /*
92
     * (non-Javadoc)
93
     *
94
     * @see org.gvsig.fmap.geom.Geometry#getDimension()
95
     */
96
    @Override
97
    public int getDimension() {
98
        return 3;
99
    }
100

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

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

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

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

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

    
163
    /* (non-Javadoc)
164
     * @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
165
     */
166
    @Override
167
    public void setJTSCoordinate(Coordinate coordinate) {
168
        this.coordinate = coordinate;
169
    }
170

    
171
    /*
172
     * (non-Javadoc)
173
     *
174
     * @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
175
     */
176
    @Override
177
    public GeneralPathX getGeneralPath() {
178
        return new DefaultGeneralPathX(new PointIterator(null),false,0);
179
    }
180

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

    
194
    @Override
195
    public int hashCode() {
196
        int hash = 3;
197
        return hash;
198
    }
199

    
200
    public class PointIterator extends GeneralPathXIterator {
201
        /** Transform applied on the coordinates during iteration */
202
        private final AffineTransform at;
203

    
204
        /** True when the point has been read once */
205
        private boolean done;
206

    
207
        /**
208
         * Creates a new PointIterator object.
209
         *
210
         * @param at The affine transform applied to coordinates during iteration
211
         */
212
        public PointIterator(AffineTransform at) {
213
            super(new GeneralPathX());
214
            if (at == null) {
215
                at = new AffineTransform();
216
            }
217

    
218
            this.at = at;
219
            done = false;
220
        }
221

    
222
        /**
223
         * Return the winding rule for determining the interior of the path.
224
         *
225
         * @return <code>WIND_EVEN_ODD</code> by default.
226
         */
227
        @Override
228
        public int getWindingRule() {
229
            return PathIterator.WIND_EVEN_ODD;
230
        }
231

    
232
        /**
233
         * @see java.awt.geom.PathIterator#next()
234
         */
235
        @Override
236
        public void next() {
237
            done = true;
238
        }
239

    
240
        /**
241
         * @see java.awt.geom.PathIterator#isDone()
242
         */
243
        @Override
244
        public boolean isDone() {
245
            return done;
246
        }
247

    
248
        /**
249
         * @param coords
250
         * @return 
251
         * @see java.awt.geom.PathIterator#currentSegment(double[])
252
         */
253
        @Override
254
        public int currentSegment(double[] coords) {
255
            coords[0] = getX();
256
            coords[1] = getY();
257
            at.transform(coords, 0, coords, 0, 1);
258

    
259
            return PathIterator.SEG_MOVETO;
260
        }
261

    
262
        /* (non-Javadoc)
263
         * @see java.awt.geom.PathIterator#currentSegment(float[])
264
         */
265
        @Override
266
        public int currentSegment(float[] coords) {
267
            coords[0] = (float) getX();
268
            coords[1] = (float) getY();
269
            at.transform(coords, 0, coords, 0, 1);
270

    
271
            return PathIterator.SEG_MOVETO;
272
        }
273
    }
274

    
275
    /* (non-Javadoc)
276
     * @see org.gvsig.fmap.geom.primitive.Line#toPoints()
277
     */
278
    @Override
279
    public MultiPoint toPoints() throws GeometryException {
280
        MultiPoint multiPoint = new MultiPoint2DM();
281
        multiPoint.addPoint(this);
282
        return multiPoint;
283
    }
284

    
285
    @Override
286
    public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException {
287
        return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance));
288
    }
289

    
290
    @Override
291
    public String toString() {
292
        StringBuilder builder = new StringBuilder();
293
        builder.append("POINT M (");
294
        for( int i=0; i<this.getDimension()-1; i++) {
295
            builder.append(this.getCoordinateAt(i));
296
            builder.append(" ");
297
        }
298
        builder.append(this.getCoordinateAt(this.getDimension()-1));
299
        builder.append(")");
300
        return builder.toString();
301
    }
302

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

    
329
    @Override
330
    public Point force3D() {
331
        Point3D p = new Point3D(this.getProjection(), this.coordinate.x, this.coordinate.y, 0);
332
        return p;
333
    }
334

    
335
    @Override
336
    public Point force2DM() {
337
        Point2DM p = new Point2DM(this.getProjection(), this.coordinate.x, this.coordinate.y, ((MCoordinate)this.coordinate).m);
338
        return p;
339
    }
340

    
341
    @Override
342
    public Point force3DM() {
343
        Point3DM p = new Point3DM(this.getProjection(), this.coordinate.x, this.coordinate.y, 0, ((MCoordinate)this.coordinate).m);
344
        return p;
345
    }
346
}