Statistics
| Revision:

svn-gvsig-desktop / tags / v1_0_2_Build_916 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FMultiPoint2D.java @ 12327

History | View | Annotate | Download (10.6 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.v02.FLabel;
58
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
59
import com.vividsolutions.jts.geom.Coordinate;
60
import com.vividsolutions.jts.geom.Geometry;
61
import com.vividsolutions.jts.geom.GeometryFactory;
62
import com.vividsolutions.jts.geom.MultiPoint;
63

    
64

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

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

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

    
100
                for (int i = 0; i < points.length; i++) {
101
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
102
                        //Point2D.Double p = new Point2D.Double(p.getX(), p.getY());
103
                        vp.getAffineTransform().transform(p, p);
104
                        g.setColor(Color.red);
105
                        g.fillOval((int) p.getX() - size, (int) p.getY() - size, hw, hw);
106
                        g.setColor(Color.black);
107
                        g.drawOval((int) p.getX() - size, (int) p.getY() - size, hw, 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
                Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
119

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

    
125

    
126
                return geomCol;
127
        }
128

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

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

    
141
                return aux;
142
        }
143

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

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

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

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

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

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

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

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

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

    
217
        /* (non-Javadoc)
218
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
219
         */
220
        public void reProject(ICoordTrans ct) {
221
                for (int i=0; i < getNumPoints(); i++)
222
                {
223
                        points[i].reProject(ct);
224
                }
225
        }
226
        public int getNumPoints(){
227
                return points.length;
228
        }
229
        public FPoint2D getPoint(int i){
230
                return (FPoint2D)points[i].getInternalShape();
231
        }
232
        /**
233
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
234
         */
235
        public PathIterator getPathIterator(AffineTransform at) {
236
                GeneralPathX gpx=new GeneralPathX();
237
                if (getNumPoints()>0){
238
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
239
                        gpx.moveTo(p.getX(), p.getY());
240
                }
241
                for (int i=1;i<getNumPoints();i++){
242
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
243
                        gpx.lineTo(p.getX(), p.getY());
244
                }
245
                return (GeneralPathXIterator)gpx.getPathIterator(null);
246
        }
247
    /* (non-Javadoc)
248
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
249
     */
250
    public boolean fastIntersects(double x, double y, double w, double h) {
251
                for (int i=0; i < getNumPoints(); i++)
252
                {
253
                        if (points[i].intersects(x,y,w,h))
254
                                return true;
255
                }
256
        return false;
257
    }
258
    /* (non-Javadoc)
259
     * @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)
260
     */
261
    public void drawInts(Graphics2D g, ViewPort vp, ISymbol symbol) {
262
        draw(g,vp, symbol);
263

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

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

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

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

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

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

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

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

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

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

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