Statistics
| Revision:

svn-gvsig-desktop / branches / v10 / extensions / extAnnotations / src / com / iver / cit / gvsig / fmap / operation / strategies / Annotation_Strategy.java @ 11756

History | View | Annotate | Download (29 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.operation.strategies;
44

    
45
import java.awt.Color;
46
import java.awt.Font;
47
import java.awt.FontMetrics;
48
import java.awt.Graphics2D;
49
import java.awt.geom.Point2D;
50
import java.awt.geom.Rectangle2D;
51
import java.awt.image.BufferedImage;
52
import java.util.List;
53

    
54
import javax.print.attribute.PrintRequestAttributeSet;
55

    
56
import org.cresques.cts.ICoordTrans;
57

    
58
import com.hardcode.driverManager.DriverLoadException;
59
import com.hardcode.gdbms.engine.values.NullValue;
60
import com.hardcode.gdbms.engine.values.NumericValue;
61
import com.hardcode.gdbms.engine.values.StringValue;
62
import com.hardcode.gdbms.engine.values.Value;
63
import com.iver.cit.gvsig.fmap.DriverException;
64
import com.iver.cit.gvsig.fmap.ViewPort;
65
import com.iver.cit.gvsig.fmap.core.FPoint2D;
66
import com.iver.cit.gvsig.fmap.core.FShape;
67
import com.iver.cit.gvsig.fmap.core.IGeometry;
68
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
69
import com.iver.cit.gvsig.fmap.core.v02.FGraphicUtilities;
70
import com.iver.cit.gvsig.fmap.core.v02.FLabel;
71
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
72
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
73
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
74
import com.iver.cit.gvsig.fmap.layers.Annotation_Layer;
75
import com.iver.cit.gvsig.fmap.layers.Annotation_Mapping;
76
import com.iver.cit.gvsig.fmap.layers.FBitSet;
77
import com.iver.cit.gvsig.fmap.layers.FLayer;
78
import com.iver.cit.gvsig.fmap.layers.FLyrVect;
79
import com.iver.cit.gvsig.fmap.layers.ReadableVectorial;
80
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
81
import com.iver.cit.gvsig.fmap.operations.strategies.DefaultStrategy;
82
import com.iver.cit.gvsig.fmap.operations.strategies.VisitException;
83
import com.iver.cit.gvsig.fmap.rendering.Annotation_Legend;
84
import com.iver.cit.gvsig.project.documents.view.legend.CreateSpatialIndexMonitorableTask;
85
import com.iver.utiles.swing.threads.Cancellable;
86
import com.vividsolutions.jts.geom.Geometry;
87
import com.vividsolutions.jts.geom.IntersectionMatrix;
88

    
89

    
90
/**
91
 * Esta clase se encargar? de dibujar de la forma m?s eficiente los temas de
92
 * anotaciones.
93
 *
94
 * @author Vicente Caballero Navarro
95
 */
96
public class Annotation_Strategy extends DefaultStrategy {
97
    private FSymbol symbolPoint = new FSymbol(FShape.POINT, Color.black);
98
    private Annotation_Layer capa;
99

    
100
    /**
101
     * Crea un nuevo AnotationStrategy.
102
     *
103
     * @param layer
104
     */
105
    public Annotation_Strategy(FLayer layer) {
106
        super(layer);
107
        capa = (Annotation_Layer) getCapa();
108
        symbolPoint.setSize(5);
109
    }
110

    
111
    // /**
112
    // * @see
113
    // com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
114
    // * java.awt.Graphics2D, ISymbol)
115
    // */
116
    // public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
117
    // Cancellable cancel) throws DriverException {
118
    // Annotation_Legend l = (Annotation_Legend) capa.getLegend();
119
    // FSymbol sym = (FSymbol) l.getDefaultSymbol();
120
    //
121
    // Rectangle2D elExtent = viewPort.getAdjustedExtent();
122
    // List lstIndexes=null;
123
    //
124
    // try {
125
    // int sc;
126
    // ReadableVectorial source = capa.getSource();
127
    // sc=source.getShapeCount();
128
    //
129
    // SelectableDataSource recordSet = source.getRecordset();
130
    // FBitSet bitSet=recordSet.getSelection();
131
    // // If area of needed extent is less than fullExtent / 4,
132
    // // it will be worthy to use SpatialIndex.
133
    // // Otherwhise, we will not use it.
134
    // boolean bUseSpatialIndex = false;
135
    // if(capa.getISpatialIndex() != null)
136
    // {
137
    // if(isSpatialIndexNecessary(elExtent)){
138
    // lstIndexes = capa.getISpatialIndex().query(elExtent);
139
    // sc = lstIndexes.size();
140
    // System.out.println("LISTA DEL SPATIALINDEX.SIZE = " + sc);
141
    // bUseSpatialIndex = true;
142
    // }//if
143
    // }//if
144
    // FontMetrics metrics = g.getFontMetrics();
145
    // Annotation_Mapping mapping = ((Annotation_Layer) capa)
146
    // .getMapping();
147
    // int idHeightField = mapping.getColumnHeight();
148
    // int idFontName = mapping.getColumnTypeFont();
149
    // int idFontStyle = mapping.getColumnStyleFont();
150
    // int idRotationField = mapping.getColumnRotate();
151
    // int idFontColor = mapping.getColumnColor();
152
    // int idTextField = mapping.getColumnText();
153
    //
154
    // double rotation = 0D;
155
    // double size = sym.getFont().getSize();
156
    // sym.setFontSizeInPixels(((Annotation_Layer)capa).isInPixels());
157
    // String fontName = "Dialog";
158
    // int fontStyle = sym.getFont().getStyle();
159
    // int fontColor = sym.getFontColor().getRGB();
160
    // SpatialCache cache = capa.getSpatialCache();
161
    // cache.clearAll();
162
    // int numOriginal;
163
    // for (int numReg = 0; numReg < sc; numReg++) {
164
    // if (cancel.isCanceled()){
165
    // break;
166
    // }
167
    // if (bUseSpatialIndex){
168
    // Integer idRec = (Integer) lstIndexes.get(numReg);
169
    // numOriginal = idRec.intValue();
170
    // }else{
171
    // numOriginal = numReg;
172
    // }
173
    // Value[] vv = recordSet.getRow(numOriginal);
174
    // if (idHeightField != -1) {
175
    // // text size is defined in the table
176
    // try {
177
    // size = ((NumericValue) vv[idHeightField]).doubleValue()
178
    // * FConstant.FONT_HEIGHT_SCALE_FACTOR;
179
    // } catch (ClassCastException ccEx) {
180
    // if (!NullValue.class.equals(vv[idHeightField]
181
    // .getClass())) {
182
    // // throw new ReadDriverException("Unknown", ccEx);
183
    // }
184
    // // a null value
185
    // // Logger.getAnonymousLogger().
186
    // // warning("Null text height value for text
187
    // // '"+vv[idTextField].toString()+"'");
188
    // continue;
189
    // }
190
    // } else {
191
    // // otherwise will use the size in the symbol
192
    //
193
    // }
194
    //
195
    // // size = CartographicSupportToolkit.
196
    // // toScreenUnitYAxis(size,
197
    // // unit,
198
    // // viewPort
199
    // // );
200
    //
201
    // // if (size <= 3) {
202
    // // // label is too small to be readable, will be skipped
203
    // // // this speeds up the rendering in wider zooms
204
    // // continue;
205
    // // }
206
    //
207
    //
208
    // if (idFontName != -1) {
209
    // fontName = ((StringValue) vv[idFontName]).toString();
210
    // }
211
    //
212
    //
213
    // if (idFontStyle != -1) {
214
    // fontStyle = ((NumericValue) vv[idFontStyle]).intValue();
215
    // }
216
    //
217
    //
218
    //
219
    // if (idRotationField != -1) {
220
    // // text rotation is defined in the table
221
    // rotation = ((NumericValue) vv[idRotationField])
222
    // .doubleValue();
223
    // }
224
    //
225
    //
226
    // if (idFontColor != -1) {
227
    // // text rotation is defined in the table
228
    // fontColor = ((NumericValue) vv[idFontColor]).intValue();
229
    // sym.setFontColor(new Color(fontColor));
230
    // }
231
    //
232
    // if (bitSet.get(numOriginal)){
233
    // sym = (FSymbol)sym.getSymbolForSelection();
234
    // }
235
    // IGeometry geom = source.getShape(numOriginal);
236
    //
237
    // sym.setFont(new Font(fontName, fontStyle, (int) size));
238
    //
239
    //
240
    // // sym.setDescription(vv[idTextField].toString());
241
    // // sym.setRotation((int) rotation);
242
    // FLabel[] aux = geom.createLabels(0, true);
243
    // aux[0].setHeight(size);
244
    // aux[0].setRotation((int) rotation);
245
    // aux[0].setString(vv[idTextField].toString());
246
    // symbolPoint.draw((Graphics2D)g,viewPort.getAffineTransform(),new
247
    // FPoint2D(viewPort.fromMapPoint(aux[0].getOrig())));
248
    // FGraphicUtilities.DrawAnnotation(g, viewPort
249
    // .getAffineTransform(), sym, aux[0], metrics, false);
250
    // }
251
    //
252
    // // System.out.println("..................Fin del dibujado
253
    // ..............");
254
    // } catch (DriverIOException e) {
255
    // e.printStackTrace();
256
    // } catch (DriverException e) {
257
    // // TODO Auto-generated catch block
258
    // e.printStackTrace();
259
    // }
260
    // heightDefault=-1;
261
    // }
262
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
263
        Cancellable cancel) {
264
        // Copia draw AttrInTableLabeling
265
        Annotation_Legend l = (Annotation_Legend) capa.getLegend();
266
        FSymbol sym = (FSymbol) l.getDefaultSymbol();
267

    
268
        try {
269
            ReadableVectorial source = capa.getSource();
270

    
271
            // limit the labeling to the visible extent
272
            FBitSet bs = capa.queryByRect(viewPort.getAdjustedExtent());
273

    
274
            SelectableDataSource recordSet = source.getRecordset();
275
            FBitSet bitSet = recordSet.getSelection();
276
            FontMetrics metrics = g.getFontMetrics();
277
            Annotation_Mapping mapping = ((Annotation_Layer) capa).getAnnotatonMapping();
278
            int idHeightField = mapping.getColumnHeight();
279
            int idFontName = mapping.getColumnTypeFont();
280
            int idFontStyle = mapping.getColumnStyleFont();
281
            int idRotationField = mapping.getColumnRotate();
282
            int idFontColor = mapping.getColumnColor();
283
            int idTextField = mapping.getColumnText();
284

    
285
            double rotation = 0D;
286
            double size = sym.getFont().getSize();
287

    
288
            //                        sym.setFontSizeInPixels(((Annotation_Layer) capa).isInPixels());
289
            String fontName = "Dialog";
290
            int fontStyle = sym.getFont().getStyle();
291
            int fontColor = sym.getFontColor().getRGB();
292

    
293
            for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i + 1)) {
294
                if (cancel.isCanceled()) {
295
                    break;
296
                }
297

    
298
                Value[] vv = recordSet.getRow(i);
299

    
300
                if (idHeightField != -1) {
301
                    // text size is defined in the table
302
                    try {
303
                        size = ((NumericValue) vv[idHeightField]).doubleValue() * FConstant.FONT_HEIGHT_SCALE_FACTOR;
304
                    } catch (ClassCastException ccEx) {
305
                        if (!NullValue.class.equals(
306
                                    vv[idHeightField].getClass())) {
307
                            // throw new ReadDriverException("Unknown", ccEx);
308
                        }
309

    
310
                        // a null value
311
                        // Logger.getAnonymousLogger().
312
                        // warning("Null text height value for text
313
                        // '"+vv[idTextField].toString()+"'");
314
                        continue;
315
                    }
316
                } else {
317
                    // otherwise will use the size in the symbol
318
                }
319

    
320
                // size = CartographicSupportToolkit.
321
                // toScreenUnitYAxis(size,
322
                // unit,
323
                // viewPort
324
                // );
325
                // if (size <= 3) {
326
                // // label is too small to be readable, will be skipped
327
                // // this speeds up the rendering in wider zooms
328
                // continue;
329
                // }
330
                if (idFontName != -1) {
331
                    fontName = ((StringValue) vv[idFontName]).toString();
332
                }
333

    
334
                if (idFontStyle != -1) {
335
                    fontStyle = ((NumericValue) vv[idFontStyle]).intValue();
336
                }
337

    
338
                if (idRotationField != -1) {
339
                    // text rotation is defined in the table
340
                    rotation = ((NumericValue) vv[idRotationField]).doubleValue();
341
                }
342

    
343
                if (idFontColor != -1) {
344
                    // text rotation is defined in the table
345
                    fontColor = ((NumericValue) vv[idFontColor]).intValue();
346
                    sym.setFontColor(new Color(fontColor));
347
                }
348

    
349
                if (bitSet.get(i)) {
350
                    sym = (FSymbol) sym.getSymbolForSelection();
351
                }
352

    
353
                IGeometry geom = source.getShape(i);
354
                sym.setFont(new Font(fontName, fontStyle, (int) size));
355

    
356
                // sym.setDescription(vv[idTextField].toString());
357
                // sym.setRotation((int) rotation);
358
                Rectangle2D r=geom.getBounds2D();
359
                FLabel[] aux = new FLabel[]{new FLabel()};//geom.createLabels(0, true);
360
                aux[0].setOrig(new Point2D.Double(r.getX(),r.getY()));
361
                aux[0].setHeight(size);
362
                aux[0].setRotation((int) rotation);
363
                aux[0].setString(vv[idTextField].toString());
364

    
365
                if (sym.isShapeVisible()) {
366
                    symbolPoint.draw((Graphics2D) g,
367
                        viewPort.getAffineTransform(),
368
                        new FPoint2D(viewPort.fromMapPoint(aux[0].getOrig())));
369
                }
370

    
371
                FGraphicUtilities.DrawAnnotation(g,
372
                    viewPort.getAffineTransform(), sym, aux[0], metrics, false);
373
            }
374
        } catch (Exception e) {
375
            // Logger.getAnonymousLogger().log(Level.SEVERE, "Could not get the
376
            // layer extent.\n" +
377
            // e.getMessage());
378
            e.printStackTrace();
379
        }
380

    
381
        // }
382
    }
383

    
384
    // /**
385
    // * @see
386
    // com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
387
    // * java.awt.Graphics2D, ISymbol)
388
    // */
389
    // public void print(BufferedImage image, Graphics2D g, ViewPort viewPort,
390
    // Cancellable cancel) throws DriverException {
391
    //
392
    // // Rectangle2D elExtent = viewPort.getAdjustedExtent();
393
    // // graphics=g;
394
    // // Annotation_Layer lyrAnnotation=(Annotation_Layer)capa;
395
    // // List lstIndexes=null;
396
    // //
397
    // // VectorialLegend l=(VectorialLegend)lyrAnnotation.getLegend();
398
    // // FBitSet bitSet=lyrAnnotation.getRecordset().getSelection();
399
    // //
400
    // // boolean inPixels=lyrAnnotation.isInPixels();
401
    // // FSymbol theSymbol = (FSymbol) l.getDefaultSymbol();
402
    // // theSymbol.setFontSizeInPixels(inPixels);
403
    // // this.viewPort=viewPort;//capa.getFMap().getViewPort();
404
    // // AffineTransform at=viewPort.getAffineTransform();
405
    // // try {
406
    // // int sc;
407
    // // sc=lyrAnnotation.getSource().getShapeCount();
408
    // // // If area of needed extent is less than fullExtent / 4,
409
    // // // it will be worthy to use SpatialIndex.
410
    // // // Otherwhise, we will not use it.
411
    // // boolean bUseSpatialIndex = false;
412
    // // if(lyrAnnotation.getISpatialIndex() != null)
413
    // // {
414
    // // if(isSpatialIndexNecessary(elExtent)){
415
    // // lstIndexes = lyrAnnotation.getISpatialIndex().query(elExtent);
416
    // // sc = lstIndexes.size();
417
    // // bUseSpatialIndex = true;
418
    // // }//if
419
    // // }//if
420
    // //
421
    // // FontMetrics metrics = g.getFontMetrics();
422
    // // //SpatialCache cache = lyrAnnotation.createSpatialCache();
423
    // // int numOriginal;
424
    // // for (int numReg = 0; numReg < sc; numReg++) {
425
    // // if (cancel.isCanceled()){
426
    // // break;
427
    // // }
428
    // // if (bUseSpatialIndex){
429
    // // Integer idRec = (Integer) lstIndexes.get(numReg);
430
    // // numOriginal = idRec.intValue();
431
    // // }else{
432
    // // numOriginal = numReg;
433
    // // }
434
    // // /* if (lyrAnnotation.getSource() instanceof EditableAdapter)
435
    // //
436
    // numOriginal=((EditableAdapter)lyrAnnotation.getSource()).getCalculatedIndex(numOriginal);*/
437
    // //
438
    // // FLabel theLabel = lyrAnnotation.getLabel(numOriginal);
439
    // // if ((theLabel == null) || (theLabel.getOrig() == null))
440
    // // continue;
441
    // //
442
    // //
443
    // // Rectangle2D r=null;
444
    // // if (inPixels && lyrAnnotation.getMapping().getColumnHeight()==-1) {
445
    // //
446
    // r=getDefaultBoundBoxinPixels(metrics,theLabel.getOrig(),theLabel.getString());
447
    // // }else {
448
    // // r=getBoundBox(theLabel.getOrig(),(float)theLabel.getHeight(),
449
    // theLabel.getJustification(),theLabel.getString());
450
    // // }
451
    // // theLabel.setBoundBox(r);
452
    // //
453
    // // if (elExtent.intersects(r))
454
    // // {
455
    // // FPoint2D p=new FPoint2D(viewPort.fromMapPoint(new
456
    // Point2D.Double(r.getX(),r.getY())));
457
    // // FGraphicUtilities.DrawShape(g,at,p,symbolPoint);
458
    // // if (bitSet.get(numOriginal)) {
459
    // // FGraphicUtilities.DrawAnnotation(g, at, theSymbol,
460
    // theLabel,metrics,true);
461
    // // }else{
462
    // // FGraphicUtilities.DrawAnnotation(g, at, theSymbol,
463
    // theLabel,metrics,false);
464
    // // }
465
    // //
466
    // //
467
    // // } // XIntersects
468
    // //
469
    // //
470
    // // }
471
    // //
472
    // // } catch (DriverIOException e) {
473
    // // e.printStackTrace();
474
    // // }
475
    // // heightDefault=-1;
476
    // }
477
    // public Rectangle2D getDefaultBoundBoxinPixels(FontMetrics metrics,
478
    // Point2D p, String s) {
479
    // int w = metrics.stringWidth(s);
480
    // double width = viewPort.toMapDistance(w);
481
    // if (heightDefault == -1) {
482
    // int h = metrics.getMaxAscent();
483
    // heightDefault = viewPort.toMapDistance(h);
484
    // }
485
    // return new Rectangle2D.Double(p.getX(), p.getY(), width, heightDefault);
486
    //
487
    // }
488

    
489
    /**
490
     * Construcci?n del rect?ngulo
491
     *
492
     * @param g DOCUMENT ME!
493
     * @param relationship
494
     *
495
     * @return
496
     *
497
     * @throws DriverException DOCUMENT ME!
498
     * @throws VisitException DOCUMENT ME!
499
     */
500

    
501
    // public Rectangle2D getBoundBox(Point2D p, float hp,
502
    // int justification,String s) {
503
    // //Rectangle2D bounding=null;
504
    // if (((Annotation_Layer)capa).isInPixels()){
505
    // graphics.setFont(graphics.getFont().deriveFont(hp));
506
    // }else{
507
    // float alturaPixels = (float) ((hp *
508
    // viewPort.getAffineTransform().getScaleX())*FConstant.FONT_HEIGHT_SCALE_FACTOR);
509
    // graphics.setFont(graphics.getFont().deriveFont(alturaPixels));
510
    // }
511
    // FontMetrics metrics = graphics.getFontMetrics();
512
    // int w = metrics.stringWidth(s);
513
    // double width = viewPort.toMapDistance(w);
514
    // int h = metrics.getMaxAscent();
515
    // double height = viewPort.toMapDistance(h);
516
    // //double dist = viewPort.toMapDistance(3);
517
    // return new Rectangle2D.Double(p.getX(), p.getY(), width, height);
518
    // /* switch (justification) {
519
    // case FLabel.LEFT_BOTTOM:
520
    // bounding=justification(p, width,height, 0, 0);
521
    //
522
    // break;
523
    //
524
    // case FLabel.LEFT_CENTER:
525
    // bounding=justification(p, width,height, 0, -(height / 2));
526
    //
527
    // break;
528
    //
529
    // case FLabel.LEFT_TOP:
530
    // bounding=justification(p,width,height, 0, -height);
531
    //
532
    // break;
533
    //
534
    // case FLabel.CENTER_BOTTOM:
535
    // bounding=justification(p, width,height, -(width / 2), -dist);
536
    //
537
    // break;
538
    //
539
    // case FLabel.CENTER_CENTER:
540
    // bounding=justification(p, width,height, -(width / 2), -(height / 2));
541
    //
542
    // break;
543
    //
544
    // case FLabel.CENTER_TOP:
545
    // bounding=justification(p, width,height, -(width / 2), -height);
546
    //
547
    // break;
548
    //
549
    // case FLabel.RIGHT_BOTTOM:
550
    // bounding=justification(p, width,height, -width, -dist);
551
    //
552
    // break;
553
    //
554
    // case FLabel.RIGHT_CENTER:
555
    // bounding=justification(p, width,height, -width, -(height / 2));
556
    //
557
    // break;
558
    //
559
    // case FLabel.RIGHT_TOP:
560
    // bounding=justification(p, width,height, -width, -height);
561
    //
562
    // break;
563
    // }
564
    //
565
    // return bounding;
566
    // */
567
    // }
568

    
569
    /*
570
     * private Rectangle2D justification(Point2D p, double w,double h, double x,
571
     * double y) { Rectangle2D r=new Rectangle2D.Double(p.getX() + x, p.getY() -
572
     * y, w, h); return r; }
573
     */
574
    /*
575
     * (non-Javadoc)
576
     *
577
     * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByShape(com.iver.cit.gvsig.fmap.core.IGeometry,
578
     *      int)
579
     */
580
    public FBitSet queryByShape(IGeometry g, int relationship)
581
        throws DriverException, VisitException {
582
        // Si hay un ?ndice espacial, lo usamos para hacer el query.
583
        FLyrVect lyr = (FLyrVect) capa;
584

    
585
        // if (lyr.getSpatialIndex() == null)
586
        if (lyr.getISpatialIndex() == null) {
587
            return super.queryByShape(g, relationship);
588
        }
589

    
590
        long t1 = System.currentTimeMillis();
591
        ReadableVectorial va = lyr.getSource();
592
        ICoordTrans ct = lyr.getCoordTrans();
593
        Rectangle2D bounds = g.getBounds2D();
594

    
595
        // Coordinate c1 = new Coordinate(bounds.getMinX(), bounds.getMinY());
596
        // Coordinate c2 = new Coordinate(bounds.getMaxX(), bounds.getMaxY());
597
        // Envelope env = new Envelope(c1, c2);
598
        // List lstRecs = lyr.getSpatialIndex().query(env);
599
        List lstRecs = lyr.getISpatialIndex().query(bounds);
600
        Integer idRec;
601
        FBitSet bitset = new FBitSet();
602
        Geometry jtsShape = g.toJTSGeometry();
603
        IntersectionMatrix m;
604
        int index;
605

    
606
        try {
607
            va.start();
608

    
609
            // Annotation_Legend aLegend=(Annotation_Legend)capa.getLegend();
610
            for (int i = 0; i < lstRecs.size(); i++) {
611
                idRec = (Integer) lstRecs.get(i);
612
                index = idRec.intValue();
613

    
614
                IGeometry geom = va.getShape(index);
615

    
616
                // FSymbol symbol=(FSymbol)aLegend.getSymbol(index);
617
                // IGeometry geom=aLegend.getTextWrappingGeometry(symbol,index);
618
                // IGeometry
619
                // geom=getGeometry(((Annotation_Layer)capa).getLabel(index).getBoundBox());
620
                if (ct != null) {
621
                    geom.reProject(ct);
622
                }
623

    
624
                Geometry jtsGeom = geom.toJTSGeometry();
625

    
626
                switch (relationship) {
627
                case CONTAINS:
628
                    m = jtsShape.relate(jtsGeom);
629

    
630
                    if (m.isContains()) {
631
                        bitset.set(index, true);
632
                    }
633

    
634
                    break;
635

    
636
                case CROSSES:
637
                    m = jtsShape.relate(jtsGeom);
638

    
639
                    if (m.isCrosses(jtsGeom.getDimension(),
640
                                jtsShape.getDimension())) {
641
                        bitset.set(index, true);
642
                    }
643

    
644
                    break;
645

    
646
                case DISJOINT:
647

    
648
                    // TODO: CREO QUE EL DISJOINT NO SE PUEDE METER AQUI
649
                    m = jtsShape.relate(jtsGeom);
650

    
651
                    if (m.isDisjoint()) {
652
                        bitset.set(index, true);
653
                    }
654

    
655
                    break;
656

    
657
                case EQUALS:
658
                    m = jtsShape.relate(jtsGeom);
659

    
660
                    if (m.isEquals(jtsGeom.getDimension(),
661
                                jtsShape.getDimension())) {
662
                        bitset.set(index, true);
663
                    }
664

    
665
                    break;
666

    
667
                case INTERSECTS:
668
                    m = jtsShape.relate(jtsGeom);
669

    
670
                    if (m.isIntersects()) {
671
                        bitset.set(index, true);
672
                    }
673

    
674
                    break;
675

    
676
                case OVERLAPS:
677
                    m = jtsShape.relate(jtsGeom);
678

    
679
                    if (m.isOverlaps(jtsGeom.getDimension(),
680
                                jtsShape.getDimension())) {
681
                        bitset.set(index, true);
682
                    }
683

    
684
                    break;
685

    
686
                case TOUCHES:
687
                    m = jtsShape.relate(jtsGeom);
688

    
689
                    if (m.isTouches(jtsGeom.getDimension(),
690
                                jtsShape.getDimension())) {
691
                        bitset.set(index, true);
692
                    }
693

    
694
                    break;
695

    
696
                case WITHIN:
697
                    m = jtsShape.relate(jtsGeom);
698

    
699
                    if (m.isWithin()) {
700
                        bitset.set(index, true);
701
                    }
702

    
703
                    break;
704
                }
705
            }
706

    
707
            va.stop();
708
        } catch (DriverIOException e) {
709
            // TODO Auto-generated catch block
710
            e.printStackTrace();
711
        }
712

    
713
        long t2 = System.currentTimeMillis();
714

    
715
        // logger.debug("queryByShape optimizado sobre la capa " + lyr.getName()
716
        // + ". " + (t2-t1) + " mseg.");
717
        return bitset;
718
    }
719

    
720
    /**
721
     * DOCUMENT ME!
722
     *
723
     * @param rect DOCUMENT ME!
724
     *
725
     * @return DOCUMENT ME!
726
     *
727
     * @throws DriverException DOCUMENT ME!
728
     */
729
    public FBitSet queryByRect(Rectangle2D rect) throws DriverException {
730
        // Si hay un ?ndice espacial, lo usamos para hacer el query.
731
        Annotation_Layer lyr = (Annotation_Layer) capa;
732
        ReadableVectorial va = lyr.getSource();
733
        ICoordTrans ct = lyr.getCoordTrans();
734
        Rectangle2D bounds = rect;
735
        // if (lyr.getSpatialIndex() == null)
736
        if (lyr.getISpatialIndex() == null) {
737
                return super.queryByRect(rect);
738
        }
739
        // Coordinate c1 = new Coordinate(bounds.getMinX(), bounds.getMinY());
740
        // Coordinate c2 = new Coordinate(bounds.getMaxX(), bounds.getMaxY());
741
        // Envelope env = new Envelope(c1, c2);
742
        //
743
        // List lstRecs = lyr.getSpatialIndex().query(env);
744
        // azabala
745
        List lstRecs = lyr.getISpatialIndex().query(bounds);
746
        Integer idRec;
747
        FBitSet bitset = new FBitSet();
748
        int index;
749

    
750
        try {
751
            va.start();
752

    
753
            DriverAttributes attr = va.getDriverAttributes();
754
            boolean bMustClone = false;
755

    
756
            if (attr != null) {
757
                if (attr.isLoadedInMemory()) {
758
                    bMustClone = attr.isLoadedInMemory();
759
                }
760
            }
761

    
762
            // Annotation_Legend aLegend=(Annotation_Legend)capa.getLegend();
763
            for (int i = 0; i < lstRecs.size(); i++) {
764
                idRec = (Integer) lstRecs.get(i);
765
                index = idRec.intValue();
766

    
767
                Annotation_Mapping mapping = ((Annotation_Layer) capa).getAnnotatonMapping();
768
                NumericValue vRotation = (NumericValue) va.getRecordset()
769
                                                          .getFieldValue(index,
770
                        mapping.getColumnRotate());
771
                NumericValue vHeight = (NumericValue) va.getRecordset()
772
                                                        .getFieldValue(index,
773
                        mapping.getColumnHeight());
774
                Value vText = va.getRecordset().getFieldValue(index,
775
                        mapping.getColumnText());
776
                IGeometry geom = ((Annotation_Layer) capa).getTextWrappingGeometry(vHeight.floatValue(),
777
                        vText.toString(), vRotation.doubleValue(), index);
778

    
779
                if (ct != null) {
780
                    if (bMustClone) {
781
                        geom = geom.cloneGeometry();
782
                    }
783

    
784
                    geom.reProject(ct);
785
                }
786

    
787
                if (geom.intersects(rect)) {
788
                    bitset.set(index, true);
789
                }
790
            }
791

    
792
            va.stop();
793
        } catch (DriverIOException e) {
794
            // TODO Auto-generated catch block
795
            e.printStackTrace();
796
        } catch (DriverLoadException e) {
797
            // TODO Auto-generated catch block
798
            e.printStackTrace();
799
        } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
800
            // TODO Auto-generated catch block
801
            e.printStackTrace();
802
        }
803

    
804
        return bitset;
805
    }
806

    
807
    /*
808
     * (non-Javadoc)
809
     *
810
     * @see com.iver.cit.gvsig.fmap.operations.strategies.Strategy#queryByPoint(java.awt.geom.Point2D,
811
     *      double)
812
     */
813
    public FBitSet queryByPoint(Point2D p, double tolerance)
814
        throws DriverException {
815
        // TODO: OJO!!!!. Est? implementado como un rectangulo.
816
        // Lo correcto deber?a ser calculando las distancias reales
817
        // es decir, con un c?rculo.
818
        Rectangle2D recPoint = new Rectangle2D.Double(p.getX() -
819
                (tolerance / 2), p.getY() - (tolerance / 2), tolerance,
820
                tolerance);
821

    
822
        return queryByRect(recPoint);
823
    }
824

    
825
    // private IGeometry getGeometry(Rectangle2D r){
826
    // GeneralPathX resul = new GeneralPathX();
827
    // Point2D[] vs=new Point2D[4];
828
    // vs[0]=new Point2D.Double(r.getX(),r.getY());
829
    // vs[1]=new Point2D.Double(r.getMaxX(),r.getY());
830
    // vs[2]=new Point2D.Double(r.getMaxX(),r.getMaxY());
831
    // vs[3]=new Point2D.Double(r.getX(),r.getMaxY());
832
    // //vs[4]=new Point2D.Double(r.getX(),r.getY());
833
    // for (int i = 0; i < vs.length; i++) {
834
    // if (i == 0) {
835
    // resul.moveTo(vs[i].getX(),vs[i].getY());
836
    // } else {
837
    // resul.lineTo(vs[i].getX(),vs[i].getY());
838
    // }
839
    // }
840
    // resul.closePath();
841
    // return ShapeFactory.createPolygon2D(resul);
842
    // }
843
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
844
        PrintRequestAttributeSet properties) throws DriverException {
845
        capa.beforePrinting(properties);
846
        draw(null, g, viewPort, cancel);
847
        capa.afterPrinting();
848
    }
849
}