Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFDriver.java @ 1198

History | View | Annotate | Download (9.1 KB)

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

    
48
import java.awt.geom.Point2D;
49
import java.awt.geom.Rectangle2D;
50
import java.io.File;
51
import java.io.IOException;
52

    
53
import org.cresques.cts.IProjection;
54
import org.cresques.cts.ProjectionPool;
55
import org.cresques.io.DxfFile;
56
import org.cresques.px.Extent;
57
import org.cresques.px.IObjList;
58
import org.cresques.px.dxf.DxfFeatureMaker;
59
import org.cresques.px.gml.Feature;
60
import org.cresques.px.gml.Geometry;
61
import org.cresques.px.gml.InsPoint;
62
import org.cresques.px.gml.LineString;
63
import org.cresques.px.gml.Point;
64
import org.cresques.px.gml.Polygon;
65

    
66
import com.hardcode.gdbms.engine.data.DriverException;
67
import com.hardcode.gdbms.engine.data.FileDriver;
68
import com.hardcode.gdbms.engine.values.Value;
69
import com.iver.cit.gvsig.fmap.core.FShape;
70
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
71
import com.iver.cit.gvsig.fmap.core.IGeometry;
72
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
73
import com.iver.cit.gvsig.fmap.drivers.BoundedShapes;
74
import com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver;
75
import com.iver.cit.gvsig.fmap.drivers.shp.SHP;
76

    
77
/**
78
 * @author jmorell (jose.morell@gmail.com)
79
 * @version 13-dic-2004
80
 */
81
public class DXFDriver implements VectorialFileDriver, BoundedShapes, FileDriver {
82

    
83
        private String path;
84
        
85
        private final int ID_FIELD_ID=0;
86
        private final int ID_FIELD_ENTITY=1;
87
        private final int ID_FIELD_LAYER=2;
88
        private final int ID_FIELD_COLOR=3;
89
        private final int ID_FIELD_ELEVATION=4;
90
        private final int ID_FIELD_THICKNESS=5;
91
        private final int ID_FIELD_TEXT=6;
92
        private final int ID_FIELD_HEIGHTTEXT=7;
93
        private final int ID_FIELD_ROTATIONTEXT=8;
94
        
95
        private DxfFile.EntityFactory featureMaker;
96
        private DxfFile dxfFeatureFile;
97
        
98
        private IObjList.vector features;
99
        private File dxfFile = null;
100
        
101
        /* (non-Javadoc)
102
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#open(java.io.File)
103
         */
104
        public void open(File f) throws IOException {
105
                dxfFile = f;
106
        }
107

    
108
        /* (non-Javadoc)
109
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#close()
110
         */
111
        public void close() throws IOException {
112
                // TODO Auto-generated method stub
113
        }
114

    
115
        /* (non-Javadoc)
116
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShape(int)
117
         */
118
        public IGeometry getShape(int i) throws IOException {
119
                Feature fea = (Feature)features.get(i);
120
                if (fea.getGeometry() instanceof Point) {
121
                        Point point = (Point)fea.getGeometry();
122
                        Point2D pto = new Point2D.Double();
123
                        pto = (Point2D.Double)point.get(0);
124
                //DGNElemMultiPoint psLine = new DGNElemMultiPoint();
125
                //return ShapeFactory.createPoint3D(psLine.vertices[0].x, psLine.vertices[0].y,psLine.vertices[0].z);
126
                return ShapeFactory.createPoint3D(pto.getX(),pto.getY(),0);
127
                } else if (fea.getGeometry() instanceof InsPoint) {
128
                        InsPoint insPoint = (InsPoint)fea.getGeometry();
129
                        Point2D pto = new Point2D.Double();
130
                        pto = (Point2D.Double)insPoint.get(0);
131
                        //GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD);
132
                        //return ShapeFactory.createPolygon2D(elShape);
133
                        return ShapeFactory.createPoint3D(pto.getX(),pto.getY(),0);
134
                } else if (fea.getGeometry() instanceof LineString) {
135
                        GeneralPathX genPathX = new GeneralPathX();
136
                        Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
137
                        for (int j=0; j<fea.getGeometry().pointNr(); j++) {
138
                                pts[j] = (Point2D)fea.getGeometry().get(j);
139
                        }
140
                        genPathX.moveTo(pts[0].getX(), pts[0].getY());
141
                        for (int j=1; j < pts.length; j++) {
142
                                genPathX.lineTo(pts[j].getX(), pts[j].getY());
143
                        }
144
                        double[] zs = new double[1];
145
                        zs[0] = 0.0;
146
                        return ShapeFactory.createPolyline3D(genPathX,zs);
147
                } else if (fea.getGeometry() instanceof Polygon) {
148
                        GeneralPathX genPathX = new GeneralPathX();
149
                        Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
150
                        for (int j=0; j<fea.getGeometry().pointNr(); j++) {
151
                                pts[j] = (Point2D)fea.getGeometry().get(j);
152
                        }
153
                        genPathX.moveTo(pts[0].getX(), pts[0].getY());
154
                        for (int j=1; j < pts.length; j++) {
155
                                genPathX.lineTo(pts[j].getX(), pts[j].getY());
156
                        }
157
                        return ShapeFactory.createPolygon2D(genPathX);
158
                } else {
159
                        System.out.println("getShape(): ERROR:Geometr?a desconocida.");
160
                        return null;
161
                }
162
        }
163

    
164
        /* (non-Javadoc)
165
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getShapeCount()
166
         */
167
        public int getShapeCount() throws IOException {
168
                return features.size();
169
        }
170

    
171
        /* (non-Javadoc)
172
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getFullExtent()
173
         */
174
        public Rectangle2D getFullExtent() throws IOException {
175
                return (Rectangle2D)featureMaker.getExtent().toRectangle2D();
176
        }
177

    
178
        /* (non-Javadoc)
179
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#initialize()
180
         */
181
        public void initialize() throws IOException {
182
                IProjection proj = ProjectionPool.get("EPSG:23030");
183
                featureMaker = new DxfFeatureMaker(proj);
184
                dxfFeatureFile = new DxfFile(proj, dxfFile.getAbsolutePath(), featureMaker);
185
                dxfFeatureFile.load();
186
                features = (IObjList.vector)((DxfFeatureMaker)featureMaker).getObjects();
187
        }
188

    
189
        /* (non-Javadoc)
190
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#accept(java.io.File)
191
         */
192
        public boolean accept(File f) {
193
                return f.getName().toUpperCase().endsWith("DXF");
194
        }
195

    
196
        /* (non-Javadoc)
197
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataDriverName()
198
         */
199
        public String getDataDriverName() {
200
                // TODO Auto-generated method stub
201
                return null;
202
        }
203

    
204
        /* (non-Javadoc)
205
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialFileDriver#getDataFile(java.io.File)
206
         */
207
        public File getDataFile(File f) {
208
                // TODO Auto-generated method stub
209
                return null;
210
        }
211

    
212
        /* (non-Javadoc)
213
         * @see com.iver.cit.gvsig.fmap.drivers.VectorialDriver#getShapeType()
214
         */
215
        public int getShapeType() {
216
                return 1;
217
        }
218

    
219
        /* (non-Javadoc)
220
         * @see com.hardcode.driverManager.Driver#getName()
221
         */
222
        public String getName() {
223
                return "gvSIG DXF Driver";
224
        }
225

    
226
        /* (non-Javadoc)
227
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeBounds(int)
228
         */
229
        public Rectangle2D getShapeBounds(int index) throws IOException {
230
                Feature fea = (Feature)features.get(index);
231
                Geometry geo = (Geometry)fea.getGeometry();
232
                return ((Extent)(geo.getExtent())).toRectangle2D();
233
        }
234

    
235
        /* (non-Javadoc)
236
         * @see com.iver.cit.gvsig.fmap.drivers.BoundedShapes#getShapeType(int)
237
         */
238
        public int getShapeType(int index) {
239
        int auxType = 0;
240
        Feature fea = (Feature)features.get(index);
241
                if (fea.getGeometry() instanceof Point) {
242
            auxType = auxType | FShape.POINT;
243
                } else if (fea.getGeometry() instanceof InsPoint) {
244
            auxType = auxType | FShape.POINT;
245
                } else if (fea.getGeometry() instanceof LineString) {
246
            auxType = auxType | FShape.LINE;
247
                } else if (fea.getGeometry() instanceof Polygon) {
248
            auxType = auxType | FShape.POLYGON;
249
                } else {
250
                        System.out.println("getShape(): ERROR:Geometr?a desconocida.");
251
                }
252
        return auxType;
253
        }
254

    
255
        /* (non-Javadoc)
256
         * @see com.hardcode.gdbms.engine.data.FileDriver#fileAccepted(java.io.File)
257
         */
258
        public boolean fileAccepted(File f) {
259
                return f.getName().toUpperCase().endsWith("DXF");
260
        }
261

    
262
        /* (non-Javadoc)
263
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldValue(long, int)
264
         */
265
        public Value getFieldValue(long arg0, int arg1) throws DriverException {
266
                // TODO Auto-generated method stub
267
                return null;
268
        }
269

    
270
        /* (non-Javadoc)
271
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldCount()
272
         */
273
        public int getFieldCount() throws DriverException {
274
                // TODO Auto-generated method stub
275
                return 0;
276
        }
277

    
278
        /* (non-Javadoc)
279
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getFieldName(int)
280
         */
281
        public String getFieldName(int arg0) throws DriverException {
282
                // TODO Auto-generated method stub
283
                return null;
284
        }
285

    
286
        /* (non-Javadoc)
287
         * @see com.hardcode.gdbms.engine.data.ReadDriver#getRowCount()
288
         */
289
        public long getRowCount() throws DriverException {
290
                try {
291
                        return getShapeCount();
292
                } catch (IOException e) {
293
                        e.printStackTrace();
294
                }
295
                return -1;
296
                //return 0;
297
        }
298

    
299
}