Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 1034

History | View | Annotate | Download (5.89 KB)

1
package com.iver.cit.gvsig.fmap.operations.strategies;
2

    
3
import com.iver.cit.gvsig.fmap.DriverException;
4
import com.iver.cit.gvsig.fmap.ViewPort;
5
import com.iver.cit.gvsig.fmap.core.FShape;
6
import com.iver.cit.gvsig.fmap.core.IGeometry;
7
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
8
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
9
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
10
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
11
import com.iver.cit.gvsig.fmap.layers.FLayer;
12
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
13
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
14
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
15
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
16
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
17
import com.iver.cit.gvsig.fmap.operations.Cancellable;
18
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
19
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
20
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
21

    
22
import org.apache.log4j.Logger;
23

    
24
import org.cresques.cts.ICoordTrans;
25

    
26
import org.geotools.resources.geometry.XRectangle2D;
27

    
28
import java.awt.Graphics2D;
29
import java.awt.geom.AffineTransform;
30
import java.awt.geom.Point2D;
31
import java.awt.geom.Rectangle2D;
32
import java.awt.image.BufferedImage;
33

    
34
import java.io.IOException;
35

    
36
import java.util.BitSet;
37

    
38

    
39
/**
40
 * Esta clase definir? las operaciones de la interfaz FLyrVect de la manera m?s
41
 * ?ptima para los ficheros shp.
42
 */
43
public class ShpStrategy extends DefaultStrategy {
44
        private static Logger logger = Logger.getLogger(ShpStrategy.class.getName());
45

    
46
        /**
47
         * Crea una ShpStrategy.
48
         *
49
         * @param capa
50
         */
51
        public ShpStrategy(FLayer capa) {
52
                super(capa);
53
        }
54

    
55
        /**
56
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
57
         *                 java.awt.Graphics2D, FStyle2D)
58
         */
59
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
60
                Cancellable cancel) throws DriverException {
61
                try {
62
                        VectorialAdapter adapter = ((SingleLayer) getCapa()).getSource();
63
                        Selectable selection = (Selectable) getCapa();
64
                        ICoordTrans ct = getCapa().getCoordTrans();
65
                        BitSet bitSet = selection.getSelection();
66
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
67
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
68
                        logger.debug("adapter.start()");
69
                        adapter.start();
70

    
71
                        IGeometry geom = adapter.getShape(0);
72
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa()).getLegend();
73

    
74
                        if (l instanceof ClassifiedLegendInfo) {
75
                                ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) l;
76
                                FSymbol[] symbs = clsfLegend.getSymbols();
77
                                double rSym = 0;
78
                                double maxRSym = -1;
79

    
80
                                for (int i = 0; i < symbs.length; i++) {
81
                                        // TODO: REVISAR LOS SIMBOLOS Y SUS TAMA?OS
82

    
83
                                        /* Style2D pointSymbol = symbs[i].getPointStyle2D();
84
                                           if (pointSymbol instanceof MarkStyle2D)
85
                                           {
86
                                                   MarkStyle2D mrk2D = (MarkStyle2D) pointSymbol;
87
                                                   rSym = viewPort.toMapDistance(mrk2D.getSize());
88
                                                   if (maxRSym < rSym)
89
                                                           maxRSym = rSym;
90
                                           }*/
91
                                }
92
                        }
93

    
94
                        Rectangle2D extent = viewPort.getAdjustedExtent();
95
                        AffineTransform at = viewPort.getAffineTransform();
96

    
97
                        int sc;
98

    
99
                        Rectangle2D bounds;
100

    
101
                        sc = adapter.getShapeCount();
102

    
103
                        long t1 = System.currentTimeMillis();
104
                        logger.debug("getCapa().getRecordset().start()");
105
                        ((AlphanumericData) getCapa()).getRecordset().start();
106

    
107
                        for (int i = 0; i < sc; i++) {
108
                                // Salimos si alguien cancela
109
                                if (cancel.isCanceled()) {
110
                                        break;
111
                                }
112

    
113
                                bounds = shapeBounds.getShapeBounds(i);
114

    
115
                                if (ct != null) {
116
                                        Point2D pt1 = new Point2D.Double(bounds.getMinX(),
117
                                                        bounds.getMinY());
118
                                        Point2D pt2 = new Point2D.Double(bounds.getMaxX(),
119
                                                        bounds.getMaxY());
120
                                        pt1 = ct.convert(pt1, null);
121
                                        pt2 = ct.convert(pt2, null);
122
                                        bounds.setFrameFromDiagonal(pt1, pt2);
123
                                }
124

    
125
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
126
                                        FSymbol symbol = l.getSymbol(i);
127

    
128
                                        if (bitSet.get(i)) {
129
                                                symbol = FSymbol.getSymbolForSelection(symbol);
130
                                        }
131

    
132
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
133

    
134
                                        if (bPoint ||
135
                                                        ((bounds.getHeight() > viewPort.getDist1pixel()) ||
136
                                                        (bounds.getWidth() > viewPort.getDist1pixel()))) {
137
                                                geom = driver.getShape(i);
138

    
139
                                                if (ct != null) {
140
                                                        geom.reProject(ct);
141
                                                }
142

    
143
                                                geom.draw(g, viewPort, symbol);
144
                                        } else {
145
                                                Point2D.Double pOrig = new Point2D.Double(bounds.getMinX(),
146
                                                                bounds.getMinY());
147
                                                Point2D pDest;
148

    
149
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
150
                                                                null);
151

    
152
                                                int pixX = (int) pDest.getX();
153
                                                int pixY = (int) pDest.getY();
154

    
155
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
156
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
157
                                                                image.setRGB(pixX, pixY, symbol.getRgb());
158
                                                        }
159
                                                }
160
                                        }
161
                                }
162
                        }
163

    
164
                        logger.debug("getCapa().getRecordset().stop()");
165
                        ((AlphanumericData) getCapa()).getRecordset().stop();
166

    
167
                        long t2 = System.currentTimeMillis();
168
                        logger.debug("adapter.stop()");
169
                        adapter.stop();
170

    
171
                        System.out.println(t2 - t1);
172
                } catch (DriverIOException e) {
173
                        throw new DriverException(e);
174
                } catch (com.hardcode.gdbms.engine.data.DriverException e) {
175
                        throw new DriverException(e);
176
                } catch (DriverException e) {
177
                        throw new DriverException(e);
178
                } catch (IOException e) {
179
                        throw new DriverException(e);
180
                }
181
        }
182

    
183
        /**
184
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
185
         * par?metro, pensado para utilizarse para imprimir.
186
         *
187
         * @param g Graphics2D
188
         * @param viewPort ViewPort.
189
         * @param cancel
190
         *
191
         * @throws DriverException
192
         */
193
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
194
                throws DriverException {
195
                super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
196
        }
197
}