Statistics
| Revision:

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

History | View | Annotate | Download (10.7 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.cit.gvsig.fmap.rendering.styling.FStyle2D;
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 implements IGeometry {
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, 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 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) {
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()));
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) {
263
        draw(g,vp, symbol);
264

    
265
    }
266
        /* (non-Javadoc)
267
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
268
         */
269
        public Handler[] getHandlers(int type) {
270
                int numPoints=getNumPoints();
271
                Handler[] handlers=new Handler[numPoints];
272
                for (int i = 0; i < numPoints; i++){
273
                        handlers[i]=points[i].getHandlers(type)[0];
274
                }
275
                return handlers;
276
        }
277
        /**
278
         * DOCUMENT ME!
279
         *
280
         * @author Vicente Caballero Navarro
281
         */
282
        class PointHandler extends AbstractHandler {
283
                /**
284
                 * Crea un nuevo PointHandler.
285
                 *
286
                 * @param x DOCUMENT ME!
287
                 * @param y DOCUMENT ME!
288
                 */
289
                public PointHandler(int i,FPoint2D p) {
290
                        point = new Point2D.Double(p.getX(), p.getY());
291
                        index=i;
292
                }
293

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

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

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

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

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

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

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

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

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

    
393
        }
394
        public Shape getInternalShape() {
395
                return this;
396
        }
397
}