Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FMultiPoint2D.java @ 29581

History | View | Annotate | Download (11.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

    
53
import org.cresques.cts.ICoordTrans;
54
import org.geotools.data.postgis.attributeio.WKBEncoder;
55

    
56
import com.iver.cit.gvsig.fmap.ViewPort;
57
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
58
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
59
import com.iver.utiles.swing.threads.Cancellable;
60
import com.vividsolutions.jts.geom.Coordinate;
61
import com.vividsolutions.jts.geom.Geometry;
62
import com.vividsolutions.jts.geom.GeometryFactory;
63
import com.vividsolutions.jts.geom.MultiPoint;
64

    
65

    
66
/**
67
 * Multipunto 2D.
68
 *
69
 * @author Vicente Caballero Navarro
70
 */
71
public class FMultiPoint2D extends AbstractGeometry {
72
        FGeometry[] points = null;
73

    
74
        /**
75
         * Crea un nuevo MultiPoint2D.
76
         *
77
         * @param x DOCUMENT ME!
78
         * @param y DOCUMENT ME!
79
         */
80
        public FMultiPoint2D(double[] x, double[] y) {
81
                points = new FGeometry[x.length];
82
                for (int i=0;i<x.length;i++){
83
                        points[i] = new FGeometry(new FPoint2D(x[i], y[i]));
84
                }
85

    
86
        }
87
        public FMultiPoint2D(FPoint2D[] points) {
88
                this.points=new FGeometry[points.length];
89
                for (int i=0;i<points.length;i++){
90
                        this.points[i] = new FGeometry(points[i]);
91
                }
92
   }
93
        /**
94
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
95
         *                 ViewPort, ISymbol)
96
         */
97
        public void draw(Graphics2D g, ViewPort vp, ISymbol symbol) {
98
                int size = 2;
99
                int hw = 4;
100

    
101
                for (int i = 0; i < points.length; i++) {
102
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
103
                        //Point2D.Double p = new Point2D.Double(p.getX(), p.getY());
104
                        vp.getAffineTransform().transform(p, p);
105
                        g.setColor(Color.red);
106
                        g.fillOval((int) p.getX() - size, (int) p.getY() - size, hw, hw);
107
                        g.setColor(Color.black);
108
                        g.drawOval((int) p.getX() - size, (int) p.getY() - size, hw, hw);
109
                }
110
        }
111

    
112
        /**
113
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
114
         */
115
        public Geometry toJTSGeometry() {
116
        Coordinate[] theGeoms = new Coordinate[points.length];
117
        for (int i = 0; i < theGeoms.length; i++)
118
        {
119
                Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
120

    
121
                Coordinate c = new Coordinate(p.getX(), p.getY());
122
            theGeoms[i] = c;
123
        }
124
        MultiPoint geomCol = new GeometryFactory().createMultiPoint(theGeoms);
125

    
126

    
127
                return geomCol;
128
        }
129

    
130
        /**
131
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
132
         */
133
        public FLabel[] createLabels(int position, boolean duplicates) {
134
        FLabel[] aux = new FLabel[getNumPoints()];
135
        for (int i=0; i < getNumPoints(); i++)
136
        {
137
                Shape p=points[i].getInternalShape();
138

    
139
            aux[i] = FLabel.createFLabel((FShape)p);
140
        }
141

    
142
                return aux;
143
        }
144

    
145
        /* (non-Javadoc)
146
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
147
         */
148
        public boolean intersects(Rectangle2D r) {
149
                for (int i=0;i<getNumPoints();i++){
150
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
151
                        if (r.contains(p.getX(),p.getY()))
152
                                return true;
153
                }
154
                return false;
155
        }
156

    
157
        /* (non-Javadoc)
158
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
159
         */
160
        public Rectangle2D getBounds2D() {
161
                Rectangle2D r=null;
162
                if (getNumPoints()>0){
163
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
164

    
165
                        r=new Rectangle2D.Double(p.getX(),p.getY(),0.001,0.001);
166
                }
167
                for (int i=1;i<getNumPoints();i++){
168
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
169
                        r.add(p.getX(),p.getY());
170
                }
171
                return r;
172
        }
173

    
174
        /**
175
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
176
         */
177
        public int getGeometryType() {
178
                return FShape.MULTIPOINT;
179
        }
180

    
181
        /* (non-Javadoc)
182
         * @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)
183
         */
184
        public void draw(Graphics2D g, ViewPort vp, ISymbol symbol, Cancellable cancel) {
185
                //int size = 2;
186
                //int hw = 4;
187

    
188
                for (int i = 0; i < getNumPoints(); i++) {
189
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
190

    
191
                        vp.getAffineTransform().transform(p, p);
192
                        symbol.draw(g, vp.getAffineTransform(), new FPoint2D(p.getX(),p.getY()), cancel);
193
                        // FGraphicUtilities.DrawShape(g, vp.getAffineTransform(), new FPoint2D(p.getX(),p.getY()), symbol);
194

    
195
                /*        java.awt.geom.Point2D.Double p = new java.awt.geom.Point2D.Double(x[i],
196
                                        y[i]);
197
                        vp.getAffineTransform().transform(p, p);
198
                        g.setColor(Color.red);
199
                        g.fillOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw);
200
                        g.setColor(Color.black);
201
                        g.drawOval((int) p.x - size, (int) p.y - size, (int) hw, (int) hw);
202
                */
203
                }
204
        }
205

    
206
        /* (non-Javadoc)
207
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
208
         */
209
        public IGeometry cloneGeometry() {
210
                FPoint2D[] aux = new FPoint2D[getNumPoints()];
211
                for (int i=0; i < getNumPoints(); i++)
212
                {
213
                        aux[i] = (FPoint2D) points[i].cloneGeometry().getInternalShape();
214
                }
215
                return new FMultiPoint2D(aux);
216
        }
217

    
218
        /* (non-Javadoc)
219
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
220
         */
221
        public void reProject(ICoordTrans ct) {
222
                for (int i=0; i < getNumPoints(); i++)
223
                {
224
                        points[i].reProject(ct);
225
                }
226
        }
227
        public int getNumPoints(){
228
                return points.length;
229
        }
230
        public FPoint2D getPoint(int i){
231
                return (FPoint2D)points[i].getInternalShape();
232
        }
233
        /**
234
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
235
         */
236
        public PathIterator getPathIterator(AffineTransform at) {
237
                GeneralPathX gpx=new GeneralPathX();
238
                if (getNumPoints()>0){
239
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
240
                        gpx.moveTo(p.getX(), p.getY());
241
                }
242
                for (int i=1;i<getNumPoints();i++){
243
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
244
                        gpx.lineTo(p.getX(), p.getY());
245
                }
246
                return (GeneralPathXIterator)gpx.getPathIterator(null);
247
        }
248
    /* (non-Javadoc)
249
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
250
     */
251
    public boolean fastIntersects(double x, double y, double w, double h) {
252
                for (int i=0; i < getNumPoints(); i++)
253
                {
254
                        if (points[i].intersects(x,y,w,h))
255
                                return true;
256
                }
257
        return false;
258
    }
259
    /* (non-Javadoc)
260
     * @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)
261
     */
262
    public void drawInts(Graphics2D g, ViewPort vp, ISymbol symbol, Cancellable cancel) {
263
//        draw(g,vp, symbol, cancel);
264
            for (int i = 0; i < points.length; i++) {
265
                        points[i].drawInts(g,vp,symbol,cancel);
266
                }
267
    }
268
        /* (non-Javadoc)
269
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
270
         */
271
        public Handler[] getHandlers(int type) {
272
                int numPoints=getNumPoints();
273
                Handler[] handlers=new Handler[numPoints];
274
                for (int i = 0; i < numPoints; i++){
275
                        handlers[i]=points[i].getHandlers(type)[0];
276
                }
277
                return handlers;
278
        }
279
        /**
280
         * DOCUMENT ME!
281
         *
282
         * @author Vicente Caballero Navarro
283
         */
284
        class PointHandler extends AbstractHandler {
285
                /**
286
                 * Crea un nuevo PointHandler.
287
                 *
288
                 * @param x DOCUMENT ME!
289
                 * @param y DOCUMENT ME!
290
                 */
291
                public PointHandler(int i,FPoint2D p) {
292
                        point = new Point2D.Double(p.getX(), p.getY());
293
                        index=i;
294
                }
295

    
296
                /**
297
                 * DOCUMENT ME!
298
                 *
299
                 * @param x DOCUMENT ME!
300
                 * @param y DOCUMENT ME!
301
                 *
302
                 * @return DOCUMENT ME!
303
                 */
304
                public void move(double x, double y) {
305
                        Point2D p=points[index].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
306

    
307
                        point.setLocation(p.getX()+x,
308
                                        p.getY()+y);
309
                }
310

    
311
                /**
312
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
313
                 */
314
                public void set(double x, double y) {
315
                        point.setLocation(x, y);
316
                }
317

    
318
        }
319
        public void transform(AffineTransform at) {
320
                for (int i=0; i < getNumPoints(); i++)
321
                {
322
                        points[i].transform(at);
323
                }
324

    
325
        }
326
        public byte[] toWKB() throws IOException {
327
                return WKBEncoder.encodeGeometry(toJTSGeometry());
328
        }
329
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
330
                GeneralPathX gpx=new GeneralPathX();
331
                if (getNumPoints()>0){
332
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
333

    
334
                        gpx.moveTo(p.getX(), p.getY());
335
                }
336
                for (int i=1;i<getNumPoints();i++){
337
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
338

    
339
                        gpx.lineTo(p.getX(), p.getY());
340
                }
341
                return gpx.getPathIterator(at, flatness);
342

    
343
        }
344
        public boolean contains(double x, double y) {
345
                boolean bResul;
346
                for (int i=0; i < getNumPoints(); i++)
347
                {
348
                        bResul = points[i].contains(x,y);
349
                        if (bResul) return true;
350
                }
351
                return false;
352
        }
353
        public boolean contains(double x, double y, double w, double h) {
354
                return false;
355
        }
356
        public boolean intersects(double x, double y, double w, double h) {
357
                boolean bResul;
358
                for (int i=0; i < getNumPoints(); i++)
359
                {
360
                        bResul = points[i].contains(x,y,w,h);
361
                        if (bResul) return true;
362
                }
363
                return false;
364
        }
365
        public Rectangle getBounds() {
366
                Rectangle r=null;
367
                if (getNumPoints()>0){
368
                        r= points[0].getBounds();
369
                }
370
                for (int i=1;i<getNumPoints();i++){
371
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
372
                        r.add(p.getX(),p.getY());
373
                }
374
                return r;
375
        }
376
        public boolean contains(Point2D p) {
377
                boolean bResul;
378
                for (int i=0; i < getNumPoints(); i++)
379
                {
380
                        bResul = points[i].contains(p);
381
                        if (bResul) return true;
382
                }
383
                return false;
384

    
385
        }
386
        public boolean contains(Rectangle2D r) {
387
                boolean bResul;
388
                for (int i=0; i < getNumPoints(); i++)
389
                {
390
                        bResul = points[i].contains(r);
391
                        if (bResul) return true;
392
                }
393
                return false;
394

    
395
        }
396
        public Shape getInternalShape() {
397
                return this;
398
        }
399
        public void drawInts(Graphics2D graphics2D, ViewPort viewPort, double dpi,
400
                        CartographicSupport cartographicSymbol, Cancellable cancel) {
401
                for (int i = 0; i < points.length; i++) {
402
                        points[i].drawInts(graphics2D,viewPort,dpi,cartographicSymbol,cancel);
403
                }
404

    
405
        }
406
}