Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / FMap.java @ 558

History | View | Annotate | Download (16.9 KB)

1
/* Generated by Together */
2
package com.iver.cit.gvsig.fmap;
3

    
4
import java.awt.Graphics2D;
5
import java.awt.Toolkit;
6
import java.awt.geom.Point2D;
7
import java.awt.geom.Rectangle2D;
8
import java.awt.image.BufferedImage;
9
import java.io.File;
10
import java.util.ArrayList;
11
import java.util.Iterator;
12

    
13
import org.cresques.cts.ICoordTrans;
14
import org.cresques.cts.IProjection;
15
import org.cresques.cts.ProjectionPool;
16
import org.cresques.geo.Projected;
17

    
18
import com.hardcode.driverManager.DriverLoadException;
19
import com.hardcode.gdbms.engine.data.DriverException;
20
import com.iver.cit.gvsig.fmap.core.IGeometry;
21
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
22
import com.iver.cit.gvsig.fmap.layers.CancelationException;
23
import com.iver.cit.gvsig.fmap.layers.CommonOperations;
24
import com.iver.cit.gvsig.fmap.layers.FLayer;
25
import com.iver.cit.gvsig.fmap.layers.FLayers;
26
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
27
import com.iver.cit.gvsig.fmap.layers.LayerChangeSupport;
28
import com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent;
29
import com.iver.cit.gvsig.fmap.layers.LayerCollectionListener;
30
import com.iver.cit.gvsig.fmap.layers.LayerFactory;
31
import com.iver.cit.gvsig.fmap.layers.LayerListener;
32
import com.iver.cit.gvsig.fmap.layers.LayerPositionEvent;
33
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
34
import com.iver.cit.gvsig.fmap.layers.XMLException;
35
import com.iver.cit.gvsig.fmap.operations.Cancellable;
36
import com.iver.cit.gvsig.fmap.operations.QueriedPoint;
37
import com.iver.cit.gvsig.fmap.operations.selection.Record;
38
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
39
import com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent;
40
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
41
import com.iver.utiles.XMLEntity;
42

    
43

    
44
/**
45
 * DOCUMENT ME!
46
 *
47
 * @author Fernando Gonz?lez Cort?s
48
 */
49
public class FMap implements Projected {
50
        public static final double[] CHANGEM = {
51
                        1000, 1, 0.01, 0.001, 1609.344, 0.9144, 0.3048, 0.0254
52
                };
53

    
54
        /** DOCUMENT ME! */
55
        public static final double[] CHANGE = {
56
                        100000, 100, 1, 0.1, 160934.4, 91.44, 30.48, 2.54
57
                };
58
        public static final int EQUALS = 0;
59
        public static final int DISJOINT = 1;
60
        public static final int INTERSECTS = 2;
61
        public static final int TOUCHES = 3;
62
        public static final int CROSSES = 4;
63
        public static final int WITHIN = 5;
64
        public static final int CONTAINS = 6;
65
        public static final int OVERLAPS = 7;
66
        private FLayers layers = new FLayers();
67
        private ViewPort viewPort;
68
        private ArrayList invalidationListeners = new ArrayList();
69
        private LayerChangeSupport layerChangeSupport = new LayerChangeSupport();
70
        private LegendChanges legendChanges = new LegendChanges();
71

    
72
        /**
73
         * Crea un nuevo FMap.
74
         *
75
         * @param vp DOCUMENT ME!
76
         */
77
        public FMap(ViewPort vp) {
78
                this.viewPort = vp;
79
                layers.addLayerCollectionListener(new LayerEventListener());
80
        }
81

    
82
        /**
83
         * DOCUMENT ME!
84
         *
85
         * @param listener DOCUMENT ME!
86
         */
87
        public void addLayerListener(LayerListener listener) {
88
                layerChangeSupport.addLayerListener(listener);
89
        }
90

    
91
        /**
92
         * DOCUMENT ME!
93
         *
94
         * @param e DOCUMENT ME!
95
         */
96
        public void callLegendChanged(LegendChangedEvent e) {
97
                layerChangeSupport.callLegendChanged(e);
98
        }
99

    
100
        /**
101
         * DOCUMENT ME!
102
         *
103
         * @param listener DOCUMENT ME!
104
         */
105
        public void removeLayerListener(LayerListener listener) {
106
                layerChangeSupport.removeLayerListener(listener);
107
        }
108

    
109
        /**
110
         * DOCUMENT ME!
111
         *
112
         * @return DOCUMENT ME!
113
         */
114
        public FLayers getLayers() {
115
                return layers;
116
        }
117

    
118
        /**
119
         * Dibuja en la imagen que se pasa como par?metro el contenido de las capas
120
         * visibles del mapa y teniendo en cuenta los datos del ViewPort contenido
121
         * en este FMap
122
         *
123
         * @param b DOCUMENT ME!
124
         */
125
        public void drawLabels(BufferedImage b) {
126
        }
127

    
128
        /**
129
         * DOCUMENT ME!
130
         *
131
         * @param g DOCUMENT ME!
132
         *
133
         * @throws DriverIOException
134
         * @throws DriverLoadException
135
         * @throws DriverException
136
         */
137
        public void print(Graphics2D g)
138
                throws DriverIOException, DriverLoadException, DriverException {
139
                draw(null, g);
140
        }
141

    
142
        /**
143
         * Crea un nuevo FMap con la informaci?n del ViewPort que se pasa como
144
         * par?metro.
145
         *
146
         * @param vp DOCUMENT ME!
147
         *
148
         * @return DOCUMENT ME!
149
         */
150
        public FMap createNewFMap(ViewPort vp) {
151
                FMap ret = new FMap(vp);
152
                ret.layers = this.layers;
153

    
154
                return ret;
155
        }
156

    
157
        /**
158
         * Crea un nuevo FMap totalmente desligado, se replican las capas y el
159
         * ViewPort
160
         *
161
         * @return DOCUMENT ME!
162
         * @throws DriverException
163
         */
164
        public FMap cloneFMap() throws DriverException {
165
                return createXMLEntity(getXMLEntity());
166
        }
167

    
168
        /**
169
         * DOCUMENT ME!
170
         *
171
         * @param xe DOCUMENT ME!
172
         *
173
         * @return DOCUMENT ME!
174
         */
175
        public FMap createXMLEntity(XMLEntity xe) {
176
                // TODO Implementar bien
177
                return new FMap(null);
178
        }
179

    
180
        /**
181
         * A?ade la capa que se pasa como par?metro al nodo que se pasa como
182
         * parametro y lanza ProjectionMismatchException si no est?n todas las
183
         * capas de este FMap en la misma proyecci?n. Lanza un
184
         * ChildNotAllowedException si la capa no es un FLayers y no permite hijos
185
         *
186
         * @param vectorial DOCUMENT ME!
187
         */
188

    
189
        /*        public void addLayer(LayerPath parent, FLayer layer)
190
           throws ProjectionMismatchException, ChildrenNotAllowedException {
191
           layers.addLayer(parent, layer);
192
           }
193
           public void removeLayer(LayerPath parent)throws ChildrenNotAllowedException{
194
                   layers.removeLayer(parent);
195
           }
196
         */
197

    
198
        /**
199
         * A?ade una capa al grupo de capas que se sit?a por encima de todas las
200
         * otras capas
201
         *
202
         * @param vectorial DOCUMENT ME!
203
         */
204
        public void addToTrackLayer(FLyrVect vectorial) {
205
        }
206

    
207
        /**
208
         * DOCUMENT ME!
209
         *
210
         * @return DOCUMENT ME!
211
         */
212
        public long getScaleView() {
213
                Toolkit kit = Toolkit.getDefaultToolkit();
214
                double dpi = kit.getScreenResolution();
215
                IProjection proj = viewPort.getProjection();
216

    
217
                double w = ((viewPort.getImageSize().getWidth() / dpi) * 2.54);
218

    
219
                if (viewPort.getAdjustedExtent() == null) {
220
                        return 0;
221
                }
222

    
223
                if (proj == null) {
224
                        return (long) (viewPort.getAdjustedExtent().getWidth() / w * CHANGE[getViewPort()
225
                                                                                                                                                                        .getMapUnits()]);
226
                }
227

    
228
                return (long) proj.getScale(viewPort.getAdjustedExtent().getMinX(),
229
                        viewPort.getAdjustedExtent().getMaxX(),
230
                        viewPort.getImageSize().getWidth(), dpi);
231
        }
232

    
233
        /**
234
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#setVectorial(com.iver.cit.gvsig.fmap.VectorialAdapter)
235
         */
236
        public void setVectorial(VectorialAdapter v) {
237
        }
238

    
239
        /**
240
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#process(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
241
         *                 VectorialSubSet)
242
         */
243
        public void process(FeatureVisitor visitor) {
244
        }
245

    
246
        /**
247
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#processSelected(com.iver.cit.gvsig.fmap.FeatureVisitor)
248
         */
249
        public void processSelected(FeatureVisitor visitor) {
250
        }
251

    
252
        /**
253
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#select(com.iver.cit.gvsig.fmap.FeatureSelectorVisitor,
254
         *                 VectorialSubSet)
255
         */
256
        public void select(FeatureVisitor visitor) {
257
        }
258

    
259
        /**
260
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectFromSelection()
261
         */
262
        public void selectFromSelection() {
263
        }
264

    
265
        /**
266
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#createIndex()
267
         */
268
        public void createIndex() {
269
        }
270

    
271
        /**
272
         * @see org.cresques.geo.Projected#getProjection()
273
         */
274
        public IProjection getProjection() {
275
                return null;
276
        }
277

    
278
        /**
279
         * @see org.cresques.geo.Projected#reProject(org.cresques.cts.ICoordTrans)
280
         */
281
        public void reProject(ICoordTrans arg0) {
282
        }
283

    
284
        /**
285
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByPoint(java.awt.geom.Point2D,
286
         *                 double)
287
         */
288
        public void selectByPoint(Point2D p, double tolerance)
289
                throws DriverIOException {
290
                QueriedPoint qp = new QueriedPoint(p.getX(), p.getY());
291
                Point2D mapPoint = viewPort.toMapPoint((int) p.getX(), (int) p.getY());
292
                qp.setRealCoords(mapPoint.getX(), mapPoint.getY());
293
                layers.selectByPoint(qp, tolerance);
294
                fireInvalidEvent();
295
        }
296

    
297
        /**
298
         * DOCUMENT ME!
299
         *
300
         * @param rect DOCUMENT ME!
301
         *
302
         * @throws DriverIOException
303
         *
304
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByRect(java.awt.geom.Rectangle2D)
305
         */
306
        public void selectByRect(Rectangle2D rect) throws DriverIOException {
307
                layers.selectByRect(rect);
308
                fireInvalidEvent();
309
        }
310

    
311
        /**
312
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#selectByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
313
         *                 int)
314
         */
315
        public void selectByShape(IGeometry g, int relationship) {
316
        }
317

    
318
        /**
319
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
320
         *                 double)
321
         */
322
        public Record[] queryByPoint(Point2D p, double tolerance) {
323
                return null;
324
        }
325

    
326
        /**
327
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByRect(java.awt.geom.Rectangle2D)
328
         */
329
        public Record[] queryByRect(Rectangle2D rect) {
330
                return null;
331
        }
332

    
333
        /**
334
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.fshape.IGeometry,
335
         *                 int)
336
         */
337
        public Record[] queryByShape(IGeometry g, int relationship) {
338
                return null;
339
        }
340

    
341
        /**
342
         * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#getSelectionBounds()
343
         */
344
        public Rectangle2D getSelectionBounds() {
345
                return null;
346
        }
347

    
348
        /**
349
         * DOCUMENT ME!
350
         *
351
         * @param image DOCUMENT ME!
352
         * @param g DOCUMENT ME!
353
         * @param cancel DOCUMENT ME!
354
         *
355
         * @throws DriverIOException
356
         * @throws DriverLoadException
357
         * @throws DriverException
358
         *
359
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
360
         *                 java.awt.Graphics2D, FStyle2D)
361
         */
362
        public void draw(BufferedImage image, Graphics2D g, Cancellable cancel)
363
                throws DriverIOException, DriverLoadException, DriverException {
364
                if (viewPort.getExtent() == null) {
365
                        return;
366
                }
367

    
368
                layers.draw(image, g, viewPort, cancel);
369
        }
370

    
371
        /**
372
         * DOCUMENT ME!
373
         *
374
         * @param image DOCUMENT ME!
375
         * @param g DOCUMENT ME!
376
         *
377
         * @throws DriverIOException
378
         * @throws DriverLoadException
379
         * @throws DriverException
380
         *
381
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
382
         *                 java.awt.Graphics2D, FStyle2D)
383
         */
384
        public void draw(BufferedImage image, Graphics2D g)
385
                throws DriverIOException, DriverLoadException, DriverException {
386
                layers.draw(image, g, viewPort,
387
                        new Cancellable() {
388
                                /**
389
                                 * @see com.iver.cit.gvsig.fmap.operations.Cancellable#isCanceled()
390
                                 */
391
                                public boolean isCanceled() {
392
                                        return false;
393
                                }
394
                        });
395
        }
396

    
397
        /**
398
         * DOCUMENT ME!
399
         *
400
         * @return Returns the viewPort.
401
         */
402
        public ViewPort getViewPort() {
403
                return viewPort;
404
        }
405

    
406
        /**
407
         * DOCUMENT ME!
408
         *
409
         * @param viewPort The viewPort to set.
410
         */
411
        public void setViewPort(ViewPort viewPort) {
412
                this.viewPort = viewPort;
413
        }
414

    
415
        /**
416
         * M?todo de conveniencia. Recorre las capas y te da el fullExtent
417
         *
418
         * @return fullExtent de todas las capas.
419
         *
420
         * @throws DriverIOException
421
         */
422
        public Rectangle2D getFullExtent() throws DriverIOException {
423
                return layers.getFullExtent();
424
        }
425

    
426
        /**
427
         * DOCUMENT ME!
428
         *
429
         * @return DOCUMENT ME!
430
         * @throws DriverException
431
         */
432
        public XMLEntity getXMLEntity() throws DriverException {
433
                XMLEntity xml = new XMLEntity();
434
                xml.addChild(viewPort.getXMLEntity());
435
                xml.putProperty("numLayers", layers.getLayersCount());
436
                String[] s=new String[layers.getLayersCount()];
437
                for (int i=0;i<layers.getLayersCount();i++){
438
                        s[i]=layers.getLayer(i).getName();
439
                }
440
                xml.putProperty("LayerNames",s);
441
                xml.addChild(layers.getXMLEntity());
442

    
443
                return xml;
444
        }
445

    
446
        /**
447
         * DOCUMENT ME!
448
         *
449
         * @param xml DOCUMENT ME!
450
         *
451
         * @return DOCUMENT ME!
452
         *
453
         * @throws XMLException DOCUMENT ME!
454
         */
455
        public static FMap createFromXML(XMLEntity xml)
456
                throws XMLException {
457
                ViewPort vp = ViewPort.createFromXML(xml.getChild(0));
458
                FMap fmap = new FMap(vp);
459

    
460
                ///fmap.layers=(FLayers)FLayers.createFromXML(xml.getChild(1));
461
                int numLayers = xml.getIntProperty("numLayers");
462

    
463
                for (int i = 0; i < numLayers; i++) {
464
                        IProjection proj = null;
465

    
466
                        if (xml.getChild(0).getStringProperty("proj") != null) {
467
                                proj = ProjectionPool.get(xml.getChild(0).getStringProperty("proj"));
468
                        }
469

    
470
                        FLayer layer = null;
471
                        String[] s=xml.getStringArrayProperty("LayerNames");
472
                        if (xml.getChild(1).getChild(i).getStringProperty("file") != null) {
473
                                try {
474
                                        // TODO cambiar el "nombre" por el nombre de la capa
475
                                        layer = LayerFactory.createLayer(s[i], xml.getChild(1).getChild(i)
476
                                                                                                                .getStringProperty("driverName"),
477
                                                        new File(xml.getChild(1).getChild(i)
478
                                                                                .getStringProperty("file")), proj);
479
                                } catch (DriverLoadException e1) {
480
                                        throw new XMLException(e1);
481
                                } catch (DriverIOException e1) {
482
                                        throw new XMLException(e1);
483
                                }
484
                        } else if (true) {
485
                        } else if (true) {
486
                        }
487

    
488
                        try {
489
                                layer.setXMLEntity(xml.getChild(1).getChild(i));
490
                        } catch (XMLException e) {
491
                                throw new XMLException(e);
492
                        }
493

    
494
                        try {
495
                                fmap.getLayers().addLayer(layer);
496
                        } catch (CancelationException e1) {
497
                                throw new XMLException(e1);
498
                        }
499
                }
500

    
501
                return fmap;
502
        }
503
        
504
/**
505
         * DOCUMENT ME!
506
         */
507
        public void fireLegendEvent() {
508
                LegendChangedEvent e = legendChanges.getEvent();
509

    
510
                if (e.getEvents().length == 0) {
511
                        return;
512
                }
513

    
514
                layerChangeSupport.callLegendChanged(e);
515
                legendChanges.clear();
516
        }
517

    
518
        /**
519
         * Invoca el evento invalid de todos los listeners de este FMap
520
         */
521
        public void fireInvalidEvent() {
522
                for (Iterator iter = invalidationListeners.iterator(); iter.hasNext();) {
523
                        InvalidationListener listener = (InvalidationListener) iter.next();
524

    
525
                        listener.invalid();
526
                }
527
        }
528

    
529
        /**
530
         * DOCUMENT ME!
531
         *
532
         * @param listener DOCUMENT ME!
533
         *
534
         * @return DOCUMENT ME!
535
         */
536
        public boolean addInvalidationListener(InvalidationListener listener) {
537
                return invalidationListeners.add(listener);
538
        }
539

    
540
        /**
541
         * DOCUMENT ME!
542
         *
543
         * @param listener DOCUMENT ME!
544
         *
545
         * @return DOCUMENT ME!
546
         */
547
        public boolean removeInvalidationListener(InvalidationListener listener) {
548
                return invalidationListeners.remove(listener);
549
        }
550

    
551
        /**
552
         * DOCUMENT ME!
553
         *
554
         * @author Fernando Gonz?lez Cort?s
555
         */
556
        public class LegendChanges implements LayerListener {
557
                private ArrayList events = new ArrayList();
558

    
559
                /**
560
                 * DOCUMENT ME!
561
                 *
562
                 * @return DOCUMENT ME!
563
                 */
564
                public LegendChangedEvent getEvent() {
565
                        LegendChangedEvent e = new LegendChangedEvent();
566
                        e.setEvents((LegendChangedEvent[]) events.toArray(
567
                                        new LegendChangedEvent[0]));
568

    
569
                        return e;
570
                }
571

    
572
                /**
573
                 * @see com.iver.cit.gvsig.fmap.layers.LayerListener#legendChanged(com.iver.cit.gvsig.fmap.rendering.LegendChangedEvent)
574
                 */
575
                public void legendChanged(LegendChangedEvent e) {
576
                        events.add(e);
577
                }
578

    
579
                /**
580
                 * DOCUMENT ME!
581
                 */
582
                public void clear() {
583
                        events.clear();
584
                }
585
        }
586

    
587
        /**
588
         * DOCUMENT ME!
589
         *
590
         * @author Fernando Gonz?lez Cort?s
591
         */
592
        public class LayerEventListener implements LayerCollectionListener {
593
                /**
594
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdded(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
595
                 */
596
                public void layerAdded(LayerCollectionEvent e) {
597
                        // Si es la primera capa, fijamos su extent al ViewPort
598
                        if (getLayers().getLayersCount() == 1) {
599
                                FLayer lyr = e.getAffectedLayer();
600

    
601
                                try {
602
                                        getViewPort().setExtent(lyr.getFullExtent());
603
                                } catch (DriverIOException e1) {
604
                                }
605
                        }
606

    
607
                        //Registramos al FMap como listener del legend de las capas
608
                        FLayer lyr = e.getAffectedLayer();
609

    
610
                        if (lyr instanceof CommonOperations) {
611
                                CommonOperations c = (CommonOperations) lyr;
612
                                c.addLayerListener(legendChanges);
613
                        }
614
                }
615

    
616
                /**
617
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoved(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
618
                 */
619
                public void layerMoved(LayerPositionEvent e) {
620
                }
621

    
622
                /**
623
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoved(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
624
                 */
625
                public void layerRemoved(LayerCollectionEvent e) {
626
                        FLayer lyr = e.getAffectedLayer();
627

    
628
                        if (lyr instanceof CommonOperations) {
629
                                CommonOperations c = (CommonOperations) lyr;
630
                                c.removeLayerListener(legendChanges);
631
                        }
632
                }
633

    
634
                /**
635
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerAdding(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
636
                 */
637
                public void layerAdding(LayerCollectionEvent e)
638
                        throws CancelationException {
639
                }
640

    
641
                /**
642
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerMoving(com.iver.cit.gvsig.fmap.layers.LayerPositionEvent)
643
                 */
644
                public void layerMoving(LayerPositionEvent e)
645
                        throws CancelationException {
646
                }
647

    
648
                /**
649
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#layerRemoving(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
650
                 */
651
                public void layerRemoving(LayerCollectionEvent e)
652
                        throws CancelationException {
653
                }
654

    
655
                /**
656
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#activationChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
657
                 */
658
                public void activationChanged(LayerCollectionEvent e)
659
                        throws CancelationException {
660
                }
661

    
662
                /**
663
                 * @see com.iver.cit.gvsig.fmap.layers.LayerCollectionListener#visibilityChanged(com.iver.cit.gvsig.fmap.layers.LayerCollectionEvent)
664
                 */
665
                public void visibilityChanged(LayerCollectionEvent e)
666
                        throws CancelationException {
667
                }
668
        }
669
}