Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / layers / FLyrGT2.java @ 10627

History | View | Annotate | Download (8.57 KB)

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

    
46
import java.awt.Graphics2D;
47
import java.awt.geom.Rectangle2D;
48
import java.awt.image.BufferedImage;
49
import java.io.IOException;
50
import java.util.NoSuchElementException;
51

    
52
import javax.print.attribute.PrintRequestAttributeSet;
53

    
54
import org.geotools.data.DataStore;
55
import org.geotools.data.DefaultQuery;
56
import org.geotools.data.DefaultTransaction;
57
import org.geotools.data.FeatureReader;
58
import org.geotools.data.FeatureSource;
59
import org.geotools.data.Query;
60
import org.geotools.data.Transaction;
61
import org.geotools.feature.Feature;
62
import org.geotools.feature.IllegalAttributeException;
63
import org.geotools.filter.AbstractFilter;
64
import org.geotools.filter.BBoxExpression;
65
import org.geotools.filter.FilterFactory;
66
import org.geotools.filter.FilterType;
67
import org.geotools.filter.GeometryFilter;
68
import org.geotools.filter.IllegalFilterException;
69

    
70
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
71
import com.iver.cit.gvsig.exceptions.visitors.VisitorException;
72
import com.iver.cit.gvsig.fmap.ViewPort;
73
import com.iver.cit.gvsig.fmap.core.IGeometry;
74
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
75
import com.iver.cit.gvsig.fmap.core.gt2.FLiteShape;
76
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
77
import com.iver.cit.gvsig.fmap.core.v02.FConverter;
78
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
79
import com.iver.cit.gvsig.fmap.operations.strategies.FeatureVisitor;
80
import com.iver.utiles.swing.threads.Cancellable;
81
import com.vividsolutions.jts.geom.Envelope;
82
import com.vividsolutions.jts.geom.Geometry;
83

    
84
public class FLyrGT2 extends FLyrSecuential {
85

    
86
    DataStore dataStore;
87
    String typeName;
88
    String tableName;
89
    static FilterFactory ff = FilterFactory.createFilterFactory();
90

    
91
    public Rectangle2D getFullExtent() throws ReadDriverException {
92
        Rectangle2D r;
93
        Envelope jtsR = null;
94
        try {
95
            jtsR = dataStore.getFeatureSource(tableName).getBounds();
96
        } catch (IOException e) {
97
            throw new ReadDriverException(getName(),e);
98
        }
99
        r = FConverter.convertEnvelopeToRectangle2D(jtsR);
100
        return r;
101
    }
102

    
103
    private void prepareDrawing()
104
    {
105

    
106
    }
107
    private void drawSelectedFeatures()
108
    {
109

    
110
    }
111
    public void draw(BufferedImage image, Graphics2D g, ViewPort viewPort,
112
            Cancellable cancel, double scale) throws ReadDriverException {
113
        if (!isWithinScale(scale)) return;
114
        try {
115
            prepareDrawing();
116
            FeatureSource featSource = dataStore.getFeatureSource(tableName);
117

    
118
            Envelope env = new Envelope(viewPort.getExtent().getMinX(),
119
                    viewPort.getExtent().getMinY(), viewPort.getExtent().getMaxX(),
120
                    viewPort.getExtent().getMaxY());
121

    
122
//          create filter to select only features that satify 300000 >= "field" >= 100000
123

    
124
            BBoxExpression bb = ff.createBBoxExpression(env);
125
            GeometryFilter bboxFilter =
126
                    ff.createGeometryFilter(AbstractFilter.GEOMETRY_BBOX);
127
            bboxFilter.addRightGeometry(bb);
128
            String strGeom = dataStore.getSchema(tableName).getDefaultGeometry().getName();
129
            bboxFilter.addLeftGeometry(
130
                    ff.createAttributeExpression(dataStore.getSchema(tableName),strGeom));
131

    
132
            Query theQuery = new DefaultQuery(tableName, bboxFilter);
133
            // Query theQuery = new DefaultQuery(tableName, Filter.NONE);
134
            Transaction t = new DefaultTransaction();
135
            FeatureReader reader = dataStore.getFeatureReader(theQuery, t);
136
            Feature f = null;
137
            FSymbol sym = new FSymbol(FConstant.SYMBOL_TYPE_FILL);
138
            while ( reader.hasNext() ) {
139
                if (cancel.isCanceled()) {
140
                    reader.close();
141
                    break;
142
                }
143

    
144
               f = reader.next();
145
               Geometry geom = f.getDefaultGeometry();
146
               FLiteShape shpLite = new FLiteShape(geom);
147
               IGeometry gAux = ShapeFactory.createGeometry(shpLite);
148
               gAux.draw(g,viewPort,sym);
149
            }
150

    
151
            reader.close();
152

    
153
        } catch (IOException e) {
154
            throw new ReadDriverException(getName(),e);
155
        } catch (IllegalFilterException e) {
156
            throw new ReadDriverException(getName(),e);
157
        } catch (NoSuchElementException e) {
158
            throw new ReadDriverException(getName(),e);
159
        } catch (IllegalAttributeException e) {
160
            throw new ReadDriverException(getName(),e);
161
        }
162

    
163
    }
164

    
165
    public void print(Graphics2D g, ViewPort viewPort, Cancellable cancel,
166
            double scale, PrintRequestAttributeSet properties) throws ReadDriverException {
167
        // TODO Auto-generated method stub
168

    
169
    }
170

    
171
    /**
172
     * @return Returns the dataStore.
173
     */
174
    public DataStore getDataStore() {
175
        return dataStore;
176
    }
177

    
178
    /**
179
     * @param dataStore The dataStore to set.
180
     * @throws IOException
181
     */
182
    public void setDataStore(DataStore dataStore) throws IOException {
183
        this.dataStore = dataStore;
184
//      feature type name is defaulted to the name of shapefile (without extension)
185
        typeName = dataStore.getTypeNames()[0];
186
    }
187

    
188
    /**
189
     * @param tableName The tableName to set.
190
     */
191
    public void setTableName(String tableName) {
192
        this.tableName = tableName;
193
    }
194

    
195
        public void process(FeatureVisitor visitor, FBitSet subset) throws ReadDriverException, VisitorException {
196
                // TODO Auto-generated method stub
197

    
198
        }
199

    
200
        public void process(FeatureVisitor visitor, Rectangle2D rect) throws ReadDriverException, VisitorException {
201
                try
202
                {
203

    
204
                Envelope env = FConverter.convertRectangle2DtoEnvelope(rect);
205

    
206

    
207
                BBoxExpression bb = ff.createBBoxExpression(env);
208
                GeometryFilter bboxFilter =
209
                        ff.createGeometryFilter(FilterType.GEOMETRY_BBOX);
210
                bboxFilter.addRightGeometry(bb);
211
                String strGeom = dataStore.getSchema(tableName).getDefaultGeometry().getName();
212
                bboxFilter.addLeftGeometry(
213
                        ff.createAttributeExpression(dataStore.getSchema(tableName),strGeom));
214

    
215
                Query theQuery = new DefaultQuery(tableName, bboxFilter);
216
                // Query theQuery = new DefaultQuery(tableName, Filter.NONE);
217
                Transaction t = new DefaultTransaction();
218
                FeatureReader reader = dataStore.getFeatureReader(theQuery, t);
219
                Feature f = null;
220
                while ( reader.hasNext() ) {
221
                   f = reader.next();
222
                   Geometry geom = f.getDefaultGeometry();
223
                   FLiteShape shpLite = new FLiteShape(geom);
224
                   IGeometry gAux = ShapeFactory.createGeometry(shpLite);
225
                   visitor.visit(gAux, -1);
226
                }
227

    
228
                reader.close();
229

    
230
    } catch (IOException e) {
231
        throw new ReadDriverException(getName(),e);
232
    } catch (IllegalFilterException e) {
233
        throw new ReadDriverException(getName(),e);
234
    } catch (NoSuchElementException e) {
235
        throw new ReadDriverException(getName(),e);
236
    } catch (IllegalAttributeException e) {
237
        throw new ReadDriverException(getName(),e);
238
    }
239

    
240

    
241
        }
242

    
243
        public void process(FeatureVisitor visitor) throws ReadDriverException, VisitorException {
244
                // TODO Auto-generated method stub
245

    
246
        }
247

    
248
        public SelectableDataSource getRecordset() {
249
                // TODO Auto-generated method stub
250
                // fjp: simplificar SelectableDataSource y quitar los m?todos que no
251
                // necesitamos. (getPrimeryKeys, getDataWare, getDataFactory... etc)
252
                return null;
253
        }
254

    
255
}