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 @ 44099
History | View | Annotate | Download (8.43 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.geom.AffineTransform; |
26 |
import java.awt.geom.PathIterator; |
27 |
|
28 |
import com.vividsolutions.jts.geom.Coordinate; |
29 |
import com.vividsolutions.jts.geom.CoordinateSequence; |
30 |
import org.cresques.cts.IProjection; |
31 |
|
32 |
import org.gvsig.fmap.geom.Geometry; |
33 |
import org.gvsig.fmap.geom.GeometryException; |
34 |
import org.gvsig.fmap.geom.GeometryLocator; |
35 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
36 |
import org.gvsig.fmap.geom.jts.MCoordinate; |
37 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2DM; |
38 |
import org.gvsig.fmap.geom.jts.gputils.DefaultGeneralPathX; |
39 |
import org.gvsig.fmap.geom.jts.gputils.GeneralPathXIterator; |
40 |
import org.gvsig.fmap.geom.jts.primitive.curve.line.Line2DM; |
41 |
import org.gvsig.fmap.geom.jts.util.JTSUtils; |
42 |
import org.gvsig.fmap.geom.operation.GeometryOperationException; |
43 |
import org.gvsig.fmap.geom.operation.GeometryOperationNotSupportedException; |
44 |
import org.gvsig.fmap.geom.primitive.GeneralPathX; |
45 |
import org.gvsig.fmap.geom.primitive.Point; |
46 |
import org.gvsig.fmap.geom.type.GeometryType; |
47 |
|
48 |
/**
|
49 |
* @author fdiaz
|
50 |
*
|
51 |
*/
|
52 |
public class Point2DM extends AbstractPoint { |
53 |
|
54 |
/**
|
55 |
*
|
56 |
*/
|
57 |
private static final long serialVersionUID = 7829655161985732518L; |
58 |
|
59 |
/**
|
60 |
*
|
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 |
*/
|
77 |
public Point2DM(double x, double y, double m) { |
78 |
this(null, JTSUtils.createMCoordinate(x, y, m)); |
79 |
} |
80 |
|
81 |
public double getM() { |
82 |
return ((org.hibernate.spatial.jts.mgeom.MCoordinate)this.coordinate).m; |
83 |
} |
84 |
|
85 |
/*
|
86 |
* (non-Javadoc)
|
87 |
*
|
88 |
* @see org.gvsig.fmap.geom.Geometry#getDimension()
|
89 |
*/
|
90 |
public int getDimension() { |
91 |
return 3; |
92 |
} |
93 |
|
94 |
/*
|
95 |
* (non-Javadoc)
|
96 |
*
|
97 |
* @see org.gvsig.fmap.geom.Geometry#getGeometryType()
|
98 |
*/
|
99 |
public GeometryType getGeometryType() {
|
100 |
try {
|
101 |
return GeometryLocator.getGeometryManager()
|
102 |
.getGeometryType(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2DM); |
103 |
} catch (Exception e) { |
104 |
return null; |
105 |
} |
106 |
} |
107 |
|
108 |
/*
|
109 |
* (non-Javadoc)
|
110 |
*
|
111 |
* @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
112 |
*/
|
113 |
public Point cloneGeometry() { |
114 |
return new Point2DM(this.getProjection(), MCoordinate.convertCoordinate((Coordinate) coordinate.clone())); |
115 |
} |
116 |
|
117 |
/*
|
118 |
* (non-Javadoc)
|
119 |
*
|
120 |
* @see org.gvsig.fmap.geom.primitive.Point#getCoordinateAt(int)
|
121 |
*/
|
122 |
public double getCoordinateAt(int dimension) { |
123 |
if (dimension == 2) { |
124 |
return this.getM(); |
125 |
} |
126 |
return super.getCoordinateAt(dimension); |
127 |
} |
128 |
|
129 |
/*
|
130 |
* (non-Javadoc)
|
131 |
*
|
132 |
* @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
|
133 |
*/
|
134 |
public void setCoordinateAt(int dimension, double value) { |
135 |
if (dimension == 2) { |
136 |
this.coordinate.setOrdinate(CoordinateSequence.M, value);
|
137 |
return;
|
138 |
} |
139 |
this.coordinate.setOrdinate(dimension, value);
|
140 |
} |
141 |
|
142 |
/*
|
143 |
* (non-Javadoc)
|
144 |
*
|
145 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
|
146 |
*/
|
147 |
public boolean is3D() { |
148 |
return false; |
149 |
} |
150 |
|
151 |
/* (non-Javadoc)
|
152 |
* @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
|
153 |
*/
|
154 |
public void setJTSCoordinate(Coordinate coordinate) { |
155 |
this.coordinate = coordinate;
|
156 |
} |
157 |
|
158 |
/*
|
159 |
* (non-Javadoc)
|
160 |
*
|
161 |
* @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
|
162 |
*/
|
163 |
public GeneralPathX getGeneralPath() {
|
164 |
return new DefaultGeneralPathX(new PointIterator(null),false,0); |
165 |
} |
166 |
|
167 |
/*
|
168 |
* (non-Javadoc)
|
169 |
*
|
170 |
* @see
|
171 |
* org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
|
172 |
* )
|
173 |
*/
|
174 |
public PathIterator getPathIterator(AffineTransform at) { |
175 |
PointIterator pi = new PointIterator(at);
|
176 |
return pi;
|
177 |
} |
178 |
|
179 |
public class PointIterator extends GeneralPathXIterator { |
180 |
/** Transform applied on the coordinates during iteration */
|
181 |
private AffineTransform at; |
182 |
|
183 |
/** True when the point has been read once */
|
184 |
private boolean done; |
185 |
|
186 |
/**
|
187 |
* Creates a new PointIterator object.
|
188 |
*
|
189 |
* @param p The polygon
|
190 |
* @param at The affine transform applied to coordinates during iteration
|
191 |
*/
|
192 |
public PointIterator(AffineTransform at) { |
193 |
super(new GeneralPathX()); |
194 |
if (at == null) { |
195 |
at = new AffineTransform(); |
196 |
} |
197 |
|
198 |
this.at = at;
|
199 |
done = false;
|
200 |
} |
201 |
|
202 |
/**
|
203 |
* Return the winding rule for determining the interior of the path.
|
204 |
*
|
205 |
* @return <code>WIND_EVEN_ODD</code> by default.
|
206 |
*/
|
207 |
public int getWindingRule() { |
208 |
return PathIterator.WIND_EVEN_ODD; |
209 |
} |
210 |
|
211 |
/**
|
212 |
* @see java.awt.geom.PathIterator#next()
|
213 |
*/
|
214 |
public void next() { |
215 |
done = true;
|
216 |
} |
217 |
|
218 |
/**
|
219 |
* @see java.awt.geom.PathIterator#isDone()
|
220 |
*/
|
221 |
public boolean isDone() { |
222 |
return done;
|
223 |
} |
224 |
|
225 |
/**
|
226 |
* @see java.awt.geom.PathIterator#currentSegment(double[])
|
227 |
*/
|
228 |
public int currentSegment(double[] coords) { |
229 |
coords[0] = getX();
|
230 |
coords[1] = getY();
|
231 |
at.transform(coords, 0, coords, 0, 1); |
232 |
|
233 |
return PathIterator.SEG_MOVETO; |
234 |
} |
235 |
|
236 |
/* (non-Javadoc)
|
237 |
* @see java.awt.geom.PathIterator#currentSegment(float[])
|
238 |
*/
|
239 |
public int currentSegment(float[] coords) { |
240 |
coords[0] = (float) getX(); |
241 |
coords[1] = (float) getY(); |
242 |
at.transform(coords, 0, coords, 0, 1); |
243 |
|
244 |
return PathIterator.SEG_MOVETO; |
245 |
} |
246 |
} |
247 |
|
248 |
/* (non-Javadoc)
|
249 |
* @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
250 |
*/
|
251 |
public MultiPoint toPoints() throws GeometryException { |
252 |
MultiPoint multiPoint = new MultiPoint2DM();
|
253 |
multiPoint.addPoint(this);
|
254 |
return multiPoint;
|
255 |
} |
256 |
|
257 |
/* (non-Javadoc)
|
258 |
* @see org.gvsig.fmap.geom.Geometry#offset(double)
|
259 |
*/
|
260 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
261 |
return JTSUtils.createGeometry(this.getProjection(), getJTS().buffer(distance)); |
262 |
} |
263 |
|
264 |
public String toString() { |
265 |
StringBuilder builder = new StringBuilder(); |
266 |
builder.append("POINT M (");
|
267 |
for( int i=0; i<this.getDimension()-1; i++) { |
268 |
builder.append(this.getCoordinateAt(i));
|
269 |
builder.append(" ");
|
270 |
} |
271 |
builder.append(this.getCoordinateAt(this.getDimension()-1)); |
272 |
builder.append(")");
|
273 |
return builder.toString();
|
274 |
} |
275 |
|
276 |
@Override
|
277 |
public boolean equals(Object obj) { |
278 |
Point other;
|
279 |
try {
|
280 |
other = (Point) obj;
|
281 |
} catch(ClassCastException e) { |
282 |
return false; |
283 |
} |
284 |
if( this.getGeometryType().getSubType() != other.getGeometryType().getSubType() ) { |
285 |
return false; |
286 |
} |
287 |
if( this.coordinate.x != other.getX() ) { |
288 |
return false; |
289 |
} |
290 |
if( this.coordinate.y != other.getY() ) { |
291 |
return false; |
292 |
} |
293 |
if( ((MCoordinate)this.coordinate).m != other.getCoordinateAt(2)) { |
294 |
return false; |
295 |
} |
296 |
return true; |
297 |
} |
298 |
|
299 |
} |