Statistics
| Revision:

root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / drivers / dxf / DXFMemoryDriver.java @ 1100

History | View | Annotate | Download (15.5 KB)

1
/*
2
 * @(#)DXFMemoryDriver    29-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.Color;
49
import java.awt.Font;
50
import java.awt.geom.Point2D;
51
import java.io.File;
52
import java.io.IOException;
53
import java.util.ArrayList;
54

    
55
import org.cresques.cts.IProjection;
56
import org.cresques.cts.ProjectionPool;
57
import org.cresques.io.DxfFile;
58
import org.cresques.px.IObjList;
59
import org.cresques.px.dxf.AcadColor;
60
import org.cresques.px.dxf.DxfFeatureMaker;
61
import org.cresques.px.dxf.DxfHeaderManager;
62
import org.cresques.px.gml.Feature;
63
import org.cresques.px.gml.InsPoint;
64
import org.cresques.px.gml.LineString;
65
import org.cresques.px.gml.Point;
66
import org.cresques.px.gml.Polygon;
67

    
68
import com.hardcode.gdbms.engine.data.DriverException;
69
import com.hardcode.gdbms.engine.data.ReadDriver;
70
import com.hardcode.gdbms.engine.values.IntValue;
71
import com.hardcode.gdbms.engine.values.Value;
72
import com.hardcode.gdbms.engine.values.ValueFactory;
73
import com.iver.cit.gvsig.fmap.core.FPoint3D;
74
import com.iver.cit.gvsig.fmap.core.FPolygon2D;
75
import com.iver.cit.gvsig.fmap.core.FPolyline2D;
76
import com.iver.cit.gvsig.fmap.core.FShape;
77
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
78
import com.iver.cit.gvsig.fmap.core.v02.FConstant;
79
import com.iver.cit.gvsig.fmap.core.v02.FSymbol;
80
import com.iver.cit.gvsig.fmap.drivers.MemoryDriver;
81
import com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend;
82
import com.iver.cit.gvsig.fmap.rendering.Legend;
83
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
84
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
85

    
86
/**
87
 * @author jmorell (jose.morell@gmail.com)
88
 * @version 29-dic-2004
89
 */
90
public class DXFMemoryDriver extends MemoryDriver implements WithDefaultLegend {
91
        private final int ID_FIELD_ID=0;
92
        private final int ID_FIELD_ENTITY=1;
93
        private final int ID_FIELD_LAYER=2;
94
        private final int ID_FIELD_COLOR=3;
95
        private final int ID_FIELD_ELEVATION=4;
96
        private final int ID_FIELD_THICKNESS=5;
97
        private final int ID_FIELD_TEXT=6;
98
        private final int ID_FIELD_HEIGHTTEXT=7;
99
        private final int ID_FIELD_ROTATIONTEXT=8;
100

    
101
        VectorialUniqueValueLegend defaultLegend;
102
        private String path;
103
        private File m_Fich;
104
        
105
        private DxfFile.EntityFactory featureMaker;
106
        private DxfFile dxfFeatureFile;
107
        private IObjList.vector features;
108
        
109
        /**
110
         * Habilita la utilizaci?n del lector del HEADER del DXF.
111
         */
112
        private DxfFile.VarSettings headerManager;
113

    
114
        /* (non-Javadoc)
115
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#open(java.io.File)
116
         */
117
        public void open(File f) throws IOException {
118
            m_Fich = f;
119
        }
120
        
121
        public void initialize() throws IOException {
122
                float heightText = 10;
123
                
124
                IProjection proj = ProjectionPool.get("ed50utm30");
125
                featureMaker = new DxfFeatureMaker(proj);
126
                dxfFeatureFile = new DxfFile(proj, m_Fich.getAbsolutePath(), featureMaker);
127
                dxfFeatureFile.load();
128
                features = (IObjList.vector)((DxfFeatureMaker)featureMaker).getObjects();
129
                
130
                //String acadVersion = (String)((DxfHeaderManager)headerManager).getAcadVersion();
131
                //System.out.println("initialize(): acadVersion = " + acadVersion);
132
                
133
                int nAtt = featureMaker.getAttributes().size();
134
                
135
                // Campos de las MemoryLayer:
136
            Value[] auxRow = new Value[9+nAtt];
137
                ArrayList arrayFields = new ArrayList();
138
                arrayFields.add("ID");
139
                arrayFields.add("Entity");
140
                arrayFields.add("Layer");
141
                arrayFields.add("Color");
142
                arrayFields.add("Elevation");
143
                arrayFields.add("Thickness");
144
                arrayFields.add("Text");
145
                arrayFields.add("HeightText");
146
                arrayFields.add("RotationText");
147
                for (int i=0;i<nAtt;i++) {
148
                        String att[] = new String[2];
149
                        att = (String[])featureMaker.getAttributes().get(i);
150
                        arrayFields.add(att[0]);
151
                }
152
                
153
            getTableModel().setColumnIdentifiers(arrayFields.toArray());
154
                
155
                for (int i=0;i<features.size();i++) {
156
                        
157
            auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0);
158
            auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0);
159
            auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
160
                        
161
                        Feature fea = (Feature)features.get(i);
162
                        if (fea.getGeometry() instanceof Point) {
163
                                Point point = (Point)fea.getGeometry();
164
                                Point2D pto = new Point2D.Double();
165
                                pto = (Point2D.Double)point.get(0);
166
                                FShape nuevoShp;
167
                if (point.isText) {
168
                                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
169
                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
170
                        auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
171
                        int auxInt = Integer.parseInt(fea.getProp("color"));
172
                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
173
                        auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(new String(fea.getProp("text")));
174
                        heightText = Float.parseFloat(fea.getProp("textHeight"));
175
                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(heightText);
176
                        double auxR = Double.parseDouble(fea.getProp("textRotation"));
177
                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(auxR);
178
                        double auxE = Double.parseDouble(fea.getProp("elevation"));
179
                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
180
                        double auxT = Double.parseDouble(fea.getProp("thickness"));
181
                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
182
                        // Attributes
183
                        for (int j=0;j<nAtt;j++) {
184
                                            String[] attributes = new String[2];
185
                                            attributes = (String[])featureMaker.getAttributes().get(j);
186
                                auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
187
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
188
                                        auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
189
                                }
190
                        }
191
                                        nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);
192
                                        addShape(nuevoShp, auxRow);
193
                } else {
194
                        auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
195
                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
196
                        auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
197
                        int auxInt = Integer.parseInt(fea.getProp("color"));
198
                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
199
                        double auxE = Double.parseDouble(fea.getProp("elevation"));
200
                        auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
201
                        double auxT = Double.parseDouble(fea.getProp("thickness"));
202
                        auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
203
                        // Attributes
204
                        for (int j=0;j<nAtt;j++) {
205
                                            String[] attributes = new String[2];
206
                                            attributes = (String[])featureMaker.getAttributes().get(j);
207
                                auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
208
                                if (!fea.getProp(attributes[0]).equals(attributes[1])) {
209
                                        auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
210
                                }
211
                        }
212
                        nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);                                
213
                                        addShape(nuevoShp, auxRow);
214
                }
215
                        } else if (fea.getGeometry() instanceof InsPoint) {
216
                                InsPoint insPoint = (InsPoint)fea.getGeometry();
217
                                Point2D pto = new Point2D.Double();
218
                                pto = (Point2D.Double)insPoint.get(0);
219
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
220
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
221
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
222
                int auxInt = Integer.parseInt(fea.getProp("color"));
223
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
224
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
225
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
226
                    // Attributes
227
                    for (int j=0;j<nAtt;j++) {
228
                                    String[] attributes = new String[2];
229
                                    attributes = (String[])featureMaker.getAttributes().get(j);
230
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
231
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
232
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
233
                        }
234
                    }
235
                                FShape nuevoShp = new FPoint3D(pto.getX(),pto.getY(),0);
236
                                addShape(nuevoShp, auxRow);                                        
237
                        } else if (fea.getGeometry() instanceof LineString) {
238
                                GeneralPathX genPathX = new GeneralPathX();
239
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr()];
240
                                for (int j=0; j<fea.getGeometry().pointNr(); j++) {
241
                                        pts[j] = (Point2D)fea.getGeometry().get(j);
242
                                }
243
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
244
                                for (int j=1; j < pts.length; j++) {
245
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
246
                                }
247
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
248
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
249
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
250
                int auxInt = Integer.parseInt(fea.getProp("color"));
251
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
252
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
253
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
254
                    double auxT = Double.parseDouble(fea.getProp("thickness"));
255
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
256
                    // Attributes
257
                    for (int j=0;j<nAtt;j++) {
258
                                    String[] attributes = new String[2];
259
                                    attributes = (String[])featureMaker.getAttributes().get(j);
260
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
261
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
262
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
263
                        }
264
                    }
265
                                FShape nuevoShp = new FPolyline2D(genPathX);
266
                                addShape(nuevoShp, auxRow);
267
                        } else if (fea.getGeometry() instanceof Polygon) {
268
                                GeneralPathX genPathX = new GeneralPathX();
269
                                // 050112: A?ado una posici?n m?s para el punto que cierra y 
270
                                //                   creo el objeto firstPt.
271
                                Point2D firstPt = new Point2D.Double();
272
                                firstPt = (Point2D)fea.getGeometry().get(0);
273
                                Point2D[] pts = new Point2D[fea.getGeometry().pointNr()+1];
274
                                for (int j=0; j<fea.getGeometry().pointNr(); j++) {
275
                                        pts[j] = (Point2D)fea.getGeometry().get(j);
276
                                }
277
                                // 050112: A?ado el primer punto al final para cerrar los pol?gonos.
278
                                pts[fea.getGeometry().pointNr()] = firstPt;
279
                                genPathX.moveTo(pts[0].getX(), pts[0].getY());
280
                                for (int j=1; j < pts.length; j++) {
281
                                        genPathX.lineTo(pts[j].getX(), pts[j].getY());
282
                                }
283
                auxRow[ID_FIELD_ID] = ValueFactory.createValue(i);
284
                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(new String(fea.getProp("dxfEntity")));
285
                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(new String(fea.getProp("layer")));
286
                int auxInt = Integer.parseInt(fea.getProp("color"));
287
                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(auxInt);
288
                    double auxE = Double.parseDouble(fea.getProp("elevation"));
289
                    auxRow[ID_FIELD_ELEVATION] = ValueFactory.createValue(auxE);
290
                    double auxT = Double.parseDouble(fea.getProp("thickness"));
291
                    auxRow[ID_FIELD_THICKNESS] = ValueFactory.createValue(auxT);
292
                    // Attributes
293
                    for (int j=0;j<nAtt;j++) {
294
                                    String[] attributes = new String[2];
295
                                    attributes = (String[])featureMaker.getAttributes().get(j);
296
                        auxRow[9+j] = ValueFactory.createValue(new String((String)attributes[1]));
297
                        if (!fea.getProp(attributes[0]).equals(attributes[1])) {
298
                                auxRow[9+j] = ValueFactory.createValue(new String(fea.getProp(attributes[0])));
299
                        }
300
                    }
301
                                FShape nuevoShp = new FPolygon2D(genPathX);
302
                                addShape(nuevoShp, auxRow);
303
                        } else {
304
                                System.out.println("Detectado feature desconocido");
305
                        }
306
                }
307
                
308
                defaultLegend = LegendFactory.createVectorialUniqueValueLegend(getShapeType());
309
                defaultLegend.setFieldName("Color");
310
                
311
                defaultLegend.setLabelField("Text");
312
                defaultLegend.setDefaultSymbol(new FSymbol(getShapeType()));
313
                defaultLegend.getDefaultSymbol().setShapeVisible(false);
314
                defaultLegend.getDefaultSymbol().setFontSizeInPixels(false);
315
                defaultLegend.getDefaultSymbol().setFont(new Font("SansSerif", Font.PLAIN, 9));
316
                defaultLegend.getDefaultSymbol().setFontColor(Color.BLACK);
317
                defaultLegend.getDefaultSymbol().setFontSize(heightText);
318
                defaultLegend.getDefaultSymbol().setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE);
319
                defaultLegend.setLabelHeightField("HeightText");
320
                defaultLegend.setLabelRotationField("RotationText");
321
                
322
        ReadDriver rs = this;
323
                IntValue clave;
324
                FSymbol theSymbol = null;
325
                
326
                try {
327
                        for (long j = 0; j < rs.getRowCount(); j++)
328
                        {
329
                                clave = (IntValue)rs.getFieldValue(j,ID_FIELD_COLOR);
330
                                if (defaultLegend.getSymbolByValue(clave) == null)
331
                                {
332
                                        theSymbol = new FSymbol(getShapeType());
333
                                        theSymbol.setDescription(clave.toString());
334
                                        theSymbol.setColor(AcadColor.getColor(clave.getValue()));
335
                                        theSymbol.setStyle(FConstant.SYMBOL_STYLE_MARKER_SQUARE);
336
                                        defaultLegend.addSymbol(clave, theSymbol);
337
                                }
338
                        } // for
339
                } catch (DriverException e) {
340
                        e.printStackTrace();
341
                        throw new IOException("Error al poner la leyenda por defecto en el DXF");
342
                }
343
        }
344

    
345
        /* (non-Javadoc)
346
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#accept(java.io.File)
347
         */
348
        public boolean accept(File f) {
349
                return f.getName().toUpperCase().endsWith("DXF");
350
        }
351

    
352
        /* (non-Javadoc)
353
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getShapeType()
354
         */
355
        public int getShapeType() {
356
                return FShape.MULTI;
357
        }
358

    
359
        /* (non-Javadoc)
360
         * @see com.iver.cit.gvsig.fmap.drivers.MemoryDriver#getName()
361
         */
362
        public String getName() {
363
                return "gvSIG DXF Memory Driver";
364
        }
365

    
366
        /* (non-Javadoc)
367
         * @see com.iver.cit.gvsig.fmap.drivers.WithDefaultLegend#getDefaultLegend()
368
         */
369
        public Legend getDefaultLegend() {
370
                return defaultLegend;
371
        }
372

    
373
}