Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap / src / es / prodevelop / gvsig / mobile / fmap / core / FMultiPoint2D.java @ 21606

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
/************************************************
42
 *                                                                                                *
43
 *   Modfied By:                                                                *
44
 *   Prodevelop Integraci?n de Tecnolog?as SL        *
45
 *   Conde Salvatierra de ?lava , 34-10                        *
46
 *   46004 Valencia                                                                *
47
 *   Spain                                                                                *
48
 *                                                                                                *
49
 *   +34 963 510 612                                                        *
50
 *   +34 963 510 968                                                        *
51
 *   gis@prodevelop.es                                                        *
52
 *   http://www.prodevelop.es                                        *
53
 *                                                                                                *
54
 *   gvSIG Mobile Team 2006                                         *
55
 *                                                                                          *         
56
 ************************************************/
57

    
58
package es.prodevelop.gvsig.mobile.fmap.core;
59

    
60
import java.awt.Color;
61
import java.awt.Graphics2D;
62
import java.awt.Rectangle;
63
import java.awt.Shape;
64
import java.awt.geom.AffineTransform;
65
import java.awt.geom.PathIterator;
66
import java.awt.geom.Point2D;
67
import java.awt.geom.Rectangle2D;
68

    
69
import com.vividsolutions.jts.geom.Coordinate;
70
import com.vividsolutions.jts.geom.Geometry;
71
import com.vividsolutions.jts.geom.GeometryFactory;
72
import com.vividsolutions.jts.geom.MultiPoint;
73

    
74
import es.prodevelop.gvsig.mobile.fmap.proj.ICoordTrans;
75
import es.prodevelop.gvsig.mobile.fmap.symbol.FSymbol;
76
import es.prodevelop.gvsig.mobile.fmap.viewport.ViewPort;
77

    
78

    
79
/**
80
 * Multipunto 2D.
81
 *
82
 * @author Vicente Caballero Navarro
83
 */
84
public class FMultiPoint2D implements IGeometry {
85
        
86
        FGeometry[] points = null;
87

    
88
        /**
89
         * Crea un nuevo MultiPoint2D.
90
         *
91
         * @param x 
92
         * @param y 
93
         */
94
        public FMultiPoint2D(double[] x, double[] y) {
95
                points = new FGeometry[x.length];
96
                for (int i=0;i<x.length;i++){
97
                        points[i] = new FGeometry(new FPoint2D(x[i], y[i]));
98
                }
99

    
100
        }
101
        public FMultiPoint2D(FPoint2D[] points) {
102
                this.points=new FGeometry[points.length];
103
                for (int i=0;i<points.length;i++){
104
                        this.points[i] = new FGeometry(points[i]);
105
                }
106
   }
107
        /**
108
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
109
         *                 ViewPort, FStyle2D)
110
         */
111
        public void draw(Graphics2D g, ViewPort vp, FSymbol symbol, AffineTransform at) {
112
                int size = 2;
113
                int hw = 4;
114

    
115
                for (int i = 0; i < points.length; i++) {
116
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
117
                        //Point2D.Double p = new Point2D.Double(p.getX(), p.getY());
118
                        vp.getAffineTransform().transform(p, p);
119
                        g.setColor(Color.red);
120
                        g.fillOval((int) p.getX() - size, (int) p.getY() - size, hw, hw);
121
                        g.setColor(Color.black);
122
                        g.drawOval((int) p.getX() - size, (int) p.getY() - size, hw, hw);
123
                }
124
        }
125

    
126
        /**
127
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
128
         */
129
        public Geometry toJTSGeometry() {
130
        Coordinate[] theGeoms = new Coordinate[points.length];
131
        for (int i = 0; i < theGeoms.length; i++)
132
        {
133
                Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
134

    
135
                Coordinate c = new Coordinate(p.getX(), p.getY());
136
            theGeoms[i] = c;
137
        }
138
        MultiPoint geomCol = new GeometryFactory().createMultiPoint(theGeoms);
139

    
140

    
141
                return geomCol;
142
        }
143

    
144
        /**
145
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
146
         */
147
        
148

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

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

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

    
178
        /**
179
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
180
         */
181
        public int getGeometryType() {
182
                return FShape.MULTIPOINT;
183
        }
184

    
185
        /* (non-Javadoc)
186
         * @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)
187
         */
188
        /* (non-Javadoc)
189
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
190
         */
191
        public IGeometry cloneGeometry() {
192
                FPoint2D[] aux = new FPoint2D[getNumPoints()];
193
                for (int i=0; i < getNumPoints(); i++)
194
                {
195
                        aux[i] = (FPoint2D) points[i].cloneGeometry().getInternalShape();
196
                }
197
                return new FMultiPoint2D(aux);
198
        }
199

    
200
        /* (non-Javadoc)
201
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
202
         */
203
        public void reProject(ICoordTrans ct) {
204
                for (int i=0; i < getNumPoints(); i++)
205
                {
206
                        points[i].reProject(ct);
207
                }
208
        }
209
        public int getNumPoints(){
210
                return points.length;
211
        }
212
        public FPoint2D getPoint(int i){
213
                return (FPoint2D)points[i].getInternalShape();
214
        }
215
        /**
216
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
217
         */
218
        public PathIterator getPathIterator(AffineTransform at) {
219
                GeneralPathX gpx=new GeneralPathX();
220
                if (getNumPoints()>0){
221
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
222
                        gpx.moveTo(p.getX(), p.getY());
223
                }
224
                for (int i=1;i<getNumPoints();i++){
225
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
226
                        gpx.lineTo(p.getX(), p.getY());
227
                }
228
                return (GeneralPathXIterator)gpx.getPathIterator(null);
229
        }
230
    /* (non-Javadoc)
231
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
232
     */
233
    public boolean fastIntersects(double x, double y, double w, double h) {
234
                for (int i=0; i < getNumPoints(); i++)
235
                {
236
                        if (points[i].intersects(x,y,w,h))
237
                                return true;
238
                }
239
        return false;
240
    }
241
    /* (non-Javadoc)
242
     * @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)
243
     */
244
    public void drawInts(Graphics2D g, ViewPort vp, FSymbol symbol, AffineTransform at) {
245
        draw(g,vp, symbol, at);
246
    }
247
        /* (non-Javadoc)
248
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
249
         */
250
        public Handler[] getHandlers(int type) {
251
                int numPoints=getNumPoints();
252
                Handler[] handlers=new Handler[numPoints];
253
                for (int i = 0; i < numPoints; i++){
254
                        handlers[i]=points[i].getHandlers(type)[0];
255
                }
256
                return handlers;
257
        }
258
        /**
259
         * 
260
         *
261
         * @author Vicente Caballero Navarro
262
         */
263
        class PointHandler extends AbstractHandler {
264
                /**
265
                 * Crea un nuevo PointHandler.
266
                 *
267
                 * @param x 
268
                 * @param y 
269
                 */
270
                public PointHandler(int i,FPoint2D p) {
271
                        point = new Point2D.Double(p.getX(), p.getY());
272
                        index=i;
273
                }
274

    
275
                /**
276
                 * 
277
                 *
278
                 * @param x 
279
                 * @param y 
280
                 *
281
                 * @return 
282
                 */
283
                public void move(double x, double y) {
284
                        Point2D p=points[index].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
285

    
286
                        point.setLocation(p.getX()+x,
287
                                        p.getY()+y);
288
                }
289

    
290
                /**
291
                 * @see com.iver.cit.gvsig.fmap.core.Handler#set(double, double)
292
                 */
293
                public void set(double x, double y) {
294
                        point.setLocation(x, y);
295
                }
296

    
297
        }
298
        public void transform(AffineTransform at) {
299
                for (int i=0; i < getNumPoints(); i++)
300
                {
301
                        points[i].transform(at);
302
                }
303

    
304
        }
305
        
306
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
307
                GeneralPathX gpx=new GeneralPathX();
308
                if (getNumPoints()>0){
309
                        Point2D p=points[0].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
310

    
311
                        gpx.moveTo(p.getX(), p.getY());
312
                }
313
                for (int i=1;i<getNumPoints();i++){
314
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
315

    
316
                        gpx.lineTo(p.getX(), p.getY());
317
                }
318
                return gpx.getPathIterator(at, flatness);
319

    
320
        }
321
        public boolean contains(double x, double y) {
322
                boolean bResul;
323
                for (int i=0; i < getNumPoints(); i++)
324
                {
325
                        bResul = points[i].contains(x,y);
326
                        if (bResul) return true;
327
                }
328
                return false;
329
        }
330
        public boolean contains(double x, double y, double w, double h) {
331
                return false;
332
        }
333
        public boolean intersects(double x, double y, double w, double h) {
334
                boolean bResul;
335
                for (int i=0; i < getNumPoints(); i++)
336
                {
337
                        bResul = points[i].contains(x,y,w,h);
338
                        if (bResul) return true;
339
                }
340
                return false;
341
        }
342
        public Rectangle getBounds() {
343
                Rectangle2D.Double r=null;
344
                if (getNumPoints()>0){
345
                        r= new Rectangle2D.Double(
346
                                        points[0].getBounds().x,
347
                                        points[0].getBounds().y,
348
                                        points[0].getBounds().width,
349
                                        points[0].getBounds().height);
350
                }
351
                for (int i=1;i<getNumPoints();i++){
352
                        Point2D p=points[i].getHandlers(IGeometry.SELECTHANDLER)[0].getPoint();
353
                        r.add(p.getX(),p.getY());
354
                }
355
                Rectangle resp = new Rectangle(
356
                                Math.round((float) r.x),
357
                                Math.round((float) r.y),
358
                                Math.round((float) r.width),
359
                                Math.round((float) r.height));
360
                return resp;
361
        }
362
        public boolean contains(Point2D p) {
363
                boolean bResul;
364
                for (int i=0; i < getNumPoints(); i++)
365
                {
366
                        bResul = points[i].contains(p);
367
                        if (bResul) return true;
368
                }
369
                return false;
370

    
371
        }
372
        public boolean contains(Rectangle2D r) {
373
                boolean bResul;
374
                for (int i=0; i < getNumPoints(); i++)
375
                {
376
                        bResul = points[i].contains(r);
377
                        if (bResul) return true;
378
                }
379
                return false;
380

    
381
        }
382
        public Shape getInternalShape() {
383
                return this;
384
        }
385
}