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 / Point2D.java @ 44099
History | View | Annotate | Download (7.38 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 org.cresques.cts.IProjection; |
30 |
|
31 |
import org.gvsig.fmap.geom.Geometry; |
32 |
import org.gvsig.fmap.geom.GeometryException; |
33 |
import org.gvsig.fmap.geom.GeometryLocator; |
34 |
import org.gvsig.fmap.geom.aggregate.MultiPoint; |
35 |
import org.gvsig.fmap.geom.jts.aggregate.MultiPoint2D; |
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.primitive.curve.circumference.Circumference2D; |
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 Point2D extends AbstractPoint { |
50 |
|
51 |
/**
|
52 |
*
|
53 |
*/
|
54 |
private static final long serialVersionUID = -5486980182751169991L; |
55 |
|
56 |
/**
|
57 |
*
|
58 |
*/
|
59 |
public Point2D(IProjection proj, Coordinate coordinates) { |
60 |
super(Geometry.SUBTYPES.GEOM2D, coordinates);
|
61 |
this.setProjection(proj);
|
62 |
} |
63 |
|
64 |
/**
|
65 |
*
|
66 |
*/
|
67 |
public Point2D() { |
68 |
this(null, new Coordinate(0, 0)); |
69 |
} |
70 |
|
71 |
/**
|
72 |
*
|
73 |
*/
|
74 |
public Point2D(double x, double y) { |
75 |
this(null, new Coordinate(x, y)); |
76 |
} |
77 |
|
78 |
/*
|
79 |
* (non-Javadoc)
|
80 |
*
|
81 |
* @see org.gvsig.fmap.geom.Geometry#cloneGeometry()
|
82 |
*/
|
83 |
public Point cloneGeometry() { |
84 |
return new Point2D(this.getProjection(), (Coordinate) this.coordinate.clone()); |
85 |
} |
86 |
|
87 |
/*
|
88 |
* (non-Javadoc)
|
89 |
*
|
90 |
* @see org.gvsig.fmap.geom.Geometry#getDimension()
|
91 |
*/
|
92 |
public int getDimension() { |
93 |
return 2; |
94 |
} |
95 |
|
96 |
/*
|
97 |
* (non-Javadoc)
|
98 |
*
|
99 |
* @see org.gvsig.fmap.geom.Geometry#getGeometryType()
|
100 |
*/
|
101 |
public GeometryType getGeometryType() {
|
102 |
try {
|
103 |
return GeometryLocator.getGeometryManager().getGeometryType(Geometry.TYPES.POINT, Geometry.SUBTYPES.GEOM2D);
|
104 |
} catch (Exception e) { |
105 |
return null; |
106 |
} |
107 |
} |
108 |
|
109 |
/*
|
110 |
* (non-Javadoc)
|
111 |
*
|
112 |
* @see org.gvsig.fmap.geom.jts.GeometryJTS#is3D()
|
113 |
*/
|
114 |
public boolean is3D() { |
115 |
return false; |
116 |
} |
117 |
|
118 |
/* (non-Javadoc)
|
119 |
* @see org.gvsig.fmap.geom.jts.primitive.point.PointJTS#setJTSCoordinate(com.vividsolutions.jts.geom.Coordinate)
|
120 |
*/
|
121 |
public void setJTSCoordinate(Coordinate coordinate) { |
122 |
this.coordinate = coordinate;
|
123 |
} |
124 |
|
125 |
/*
|
126 |
* (non-Javadoc)
|
127 |
*
|
128 |
* @see org.gvsig.fmap.geom.Geometry#getGeneralPath()
|
129 |
*/
|
130 |
public GeneralPathX getGeneralPath() {
|
131 |
return new DefaultGeneralPathX(new PointIterator(null),false,0); |
132 |
} |
133 |
|
134 |
/*
|
135 |
* (non-Javadoc)
|
136 |
*
|
137 |
* @see
|
138 |
* org.gvsig.fmap.geom.Geometry#getPathIterator(java.awt.geom.AffineTransform
|
139 |
* )
|
140 |
*/
|
141 |
public PathIterator getPathIterator(AffineTransform at) { |
142 |
PointIterator pi = new PointIterator(at);
|
143 |
return pi;
|
144 |
} |
145 |
|
146 |
|
147 |
public class PointIterator extends GeneralPathXIterator { |
148 |
/** Transform applied on the coordinates during iteration */
|
149 |
private AffineTransform at; |
150 |
|
151 |
/** True when the point has been read once */
|
152 |
private boolean done; |
153 |
|
154 |
/**
|
155 |
* Creates a new PointIterator object.
|
156 |
*
|
157 |
* @param p The polygon
|
158 |
* @param at The affine transform applied to coordinates during iteration
|
159 |
*/
|
160 |
public PointIterator(AffineTransform at) { |
161 |
super(new GeneralPathX()); |
162 |
if (at == null) { |
163 |
at = new AffineTransform(); |
164 |
} |
165 |
|
166 |
this.at = at;
|
167 |
done = false;
|
168 |
} |
169 |
|
170 |
/**
|
171 |
* Return the winding rule for determining the interior of the path.
|
172 |
*
|
173 |
* @return <code>WIND_EVEN_ODD</code> by default.
|
174 |
*/
|
175 |
public int getWindingRule() { |
176 |
return PathIterator.WIND_EVEN_ODD; |
177 |
} |
178 |
|
179 |
/**
|
180 |
* @see java.awt.geom.PathIterator#next()
|
181 |
*/
|
182 |
public void next() { |
183 |
done = true;
|
184 |
} |
185 |
|
186 |
/**
|
187 |
* @see java.awt.geom.PathIterator#isDone()
|
188 |
*/
|
189 |
public boolean isDone() { |
190 |
return done;
|
191 |
} |
192 |
|
193 |
/**
|
194 |
* @see java.awt.geom.PathIterator#currentSegment(double[])
|
195 |
*/
|
196 |
public int currentSegment(double[] coords) { |
197 |
coords[0] = getX();
|
198 |
coords[1] = getY();
|
199 |
at.transform(coords, 0, coords, 0, 1); |
200 |
|
201 |
return PathIterator.SEG_MOVETO; |
202 |
} |
203 |
|
204 |
/* (non-Javadoc)
|
205 |
* @see java.awt.geom.PathIterator#currentSegment(float[])
|
206 |
*/
|
207 |
public int currentSegment(float[] coords) { |
208 |
coords[0] = (float) getX(); |
209 |
coords[1] = (float) getY(); |
210 |
at.transform(coords, 0, coords, 0, 1); |
211 |
|
212 |
return PathIterator.SEG_MOVETO; |
213 |
} |
214 |
} |
215 |
|
216 |
/* (non-Javadoc)
|
217 |
* @see org.gvsig.fmap.geom.primitive.Line#toPoints()
|
218 |
*/
|
219 |
public MultiPoint toPoints() throws GeometryException { |
220 |
MultiPoint multiPoint = new MultiPoint2D();
|
221 |
multiPoint.addPoint(this);
|
222 |
return multiPoint;
|
223 |
} |
224 |
|
225 |
/* (non-Javadoc)
|
226 |
* @see org.gvsig.fmap.geom.Geometry#offset(double)
|
227 |
*/
|
228 |
public Geometry offset(double distance) throws GeometryOperationNotSupportedException, GeometryOperationException { |
229 |
return new Circumference2D((Point)this.cloneGeometry(), distance); |
230 |
} |
231 |
|
232 |
public String toString() { |
233 |
StringBuilder builder = new StringBuilder(); |
234 |
builder.append("POINT (");
|
235 |
for( int i=0; i<this.getDimension()-1; i++) { |
236 |
builder.append(this.getCoordinateAt(i));
|
237 |
builder.append(" ");
|
238 |
} |
239 |
builder.append(this.getCoordinateAt(this.getDimension()-1)); |
240 |
builder.append(")");
|
241 |
return builder.toString();
|
242 |
} |
243 |
|
244 |
@Override
|
245 |
public boolean equals(Object obj) { |
246 |
if( obj == null ) { |
247 |
return false; |
248 |
} |
249 |
Point other;
|
250 |
try {
|
251 |
other = (Point) obj;
|
252 |
} catch(ClassCastException e) { |
253 |
return false; |
254 |
} |
255 |
if( this.getGeometryType().getSubType() != other.getGeometryType().getSubType() ) { |
256 |
return false; |
257 |
} |
258 |
if( this.coordinate.x != other.getX() ) { |
259 |
return false; |
260 |
} |
261 |
if( this.coordinate.y != other.getY() ) { |
262 |
return false; |
263 |
} |
264 |
return true; |
265 |
} |
266 |
|
267 |
|
268 |
} |