Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / core / FGeometryCollection.java @ 38563

History | View | Annotate | Download (11.5 KB)

1 1100 fjp
/* 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 296 fernando
package com.iver.cit.gvsig.fmap.core;
42
43 3885 caballero
import java.awt.Graphics2D;
44 4879 fjp
import java.awt.Rectangle;
45 5008 fjp
import java.awt.Shape;
46 3904 fjp
import java.awt.geom.AffineTransform;
47 4799 fjp
import java.awt.geom.PathIterator;
48 4879 fjp
import java.awt.geom.Point2D;
49 3885 caballero
import java.awt.geom.Rectangle2D;
50
import java.io.IOException;
51
import java.util.ArrayList;
52
53
import org.cresques.cts.ICoordTrans;
54
import org.geotools.data.postgis.attributeio.WKBEncoder;
55
56 296 fernando
import com.iver.cit.gvsig.fmap.ViewPort;
57 9641 jaume
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
58 757 fjp
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
59 13953 jaume
import com.iver.utiles.swing.threads.Cancellable;
60 703 fjp
import com.vividsolutions.jts.geom.Geometry;
61 3207 fjp
import com.vividsolutions.jts.geom.GeometryCollection;
62
import com.vividsolutions.jts.geom.GeometryFactory;
63 296 fernando
64 1005 vcaballero
65
/**
66
 * Colecci?n de Geometr?as.
67
 *
68
 * @author Vicente Caballero Navarro
69
 */
70 14205 jdominguez
public class FGeometryCollection extends AbstractGeometry {
71 3783 caballero
        private ArrayList geometries = new ArrayList();
72 5880 fjp
73 296 fernando
        /**
74 1005 vcaballero
         * Crea un nuevo FGeometryCollection.
75
         *
76
         * @param geoms vector de geometr?as.
77 296 fernando
         */
78 1005 vcaballero
        public FGeometryCollection(IGeometry[] geoms) {
79 3783 caballero
                for (int i = 0; i < geoms.length; i++) {
80
                        geometries.add(geoms[i]);
81
                }
82 1005 vcaballero
        }
83
84 3783 caballero
        public void addGeometry(IGeometry g){
85
                geometries.add(g);
86
        }
87
88 1005 vcaballero
        /**
89
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#draw(java.awt.Graphics2D,
90 14205 jdominguez
         *                 com.iver.cit.gvsig.fmap.ViewPort, ISymbol)
91 1005 vcaballero
         */
92 14205 jdominguez
        public void draw(Graphics2D g, ViewPort vp, ISymbol symbol) {
93 3783 caballero
                for (int i = 0; i < geometries.size(); i++)
94
                        ((IGeometry)geometries.get(i)).draw(g, vp, symbol);
95 296 fernando
        }
96
97
        /**
98 305 fjp
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#createLabels(int, boolean)
99 296 fernando
         */
100 757 fjp
        public FLabel[] createLabels(int position, boolean duplicates) {
101 296 fernando
                return null;
102
        }
103
104
        /**
105 305 fjp
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
106 296 fernando
         */
107 3783 caballero
/*        public boolean intersects(Rectangle2D r, double flatness) {
108
            boolean resul = false;
109
                for (int i = 0; i < geometries.size(); i++)
110
                {
111
                        resul = ((IGeometry)geometries.get(i)).intersects(r, flatness);
112
                        if (resul) break;
113
                }
114

115
                return resul;
116
        }
117
*/
118
        /**
119
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#intersects(java.awt.geom.Rectangle2D)
120
         */
121 296 fernando
        public boolean intersects(Rectangle2D r) {
122 1298 fjp
            boolean resul = false;
123 5880 fjp
                for (int i = 0; i < geometries.size(); i++)
124
                {
125
                        resul = ((IGeometry)geometries.get(i)).intersects(r);
126
                        if (resul) break;
127 1298 fjp
                }
128 5880 fjp
129 1298 fjp
                return resul;
130 296 fernando
        }
131
132
        /**
133 305 fjp
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getBounds2D()
134 296 fernando
         */
135
        public Rectangle2D getBounds2D() {
136 310 fjp
                Rectangle2D rAux = null;
137 1005 vcaballero
138 3783 caballero
                for (int i = 0; i < geometries.size(); i++)
139
                        if (rAux==null){
140
                                rAux=((IGeometry)geometries.get(i)).getBounds2D();
141
                        }else{
142
                                rAux.add(((IGeometry)geometries.get(i)).getBounds2D());
143
                        }
144 310 fjp
                return rAux;
145 296 fernando
        }
146 324 fernando
147
        /**
148
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getGeometryType()
149
         */
150
        public int getGeometryType() {
151 3783 caballero
                /*int ret = 0;
152 1005 vcaballero

153 324 fernando
                for (int i = 0; i < geometries.length; i++) {
154 3783 caballero
                        ret = ret | geometries[i].getGeometryType();
155 324 fernando
                }
156 1005 vcaballero

157 324 fernando
                return ret;
158 3783 caballero
                */
159 22994 vcaballero
                return FShape.MULTI;
160 324 fernando
        }
161 5880 fjp
162 458 fjp
        /* (non-Javadoc)
163
         * @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)
164
         */
165 13953 jaume
        public void draw(Graphics2D g, ViewPort vp, ISymbol symbol, Cancellable cancel) {
166 5880 fjp
                for (int i = 0; i < geometries.size(); i++)
167 13953 jaume
                        ((IGeometry)geometries.get(i)).draw(g, vp, symbol, cancel);
168 458 fjp
        }
169 703 fjp
170
        /* (non-Javadoc)
171
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#cloneGeometry()
172
         */
173
        public IGeometry cloneGeometry() {
174 3783 caballero
                IGeometry[] newGeometries = new IGeometry[geometries.size()];
175 1005 vcaballero
176 3783 caballero
                for (int i = 0; i < geometries.size(); i++)
177
                        newGeometries[i] = ((IGeometry)geometries.get(i)).cloneGeometry();
178 5880 fjp
179
                return new FGeometryCollection(newGeometries);
180 703 fjp
        }
181
182
        /* (non-Javadoc)
183
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#toJTSGeometry()
184
         */
185
        public Geometry toJTSGeometry() {
186 3783 caballero
        Geometry[] theGeoms = new Geometry[geometries.size()];
187
        for (int i = 0; i < geometries.size(); i++)
188 3207 fjp
        {
189 3783 caballero
            theGeoms[i] = ((IGeometry)geometries.get(i)).toJTSGeometry();
190 3207 fjp
        }
191
        GeometryCollection geomCol = new GeometryFactory().createGeometryCollection(theGeoms);
192 12467 jaume
193
194 3207 fjp
                return geomCol;
195 703 fjp
        }
196 885 fjp
197
        /* (non-Javadoc)
198
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#reProject(org.cresques.cts.ICoordTrans)
199
         */
200
        public void reProject(ICoordTrans ct) {
201 3783 caballero
                for (int i = 0; i < geometries.size(); i++)
202
                        ((IGeometry)geometries.get(i)).reProject(ct);
203 885 fjp
        }
204 1132 vcaballero
205
        /**
206 4879 fjp
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getPathIterator(AffineTransform)
207 1132 vcaballero
         */
208 4879 fjp
        public PathIterator getPathIterator(AffineTransform at) {
209 3935 fjp
                // Necesitamos convertir todo esto a una GeneralPathX, tarde o temprano.
210
                // As? que lo intento aqu? por primera vez.
211
                // Lo necesitamos para la edici?n, porque se est?n
212
                // a?adiendo las geometr?as como FGeometryCollection
213
                // para que el explode sea sencillo. No lo veo muy
214
                // claro eso, pero bueno.
215
                /* GeneralPathX gp = new GeneralPathX();
216
                double[] coords = new double[6];
217
                for (int i=0; i < geometries.size(); i++)
218
                {
219
                        IGeometry gAux = (IGeometry) geometries.get(i);
220
                        GeneralPathXIterator pi = gAux.getGeneralPathXIterator();
221
                        // Si el primer punto y el ultimo son iguales, conectamos
222
                        // la geometr?a.
223
                        boolean bFirst = true;
224
                        double[] firstCoord = new double[6];
225
                        while (!pi.isDone())
226 4711 caballero
                        {
227 3935 fjp
                                int type = pi.currentSegment(coords);
228
                                switch (type)
229 4711 caballero
                                {
230 3935 fjp
                                        case GeneralPathXIterator.SEG_MOVETO:
231
                                                if ((!bFirst) || (firstCoord != coords))
232
                                                        gp.moveTo(coords[0], coords[1]);
233
                                                break;
234 4711 caballero
                                    case SEG_LINETO:
235 3935 fjp
                                            lineTo(coords[0], coords[1]);
236
                                            break;
237
                                    case SEG_QUADTO:
238
                                            quadTo(coords[0], coords[1],
239
                                               coords[2], coords[3]);
240
                                            break;
241
                                    case SEG_CUBICTO:
242
                                            // Not implemented
243
                                            System.err.println("ERROR. TRAMO CUBICO. SIN IMPLEMENTAR TODAV?A");
244
                                                curveTo(coords[0], coords[1],
245
                                                        coords[2], coords[3],
246
                                                        coords[4], coords[5]);
247
                                                break;
248
                                            case SEG_CLOSE:
249
                                                    closePath();
250
                                                    break;
251
                                }
252
                                pi.next();
253 4711 caballero
                        }
254 3935 fjp
                }                */
255
                GeneralPathX gp = new GeneralPathX();
256
                for (int i=0; i < geometries.size(); i++)
257
                {
258
                        IGeometry gAux = (IGeometry) geometries.get(i);
259 4879 fjp
                        gp.append(gAux.getPathIterator(null), true);
260 4711 caballero
                }
261 3935 fjp
                return (GeneralPathXIterator) gp.getPathIterator(null);
262 1132 vcaballero
        }
263 1298 fjp
264
    /* (non-Javadoc)
265
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#fastIntersects(double, double, double, double)
266
     */
267
    public boolean fastIntersects(double x, double y, double w, double h) {
268
            boolean resul = false;
269 3783 caballero
                for (int i = 0; i < geometries.size(); i++)
270 1298 fjp
                {
271 3783 caballero
                        resul = ((IGeometry)geometries.get(i)).fastIntersects(x,y,w,h);
272 1298 fjp
                        if (resul) break;
273 3783 caballero
                }
274 1298 fjp
                return resul;
275
    }
276 2183 fernando
277
    /**
278
     * @see com.iver.cit.gvsig.fmap.core.IGeometry#toWKB()
279
     */
280
    public byte[] toWKB() throws IOException {
281
        return WKBEncoder.encodeGeometry(toJTSGeometry());
282
    }
283 2859 fjp
284
    /* (non-Javadoc)
285
     * @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)
286
     */
287 13953 jaume
    public void drawInts(Graphics2D g, ViewPort vp, ISymbol symbol, Cancellable cancel) {
288 5880 fjp
        for (int i = 0; i < geometries.size(); i++)
289 13953 jaume
            ((IGeometry)geometries.get(i)).drawInts(g, vp, symbol, null);
290 3783 caballero
291 2859 fjp
    }
292 3783 caballero
293
        /**
294
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getShapes()
295
         */
296
        /*public FShape[] getShapes() {
297
                ArrayList shapes=new ArrayList();
298
                for (int i= 0;i<geometries.size();i++){
299
                        FShape[] s=((IGeometry)geometries.get(i)).getShapes();
300
                        for (int j=0;j<s.length;j++){
301
                                shapes.add(s[j]);
302
                        }
303
                }
304
                return (FShape[])shapes.toArray(new FShape[0]);
305
        }*/
306
        public IGeometry[] getGeometries(){
307
                return (IGeometry[])geometries.toArray(new IGeometry[0]).clone();
308
        }
309 3833 caballero
310
        /* (non-Javadoc)
311
         * @see com.iver.cit.gvsig.fmap.core.IGeometry#getHandlers(int)
312
         */
313
        public Handler[] getHandlers(int type) {
314
                ArrayList handlers=new ArrayList();
315
                for (int i = 0; i < geometries.size(); i++){
316
                        Handler[] handAux=((IGeometry)geometries.get(i)).getHandlers(type);
317
                        for (int j=0;j<handAux.length;j++){
318 5880 fjp
                                handlers.add(handAux[j]);
319 3833 caballero
                        }
320
                }
321
                return (Handler[])handlers.toArray(new Handler[0]);
322
        }
323 3885 caballero
324 3904 fjp
325
        public void transform(AffineTransform at) {
326 3885 caballero
                for (int i = 0; i < geometries.size(); i++){
327 3904 fjp
                        ((IGeometry)geometries.get(i)).transform(at);
328 4711 caballero
                }
329 3885 caballero
        }
330 4799 fjp
331 4879 fjp
        public PathIterator getPathIterator(AffineTransform at, double flatness) {
332 4799 fjp
                GeneralPathX gp = new GeneralPathX();
333
                for (int i=0; i < geometries.size(); i++)
334
                {
335
                        IGeometry gAux = (IGeometry) geometries.get(i);
336 4879 fjp
                        gp.append(gAux.getPathIterator(null), true);
337 4799 fjp
                }
338 5883 caballero
                return gp.getPathIterator(at, flatness);
339 4799 fjp
        }
340 4879 fjp
341
        public boolean contains(double x, double y) {
342
                boolean bRes;
343
                for (int i=0; i < geometries.size(); i++)
344
                {
345
                        IGeometry gAux = (IGeometry) geometries.get(i);
346
                        bRes = gAux.contains(x,y);
347
                        if (bRes) return bRes;
348
                }
349
350
                return false;
351
        }
352
353
        public boolean contains(double x, double y, double w, double h) {
354
                boolean bRes;
355
                for (int i=0; i < geometries.size(); i++)
356
                {
357
                        IGeometry gAux = (IGeometry) geometries.get(i);
358
                        bRes = gAux.contains(x,y, w, h);
359
                        if (bRes) return bRes;
360
                }
361
362
                return false;
363
        }
364
365
        public boolean intersects(double x, double y, double w, double h) {
366 5880 fjp
                boolean bRes;
367
                for (int i=0; i < geometries.size(); i++)
368
                {
369
                        IGeometry gAux = (IGeometry) geometries.get(i);
370
                        bRes = gAux.intersects(x,y, w, h);
371
                        if (bRes) return bRes;
372 4879 fjp
                }
373 5880 fjp
374
                return false;
375 4879 fjp
        }
376
377
        public Rectangle getBounds() {
378
                Rectangle rAux = null;
379
380
                for (int i = 0; i < geometries.size(); i++)
381
                        if (rAux==null){
382
                                rAux=((IGeometry)geometries.get(i)).getBounds();
383
                        }else{
384
                                rAux.add(((IGeometry)geometries.get(i)).getBounds());
385
                        }
386
                return rAux;
387
        }
388
389
        public boolean contains(Point2D p) {
390
                boolean bRes;
391
                for (int i=0; i < geometries.size(); i++)
392
                {
393
                        IGeometry gAux = (IGeometry) geometries.get(i);
394
                        bRes = gAux.contains(p);
395
                        if (bRes) return bRes;
396
                }
397
398
                return false;
399
        }
400
401
        public boolean contains(Rectangle2D r) {
402
                boolean bRes;
403
                for (int i=0; i < geometries.size(); i++)
404
                {
405
                        IGeometry gAux = (IGeometry) geometries.get(i);
406
                        bRes = gAux.contains(r);
407
                        if (bRes) return bRes;
408
                }
409
410
                return false;
411
        }
412 5008 fjp
413
        public Shape getInternalShape() {
414
                return this;
415
        }
416 12467 jaume
417 35335 fpenarrubia
        public void drawInts(Graphics2D graphics2D, ViewPort vp, double dpi,
418 18621 jdominguez
                        CartographicSupport cartographicSymbol, Cancellable cancel) {
419 35335 fpenarrubia
        for (int i = 0; i < geometries.size(); i++)
420
            ((IGeometry)geometries.get(i)).drawInts(graphics2D, vp, dpi,
421
                            cartographicSymbol, cancel);
422 22994 vcaballero
423 35335 fpenarrubia
424 12467 jaume
        }
425 296 fernando
}