Statistics
| Revision:

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

History | View | Annotate | Download (7.59 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 java.awt.Graphics2D;
44
import java.awt.geom.Point2D;
45
import java.awt.geom.Rectangle2D;
46
import java.awt.image.BufferedImage;
47
import java.io.IOException;
48
import java.util.BitSet;
49

    
50
import org.apache.log4j.Logger;
51
import org.cresques.cts.ICoordTrans;
52
import org.geotools.resources.geometry.XRectangle2D;
53

    
54
import com.iver.cit.gvsig.fmap.DriverException;
55
import com.iver.cit.gvsig.fmap.ViewPort;
56
import com.iver.cit.gvsig.fmap.core.FShape;
57
import com.iver.cit.gvsig.fmap.core.IGeometry;
58
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
59
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
60
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
61
import com.iver.cit.gvsig.fmap.drivers.DriverIOException;
62
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
63
import com.iver.cit.gvsig.fmap.layers.FLayer;
64
import com.iver.cit.gvsig.fmap.layers.VectorialAdapter;
65
import com.iver.cit.gvsig.fmap.layers.layerOperations.AlphanumericData;
66
import com.iver.cit.gvsig.fmap.layers.layerOperations.ClassifiableVectorial;
67
import com.iver.cit.gvsig.fmap.layers.layerOperations.Selectable;
68
import com.iver.cit.gvsig.fmap.layers.layerOperations.SingleLayer;
69
import com.iver.cit.gvsig.fmap.operations.Cancellable;
70
import com.iver.cit.gvsig.fmap.rendering.ClassifiedLegendInfo;
71
import com.iver.cit.gvsig.fmap.rendering.VectorialLegend;
72
import com.iver.cit.gvsig.fmap.rendering.styling.FStyle2D;
73

    
74

    
75
/**
76
 * Esta clase definir? las operaciones de la interfaz FLyrVect de la manera m?s
77
 * ?ptima para los ficheros shp.
78
 */
79
public class ShpStrategy extends DefaultStrategy {
80
        private static Logger logger = Logger.getLogger(ShpStrategy.class.getName());
81

    
82
        /**
83
         * Crea una ShpStrategy.
84
         *
85
         * @param capa
86
         */
87
        public ShpStrategy(FLayer capa) {
88
                super(capa);
89
        }
90

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

    
117
                        if (l instanceof ClassifiedLegendInfo) {
118
                                ClassifiedLegendInfo clsfLegend = (ClassifiedLegendInfo) l;
119
                                FSymbol[] symbs = clsfLegend.getSymbols();
120
                                //double rSym = 0;
121
                                //double maxRSym = -1;
122

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

    
126
                                        /* Style2D pointSymbol = symbs[i].getPointStyle2D();
127
                                           if (pointSymbol instanceof MarkStyle2D)
128
                                           {
129
                                                   MarkStyle2D mrk2D = (MarkStyle2D) pointSymbol;
130
                                                   rSym = viewPort.toMapDistance(mrk2D.getSize());
131
                                                   if (maxRSym < rSym)
132
                                                           maxRSym = rSym;
133
                                           }*/
134
                                }
135
                        }
136

    
137
                        Rectangle2D extent = viewPort.getAdjustedExtent();
138
                        //AffineTransform at = viewPort.getAffineTransform();
139

    
140
                        int sc;
141

    
142
                        Rectangle2D bounds;
143

    
144
                        sc = adapter.getShapeCount();
145

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

    
163
                        for (int i = 0; i < sc; i++) {
164
                                // Salimos si alguien cancela
165
                                if (cancel.isCanceled()) {
166
                                        break;
167
                                }
168

    
169
                                bounds = shapeBounds.getShapeBounds(i);
170

    
171
                                if (ct != null) {
172
                                        bounds = ct.convert(bounds);
173
                                }
174

    
175
                                if (XRectangle2D.intersectInclusive(extent, bounds)) {
176
                                        FSymbol symbol = l.getSymbol(i);
177

    
178
                                        if (bitSet.get(i)) {
179
                                                symbol = FSymbol.getSymbolForSelection(symbol);
180
                                        }
181

    
182
                                        boolean bPoint = (shapeBounds.getShapeType(i) == FShape.POINT);
183

    
184
                                        if (bPoint ||
185
                                                        ((bounds.getHeight() > viewPort.getDist1pixel()) ||
186
                                                        (bounds.getWidth() > viewPort.getDist1pixel()))) {
187
                                                geom = driver.getShape(i);
188

    
189
                                                if (ct != null) {
190
                                                    if (bMustClone)
191
                                                        geom = geom.cloneGeometry();
192
                                                        geom.reProject(ct);
193
                                                }
194

    
195
                                                geom.draw(g, viewPort, symbol);
196
                                        } else {
197
                                                Point2D.Double pOrig = new Point2D.Double(bounds.getMinX(),
198
                                                                bounds.getMinY());
199
                                                Point2D pDest;
200

    
201
                                                // pDest = viewPort.getAffineTransform().transform(pOrig, null);
202
                        pDest = g.getTransform().transform(pOrig, null);
203

    
204
                                                int pixX = (int) pDest.getX();
205
                                                int pixY = (int) pDest.getY();
206
                                                if (symbol ==null)
207
                                                        continue;
208
                                                if ((pixX > 0) && (pixX < image.getWidth())) {
209
                                                        if ((pixY > 0) && (pixY < image.getHeight())) {
210
                                                                image.setRGB(pixX, pixY, symbol.getRgb());
211
                                                        }
212
                                                }
213
                                        }
214
                                }
215
                        }
216

    
217
                        logger.debug("getCapa().getRecordset().stop()");
218
                        ((AlphanumericData) getCapa()).getRecordset().stop();
219

    
220
                        long t2 = System.currentTimeMillis();
221
                        logger.debug("adapter.stop()");
222
                        adapter.stop();
223

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

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