Statistics
| Revision:

root / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / layers / Annotation_Layer.java @ 12529

History | View | Annotate | Download (18.6 KB)

1

    
2
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
3
 *
4
 * Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
19
 *
20
 * For more information, contact:
21
 *
22
 *  Generalitat Valenciana
23
 *   Conselleria d'Infraestructures i Transport
24
 *   Av. Blasco Ib??ez, 50
25
 *   46010 VALENCIA
26
 *   SPAIN
27
 *
28
 *      +34 963862235
29
 *   gvsig@gva.es
30
 *      www.gvsig.gva.es
31
 *
32
 *    or
33
 *
34
 *   IVER T.I. S.A
35
 *   Salamanca 50
36
 *   46005 Valencia
37
 *   Spain
38
 *
39
 *   +34 963163400
40
 *   dac@iver.es
41
 */
42

    
43
package com.iver.cit.gvsig.fmap.layers;
44

    
45
import java.awt.Color;
46
import java.awt.Font;
47
import java.awt.Graphics2D;
48
import java.awt.Shape;
49
import java.awt.font.FontRenderContext;
50
import java.awt.font.GlyphVector;
51
import java.awt.geom.AffineTransform;
52
import java.awt.geom.Point2D;
53
import java.awt.geom.Rectangle2D;
54
import java.awt.image.BufferedImage;
55
import java.io.File;
56
import java.io.IOException;
57

    
58
import javax.print.attribute.PrintRequestAttributeSet;
59

    
60
import org.cresques.cts.ICoordTrans;
61
import org.cresques.cts.IProjection;
62

    
63
import com.hardcode.driverManager.Driver;
64
import com.hardcode.driverManager.DriverLoadException;
65
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
66
import com.hardcode.gdbms.engine.values.Value;
67
import com.hardcode.gdbms.engine.values.ValueFactory;
68
import com.iver.cit.gvsig.fmap.DriverException;
69
import com.iver.cit.gvsig.fmap.ViewPort;
70
import com.iver.cit.gvsig.fmap.core.FPoint2D;
71
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
72
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
73
import com.iver.cit.gvsig.fmap.core.IGeometry;
74
import com.iver.cit.gvsig.fmap.core.ISymbol;
75
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
76
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
77
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
78
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
79
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
80
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
81
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
82
import com.iver.cit.gvsig.fmap.edition.Annotation_EditableAdapter;
83
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
84
import com.iver.cit.gvsig.fmap.edition.EditionException;
85
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
86
import com.iver.cit.gvsig.fmap.operation.strategies.Annotation_Strategy;
87
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
88
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
89
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
90
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
91
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
92
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
93
import com.iver.utiles.XMLEntity;
94
import com.iver.utiles.swing.threads.Cancellable;
95

    
96

    
97
/**
98
 * Annotation's layer.
99
 *
100
 * @author Vicente Caballero Navarro
101
 */
102
public class Annotation_Layer extends FLyrVect {
103
    private Annotation_Mapping mapping = null;
104
    private int indexEditing = -1;
105
    private Annotation_Legend aLegend;
106
    private Strategy strategy = null;
107
    private final static AffineTransform ati=new AffineTransform();
108
    /**
109
     * Crea un nuevo FLyrAnnotation.
110
     */
111
    public Annotation_Layer() {
112
        super();
113
    }
114

    
115
    /**
116
     * DOCUMENT ME!
117
     *
118
     * @param mapping DOCUMENT ME!
119
     * @throws DriverException
120
     * @throws FieldNotFoundException
121
     */
122
    public void setMapping(Annotation_Mapping mapping) throws FieldNotFoundException, DriverException {
123
        this.mapping = mapping;
124
        aLegend = new Annotation_Legend();
125
        setLegend();
126
    }
127

    
128
    /**
129
     * DOCUMENT ME!
130
     *
131
     * @return DOCUMENT ME!
132
     */
133
    public Annotation_Mapping getAnnotatonMapping() {
134
        return mapping;
135
    }
136

    
137
    /**
138
     * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#draw(java.awt.image.BufferedImage,
139
     *      java.awt.Graphics2D, ISymbol)
140
     */
141
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
142
        Cancellable cancel, double scale) throws DriverException {
143
        if (isWithinScale(scale)) {
144
            // Las que solo tienen etiquetado sin pintar el shape,
145
            // no pasamos por ellas
146
            boolean bDrawShapes = true;
147

    
148
            if (bDrawShapes) {
149
                if (strategy == null) {
150
                    strategy = new Annotation_Strategy(this);
151
                }
152

    
153
                try {
154
                    g.setColor(Color.black);
155

    
156
                    ReadableVectorial adapter = getSource();
157
                    adapter.start();
158

    
159
                    strategy.draw(image, g, viewPort, cancel);
160
                    adapter.stop();
161
                } catch (DriverException e) {
162
                    this.setVisible(false);
163
                    this.setActive(false);
164
                    throw e;
165
                } catch (DriverIOException e) {
166
                    this.setVisible(false);
167
                    this.setActive(false);
168
                    throw new DriverException(e);
169
                }
170
            }
171

    
172
            if (getVirtualLayers() != null) {
173
                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
174
            }
175
        }
176
    }
177

    
178
    /**
179
     * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
180
     */
181
    public Rectangle2D getFullExtent() throws DriverException {
182
        try {
183
            Rectangle2D rAux;
184
            getSource().start();
185
            rAux = getSource().getFullExtent();
186
            getSource().stop();
187

    
188
            // Si existe reproyecci?n, reproyectar el extent
189
            ICoordTrans ct = getCoordTrans();
190

    
191
            if (ct != null) {
192
                Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
193
                Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
194
                pt1 = ct.convert(pt1, null);
195
                pt2 = ct.convert(pt2, null);
196
                rAux = new Rectangle2D.Double();
197
                rAux.setFrameFromDiagonal(pt1, pt2);
198
            }
199

    
200
            return rAux;
201
        } catch (DriverIOException e) {
202
            throw new DriverException(e);
203
        }
204
    }
205

    
206
    /**
207
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
208
     *      com.iver.cit.gvsig.fmap.ViewPort,
209
     *      com.iver.utiles.swing.threads.Cancellable)
210
     */
211
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
212
        double scale, PrintRequestAttributeSet properties)
213
        throws DriverException {
214
        if (isVisible() && isWithinScale(scale)) {
215
            Strategy strategy = StrategyManager.getStrategy(this);
216
            strategy.print(g, viewPort, cancel, properties);
217
        }
218
    }
219

    
220
    /*
221
     * (non-Javadoc)
222
     *
223
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
224
     */
225
    public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
226
        Strategy s = StrategyManager.getStrategy(this);
227

    
228
        return s.queryByRect(rect);
229
    }
230

    
231
    /**
232
     * DOCUMENT ME!
233
     *
234
     * @param p DOCUMENT ME!
235
     * @param tolerance DOCUMENT ME!
236
     *
237
     * @return DOCUMENT ME!
238
     *
239
     * @throws DriverException DOCUMENT ME!
240
     */
241
    public FBitSet queryByPoint(Point2D p, double tolerance)
242
        throws DriverException {
243
        Strategy s = StrategyManager.getStrategy(this);
244

    
245
        return s.queryByPoint(p, tolerance);
246
    }
247

    
248
    /**
249
     * DOCUMENT ME!
250
     *
251
     * @param g DOCUMENT ME!
252
     * @param relationship DOCUMENT ME!
253
     *
254
     * @return DOCUMENT ME!
255
     *
256
     * @throws DriverException DOCUMENT ME!
257
     * @throws VisitException DOCUMENT ME!
258
     */
259
    public FBitSet queryByShape(IGeometry g, int relationship)
260
        throws DriverException, VisitException {
261
        Strategy s = StrategyManager.getStrategy(this);
262

    
263
        return s.queryByShape(g, relationship);
264
    }
265

    
266
    /**
267
     * DOCUMENT ME!
268
     *
269
     * @return DOCUMENT ME!
270
     *
271
     * @throws XMLException
272
     *
273
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#getProperties()
274
     */
275
    public XMLEntity getXMLEntity() throws XMLException {
276
        XMLEntity xml = super.getXMLEntity();
277
        return xml;
278
    }
279

    
280
    /**
281
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
282
     */
283
    public void setXMLEntity(XMLEntity xml) throws XMLException {
284
        IProjection proj = null;
285

    
286
        if (xml.contains("proj")) {
287
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
288
        }
289

    
290
        if (xml.contains("file")) {
291
            Driver d;
292

    
293
            try {
294
                d = LayerFactory.getDM().getDriver(xml.getStringProperty(
295
                            "driverName"));
296
            } catch (DriverLoadException e1) {
297
                throw new XMLException(e1);
298
            }
299

    
300
            FLyrVect lv = (FLyrVect) LayerFactory.createLayer(xml.getName(),
301
                    (VectorialFileDriver) d,
302
                    new File(xml.getStringProperty("file")), proj);
303

    
304
            try {
305
                this.setSource(lv.getSource());
306
                this.setRecordset(lv.getRecordset());
307
                this.setProjection(lv.getProjection());
308
                this.setLegend((VectorialLegend) lv.getLegend());
309
                Annotation_Mapping.addAnnotationMapping(this);
310
            } catch (DriverException e) {
311
                throw new XMLException(e);
312
            } catch (FieldNotFoundException e) {
313
                    throw new XMLException(e);
314
            } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
315
                    throw new XMLException(e);
316
            }
317
        }
318
        super.setXMLEntity(xml);
319
    }
320

    
321
    public Value getSymbolKey(int i)
322
        throws com.hardcode.gdbms.engine.data.driver.DriverException,
323
            DriverException {
324
        SelectableDataSource ds = getRecordset();
325
        String t = new String();
326
        Value val = ds.getFieldValue(i, mapping.getColumnText());
327
        t = val.toString();
328

    
329
        if (mapping.getColumnColor() != -1) {
330
            Value valColor = ds.getFieldValue(i, mapping.getColumnColor());
331
            t = t.concat(valColor.toString());
332
        }
333

    
334
        if (mapping.getColumnTypeFont() != -1) {
335
            Value valTypeFont = ds.getFieldValue(i, mapping.getColumnTypeFont());
336
            t = t.concat(valTypeFont.toString());
337
        }
338

    
339
        if (mapping.getColumnStyleFont() != -1) {
340
            Value valStyleFont = ds.getFieldValue(i,
341
                    mapping.getColumnStyleFont());
342
            t = t.concat(valStyleFont.toString());
343
        }
344

    
345
        Value total = ValueFactory.createValue(t);
346

    
347
        return total;
348
    }
349

    
350
    /*
351
     * (non-Javadoc)
352
     *
353
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#createIndex()
354
     */
355
    public void createSpatialIndex() {
356
        // FJP: ESTO HABR? QUE CAMBIARLO. PARA LAS CAPAS SECUENCIALES, TENDREMOS
357
        // QUE ACCEDER CON UN WHILE NEXT. (O mejorar lo de los FeatureVisitor
358
        // para que acepten recorrer sin geometria, solo con rectangulos.
359
        // AZABALA: Como no tengo claro de donde se crean las capas de textos
360
        // el ?ndice espacial creado seguir? siendo el Quadtree en memoria
361
        // de JTS (QuadtreeJts es un adapter a nuestra api de indices)
362
        spatialIndex = new QuadtreeJts();
363

    
364
        ReadableVectorial va = getSource();
365
        ICoordTrans ct = getCoordTrans();
366
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
367

    
368
        try {
369
            va.start();
370

    
371
            for (int i = 0; i < va.getShapeCount(); i++) {
372
                Rectangle2D r = null;
373
                IGeometry geom = va.getShape(i);
374
                 if (geom != null) {
375
                    r = geom.getBounds2D();
376
                } else {
377
                    r = shapeBounds.getShapeBounds(i);
378
                }
379

    
380
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
381
                if (ct != null) {
382
                    r = ct.convert(r);
383
                }
384

    
385
                if (r != null) {
386
                    spatialIndex.insert(r, i);
387
                }
388
            } // for
389

    
390
            va.stop();
391
        } catch (DriverIOException e) {
392
            e.printStackTrace();
393
        } catch (IOException e) {
394
            e.printStackTrace();
395
        }
396
    }
397

    
398
    /**
399
     * DOCUMENT ME!
400
     *
401
     * @throws DriverException DOCUMENT ME!
402
     */
403
    public void setSelectedEditing() throws DriverException {
404
        FBitSet bitSet = getRecordset().getSelection();
405

    
406
        if (bitSet.cardinality() == 0) {
407
            return;
408
        }
409

    
410
        indexEditing = bitSet.nextSetBit(0);
411
    }
412

    
413
    public void setInEdition(int i) {
414
        indexEditing = i;
415
    }
416

    
417
    /**
418
     * DOCUMENT ME!
419
     *
420
     * @return DOCUMENT ME!
421
     */
422
    public int getInEdition() {
423
        return indexEditing;
424
    }
425

    
426
    public void setLegend() throws DriverException,
427
        FieldNotFoundException {
428
        try {
429
            getSource().getRecordset().start();
430
            aLegend.setFieldName(getSource().getRecordset().getFieldName(mapping.getColumnText()));
431
            setLegend(aLegend);
432
            getSource().getRecordset().stop();
433
        } catch (DriverLoadException e) {
434
                throw new DriverException(e);
435
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
436
                throw new DriverException(e);
437
        }
438
    }
439

    
440
    /**
441
     * DOCUMENT ME!
442
     *
443
     * @return DOCUMENT ME!
444
     */
445
    public Strategy getStrategy() {
446
        return strategy;
447
    }
448

    
449
    /**
450
     * DOCUMENT ME!
451
     *
452
     * @param b DOCUMENT ME!
453
     *
454
     * @throws EditionException DOCUMENT ME!
455
     */
456
    public void setEditing(boolean b) throws EditionException {
457
       super.setEditing(b);
458

    
459
        try {
460
            if (b) {
461
                Annotation_EditableAdapter aea = new Annotation_EditableAdapter(this);
462
                aea.setOriginalVectorialAdapter(((VectorialEditableAdapter) super.getSource()).getOriginalAdapter());
463
                ((VectorialEditableAdapter) super.getSource()).cancelEdition(EditionEvent.GRAPHIC);
464
                aea.start();
465

    
466
                // CHEMA
467
                aea.startEdition(EditionEvent.GRAPHIC);
468
                setSource(aea);
469
                getRecordset().setSelectionSupport(aea.getOriginalAdapter()
470
                                                      .getRecordset()
471
                                                      .getSelectionSupport());
472
            } else {
473
            }
474
        } catch (DriverIOException e) {
475
            throw new EditionException();
476
        } catch (DriverLoadException e) {
477
            throw new EditionException();
478
        } catch (DriverException e) {
479
            throw new EditionException();
480
        } catch (IOException e) {
481
            throw new EditionException();
482
        }
483

    
484
        deleteSpatialIndex();
485
    }
486

    
487
    /**
488
     * DOCUMENT ME!
489
     *
490
     * @param layer DOCUMENT ME!
491
     *
492
     * @return DOCUMENT ME!
493
     *
494
     * @throws DriverException DOCUMENT ME!
495
     * @throws FieldNotFoundException DOCUMENT ME!
496
     */
497
    public static Annotation_Layer createLayerFromVect(FLyrVect layer)
498
        throws DriverException, FieldNotFoundException {
499
        Annotation_Layer la = new Annotation_Layer();
500
        FLyrVect lv=(FLyrVect)LayerFactory.createLayer(layer.getName(),layer.getSource().getDriver(),layer.getProjection());
501
        la.setSource(lv.getSource());
502
        la.setRecordset(lv.getRecordset());
503
        la.setProjection(layer.getProjection());
504
        la.getRecordset().setSelection((FBitSet)layer.getRecordset().getSelection());
505

    
506
        return la;
507
    }
508

    
509
    /**
510
     * DOCUMENT ME!
511
     */
512
    public void removingThisLayer() {
513
        super.removingThisLayer();
514
        spatialIndex = null;
515
        aLegend = null;
516
        strategy = null;
517
        System.gc();
518
    }
519
    public IGeometry getTextWrappingGeometryInPixels(double height, String description,
520
            double rotation, String type, int style, int numReg,ViewPort vp, IGeometry geom) throws DriverIOException {
521
            ICoordTrans ct = getCoordTrans();
522

    
523
            boolean bMustClone = false;
524

    
525
            if (geom == null) {
526
                return null;
527
            }
528

    
529
            if (ct != null) {
530
                if (bMustClone) {
531
                    geom = geom.cloneGeometry();
532
                }
533

    
534
                geom.reProject(ct);
535
            }
536
            FPoint2D p =(FPoint2D)geom.getInternalShape();
537
//            if (geom.getInternalShape() instanceof FPolygon2D){
538
//                    r=new Rectangle2D.Double(r.getX()-r.getWidth()*0.5,r.getY()-r.getHeight()*0.5,r.getWidth(),r.getHeight());
539
//            }
540
//            p=new Point2D.Double(r.getCenterX(),r.getCenterY());
541

    
542
            if (!((FSymbol) getLegend().getDefaultSymbol()).isFontSizeInPixels()) {
543
                height = vp.fromMapDistance((int) (height));
544
            }
545

    
546
            Font font = new Font(type, style,
547
                    (int) (height));
548
            FontRenderContext frc = new FontRenderContext(ati,
549
                    false, true);
550
            GlyphVector gv = font.createGlyphVector(frc, description);
551
            Shape shape = gv.getOutline((float) p.getX(), (float) (p.getY()));// - height));
552
            Rectangle2D rGeom=shape.getBounds2D();
553
            IGeometry geomResult = ShapeFactory.createPolygon2D(new GeneralPathX(
554
                            rGeom));
555

    
556
            if (rotation != 0) {
557
                    AffineTransform Tx = AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY());
558
                        geomResult.transform(Tx);
559
//                geomResult.transform(AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY()));
560
//                geomResult.rotate(Math.toRadians(rotation), p.getX(),
561
//                        p.getY());
562
            }
563

    
564
            return geomResult;
565
        }
566
    public FLayer cloneLayer() throws Exception {
567
                Annotation_Layer clonedLayer = new Annotation_Layer();
568
                clonedLayer.setSource(getSource());
569
                clonedLayer.setVisible(isVisible());
570
                clonedLayer.setISpatialIndex(getISpatialIndex());
571
                clonedLayer.setName(getName());
572
                clonedLayer.setCoordTrans(getCoordTrans());
573
                clonedLayer.setLegend((VectorialLegend)getLegend());
574
                clonedLayer.mapping=mapping;
575
                clonedLayer.aLegend=aLegend;
576
                return clonedLayer;
577
        }
578

    
579
}