Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / operations / strategies / ShpStrategy.java @ 1828

History | View | Annotate | Download (7.51 KB)

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

    
43
import com.iver.cit.gvsig.fmap.DriverException;
44
import com.iver.cit.gvsig.fmap.ViewPort;
45
import com.iver.cit.gvsig.fmap.core.FShape;
46
import com.iver.cit.gvsig.fmap.core.IGeometry;
47
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
48
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
49
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
50
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
51
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
52
import com.iver.cit.gvsig.fmap.layers.FLayer;
53
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
54
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
55
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
56
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
57
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
58
import com.iver.cit.gvsig.fmap.operations.Cancellable;
59
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
60
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
61
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
62

    
63
import org.apache.log4j.Logger;
64

    
65
import org.cresques.cts.ICoordTrans;
66

    
67
import org.geotools.resources.geometry.XRectangle2D;
68

    
69
import java.awt.Graphics2D;
70
import java.awt.geom.AffineTransform;
71
import java.awt.geom.Point2D;
72
import java.awt.geom.Rectangle2D;
73
import java.awt.image.BufferedImage;
74

    
75
import java.io.IOException;
76

    
77
import java.util.BitSet;
78

    
79

    
80
/**
81
 * Esta clase definir? las operaciones de la interfaz FLyrVect de la manera m?s
82
 * ?ptima para los ficheros shp.
83
 */
84
public class ShpStrategy extends DefaultStrategy {
85
        private static Logger logger = Logger.getLogger(ShpStrategy.class.getName());
86

    
87
        /**
88
         * Crea una ShpStrategy.
89
         *
90
         * @param capa
91
         */
92
        public ShpStrategy(FLayer capa) {
93
                super(capa);
94
        }
95

    
96
        /**
97
         * @see com.iver.cit.gvsig.fmap.operations.LayerOperations#draw(java.awt.image.BufferedImage,
98
         *                 java.awt.Graphics2D, FStyle2D)
99
         */
100
        public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
101
                Cancellable cancel) throws DriverException {
102
                try {
103
                        VectorialAdapter adapter = ((SingleLayer) getCapa()).getSource();
104
                        if (adapter.getShapeCount() <= 0)
105
                        {
106
                            logger.debug("Layer:" + getCapa().getName() + " sin registros");
107
                            return;
108
                        }
109
                        Selectable selection = (Selectable) getCapa();
110
                        ICoordTrans ct = getCapa().getCoordTrans();
111
                        BitSet bitSet = selection.getSelection();
112
                        BoundedShapes shapeBounds = (BoundedShapes) adapter.getDriver();
113
                        VectorialFileDriver driver = (VectorialFileDriver) adapter.getDriver();
114
                        logger.debug("adapter.start() -> Layer:" + getCapa().getName());
115
                        adapter.start();
116
                        IGeometry geom;
117
                        if (adapter.getShapeCount()>0){
118
                        geom = adapter.getShape(0);
119
                        }
120
                        VectorialLegend l = (VectorialLegend) ((ClassifiableVectorial) getCapa()).getLegend();
121

    
122
                        if (l instanceof ClassifiedLegendInfo) {
123
                                ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) l;
124
                                FSymbol[] symbs = clsfLegend.getSymbols();
125
                                double rSym = 0;
126
                                double maxRSym = -1;
127

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

    
131
                                        /* Style2D pointSymbol = symbs[i].getPointStyle2D();
132
                                           if (pointSymbol instanceof MarkStyle2D)
133
                                           {
134
                                                   MarkStyle2D mrk2D = (MarkStyle2D) pointSymbol;
135
                                                   rSym = viewPort.toMapDistance(mrk2D.getSize());
136
                                                   if (maxRSym < rSym)
137
                                                           maxRSym = rSym;
138
                                           }*/
139
                                }
140
                        }
141

    
142
                        Rectangle2D extent = viewPort.getAdjustedExtent();
143
                        AffineTransform at = viewPort.getAffineTransform();
144

    
145
                        int sc;
146

    
147
                        Rectangle2D bounds;
148

    
149
                        sc = adapter.getShapeCount();
150

    
151
                        long t1 = System.currentTimeMillis();
152
                        logger.debug("getCapa().getRecordset().start()");
153
                        ((AlphanumericData) getCapa()).getRecordset().start();
154
                        
155
                        // TODO: A revisar si es o no conveniente este sistema
156
                        // de comunicaci?n con los drivers.
157
                        DriverAttributes attr = adapter.getDriverAttributes();
158
                        boolean bMustClone = false;
159
                        if (attr != null)
160
                        {
161
                            if (attr.isLoadedInMemory())
162
                            {
163
                                bMustClone = attr.isLoadedInMemory();                            
164
                            }
165
                        }
166
                        
167

    
168
                        for (int i = 0; i < sc; i++) {
169
                                // Salimos si alguien cancela
170
                                if (cancel.isCanceled()) {
171
                                        break;
172
                                }
173

    
174
                                bounds = shapeBounds.getShapeBounds(i);
175

    
176
                                if (ct != null) {
177
                                        bounds = ct.convert(bounds);
178
                                }
179

    
180
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
181
                                        FSymbol symbol = l.getSymbol(i);
182

    
183
                                        if (bitSet.get(i)) {
184
                                                symbol = FSymbol.getSymbolForSelection(symbol);
185
                                        }
186

    
187
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
188

    
189
                                        if (bPoint ||
190
                                                        ((bounds.getHeight() > viewPort.getDist1pixel()) ||
191
                                                        (bounds.getWidth() > viewPort.getDist1pixel()))) {
192
                                                geom = driver.getShape(i);
193

    
194
                                                if (ct != null) {
195
                                                    if (bMustClone)
196
                                                        geom = geom.cloneGeometry();
197
                                                        geom.reProject(ct);
198
                                                }
199

    
200
                                                geom.draw(g, viewPort, symbol);
201
                                        } else {
202
                                                Point2D.Double pOrig = new Point2D.Double(bounds.getMinX(),
203
                                                                bounds.getMinY());
204
                                                Point2D pDest;
205

    
206
                                                pDest = viewPort.getAffineTransform().transform(pOrig,
207
                                                                null);
208

    
209
                                                int pixX = (int) pDest.getX();
210
                                                int pixY = (int) pDest.getY();
211

    
212
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
213
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
214
                                                                image.setRGB(pixX, pixY, symbol.getRgb());
215
                                                        }
216
                                                }
217
                                        }
218
                                }
219
                        }
220

    
221
                        logger.debug("getCapa().getRecordset().stop()");
222
                        ((AlphanumericData) getCapa()).getRecordset().stop();
223

    
224
                        long t2 = System.currentTimeMillis();
225
                        logger.debug("adapter.stop()");
226
                        adapter.stop();
227

    
228
                        System.out.println(t2 - t1);
229
                } catch (DriverIOException e) {
230
                        throw new DriverException(e);
231
                } catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
232
                        throw new DriverException(e);
233
                } catch (DriverException e) {
234
                        throw new DriverException(e);
235
                } catch (IOException e) {
236
                        throw new DriverException(e);
237
                }
238
        }
239

    
240
        /**
241
         * M?todo utilizado para dibujar sobre el graphics que se pasa como
242
         * par?metro, pensado para utilizarse para imprimir.
243
         *
244
         * @param g Graphics2D
245
         * @param viewPort ViewPort.
246
         * @param cancel
247
         *
248
         * @throws DriverException
249
         */
250
        public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel)
251
                throws DriverException {
252
                super.draw(null, g, viewPort, cancel); // Quiero ejecutar el draw del padre, que es el que va sin acelaraci?n!!
253
        }
254
}