Statistics
| Revision:

root / trunk / libraries / libTopology / src / com / iver / cit / gvsig / drivers / VectorErrorMemoryDriver.java @ 22874

History | View | Annotate | Download (6.61 KB)

1
/*
2
 * Created on 10-abr-2006
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
/* CVS MESSAGES:
45
 *
46
 * $Id: 
47
 * $Log: 
48
 */
49
package com.iver.cit.gvsig.drivers;
50

    
51
import java.awt.geom.Rectangle2D;
52
import java.sql.Types;
53

    
54
import org.geotools.referencefork.referencing.operation.builder.MappedPosition;
55
import org.gvsig.referencing.MappedPositionContainer;
56

    
57
import com.hardcode.gdbms.driver.exceptions.ReadDriverException;
58
import com.hardcode.gdbms.driver.exceptions.ReloadDriverException;
59
import com.hardcode.gdbms.driver.exceptions.WriteDriverException;
60
import com.hardcode.gdbms.engine.data.DataSourceFactory;
61
import com.hardcode.gdbms.engine.data.driver.ObjectDriver;
62
import com.hardcode.gdbms.engine.data.edition.DataWare;
63
import com.hardcode.gdbms.engine.values.Value;
64
import com.hardcode.gdbms.engine.values.ValueFactory;
65
import com.iver.cit.gvsig.exceptions.expansionfile.ExpansionFileReadException;
66
import com.iver.cit.gvsig.fmap.core.FShape;
67
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
68
import com.iver.cit.gvsig.fmap.core.IGeometry;
69
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
70
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
71
import com.iver.cit.gvsig.fmap.drivers.DriverAttributes;
72
import com.iver.cit.gvsig.fmap.drivers.VectorialDriver;
73

    
74
/**
75
 * Memory driver to adapt MappedPositionContainer (which contains MappedPosition)
76
 * to fmap driver interfaces. It only returns geometries (no fields)
77
 * 
78
 * @author Alvaro Zabala
79
 *
80
 */
81
public class VectorErrorMemoryDriver implements VectorialDriver, ObjectDriver,
82
                BoundedShapes {
83

    
84
        public static final Rectangle2D EMPTY_FULL_EXTENT = new Rectangle2D.Double();
85

    
86
        private DriverAttributes attr = new DriverAttributes();
87

    
88
        /**
89
         * Name of the data source of this driver
90
         */
91
        String name;
92
        
93
        /**
94
         * Container of MappedPosition
95
         */
96
        MappedPositionContainer mappedPositionContainer;
97

    
98
        /**
99
         * Full extent of all features
100
         */
101
        Rectangle2D fullExtent;
102

    
103
        /**
104
         * Constructor
105
         * 
106
         * @param name
107
         *            descriptive name of the data source
108
         * @param features
109
         *            collection of features in memory
110
         * @param definition
111
         *            definition of the layer of these features
112
         */
113
        public VectorErrorMemoryDriver(String name,
114
                        MappedPositionContainer mappedPositionContainer) {
115
                this.name = name;
116
                this.mappedPositionContainer = mappedPositionContainer;
117
                attr.setLoadedInMemory(true);
118
                try {
119
                        computeFullExtent();
120
                } catch (ExpansionFileReadException e) {
121
                        e.printStackTrace();
122
                } catch (ReadDriverException e) {
123
                        e.printStackTrace();
124
                }
125
        }
126

    
127
        public int getShapeType() {
128
                return FShape.LINE;
129
        }
130

    
131
        public String getName() {
132
                return name;
133
        }
134

    
135
        public int getShapeCount() throws ReadDriverException {
136
                return mappedPositionContainer.getCount();
137
        }
138

    
139
        public Rectangle2D getFullExtent() throws ReadDriverException,
140
                        ExpansionFileReadException {
141
                if (fullExtent == null) {
142
                        // collection is empty
143
                        return EMPTY_FULL_EXTENT;
144
                }
145
                return fullExtent;
146
        }
147

    
148
        public IGeometry getShape(int index) throws ReadDriverException {
149
                if (index < mappedPositionContainer.getCount()){
150
                        MappedPosition mappedPosition = mappedPositionContainer.getMappedPosition(index);
151
                        double[] sourceCoords = mappedPosition.getSource().getCoordinates();
152
                        double[] targetCoords = mappedPosition.getTarget().getCoordinates();
153
                        GeneralPathX gpx = new GeneralPathX();
154
                        gpx.moveTo(sourceCoords[0], sourceCoords[1]);
155
                        gpx.lineTo(targetCoords[0], targetCoords[1]);
156
                        return ShapeFactory.createPolyline2D(gpx);
157
                
158
        
159
                }
160
                else
161
                        return null;
162
        }
163

    
164
        public void reload() throws ReloadDriverException {
165
        }
166

    
167
        public DriverAttributes getDriverAttributes() {
168
                return attr;
169
        }
170

    
171
        public boolean isWritable() {
172
                return false;
173
        }
174

    
175
        public int[] getPrimaryKeys() throws ReadDriverException {
176
                return null;
177
        }
178

    
179
        public void write(DataWare dataWare) throws WriteDriverException,
180
                        ReadDriverException {
181
        }
182

    
183
        public void setDataSourceFactory(DataSourceFactory dsf) {
184
        }
185

    
186
        public Value getFieldValue(long rowIndex, int fieldId)
187
                        throws ReadDriverException {
188
                if(fieldId == 0)
189
                        return ValueFactory.createValue(rowIndex);
190
                else
191
                        return ValueFactory.createNullValue();
192
        }
193

    
194
        public int getFieldCount() throws ReadDriverException {
195
                return 1;
196
        }
197

    
198
        public String getFieldName(int fieldId) throws ReadDriverException {
199
                if(fieldId == 0)
200
                        return "fid";
201
                else
202
                        return "";
203
        }
204

    
205
        public long getRowCount() throws ReadDriverException {
206
                return getShapeCount();
207
        }
208

    
209
        public int getFieldType(int i) throws ReadDriverException {
210
                if(i == 0)
211
                        return Types.NUMERIC;
212
                else
213
                        return -1;
214
        }
215

    
216
        public int getFieldWidth(int i) throws ReadDriverException {
217
                return 20;
218
        }
219

    
220
        public Rectangle2D getShapeBounds(int index) throws ReadDriverException,
221
                        ExpansionFileReadException {
222
                IGeometry geometry = getShape(index);
223
                return geometry.getBounds2D();
224
        }
225

    
226
        public int getShapeType(int index) throws ReadDriverException {
227
                IGeometry geometry = getShape(index);
228
                return geometry.getGeometryType();
229
        }
230

    
231
        private void computeFullExtent() throws ExpansionFileReadException, ReadDriverException {
232

    
233
                 for (int i = 0; i < getRowCount(); i++) {
234
                         Rectangle2D rAux = getShapeBounds(i);
235
                         if (fullExtent == null)
236
                                 fullExtent = rAux;
237
                         else
238
                                 fullExtent.add(rAux);
239
                 }//for
240
        }
241

    
242
        public MappedPositionContainer getMappedPositionContainer() {
243
                return mappedPositionContainer;
244
        }
245

    
246
}