root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FMultiPoint2D.java @ 5878
History | View | Annotate | Download (10.1 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* IVER T.I. S.A
|
34 |
* Salamanca 50
|
35 |
* 46005 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963163400
|
39 |
* dac@iver.es
|
40 |
*/
|
41 |
package com.iver.cit.gvsig.fmap.core; |
42 |
|
43 |
import java.awt.Color; |
44 |
import java.awt.Graphics2D; |
45 |
import java.awt.Rectangle; |
46 |
import java.awt.Shape; |
47 |
import java.awt.geom.AffineTransform; |
48 |
import java.awt.geom.PathIterator; |
49 |
import java.awt.geom.Point2D; |
50 |
import java.awt.geom.Rectangle2D; |
51 |
import java.io.IOException; |
52 |
import java.util.ArrayList; |
53 |
|
54 |
import org.cresques.cts.ICoordTrans; |
55 |
import org.geotools.data.postgis.attributeio.WKBEncoder; |
56 |
|
57 |
import com.iver.cit.gvsig.fmap.ViewPort; |
58 |
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities; |
59 |
import com.iver.cit.gvsig.fmap.core.v02.FLabel; |
60 |
import com.iver.cit.gvsig.fmap.core.v02.FSymbol; |
61 |
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D; |
62 |
import com.vividsolutions.jts.geom.Coordinate; |
63 |
import com.vividsolutions.jts.geom.Geometry; |
64 |
import com.vividsolutions.jts.geom.GeometryFactory; |
65 |
import com.vividsolutions.jts.geom.MultiPoint; |
66 |
|
67 |
|
68 |
/**
|
69 |
* Multipunto 2D.
|
70 |
*
|
71 |
* @author Vicente Caballero Navarro
|
72 |
*/
|
73 |
public class FMultiPoint2D implements IGeometry { |
74 |
FPoint2D[] points = null; |
75 |
private int type=FShape.POINT; |
76 |
|
77 |
/**
|
78 |
* Crea un nuevo MultiPoint2D.
|
79 |
*
|
80 |
* @param x DOCUMENT ME!
|
81 |
* @param y DOCUMENT ME!
|
82 |
*/
|
83 |
public FMultiPoint2D(double[] x, double[] y) { |
84 |
points = new FPoint2D[x.length];
|
85 |
for (int i=0;i<x.length;i++){ |
86 |
points[i] = new FPoint2D(x[i], y[i]);
|
87 |
} |
88 |
|
89 |
} |
90 |
public FMultiPoint2D(FPoint2D[] points) { |
91 |
this.points = points;
|
92 |
} |
93 |
/**
|
94 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
|
95 |
* ViewPort, FStyle2D)
|
96 |
*/
|
97 |
public void draw(Graphics2D g, ViewPort vp, FStyle2D symbol) { |
98 |
int size = 2; |
99 |
int hw = 4; |
100 |
|
101 |
for (int i = 0; i < points.length; i++) { |
102 |
Point2D.Double p = new Point2D.Double(points[i].getX(), points[i].getY()); |
103 |
vp.getAffineTransform().transform(p, p); |
104 |
g.setColor(Color.red);
|
105 |
g.fillOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw); |
106 |
g.setColor(Color.black);
|
107 |
g.drawOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw); |
108 |
} |
109 |
} |
110 |
|
111 |
/**
|
112 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
|
113 |
*/
|
114 |
public Geometry toJTSGeometry() {
|
115 |
Coordinate[] theGeoms = new Coordinate[points.length]; |
116 |
for (int i = 0; i < theGeoms.length; i++) |
117 |
{ |
118 |
Coordinate c = new Coordinate(points[i].getX(), points[i].getY());
|
119 |
theGeoms[i] = c; |
120 |
} |
121 |
MultiPoint geomCol = new GeometryFactory().createMultiPoint(theGeoms);
|
122 |
|
123 |
|
124 |
return geomCol;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
|
129 |
*/
|
130 |
public FLabel[] createLabels(int position, boolean duplicates) { |
131 |
FLabel[] aux = new FLabel[getNumPoints()]; |
132 |
for (int i=0; i < getNumPoints(); i++) |
133 |
{ |
134 |
aux[i] = FLabel.createFLabel(points[i]); |
135 |
} |
136 |
|
137 |
return aux;
|
138 |
} |
139 |
|
140 |
/* (non-Javadoc)
|
141 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
|
142 |
*/
|
143 |
public boolean intersects(Rectangle2D r) { |
144 |
for (int i=0;i<getNumPoints();i++){ |
145 |
if (r.contains(points[i].getX(),points[i].getY()))return true; |
146 |
} |
147 |
return false; |
148 |
} |
149 |
|
150 |
/* (non-Javadoc)
|
151 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
|
152 |
*/
|
153 |
public Rectangle2D getBounds2D() { |
154 |
Rectangle2D r=null; |
155 |
if (getNumPoints()>0){ |
156 |
r=new Rectangle2D.Double(points[0].getX(),points[0].getY(),0.001,0.001); |
157 |
} |
158 |
for (int i=1;i<getNumPoints();i++){ |
159 |
r.add(points[i].getX(),points[i].getY()); |
160 |
} |
161 |
return r;
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
|
166 |
*/
|
167 |
public int getGeometryType() { |
168 |
return type;
|
169 |
} |
170 |
|
171 |
/* (non-Javadoc)
|
172 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.core.v02.FSymbol)
|
173 |
*/
|
174 |
public void draw(Graphics2D g, ViewPort vp, FSymbol symbol) { |
175 |
//int size = 2;
|
176 |
//int hw = 4;
|
177 |
|
178 |
for (int i = 0; i < getNumPoints(); i++) { |
179 |
Point2D.Double p = new Point2D.Double(points[i].getX(), |
180 |
points[i].getY()); |
181 |
vp.getAffineTransform().transform(p, p); |
182 |
FGraphicUtilities.DrawShape(g, vp.getAffineTransform(), new FPoint2D(p.getX(),p.getY()), symbol);
|
183 |
|
184 |
/* java.awt.geom.Point2D.Double p = new java.awt.geom.Point2D.Double(x[i],
|
185 |
y[i]);
|
186 |
vp.getAffineTransform().transform(p, p);
|
187 |
g.setColor(Color.red);
|
188 |
g.fillOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw);
|
189 |
g.setColor(Color.black);
|
190 |
g.drawOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw);
|
191 |
*/
|
192 |
} |
193 |
} |
194 |
|
195 |
/* (non-Javadoc)
|
196 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
|
197 |
*/
|
198 |
public IGeometry cloneGeometry() {
|
199 |
FPoint2D[] aux = new FPoint2D[getNumPoints()]; |
200 |
for (int i=0; i < getNumPoints(); i++) |
201 |
{ |
202 |
aux[i] = (FPoint2D) points[i].cloneFShape(); |
203 |
} |
204 |
IGeometry geom=new FMultiPoint2D(aux);
|
205 |
//geom.setGeometryType(type);
|
206 |
return geom;
|
207 |
} |
208 |
|
209 |
/* (non-Javadoc)
|
210 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
|
211 |
*/
|
212 |
public void reProject(ICoordTrans ct) { |
213 |
for (int i=0; i < getNumPoints(); i++) |
214 |
{ |
215 |
points[i].reProject(ct); |
216 |
} |
217 |
} |
218 |
public int getNumPoints(){ |
219 |
return points.length;
|
220 |
} |
221 |
public FPoint2D getPoint(int i){ |
222 |
return points[i];
|
223 |
} |
224 |
/**
|
225 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
|
226 |
*/
|
227 |
public PathIterator getPathIterator(AffineTransform at) { |
228 |
GeneralPathX gpx=new GeneralPathX();
|
229 |
if (getNumPoints()>0){ |
230 |
gpx.moveTo(points[0].getX(), points[0].getY()); |
231 |
} |
232 |
for (int i=1;i<getNumPoints();i++){ |
233 |
gpx.lineTo(points[i].getX(), points[i].getY()); |
234 |
} |
235 |
return (GeneralPathXIterator)gpx.getPathIterator(null); |
236 |
} |
237 |
/* (non-Javadoc)
|
238 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
|
239 |
*/
|
240 |
public boolean fastIntersects(double x, double y, double w, double h) { |
241 |
for (int i=0; i < getNumPoints(); i++) |
242 |
{ |
243 |
if (points[i].intersects(x,y,w,h))
|
244 |
return true; |
245 |
} |
246 |
return false; |
247 |
} |
248 |
/* (non-Javadoc)
|
249 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#drawInts(java.awt.Graphics2D, com.iver.cit.gvsig.fmap.ViewPort, com.iver.cit.gvsig.fmap.core.v02.FSymbol)
|
250 |
*/
|
251 |
public void drawInts(Graphics2D g, ViewPort vp, FSymbol symbol) { |
252 |
draw(g,vp, symbol); |
253 |
|
254 |
} |
255 |
/* (non-Javadoc)
|
256 |
* @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
|
257 |
*/
|
258 |
public Handler[] getHandlers(int type) { |
259 |
ArrayList handlers=new ArrayList(); |
260 |
for (int i = 0; i < getNumPoints(); i++){ |
261 |
handlers.add(new PointHandler(i,points[i]));
|
262 |
} |
263 |
return (Handler[])handlers.toArray(new Handler[0]); |
264 |
} |
265 |
/**
|
266 |
* DOCUMENT ME!
|
267 |
*
|
268 |
* @author Vicente Caballero Navarro
|
269 |
*/
|
270 |
class PointHandler extends AbstractHandler { |
271 |
/**
|
272 |
* Crea un nuevo PointHandler.
|
273 |
*
|
274 |
* @param x DOCUMENT ME!
|
275 |
* @param y DOCUMENT ME!
|
276 |
*/
|
277 |
public PointHandler(int i,FPoint2D p) { |
278 |
point = new Point2D.Double(p.getX(), p.getY()); |
279 |
index=i; |
280 |
} |
281 |
|
282 |
/**
|
283 |
* DOCUMENT ME!
|
284 |
*
|
285 |
* @param x DOCUMENT ME!
|
286 |
* @param y DOCUMENT ME!
|
287 |
*
|
288 |
* @return DOCUMENT ME!
|
289 |
*/
|
290 |
public void move(double x, double y) { |
291 |
point.setLocation(points[index].getX()+x, |
292 |
points[index].getY()+y); |
293 |
} |
294 |
|
295 |
/**
|
296 |
* @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
|
297 |
*/
|
298 |
public void set(double x, double y) { |
299 |
point.setLocation(x, y); |
300 |
} |
301 |
|
302 |
} |
303 |
public void transform(AffineTransform at) { |
304 |
for (int i=0; i < getNumPoints(); i++) |
305 |
{ |
306 |
points[i].transform(at); |
307 |
} |
308 |
|
309 |
} |
310 |
public byte[] toWKB() throws IOException { |
311 |
return WKBEncoder.encodeGeometry(toJTSGeometry());
|
312 |
} |
313 |
public PathIterator getPathIterator(AffineTransform at, double flatness) { |
314 |
GeneralPathX gpx=new GeneralPathX();
|
315 |
if (getNumPoints()>0){ |
316 |
gpx.moveTo(points[0].getX(), points[0].getY()); |
317 |
} |
318 |
for (int i=1;i<getNumPoints();i++){ |
319 |
gpx.lineTo(points[i].getX(), points[i].getY()); |
320 |
} |
321 |
return (GeneralPathXIterator)gpx.getPathIterator(at, flatness);
|
322 |
|
323 |
} |
324 |
public boolean contains(double x, double y) { |
325 |
boolean bResul;
|
326 |
for (int i=0; i < getNumPoints(); i++) |
327 |
{ |
328 |
bResul = points[i].contains(x,y); |
329 |
if (bResul) return true; |
330 |
} |
331 |
return false; |
332 |
} |
333 |
public boolean contains(double x, double y, double w, double h) { |
334 |
return false; |
335 |
} |
336 |
public boolean intersects(double x, double y, double w, double h) { |
337 |
boolean bResul;
|
338 |
for (int i=0; i < getNumPoints(); i++) |
339 |
{ |
340 |
bResul = points[i].contains(x,y,w,h); |
341 |
if (bResul) return true; |
342 |
} |
343 |
return false; |
344 |
} |
345 |
public Rectangle getBounds() { |
346 |
Rectangle r=null; |
347 |
if (getNumPoints()>0){ |
348 |
r= points[0].getBounds();
|
349 |
} |
350 |
for (int i=1;i<getNumPoints();i++){ |
351 |
r.add(points[i].getX(),points[i].getY()); |
352 |
} |
353 |
return r;
|
354 |
} |
355 |
public boolean contains(Point2D p) { |
356 |
boolean bResul;
|
357 |
for (int i=0; i < getNumPoints(); i++) |
358 |
{ |
359 |
bResul = points[i].contains(p); |
360 |
if (bResul) return true; |
361 |
} |
362 |
return false; |
363 |
|
364 |
} |
365 |
public boolean contains(Rectangle2D r) { |
366 |
boolean bResul;
|
367 |
for (int i=0; i < getNumPoints(); i++) |
368 |
{ |
369 |
bResul = points[i].contains(r); |
370 |
if (bResul) return true; |
371 |
} |
372 |
return false; |
373 |
|
374 |
} |
375 |
public Shape getInternalShape() { |
376 |
return this; |
377 |
} |
378 |
public int getOriginalGeometryType() { |
379 |
return FShape.MULTIPOINT;
|
380 |
} |
381 |
public void setGeometryType(int type) { |
382 |
this.type=type;
|
383 |
} |
384 |
} |