22 |
22 |
package org.gvsig.fmap.geom.primitive.impl;
|
23 |
23 |
|
24 |
24 |
import java.awt.Rectangle;
|
|
25 |
import java.awt.Shape;
|
25 |
26 |
import java.awt.geom.AffineTransform;
|
26 |
27 |
import java.awt.geom.PathIterator;
|
27 |
28 |
import java.awt.geom.Rectangle2D;
|
28 |
|
import java.util.ArrayList;
|
29 |
29 |
|
30 |
30 |
import org.cresques.cts.ICoordTrans;
|
31 |
31 |
import org.cresques.cts.IProjection;
|
... | ... | |
43 |
43 |
import org.gvsig.fmap.geom.type.GeometryType;
|
44 |
44 |
|
45 |
45 |
/**
|
46 |
|
* Punto 2D.
|
|
46 |
* 2D point implementation.
|
47 |
47 |
*
|
48 |
48 |
* @author Vicente Caballero Navarro
|
|
49 |
* @author gvSIG team
|
49 |
50 |
*/
|
50 |
51 |
public class Point2D extends AbstractPrimitive implements Point, DirectPosition {
|
|
52 |
|
|
53 |
private static final long serialVersionUID = 1836257305083881299L;
|
51 |
54 |
|
52 |
55 |
protected static final String COORDINATES_FIELD = "coordinates";
|
|
56 |
protected double x;
|
|
57 |
protected double y;
|
53 |
58 |
|
54 |
|
private static final long serialVersionUID = 1836257305083881299L;
|
55 |
|
protected double[] coordinates;
|
56 |
|
|
57 |
59 |
/**
|
58 |
60 |
* The constructor with the GeometryType like and argument
|
59 |
61 |
* is used by the {@link GeometryType}{@link #create()} to create the
|
... | ... | |
63 |
65 |
* The geometry type
|
64 |
66 |
*/
|
65 |
67 |
public Point2D(GeometryType geometryType) {
|
66 |
|
this(geometryType, null, null, 0.0, 0.0);
|
|
68 |
super(geometryType, null, null);
|
|
69 |
this.x = 0.0d;
|
|
70 |
this.y = 0.0d;
|
67 |
71 |
}
|
68 |
72 |
|
69 |
73 |
/**
|
... | ... | |
72 |
76 |
Point2D(GeometryType geometryType, String id, IProjection projection,
|
73 |
77 |
double x, double y) {
|
74 |
78 |
super(geometryType, id, projection);
|
75 |
|
coordinates = new double[getDimension()];
|
76 |
|
coordinates[0] = x;
|
77 |
|
coordinates[1] = y;
|
|
79 |
this.x = x;
|
|
80 |
this.y = y;
|
78 |
81 |
}
|
79 |
82 |
|
80 |
83 |
public Point2D(double x, double y) {
|
81 |
84 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
82 |
|
coordinates = new double[getDimension()];
|
83 |
|
coordinates[0] = x;
|
84 |
|
coordinates[1] = y;
|
|
85 |
this.x = x;
|
|
86 |
this.y = y;
|
85 |
87 |
}
|
86 |
88 |
|
|
89 |
public Point2D(GeometryType geometryType, double x, double y) {
|
|
90 |
super(geometryType, null, null);
|
|
91 |
this.x = x;
|
|
92 |
this.y = y;
|
|
93 |
}
|
|
94 |
|
87 |
95 |
public Point2D(int type, int subtype) {
|
88 |
96 |
super(type, subtype);
|
89 |
|
coordinates = new double[getDimension()];
|
|
97 |
this.x = 0.0d;
|
|
98 |
this.y = 0.0d;
|
90 |
99 |
}
|
91 |
100 |
|
92 |
101 |
public Point2D(java.awt.geom.Point2D point) {
|
93 |
102 |
super(TYPES.POINT, SUBTYPES.GEOM2D);
|
94 |
|
coordinates = new double[getDimension()];
|
95 |
|
coordinates[0] = point.getX();
|
96 |
|
coordinates[1] = point.getY();
|
|
103 |
this.x = point.getX();
|
|
104 |
this.y = point.getY();
|
97 |
105 |
}
|
98 |
106 |
|
99 |
107 |
/**
|
... | ... | |
104 |
112 |
* Matriz de transformaci?n.
|
105 |
113 |
*/
|
106 |
114 |
public void transform(AffineTransform at) {
|
|
115 |
double[] coordinates = getCoordinates();
|
107 |
116 |
at.transform(coordinates, 0, coordinates, 0, 1);
|
|
117 |
setCoordinates(coordinates);
|
108 |
118 |
}
|
109 |
119 |
|
110 |
|
/*
|
111 |
|
* (non-Javadoc)
|
112 |
|
*
|
113 |
|
* @see java.awt.Shape#contains(double, double)
|
114 |
|
*/
|
115 |
120 |
public boolean contains(double x, double y) {
|
116 |
|
if ((x == coordinates[0]) || (y == coordinates[1])) {
|
|
121 |
if ((this.x == x) || (this.y == y)) {
|
117 |
122 |
return true;
|
118 |
123 |
} else {
|
119 |
124 |
return false;
|
120 |
125 |
}
|
121 |
126 |
}
|
122 |
127 |
|
123 |
|
/*
|
124 |
|
* (non-Javadoc)
|
125 |
|
*
|
126 |
|
* @see java.awt.Shape#contains(double, double, double, double)
|
127 |
|
*/
|
128 |
128 |
public boolean contains(double x, double y, double w, double h) {
|
129 |
129 |
return false;
|
130 |
130 |
}
|
131 |
131 |
|
132 |
|
/*
|
133 |
|
* (non-Javadoc)
|
134 |
|
*
|
135 |
|
* @see java.awt.Shape#intersects(double, double, double, double)
|
136 |
|
*/
|
137 |
132 |
public boolean intersects(double x, double y, double w, double h) {
|
138 |
133 |
Rectangle2D.Double rAux = new Rectangle2D.Double(x, y, w, h);
|
139 |
134 |
|
140 |
|
return rAux.contains(coordinates[0], coordinates[1]);
|
|
135 |
return rAux.contains(this.x, this.y);
|
141 |
136 |
}
|
142 |
137 |
|
143 |
|
/*
|
144 |
|
* (non-Javadoc)
|
145 |
|
*
|
146 |
|
* @see java.awt.Shape#getBounds()
|
147 |
|
*/
|
148 |
138 |
public Rectangle getBounds() {
|
149 |
|
return new Rectangle((int) coordinates[0], (int) coordinates[1], 0, 0);
|
|
139 |
return new Rectangle((int) x, (int) y, 0, 0);
|
150 |
140 |
}
|
151 |
141 |
|
152 |
|
/**
|
153 |
|
* Devuelve la coordenada x del punto.
|
154 |
|
*
|
155 |
|
* @return Coordenada x.
|
156 |
|
*/
|
157 |
142 |
public double getX() {
|
158 |
|
return coordinates[0];
|
|
143 |
return x;
|
159 |
144 |
}
|
160 |
145 |
|
161 |
|
/**
|
162 |
|
* Devuelve la coordenada y del punto.
|
163 |
|
*
|
164 |
|
* @return Coordenada y.
|
165 |
|
*/
|
166 |
146 |
public double getY() {
|
167 |
|
return coordinates[1];
|
|
147 |
return y;
|
168 |
148 |
}
|
169 |
149 |
|
170 |
|
/*
|
171 |
|
* (non-Javadoc)
|
172 |
|
*
|
173 |
|
* @see java.awt.Shape#contains(java.awt.geom.Point2D)
|
174 |
|
*/
|
175 |
150 |
public boolean contains(java.awt.geom.Point2D p) {
|
176 |
151 |
return false;
|
177 |
152 |
}
|
178 |
153 |
|
179 |
|
/*
|
180 |
|
* (non-Javadoc)
|
181 |
|
*
|
182 |
|
* @see java.awt.Shape#getBounds2D()
|
183 |
|
*/
|
184 |
154 |
public Rectangle2D getBounds2D() {
|
185 |
|
return new Rectangle2D.Double(coordinates[0] - 0.01,
|
186 |
|
coordinates[1] - 0.01, 0.02, 0.02);
|
|
155 |
return new Rectangle2D.Double(x - 0.01, y - 0.01, 0.02, 0.02);
|
187 |
156 |
}
|
188 |
157 |
|
189 |
|
/*
|
190 |
|
* (non-Javadoc)
|
191 |
|
*
|
192 |
|
* @see java.awt.Shape#contains(java.awt.geom.Rectangle2D)
|
193 |
|
*/
|
194 |
158 |
public boolean contains(Rectangle2D r) {
|
195 |
159 |
return false;
|
196 |
160 |
}
|
197 |
161 |
|
198 |
|
/*
|
199 |
|
* (non-Javadoc)
|
200 |
|
*
|
201 |
|
* @see java.awt.Shape#intersects(java.awt.geom.Rectangle2D)
|
202 |
|
*/
|
203 |
162 |
public boolean intersects(Rectangle2D r) {
|
204 |
|
return r.contains(coordinates[0], coordinates[1]);
|
|
163 |
return r.contains(x, y);
|
205 |
164 |
}
|
206 |
165 |
|
207 |
|
/*
|
208 |
|
* (non-Javadoc)
|
209 |
|
*
|
210 |
|
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform)
|
211 |
|
*/
|
|
166 |
public Shape getShape(AffineTransform affineTransform) {
|
|
167 |
return this;
|
|
168 |
}
|
|
169 |
|
212 |
170 |
public PathIterator getPathIterator(AffineTransform at) {
|
213 |
171 |
return new PointIterator(getPoint2D(), at);
|
214 |
172 |
}
|
215 |
173 |
|
216 |
|
/*
|
217 |
|
* (non-Javadoc)
|
218 |
|
*
|
219 |
|
* @see java.awt.Shape#getPathIterator(java.awt.geom.AffineTransform,
|
220 |
|
* double)
|
221 |
|
*/
|
222 |
174 |
public PathIterator getPathIterator(AffineTransform at, double flatness) {
|
223 |
175 |
return new PointIterator(getPoint2D(), at);
|
224 |
176 |
}
|
225 |
177 |
|
226 |
178 |
private java.awt.geom.Point2D getPoint2D() {
|
227 |
|
return new java.awt.geom.Point2D.Double(coordinates[0], coordinates[1]);
|
|
179 |
return new java.awt.geom.Point2D.Double(x, y);
|
228 |
180 |
}
|
229 |
181 |
|
230 |
|
/**
|
231 |
|
* @see org.gvsig.fmap.geom.primitive.FShape#getShapeType()
|
232 |
|
*/
|
233 |
182 |
public int getShapeType() {
|
234 |
183 |
return TYPES.POINT;
|
235 |
184 |
}
|
236 |
185 |
|
237 |
|
/*
|
238 |
|
* (non-Javadoc)
|
239 |
|
*
|
240 |
|
* @see com.iver.cit.gvsig.fmap.core.FShape#cloneFShape()
|
241 |
|
*/
|
242 |
186 |
public FShape cloneFShape() {
|
243 |
|
return new Point2D(getGeometryType(), id, projection, coordinates[0],
|
244 |
|
coordinates[1]);
|
|
187 |
return new Point2D(getGeometryType(), id, projection, x, y);
|
245 |
188 |
}
|
246 |
189 |
|
247 |
|
/*
|
248 |
|
* (non-Javadoc)
|
249 |
|
*
|
250 |
|
* @see
|
251 |
|
* com.iver.cit.gvsig.fmap.core.FShape#reProject(org.cresques.cts.ICoordTrans
|
252 |
|
* )
|
253 |
|
*/
|
254 |
190 |
public void reProject(ICoordTrans ct) {
|
255 |
191 |
java.awt.geom.Point2D p = getPoint2D();
|
256 |
192 |
p = ct.convert(p, p);
|
257 |
|
coordinates[0] = p.getX();
|
258 |
|
coordinates[1] = p.getY();
|
|
193 |
this.x = p.getX();
|
|
194 |
this.y = p.getY();
|
259 |
195 |
}
|
260 |
196 |
|
261 |
|
/*
|
262 |
|
* (non-Javadoc)
|
263 |
|
*
|
264 |
|
* @see com.iver.cit.gvsig.fmap.core.FShape#getStretchingHandlers()
|
265 |
|
*/
|
266 |
197 |
public Handler[] getStretchingHandlers() {
|
267 |
|
ArrayList handlers = new ArrayList();
|
268 |
|
handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
|
269 |
|
return (Handler[]) handlers.toArray(new Handler[0]);
|
|
198 |
return new Handler[] { new PointHandler(0, x, y, this) };
|
270 |
199 |
}
|
271 |
200 |
|
272 |
|
/*
|
273 |
|
* (non-Javadoc)
|
274 |
|
*
|
275 |
|
* @see com.iver.cit.gvsig.fmap.core.FShape#getSelectHandlers()
|
276 |
|
*/
|
277 |
201 |
public Handler[] getSelectHandlers() {
|
278 |
|
ArrayList handlers = new ArrayList();
|
279 |
|
handlers.add(new PointHandler(0, coordinates[0], coordinates[1]));
|
280 |
|
return (Handler[]) handlers.toArray(new Handler[0]);
|
|
202 |
return new Handler[] { new PointHandler(0, x, y, this) };
|
281 |
203 |
}
|
282 |
204 |
|
283 |
205 |
/**
|
284 |
|
* DOCUMENT ME!
|
285 |
206 |
*
|
286 |
207 |
* @author Vicente Caballero Navarro
|
|
208 |
* @author gvSIG team
|
287 |
209 |
*/
|
288 |
210 |
class PointHandler extends AbstractHandler implements FinalHandler {
|
289 |
211 |
|
290 |
|
/**
|
291 |
|
* Crea un nuevo PointHandler.
|
292 |
|
*
|
293 |
|
* @param x
|
294 |
|
* DOCUMENT ME!
|
295 |
|
* @param y
|
296 |
|
* DOCUMENT ME!
|
297 |
|
*/
|
298 |
|
public PointHandler(int i, double x, double y) {
|
|
212 |
private final Point gvSIGPoint;
|
|
213 |
|
|
214 |
public PointHandler(int i, double x, double y, Point gvSIGPoint) {
|
|
215 |
this.gvSIGPoint = gvSIGPoint;
|
299 |
216 |
point = new java.awt.geom.Point2D.Double(x, y);
|
300 |
217 |
index = i;
|
301 |
218 |
}
|
302 |
219 |
|
303 |
|
/**
|
304 |
|
* DOCUMENT ME!
|
305 |
|
*
|
306 |
|
* @param x
|
307 |
|
* DOCUMENT ME!
|
308 |
|
* @param y
|
309 |
|
* DOCUMENT ME!
|
310 |
|
*
|
311 |
|
* @return DOCUMENT ME!
|
312 |
|
*/
|
313 |
|
public void move(double x, double y) {
|
314 |
|
coordinates[0] = coordinates[0] + x;
|
315 |
|
coordinates[1] = coordinates[1] + y;
|
|
220 |
public void move(double movex, double movey) {
|
|
221 |
gvSIGPoint.setX(gvSIGPoint.getX() + movex);
|
|
222 |
gvSIGPoint.setY(gvSIGPoint.getY() + movey);
|
316 |
223 |
}
|
317 |
224 |
|
318 |
|
/**
|
319 |
|
* @see org.gvsig.fmap.geom.handler.Handler#set(double, double)
|
320 |
|
*/
|
321 |
|
public void set(double x, double y) {
|
322 |
|
coordinates[0] = x;
|
323 |
|
coordinates[1] = y;
|
|
225 |
public void set(double setx, double sety) {
|
|
226 |
gvSIGPoint.setX(setx);
|
|
227 |
gvSIGPoint.setY(sety);
|
324 |
228 |
}
|
325 |
229 |
}
|
326 |
230 |
|
327 |
|
/*
|
328 |
|
* (non-Javadoc)
|
329 |
|
*
|
330 |
|
* @see org.gvsig.geometries.iso.GM_Object#coordinateDimension()
|
331 |
|
*/
|
332 |
231 |
public int getDimension() {
|
333 |
232 |
return 2;
|
334 |
233 |
}
|
335 |
234 |
|
336 |
235 |
public Envelope getEnvelope() {
|
337 |
|
return new Envelope2D(coordinates[0] - 0.01, coordinates[1] - 0.01,
|
338 |
|
coordinates[0] + 0.02, coordinates[1] + 0.02);
|
|
236 |
return new Envelope2D(x - 0.01, y - 0.01, x + 0.02, y + 0.02);
|
339 |
237 |
}
|
340 |
238 |
|
341 |
239 |
public GeneralPathX getGeneralPath() {
|
342 |
|
// TODO Auto-generated method stub
|
343 |
240 |
return null;
|
344 |
241 |
}
|
345 |
242 |
|
... | ... | |
348 |
245 |
}
|
349 |
246 |
|
350 |
247 |
public double getOrdinate(int dim) {
|
351 |
|
if (dim == 0) {
|
|
248 |
if (dim == Geometry.DIMENSIONS.X) {
|
352 |
249 |
return getX();
|
353 |
250 |
} else
|
354 |
|
if (dim == 1) {
|
|
251 |
if (dim == Geometry.DIMENSIONS.Y) {
|
355 |
252 |
return getY();
|
356 |
253 |
} else {
|
357 |
254 |
return 0;
|
... | ... | |
373 |
270 |
|
374 |
271 |
}
|
375 |
272 |
|
376 |
|
/*
|
377 |
|
* (non-Javadoc)
|
378 |
|
*
|
379 |
|
* @see org.gvsig.fmap.geom.primitive.Point#getCoordinates()
|
380 |
|
*/
|
381 |
273 |
public double[] getCoordinates() {
|
382 |
|
return coordinates;
|
|
274 |
return new double[] { x, y };
|
383 |
275 |
}
|
384 |
276 |
|
385 |
|
/*
|
386 |
|
* (non-Javadoc)
|
387 |
|
*
|
388 |
|
* @see org.gvsig.fmap.geom.primitive.Point#getDoordinateAt(int)
|
389 |
|
*/
|
390 |
277 |
public double getCoordinateAt(int dimension) {
|
391 |
|
return coordinates[dimension];
|
|
278 |
switch (dimension) {
|
|
279 |
case Geometry.DIMENSIONS.X:
|
|
280 |
return x;
|
|
281 |
case Geometry.DIMENSIONS.Y:
|
|
282 |
return y;
|
|
283 |
default:
|
|
284 |
throw new ArrayIndexOutOfBoundsException(dimension);
|
|
285 |
}
|
392 |
286 |
}
|
393 |
287 |
|
394 |
|
/*
|
395 |
|
* (non-Javadoc)
|
396 |
|
*
|
397 |
|
* @see org.gvsig.fmap.geom.primitive.Point#setCoordinateAt(int, double)
|
398 |
|
*/
|
399 |
288 |
public void setCoordinateAt(int dimension, double value) {
|
400 |
|
coordinates[dimension] = value;
|
|
289 |
switch (dimension) {
|
|
290 |
case Geometry.DIMENSIONS.X:
|
|
291 |
this.x = value;
|
|
292 |
break;
|
|
293 |
case Geometry.DIMENSIONS.Y:
|
|
294 |
this.y = value;
|
|
295 |
break;
|
|
296 |
default:
|
|
297 |
throw new ArrayIndexOutOfBoundsException(dimension);
|
|
298 |
}
|
401 |
299 |
}
|
402 |
300 |
|
403 |
|
/*
|
404 |
|
* (non-Javadoc)
|
405 |
|
*
|
406 |
|
* @see org.gvsig.fmap.geom.primitive.Point#setCoordinates(double[])
|
407 |
|
*/
|
408 |
301 |
public void setCoordinates(double[] values) {
|
409 |
|
coordinates = values;
|
|
302 |
this.x = values[Geometry.DIMENSIONS.X];
|
|
303 |
this.y = values[Geometry.DIMENSIONS.Y];
|
410 |
304 |
}
|
411 |
305 |
|
412 |
|
/*
|
413 |
|
* (non-Javadoc)
|
414 |
|
*
|
415 |
|
* @see org.gvsig.fmap.geom.primitive.Point#setX(double)
|
416 |
|
*/
|
417 |
306 |
public void setX(double x) {
|
418 |
|
coordinates[0] = x;
|
|
307 |
this.x = x;
|
419 |
308 |
}
|
420 |
309 |
|
421 |
|
/*
|
422 |
|
* (non-Javadoc)
|
423 |
|
*
|
424 |
|
* @see org.gvsig.fmap.geom.primitive.Point#setY(double)
|
425 |
|
*/
|
426 |
310 |
public void setY(double y) {
|
427 |
|
coordinates[1] = y;
|
|
311 |
this.y = y;
|
428 |
312 |
}
|
429 |
313 |
|
430 |
314 |
public String toString() {
|
431 |
315 |
StringBuffer buffer = new StringBuffer();
|
432 |
|
buffer.append("Point2D(");
|
433 |
|
toStringCoordinates(buffer);
|
|
316 |
double[] coordinates = getCoordinates();
|
|
317 |
buffer.append(getFullTypeName()).append("(").append(coordinates[0]);
|
|
318 |
for (int i = 1; i < coordinates.length; i++) {
|
|
319 |
buffer.append(",").append(coordinates[i]);
|
|
320 |
}
|
434 |
321 |
buffer.append(")");
|
435 |
322 |
return buffer.toString();
|
436 |
323 |
}
|
437 |
324 |
|
438 |
|
protected void toStringCoordinates(StringBuffer buffer) {
|
439 |
|
buffer.append(coordinates[0]);
|
440 |
|
for (int i = 1; i < coordinates.length; i++) {
|
441 |
|
buffer.append(",").append(coordinates[i]);
|
442 |
|
}
|
|
325 |
protected String getFullTypeName() {
|
|
326 |
return "Point2D";
|
443 |
327 |
}
|
|
328 |
|
|
329 |
public int getType() {
|
|
330 |
return TYPES.POINT;
|
|
331 |
}
|
444 |
332 |
}
|