Statistics
| Revision:

root / trunk / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / layers / Annotation_Layer.java @ 13881

History | View | Annotate | Download (18.3 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

    
57
import javax.print.attribute.PrintRequestAttributeSet;
58

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

    
62
import com.hardcode.driverManager.Driver;
63
import com.hardcode.driverManager.DriverLoadException;
64
import com.hardcode.gdbms.driver.exceptions.InitializeDriverException;
65
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
66
import com.hardcode.gdbms.engine.data.driver.DriverException;
67
import com.hardcode.gdbms.engine.instruction.FieldNotFoundException;
68
import com.hardcode.gdbms.engine.values.Value;
69
import com.hardcode.gdbms.engine.values.ValueFactory;
70
import com.iver.andami.messages.NotificationManager;
71
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
72
import com.iver.cit.gvsig.exceptions.layers.CancelEditingLayerException;
73
import com.iver.cit.gvsig.exceptions.layers.LegendLayerException;
74
import com.iver.cit.gvsig.exceptions.layers.StartEditionLayerException;
75
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException;
76
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
77
import com.iver.cit.gvsig.fmap.ViewPort;
78
import com.iver.cit.gvsig.fmap.core.FPoint2D;
79
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
80
import com.iver.cit.gvsig.fmap.core.IGeometry;
81
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
82
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
83
import com.iver.cit.gvsig.fmap.crs.CRSFactory;
84
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
85
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
86
import com.iver.cit.gvsig.fmap.edition.Annotation_EditableAdapter;
87
import com.iver.cit.gvsig.fmap.edition.EditionEvent;
88
import com.iver.cit.gvsig.fmap.edition.VectorialEditableAdapter;
89
import com.iver.cit.gvsig.fmap.operation.strategies.Annotation_Strategy;
90
import com.iver.cit.gvsig.fmap.operations.strategies.Strategy;
91
import com.iver.cit.gvsig.fmap.operations.strategies.StrategyManager;
92
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
93
import com.iver.cit.gvsig.fmap.rendering.IVectorLegend;
94
import com.iver.cit.gvsig.fmap.spatialindex.QuadtreeJts;
95
import com.iver.utiles.XMLEntity;
96
import com.iver.utiles.swing.threads.Cancellable;
97

    
98

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

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

    
132
    /**
133
     * DOCUMENT ME!
134
     *
135
     * @return DOCUMENT ME!
136
     */
137
    public Annotation_Mapping getAnnotatonMapping() {
138
        return mapping;
139
    }
140

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

    
154
            if (bDrawShapes) {
155
                if (strategy == null) {
156
                    strategy = new Annotation_Strategy(this);
157
                }
158
                g.setColor(Color.black);
159
                ReadableVectorial adapter = getSource();
160
                adapter.start();
161
                strategy.draw(image, g, viewPort, cancel);
162
                adapter.stop();
163
            }
164

    
165
            if (getVirtualLayers() != null) {
166
                getVirtualLayers().draw(image, g, viewPort, cancel, scale);
167
            }
168
        }
169
    }
170

    
171
    /**
172
     * @throws ReadDriverException
173
     * @throws InitializeDriverException
174
     * @throws ExpansionFileReadException
175
     * @see com.iver.cit.gvsig.fmap.layers.LayerOperations#getFullExtent()
176
     */
177
    public Rectangle2D getFullExtent() throws InitializeDriverException,
178
                        ReadDriverException, ExpansionFileReadException {
179
                Rectangle2D rAux;
180
                getSource().start();
181
                rAux = getSource().getFullExtent();
182
                getSource().stop();
183

    
184
                // Si existe reproyecci?n, reproyectar el extent
185
                ICoordTrans ct = getCoordTrans();
186

    
187
                if (ct != null) {
188
                        Point2D pt1 = new Point2D.Double(rAux.getMinX(), rAux.getMinY());
189
                        Point2D pt2 = new Point2D.Double(rAux.getMaxX(), rAux.getMaxY());
190
                        pt1 = ct.convert(pt1, null);
191
                        pt2 = ct.convert(pt2, null);
192
                        rAux = new Rectangle2D.Double();
193
                        rAux.setFrameFromDiagonal(pt1, pt2);
194
                }
195

    
196
                return rAux;
197
        }
198

    
199
    /**
200
         * @throws ReadDriverException
201
     * @see com.iver.cit.gvsig.fmap.layers.FLayer#print(java.awt.Graphics2D,
202
         *      com.iver.cit.gvsig.fmap.ViewPort,
203
         *      com.iver.utiles.swing.threads.Cancellable)
204
         */
205
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
206
        double scale, PrintRequestAttributeSet properties) throws ReadDriverException{
207
        if (isVisible() && isWithinScale(scale)) {
208
            Strategy strategy = StrategyManager.getStrategy(this);
209
            strategy.print(g, viewPort, cancel, properties);
210
        }
211
    }
212

    
213
    /*
214
     * (non-Javadoc)
215
     *
216
     * @see com.iver.cit.gvsig.fmap.layers.layerOperations.RandomVectorialData#queryByRect(java.awt.geom.Rectangle2D)
217
     */
218
    public FBitSet queryByRect(Rectangle2D rect) throws ReadDriverException, VisitorException {
219
        Strategy s = StrategyManager.getStrategy(this);
220

    
221
        return s.queryByRect(rect);
222
    }
223

    
224
    /**
225
     * DOCUMENT ME!
226
     *
227
     * @param p DOCUMENT ME!
228
     * @param tolerance DOCUMENT ME!
229
     *
230
     * @return DOCUMENT ME!
231
     * @throws VisitorException
232
     * @throws ReadDriverException
233
     *
234
     * @throws DriverException DOCUMENT ME!
235
     */
236
    public FBitSet queryByPoint(Point2D p, double tolerance) throws ReadDriverException, VisitorException {
237
        Strategy s = StrategyManager.getStrategy(this);
238

    
239
        return s.queryByPoint(p, tolerance);
240
    }
241

    
242
    /**
243
     * DOCUMENT ME!
244
     *
245
     * @param g DOCUMENT ME!
246
     * @param relationship DOCUMENT ME!
247
     *
248
     * @return DOCUMENT ME!
249
     * @throws VisitorException
250
     * @throws ReadDriverException
251
     *
252
     * @throws DriverException DOCUMENT ME!
253
     * @throws VisitException DOCUMENT ME!
254
     */
255
    public FBitSet queryByShape(IGeometry g, int relationship) throws ReadDriverException, VisitorException {
256
        Strategy s = StrategyManager.getStrategy(this);
257

    
258
        return s.queryByShape(g, relationship);
259
    }
260

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

    
275
    /**
276
     * @see com.iver.cit.gvsig.fmap.layers.FLyrDefault#setXMLEntity(com.iver.utiles.XMLEntity)
277
     */
278
    public void setXMLEntity(XMLEntity xml) throws XMLException {
279
        IProjection proj = null;
280

    
281
        if (xml.contains("proj")) {
282
            proj = CRSFactory.getCRS(xml.getStringProperty("proj"));
283
        }
284

    
285
        if (xml.contains("file")) {
286
            Driver d;
287

    
288
            try {
289
                d = LayerFactory.getDM().getDriver(xml.getStringProperty(
290
                            "driverName"));
291
            } catch (DriverLoadException e1) {
292
                throw new XMLException(e1);
293
            }
294

    
295
            FLyrVect lv = (FLyrVect) LayerFactory.createLayer(xml.getName(),
296
                    (VectorialFileDriver) d,
297
                    new File(xml.getStringProperty("file")), proj);
298

    
299
            try {
300
                this.setSource(lv.getSource());
301
                this.setRecordset(lv.getRecordset());
302
                this.setProjection(lv.getProjection());
303
                this.setLegend((IVectorLegend) lv.getLegend());
304
                Annotation_Mapping.addAnnotationMapping(this);
305
            } catch (ReadDriverException e) {
306
                    throw new XMLException(e);
307
                        } catch (LegendLayerException e) {
308
                                throw new XMLException(e);
309
                        }
310
        }
311
        super.setXMLEntity(xml);
312
    }
313

    
314
    public Value getSymbolKey(int i) throws ReadDriverException {
315
        SelectableDataSource ds = getRecordset();
316
        String t = new String();
317
        Value val = ds.getFieldValue(i, mapping.getColumnText());
318
        t = val.toString();
319

    
320
        if (mapping.getColumnColor() != -1) {
321
            Value valColor = ds.getFieldValue(i, mapping.getColumnColor());
322
            t = t.concat(valColor.toString());
323
        }
324

    
325
        if (mapping.getColumnTypeFont() != -1) {
326
            Value valTypeFont = ds.getFieldValue(i, mapping.getColumnTypeFont());
327
            t = t.concat(valTypeFont.toString());
328
        }
329

    
330
        if (mapping.getColumnStyleFont() != -1) {
331
            Value valStyleFont = ds.getFieldValue(i,
332
                    mapping.getColumnStyleFont());
333
            t = t.concat(valStyleFont.toString());
334
        }
335

    
336
        Value total = ValueFactory.createValue(t);
337

    
338
        return total;
339
    }
340

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

    
355
        ReadableVectorial va = getSource();
356
        ICoordTrans ct = getCoordTrans();
357
        BoundedShapes shapeBounds = (BoundedShapes) va.getDriver();
358

    
359
        try {
360
            va.start();
361

    
362
            for (int i = 0; i < va.getShapeCount(); i++) {
363
                Rectangle2D r = null;
364
                IGeometry geom = va.getShape(i);
365
                 if (geom != null) {
366
                    r = geom.getBounds2D();
367
                } else {
368
                    r = shapeBounds.getShapeBounds(i);
369
                }
370

    
371
                // TODO: MIRAR COMO SE TRAGAR?A ESTO LO DE LAS REPROYECCIONES
372
                if (ct != null) {
373
                    r = ct.convert(r);
374
                }
375

    
376
                if (r != null) {
377
                    spatialIndex.insert(r, i);
378
                }
379
            } // for
380

    
381
            va.stop();
382
        } catch (ExpansionFileReadException e) {
383
                        NotificationManager.addError(this.getName(),e);
384
                } catch (ReadDriverException e) {
385
                        NotificationManager.addError(this.getName(),e);
386
                }
387
    }
388

    
389
    /**
390
     * DOCUMENT ME!
391
     * @throws ReadDriverException
392
     *
393
     * @throws DriverException DOCUMENT ME!
394
     */
395
    public void setSelectedEditing() throws ReadDriverException {
396
        FBitSet bitSet = getRecordset().getSelection();
397

    
398
        if (bitSet.cardinality() == 0) {
399
            return;
400
        }
401

    
402
        indexEditing = bitSet.nextSetBit(0);
403
    }
404

    
405
    public void setInEdition(int i) {
406
        indexEditing = i;
407
    }
408

    
409
    /**
410
     * DOCUMENT ME!
411
     *
412
     * @return DOCUMENT ME!
413
     */
414
    public int getInEdition() {
415
        return indexEditing;
416
    }
417

    
418
    public void setLegend() throws ReadDriverException, LegendLayerException {
419
        getSource().getRecordset().start();
420
        aLegend.setFieldName(getSource().getRecordset().getFieldName(mapping.getColumnText()));
421
        setLegend(aLegend);
422
        getSource().getRecordset().stop();
423
    }
424

    
425
    /**
426
     * DOCUMENT ME!
427
     *
428
     * @return DOCUMENT ME!
429
     */
430
    public Strategy getStrategy() {
431
        return strategy;
432
    }
433

    
434
    /**
435
     * DOCUMENT ME!
436
     *
437
     * @param b DOCUMENT ME!
438
     * @throws StartEditionLayerException
439
     *
440
     * @throws EditionException DOCUMENT ME!
441
     */
442
    public void setEditing(boolean b) throws StartEditionLayerException {
443
       super.setEditing(b);
444
       try {
445
            if (b) {
446
                Annotation_EditableAdapter aea = new Annotation_EditableAdapter(this);
447

    
448
                                        aea.setOriginalVectorialAdapter(((VectorialEditableAdapter) super.getSource()).getOriginalAdapter());
449

    
450
                ((VectorialEditableAdapter) super.getSource()).cancelEdition(EditionEvent.GRAPHIC);
451
                aea.start();
452

    
453
                aea.setCoordTrans(getCoordTrans());
454
                // CHEMA
455
                aea.startEdition(EditionEvent.GRAPHIC);
456
                setSource(aea);
457
                getRecordset().setSelectionSupport(aea.getOriginalAdapter()
458
                                                      .getRecordset()
459
                                                      .getSelectionSupport());
460
            } else {
461
            }
462

    
463
               } catch (ReadDriverException e) {
464
                        throw new StartEditionLayerException(this.getName(),e);
465
                } catch (CancelEditingLayerException e) {
466
                        throw new StartEditionLayerException(this.getName(),e);
467
                } catch (StartWriterVisitorException e) {
468
                        throw new StartEditionLayerException(this.getName(),e);
469
                }
470
        deleteSpatialIndex();
471
    }
472

    
473
    /**
474
     * DOCUMENT ME!
475
     *
476
     * @param layer DOCUMENT ME!
477
     *
478
     * @return DOCUMENT ME!
479
     * @throws ReadDriverException
480
     *
481
     * @throws DriverException DOCUMENT ME!
482
     * @throws FieldNotFoundException DOCUMENT ME!
483
     */
484
    public static Annotation_Layer createLayerFromVect(FLyrVect layer) throws ReadDriverException {
485
        Annotation_Layer la = new Annotation_Layer();
486
        FLyrVect lv=(FLyrVect)LayerFactory.createLayer(layer.getName(),layer.getSource().getDriver(),layer.getProjection());
487
        la.setSource(lv.getSource());
488
        la.setRecordset(lv.getRecordset());
489
        la.setProjection(layer.getProjection());
490
        la.getRecordset().setSelection(layer.getRecordset().getSelection());
491

    
492
        return la;
493
    }
494

    
495
    /**
496
     * DOCUMENT ME!
497
     */
498
    public void removingThisLayer() {
499
//        super.removingThisLayer();
500
        spatialIndex = null;
501
        aLegend = null;
502
        strategy = null;
503
        System.gc();
504
    }
505
    public IGeometry getTextWrappingGeometryInPixels(double height, String description,
506
            double rotation, String type, int style, int numReg,ViewPort vp, IGeometry geom) {
507

    
508
            Shape shapeP=geom.getInternalShape();
509
            FPoint2D p = null;
510
            if (!(shapeP instanceof FPoint2D)) {
511
                    Rectangle2D rP=shapeP.getBounds2D();
512
                    p=new FPoint2D(rP.getX(),rP.getMaxY());
513
            }else {
514
                    p=(FPoint2D)shapeP;
515
            }
516
//            if (geom.getInternalShape() instanceof FPolygon2D){
517
//                    r=new Rectangle2D.Double(r.getX()-r.getWidth()*0.5,r.getY()-r.getHeight()*0.5,r.getWidth(),r.getHeight());
518
//            }
519
//            p=new Point2D.Double(r.getCenterX(),r.getCenterY());
520

    
521
            if (!((Annotation_Legend) getLegend()).isFontSizeInPixels()) {
522
                height = vp.fromMapDistance((int) (height));
523
            }
524

    
525
            Font font = new Font(type, style,
526
                    (int) (height));
527
            FontRenderContext frc = new FontRenderContext(ati,
528
                    false, true);
529
            GlyphVector gv = font.createGlyphVector(frc, description);
530
            Shape shape = gv.getOutline((float) p.getX(), (float) (p.getY()));// - height));
531
            Rectangle2D rGeom=shape.getBounds2D();
532
            IGeometry geomResult = ShapeFactory.createPolygon2D(new GeneralPathX(
533
                            rGeom));
534

    
535
            if (rotation != 0) {
536
                    AffineTransform Tx = AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY());
537
                        geomResult.transform(Tx);
538
//                geomResult.transform(AffineTransform.getRotateInstance(Math.toRadians(-rotation),p.getX(),p.getY()));
539
//                geomResult.rotate(Math.toRadians(rotation), p.getX(),
540
//                        p.getY());
541
            }
542

    
543
            return geomResult;
544
        }
545
    public FLayer cloneLayer() throws Exception {
546
                Annotation_Layer clonedLayer = new Annotation_Layer();
547
                clonedLayer.setSource(getSource());
548
                clonedLayer.setVisible(isVisible());
549
                clonedLayer.setISpatialIndex(getISpatialIndex());
550
                clonedLayer.setName(getName());
551
                clonedLayer.setCoordTrans(getCoordTrans());
552
                clonedLayer.setLegend((IVectorLegend)getLegend());
553
                clonedLayer.mapping=mapping;
554
                clonedLayer.aLegend=aLegend;
555
                return clonedLayer;
556
        }
557

    
558
}