Statistics
| Revision:

root / trunk / libraries / libDataSourceBaseDrivers / src / org / gvsig / data / datastores / vectorial / driver / dgn / DGNStore.java @ 19741

History | View | Annotate | Download (31.7 KB)

1
package org.gvsig.data.datastores.vectorial.driver.dgn;
2

    
3
import java.awt.geom.AffineTransform;
4
import java.awt.geom.Arc2D;
5
import java.io.File;
6
import java.sql.Types;
7
import java.util.ArrayList;
8
import java.util.Collection;
9
import java.util.Iterator;
10
import java.util.List;
11

    
12
import org.gvsig.data.IDataCollection;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.datastores.vectorial.driver.IFeaturesReader;
15
import org.gvsig.data.datastores.vectorial.driver.IFeaturesWriter;
16
import org.gvsig.data.datastores.vectorial.driver.base.MemoryStore;
17
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemArc;
18
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemCellHeader;
19
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemComplexHeader;
20
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemCore;
21
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemMultiPoint;
22
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNElemText;
23
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNFileHeader;
24
import org.gvsig.data.datastores.vectorial.driver.dgn.filedgn.DGNReader;
25
import org.gvsig.data.exception.CloseException;
26
import org.gvsig.data.exception.InitializeException;
27
import org.gvsig.data.exception.OpenException;
28
import org.gvsig.data.exception.ReadException;
29
import org.gvsig.data.exception.WriteException;
30
import org.gvsig.data.spatialprovisional.IExtent;
31
import org.gvsig.data.vectorial.DefaultAttributeDescriptor;
32
import org.gvsig.data.vectorial.DefaultFeatureType;
33
import org.gvsig.data.vectorial.FeatureStoreNotification;
34
import org.gvsig.data.vectorial.IFeature;
35
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
36
import org.gvsig.data.vectorial.IFeatureCollection;
37
import org.gvsig.data.vectorial.IFeatureID;
38
import org.gvsig.data.vectorial.IFeatureType;
39
import org.gvsig.exceptions.BaseException;
40
import org.gvsig.metadata.IMetadata;
41
import org.gvsig.metadata.IMetadataManager;
42
import org.gvsig.metadata.MetadataManager;
43

    
44
import com.iver.cit.gvsig.fmap.core.FShape;
45
import com.iver.cit.gvsig.fmap.core.GeneralPathX;
46
import com.iver.cit.gvsig.fmap.core.IGeometry;
47
import com.iver.cit.gvsig.fmap.core.ShapeFactory;
48
import com.iver.cit.gvsig.fmap.core.SymbologyFactory;
49
import com.iver.cit.gvsig.fmap.core.symbols.ISymbol;
50
import com.iver.cit.gvsig.fmap.rendering.LegendFactory;
51
import com.iver.cit.gvsig.fmap.rendering.VectorialUniqueValueLegend;
52
import com.iver.cit.gvsig.fmap.rendering.styling.labeling.AttrInTableLabelingStrategy;
53

    
54
public class DGNStore extends MemoryStore{
55
        public static String DATASTORE_NAME = "DGNStore";
56
        private final int ID_FIELD_ID = 0;
57
        private final int ID_FIELD_ENTITY = 1;
58
        private final int ID_FIELD_LAYER = 2;
59
        private final int ID_FIELD_COLOR = 3;
60
        private final int ID_FIELD_HEIGHTTEXT = 4;
61
        private final int ID_FIELD_ROTATIONTEXT = 5;
62
        private final int ID_FIELD_TEXT = 6;
63
        private final int ID_FIELD_GEOMETRY = 7;
64

    
65
        private List featureTypes = new ArrayList();//<IFeatureType>
66
        private IFeatureType featureType;
67
        protected IMetadata metadata;
68
        private DGNStoreParameters dgnParameters=null;
69
        private File dgnFile;
70
        private DGNReader m_DgnReader;
71
        private AttrInTableLabelingStrategy labeling;
72
        private VectorialUniqueValueLegend defaultLegend;
73

    
74
         public void init(IDataStoreParameters parameters) throws InitializeException {
75
                super.init(parameters);
76
                    dgnParameters=(DGNStoreParameters)parameters;
77
                    dgnFile=dgnParameters.getDGNFile();
78
                }
79

    
80
         private void initializeFeatureType() {
81
                        featureType= new DefaultFeatureType();
82

    
83
                        DefaultAttributeDescriptor descriptorID = new DefaultAttributeDescriptor();
84
                        descriptorID.setType(IFeatureAttributeDescriptor.TYPE_INT);
85
                        descriptorID.setName("ID");
86
                        descriptorID.setDefaultValue(new Integer(0));
87
                        featureType.add(descriptorID);
88
                        featureType.setFieldId("ID");
89

    
90
                        DefaultAttributeDescriptor descriptorEntity = new DefaultAttributeDescriptor();
91
                        descriptorEntity.setType(IFeatureAttributeDescriptor.TYPE_STRING);
92
                        descriptorEntity.setName("Entity");
93
                        descriptorEntity.setDefaultValue("Entity");
94
                        featureType.add(descriptorEntity);
95

    
96
                        DefaultAttributeDescriptor descriptorLayer = new DefaultAttributeDescriptor();
97
                        descriptorLayer.setType(IFeatureAttributeDescriptor.TYPE_INT);
98
                        descriptorLayer.setName("Layer");
99
                        descriptorLayer.setDefaultValue(new Integer(0));
100
                        featureType.add(descriptorLayer);
101

    
102
                        DefaultAttributeDescriptor descriptorColor = new DefaultAttributeDescriptor();
103
                        descriptorColor.setType(IFeatureAttributeDescriptor.TYPE_INT);
104
                        descriptorColor.setName("Color");
105
                        descriptorColor.setDefaultValue(new Integer(0));
106
                        featureType.add(descriptorColor);
107

    
108
//                        DefaultAttributeDescriptor descriptorElevation = new DefaultAttributeDescriptor();
109
//                        descriptorElevation.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
110
//                        descriptorElevation.setName("Elevation");
111
//                        descriptorElevation.setDefaultValue(new Double(0));
112
//                        featureType.add(descriptorElevation);
113
        //
114
//                        DefaultAttributeDescriptor descriptorThickness = new DefaultAttributeDescriptor();
115
//                        descriptorThickness.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
116
//                        descriptorThickness.setName("Thickness");
117
//                        descriptorThickness.setDefaultValue(new Double(0));
118
//                        featureType.add(descriptorThickness);
119

    
120
                        DefaultAttributeDescriptor descriptorHeightText = new DefaultAttributeDescriptor();
121
                        descriptorHeightText.setType(IFeatureAttributeDescriptor.TYPE_FLOAT);
122
                        descriptorHeightText.setName("HeightText");
123
                        descriptorHeightText.setDefaultValue(new Float(0));
124
                        featureType.add(descriptorHeightText);
125

    
126
                        DefaultAttributeDescriptor descriptorRotationText = new DefaultAttributeDescriptor();
127
                        descriptorRotationText.setType(IFeatureAttributeDescriptor.TYPE_DOUBLE);
128
                        descriptorRotationText.setName("RotationText");
129
                        descriptorRotationText.setDefaultValue(new Double(0));
130
                        featureType.add(descriptorRotationText);
131

    
132
                        DefaultAttributeDescriptor descriptorText = new DefaultAttributeDescriptor();
133
                        descriptorText.setType(IFeatureAttributeDescriptor.TYPE_STRING);
134
                        descriptorText.setName("Text");
135
                        descriptorText.setDefaultValue("");
136
                        featureType.add(descriptorText);
137

    
138

    
139

    
140

    
141

    
142
                        DefaultAttributeDescriptor descriptorShape = new DefaultAttributeDescriptor();
143
                        descriptorShape.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY);
144
                        descriptorShape.setName("GEOMETRY");
145
                        descriptorShape.setDefaultValue(null);
146
                        featureType.add(descriptorShape);
147

    
148
                }
149
        protected void doFinishEdition() throws WriteException, ReadException {
150
                IFeaturesWriter writer = getFeaturesWriter();
151
        writer.init(this);
152
        writer.updateFeatureType(featureType);
153
        writer.preProcess();
154
        Collection collection=getDataCollection();
155
        Iterator iterator=collection.iterator();
156
        IFeature feature;
157
        while (iterator.hasNext()) {
158
                feature= (IFeature) iterator.next();
159
                        writer.insertFeature(feature);
160
                }
161
        writer.postProcess();
162

    
163
        }
164

    
165
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
166
                if (type==null){
167
                        type=getDefaultFeatureType();
168
                }
169
                IDataCollection dataCollection= getFeatutesReader().getFeatures(featureManager,type,filter,order);
170
            this.addObserver(dataCollection);
171
            return dataCollection;
172
        }
173

    
174
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
175
                if (this.alterMode){
176
                    if (featureManager.contains(id)) {
177
                            return featureManager.getFeature(id);
178
                    }
179
            }
180
                return id.getFeature(featureType);
181
        }
182

    
183
        public List getFeatureTypes() {
184
                featureTypes.set(0,getDefaultFeatureType());
185
        return featureTypes;
186
        }
187

    
188
        public IFeatureType getDefaultFeatureType() {
189
                IFeatureType ft = featureType;
190
            if (isEditing()){
191
//                    Aqu? hay que construir un FeatureType con los cambios que se hayan hecho en la edici?n.
192
                    return attributeManager.getFeatureType(ft);
193
            }
194
        return ft;
195
        }
196

    
197
        public boolean isWithDefaultLegend() {
198
                return false;
199
        }
200

    
201
        public Object getDefaultLegend() {
202
                return null;
203
        }
204

    
205
        public Object getDefaultLabelingStrategy() {
206
                return null;
207
        }
208

    
209
        public boolean canAlterFeatureType() {
210
                return true;
211
        }
212

    
213
        public String getName() {
214
                return DATASTORE_NAME;
215
        }
216

    
217
        public void open() throws OpenException {
218
                this.observable.notifyObservers(
219
                                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_OPEN)
220
                );
221
                float heightText = 10;
222
//                attr.setLoadedInMemory(true);
223
                initializeFeatureType();
224
                try {
225
                        m_DgnReader = new DGNReader(dgnFile.getAbsolutePath());
226
                } catch (ReadException e) {
227
                        throw new OpenException(getName(),e);
228
                }
229

    
230

    
231
//                Value[] auxRow = new Value[7];
232
//                Value[] cellRow = new Value[7];
233
//                Value[] complexRow = new Value[7];
234
                IFeature auxFeature=new DGNFeature(featureType,true);
235
                IFeature cellFeature=new DGNFeature(featureType,false);
236
                IFeature complexFeature=new DGNFeature(featureType,false);
237
                initializeFeatureType();
238
//                ArrayList arrayFields = new ArrayList();
239
//                arrayFields.add("ID");
240
//                arrayFields.add("Entity");
241
//                arrayFields.add("Layer");
242
//                arrayFields.add("Color");
243
//                arrayFields.add("HeightText");
244
//                arrayFields.add("RotationText");
245
//                arrayFields.add("Text");
246

    
247
//                getTableModel().setColumnIdentifiers(arrayFields.toArray());
248

    
249
                // jaume
250
                labeling = new AttrInTableLabelingStrategy();
251
                ((AttrInTableLabelingStrategy) labeling).setTextFieldId(featureType.getFieldIndex("Text"));
252
                ((AttrInTableLabelingStrategy) labeling).setRotationFieldId(featureType.getFieldIndex("RotationText"));
253
                ((AttrInTableLabelingStrategy) labeling).setHeightFieldId(featureType.getFieldIndex("HeightText"));
254
                ((AttrInTableLabelingStrategy) labeling).setUnit(1); //MapContext.NAMES[1] (meters)
255

    
256

    
257
                // Ahora las rellenamos.
258
//                FShape aux;
259
                boolean bElementoCompuesto = false;
260
                boolean bEsPoligono = false;
261
                boolean bInsideCell = false;
262
                boolean bFirstHoleEntity = false;
263
                boolean bConnect = false; // Se usa para que los pol?gonos cierren bien cuando son formas compuestas
264
//                int contadorSubElementos = 0;
265
//                int numSubElementos = 0;
266
                int complex_index_fill_color = -1;
267
                int nClass; // Para filtrar los elementos de construcci?n, etc.
268
                GeneralPathX elementoCompuesto = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD);
269

    
270
                for (int id = 0; id < m_DgnReader.getNumEntities(); id++) {
271
                        // System.out.println("Elemento " + id + " de " + m_DgnReader.getNumEntities());
272
                        m_DgnReader.DGNGotoElement(id);
273

    
274
                        DGNElemCore elemento = m_DgnReader.DGNReadElement();
275
                        nClass = 0;
276
//                        auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(0);
277
//                        auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(0);
278
//                        auxRow[ID_FIELD_TEXT] = ValueFactory.createNullValue();
279

    
280
                        if (elemento.properties != 0) {
281
                                nClass = elemento.properties & DGNFileHeader.DGNPF_CLASS;
282
                        }
283

    
284
                        if ((elemento != null) && (elemento.deleted == 0) && (nClass == 0)) //Leer un elemento
285
                         {
286
//                                aux = null;
287

    
288
                                // if ((elemento.element_id > 3800) && (elemento.element_id < 3850))
289
                                //         m_DgnReader.DGNDumpElement(m_DgnReader.getInfo(),elemento,"");
290
                                if ((elemento.stype == DGNFileHeader.DGNST_MULTIPOINT) ||
291
                                                (elemento.stype == DGNFileHeader.DGNST_ARC) ||
292
                                                (elemento.stype == DGNFileHeader.DGNST_CELL_HEADER) ||
293
                                                (elemento.stype == DGNFileHeader.DGNST_SHARED_CELL_DEFN) ||
294
                                                (elemento.stype == DGNFileHeader.DGNST_COMPLEX_HEADER)) {
295
                                        if (elemento.complex != 0) {
296
                                                bElementoCompuesto = true;
297
                                        } else {
298
                                                if (bElementoCompuesto) {
299
                                                        if (bInsideCell) {
300
                                                                auxFeature.set(ID_FIELD_ENTITY,cellFeature.get(ID_FIELD_ENTITY));
301
//                                                                auxRow[ID_FIELD_ENTITY] = cellRow[ID_FIELD_ENTITY];
302
                                                        } else {
303
                                                                auxFeature=complexFeature;
304
//                                                                auxRow = complexRow;
305
                                                        }
306

    
307
                                                        // System.err.println("Entidad compuesta. bInsideCell = " + bInsideCell + " auxRow = " + auxRow[ID_FIELD_ENTITY]);
308
                                                        IGeometry geom=ShapeFactory.createPolyline2D(elementoCompuesto);
309
                                                        auxFeature.set(ID_FIELD_GEOMETRY,geom);
310
                                                        auxFeature.setDefaultGeometry(geom);
311
                                                        addFeature(auxFeature);
312
//                                                        addShape(new FPolyline2D(elementoCompuesto), auxRow);
313

    
314
                                                        if (bEsPoligono) {
315
                                                                if (complex_index_fill_color != -1) {
316
                                                                        auxFeature.set(ID_FIELD_COLOR,complex_index_fill_color);
317
//                                                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(complex_index_fill_color);
318
                                                                }
319
                                                                geom=ShapeFactory.createPolygon2D(elementoCompuesto);
320
                                                                auxFeature.set(ID_FIELD_GEOMETRY,geom);
321
                                                                auxFeature.setDefaultGeometry(geom);
322
                                                                addFeature(auxFeature);
323
//                                                                addShape(new FPolygon2D(elementoCompuesto),        auxRow);
324
                                                        }
325

    
326
                                                        elementoCompuesto = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD);
327
                                                }
328

    
329
                                                // System.err.println("Entidad simple");
330
                                                bElementoCompuesto = false;
331
                                                bEsPoligono = false;
332
                                                bConnect = false;
333

    
334
                                                // elementoCompuesto = new GeneralPathX();
335
                                                bInsideCell = false;
336
                                        }
337
                                }
338

    
339
                                switch (elemento.stype) {
340
                                        case DGNFileHeader.DGNST_SHARED_CELL_DEFN:
341
                                                bInsideCell = true;
342
                                                cellFeature.set(ID_FIELD_ID,elemento.element_id);
343
//                                                cellRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
344
                                                cellFeature.set(ID_FIELD_LAYER,elemento.level);
345
//                                                cellRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
346
                                                cellFeature.set(ID_FIELD_COLOR,elemento.color);
347
//                                                cellRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
348
                                                cellFeature.set(ID_FIELD_ENTITY,"Shared Cell");
349
//                                                cellRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
350
//                                                                "Shared Cell");
351

    
352
                                                break;
353

    
354
                                        case DGNFileHeader.DGNST_CELL_HEADER:
355
                                                bInsideCell = true;
356

    
357
                                                DGNElemCellHeader psCellHeader = (DGNElemCellHeader) elemento;
358
                                                cellFeature.set(ID_FIELD_ID,elemento.element_id);
359
//                                                cellRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
360
                                                cellFeature.set(ID_FIELD_LAYER,elemento.level);
361
//                                                cellRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
362
                                                cellFeature.set(ID_FIELD_COLOR,elemento.color);
363
//                                                cellRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
364
                                                cellFeature.set(ID_FIELD_ENTITY,"Cell");
365
//                                                cellRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
366
//                                                                "Cell");
367
                                                complex_index_fill_color = m_DgnReader.DGNGetShapeFillInfo(elemento);
368

    
369
                                                // System.err.println("Cell Header " + complex_index_fill_color);
370
                                                break;
371

    
372
                                        case DGNFileHeader.DGNST_COMPLEX_HEADER:
373

    
374
                                                // bElementoCompuesto = true;
375
                                                // System.err.println("Complex Header");
376
//                                                contadorSubElementos = 0;
377

    
378
                                                DGNElemComplexHeader psComplexHeader = (DGNElemComplexHeader) elemento;
379
//                                                numSubElementos = psComplexHeader.numelems;
380
                                                complexFeature.set(ID_FIELD_ID,elemento.element_id);
381
//                                                complexRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
382
                                                complexFeature.set(ID_FIELD_LAYER,elemento.level);
383
//                                                complexRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
384
                                                complexFeature.set(ID_FIELD_COLOR,elemento.color);
385
//                                                complexRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
386
                                                complexFeature.set(ID_FIELD_ENTITY,"Complex");
387
//                                                complexRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
388
//                                                                "Complex");
389

    
390
                                                if (psComplexHeader.type == DGNFileHeader.DGNT_COMPLEX_SHAPE_HEADER) {
391
                                                        bEsPoligono = true;
392

    
393
                                                        // Si es un agujero, no conectamos con el anterior
394
                                                        if ((psComplexHeader.properties & 0x8000) != 0) {
395
                                                                bFirstHoleEntity = true;
396
                                                        } else {
397
                                                                // Miramos si tiene color de relleno
398
                                                                // complex_index_fill_color = -1;
399
                                                                // if (elemento.attr_bytes > 0) {
400
                                                                complex_index_fill_color = m_DgnReader.DGNGetShapeFillInfo(elemento);
401

    
402
                                                                // System.err.println("complex shape fill color = " + elemento.color);
403
                                                                // }
404
                                                        }
405

    
406
                                                        bConnect = true;
407
                                                } else {
408
                                                        bEsPoligono = false;
409
                                                        bConnect = false;
410
                                                }
411

    
412
                                                break;
413

    
414
                                        case DGNFileHeader.DGNST_MULTIPOINT:
415

    
416
                                                // OJO: Si lo que viene en este multipoint es un elemento con type=11 (curve), se trata de una "parametric
417
                                                // spline curve". La vamos a tratar como si no fuera curva, pero seg?n la documentaci?n, los 2 primeros puntos
418
                                                // y los 2 ?ltimos puntos definen "endpoint derivatives" y NO se muestran.
419
                                                // TODAV?A HAY UN PEQUE?O FALLO CON EL FICHERO dgn-sample.dgn, pero lo dejo por ahora.
420
                                                // Es posible que tenga que ver con lo de los arcos (arco distorsionado), que
421
                                                // todav?a no est? metido.
422
                                                DGNElemMultiPoint psLine = (DGNElemMultiPoint) elemento;
423
                                                auxFeature.set(ID_FIELD_ID,elemento.element_id);
424
//                                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
425
                                                auxFeature.set(ID_FIELD_ENTITY,"Multipoint");
426
//                                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
427
//                                                                "Multipoint");
428
                                                auxFeature.set(ID_FIELD_LAYER,elemento.level);
429
//                                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
430
                                                auxFeature.set(ID_FIELD_COLOR,elemento.color);
431
//                                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
432

    
433
                                                if ((psLine.num_vertices == 2) &&
434
                                                                (psLine.vertices[0].x == psLine.vertices[1].x) &&
435
                                                                (psLine.vertices[0].y == psLine.vertices[1].y)) {
436
                                                        auxFeature.set(ID_FIELD_ENTITY,"Point");
437
//                                                        auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
438
//                                                                        "Point");
439

    
440
                                                        IGeometry geom=ShapeFactory.createPoint3D(psLine.vertices[0].x,
441
                                                                        psLine.vertices[0].y, psLine.vertices[0].z);
442
                                                        auxFeature.set(ID_FIELD_GEOMETRY,geom);
443
                                                        auxFeature.setDefaultGeometry(geom);
444
                                                        addFeature(auxFeature);
445

    
446
//                                                        addShape(new FPoint3D(psLine.vertices[0].x,
447
//                                                                        psLine.vertices[0].y, psLine.vertices[0].z),
448
//                                                                auxRow);
449
                                                } else {
450
                                                        GeneralPathX elShape = new GeneralPathX(GeneralPathX.WIND_EVEN_ODD);
451

    
452
                                                        if (psLine.type == DGNFileHeader.DGNT_CURVE) {
453
                                                                psLine.num_vertices = psLine.num_vertices - 4;
454

    
455
                                                                for (int aux_n = 0;
456
                                                                                aux_n < psLine.num_vertices; aux_n++) {
457
                                                                        psLine.vertices[aux_n] = psLine.vertices[aux_n +
458
                                                                                2];
459
                                                                }
460
                                                        }
461

    
462
                                                        if ((psLine.type == DGNFileHeader.DGNT_SHAPE) &&
463
                                                                        ((psLine.properties & 0x8000) != 0)) {
464
                                                                // Invertimos el orden porque es un agujero
465
                                                                elShape.moveTo(psLine.vertices[psLine.num_vertices -
466
                                                                        1].x,
467
                                                                        psLine.vertices[psLine.num_vertices - 1].y);
468

    
469
                                                                for (int i = psLine.num_vertices - 2; i >= 0;
470
                                                                                i--)
471
                                                                        elShape.lineTo(psLine.vertices[i].x,
472
                                                                                psLine.vertices[i].y);
473
                                                        } else {
474
                                                                elShape.moveTo(psLine.vertices[0].x,
475
                                                                        psLine.vertices[0].y);
476

    
477
                                                                for (int i = 1; i < psLine.num_vertices; i++)
478
                                                                        elShape.lineTo(psLine.vertices[i].x,
479
                                                                                psLine.vertices[i].y);
480
                                                        }
481

    
482
                                                        if ((psLine.vertices[0].x == psLine.vertices[psLine.num_vertices -
483
                                                                        1].x) &&
484
                                                                        (psLine.vertices[0].y == psLine.vertices[psLine.num_vertices -
485
                                                                        1].y)) {
486
                                                                // Lo a?adimos tambi?n como pol?gono
487
                                                                bEsPoligono = true;
488

    
489
                                                                // Miramos si tiene color de relleno
490
                                                                if (elemento.attr_bytes > 0) {
491
                                                                        elemento.color = m_DgnReader.DGNGetShapeFillInfo(elemento);
492

    
493
                                                                        // System.err.println("fill color = " + elemento.color);
494
                                                                        if (elemento.color != -1) {
495
                                                                                auxFeature.set(ID_FIELD_COLOR,elemento.color);
496
//                                                                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
497
                                                                        }
498
                                                                }
499

    
500
                                                                if (elemento.complex == 0) {
501
                                                                        IGeometry geom=ShapeFactory.createPolygon2D(elShape);
502
                                                                        auxFeature.set(ID_FIELD_GEOMETRY,geom);
503
                                                                        auxFeature.setDefaultGeometry(geom);
504
                                                                        addFeature(auxFeature);
505
//                                                                        addShape(new FPolygon2D(elShape), auxRow);
506
                                                                }
507
                                                        }
508

    
509
                                                        if (elemento.complex != 0) {
510
                                                                // Si es un agujero o
511
                                                                // es la primera entidad del agujero, lo a?adimos sin unir al anterior
512
                                                                if (bFirstHoleEntity ||
513
                                                                                ((psLine.type == DGNFileHeader.DGNT_SHAPE) &&
514
                                                                                ((psLine.properties & 0x8000) != 0))) {
515
                                                                        elementoCompuesto.append(elShape, false);
516
                                                                        bFirstHoleEntity = false;
517
                                                                } else {
518
                                                                        elementoCompuesto.append(elShape, bConnect);
519
                                                                }
520
                                                        } else {
521
                                                                IGeometry geom=ShapeFactory.createPolyline2D(elShape);
522
                                                                auxFeature.set(ID_FIELD_GEOMETRY,geom);
523
                                                                auxFeature.setDefaultGeometry(geom);
524
                                                                addFeature(auxFeature);
525
//                                                                addShape(new FPolyline2D(elShape), auxRow);
526
                                                        }
527
                                                }
528

    
529
                                                break;
530

    
531
                                        case DGNFileHeader.DGNST_ARC:
532

    
533
                                                // m_DgnReader.DGNDumpElement(m_DgnReader.getInfo(), elemento,"");
534
                                                DGNElemArc psArc = (DGNElemArc) elemento;
535

    
536
                                                // La definici?n de arco de MicroStation es distinta a la de Java.
537
                                                // En el dgn el origin se entiende que es el centro del arco,
538
                                                // y a la hora de crear un Arc2D las 2 primeras coordenadas son
539
                                                // la esquina inferior izquierda del rect?ngulo que rodea al arco.
540
                                                // 1.- Creamos la elipse sin rotaci?n.
541
                                                // 2.- Creamos el arco
542
                                                // 3.- Rotamos el resultado
543

    
544
                                                /* System.out.println("Arco con primari axis: " + psArc.primary_axis +
545
                                                   " start angle: " + psArc.startang + " sweepang = " + psArc.sweepang);
546
                                                   System.out.println("secondaria axis: " + psArc.secondary_axis +
547
                                                                                    " rotation = " + psArc.rotation); */
548
                                                AffineTransform mT = AffineTransform.getRotateInstance(Math.toRadians(
549
                                                                        psArc.rotation), psArc.origin.x,
550
                                                                psArc.origin.y);
551

    
552
                                                // mT.preConcatenate(AffineTransform.getScaleInstance(100.0,100.0));
553
                                                Arc2D.Double elArco = new Arc2D.Double(psArc.origin.x -
554
                                                                psArc.primary_axis,
555
                                                                psArc.origin.y - psArc.secondary_axis,
556
                                                                2.0 * psArc.primary_axis,
557
                                                                2.0 * psArc.secondary_axis, -psArc.startang,
558
                                                                -psArc.sweepang, Arc2D.OPEN);
559

    
560
                                                // Ellipse2D.Double elArco = new Ellipse2D.Double(psArc.origin.x - psArc.primary_axis,
561
                                                //                 psArc.origin.y - psArc.secondary_axis,2.0 * psArc.primary_axis, 2.0 * psArc.secondary_axis);
562
                                                GeneralPathX elShapeArc = new GeneralPathX(elArco);
563

    
564
                                                // Transformamos el GeneralPahtX porque si transformamos elArco nos lo convierte
565
                                                // a GeneralPath y nos guarda las coordenadas en float, con la correspondiente p?rdida de precisi?n
566
                                                elShapeArc.transform(mT);
567

    
568
                                                if (m_DgnReader.getInfo().dimension == 3) {
569
                                                        //Aqu? podr?amos hacer cosas con la coordenada Z
570
                                                }
571

    
572
                                                auxFeature.set(ID_FIELD_ID,elemento.element_id);
573
//                                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
574
                                                auxFeature.set(ID_FIELD_ENTITY,"Arc");
575
//                                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
576
//                                                                "Arc");
577
                                                auxFeature.set(ID_FIELD_LAYER,elemento.level);
578
//                                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
579
                                                auxFeature.set(ID_FIELD_COLOR,elemento.color);
580
//                                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
581

    
582
                                                /* Line2D.Double ejeMayor = new Line2D.Double(psArc.origin.x - psArc.primary_axis, psArc.origin.y,
583
                                                   psArc.origin.x + psArc.primary_axis, psArc.origin.y);
584

585
                                                   lyrLines.addShape(new FShape(FConstant.SHAPE_TYPE_POLYLINE, new GeneralPathX(ejeMayor)), auxRow); */
586

    
587
                                                // lyrLines.addShape(new FShape(FConstant.SHAPE_TYPE_POLYLINE, elShapeArc), auxRow);
588
                                                if (elemento.complex != 0) {
589
                                                        // Esto es una posible fuente de fallos si detr?s de una
590
                                                        // elipse vienen m?s cosas pegadas. Deber?amos volver
591
                                                        // a conectar una vez pasada la elipse.
592
                                                        if (elemento.type == DGNFileHeader.DGNT_ELLIPSE) {
593
                                                                bConnect = false;
594
                                                        }
595

    
596
                                                        // SI LA ELIPSE ES UN AGUJERO, SE A?ADE SIN PEGAR
597
                                                        // Y EL ELEMENTO ES UN POLIGONO
598
                                                        if (bFirstHoleEntity ||
599
                                                                        ((elemento.type == DGNFileHeader.DGNT_SHAPE) &&
600
                                                                        ((elemento.properties & 0x8000) != 0))) {
601
                                                                elementoCompuesto.append(elShapeArc, false);
602
                                                                bFirstHoleEntity = false;
603
                                                        } else {
604
                                                                elementoCompuesto.append(elShapeArc, bConnect);
605
                                                        }
606
                                                } else {
607
                                                        IGeometry geom=ShapeFactory.createPolyline2D(elShapeArc);
608
                                                        auxFeature.set(ID_FIELD_GEOMETRY,geom);
609
                                                        auxFeature.setDefaultGeometry(geom);
610
                                                        addFeature(auxFeature);
611

    
612
//                                                        addShape(new FPolyline2D(elShapeArc), auxRow);
613

    
614
                                                        if (psArc.type == DGNFileHeader.DGNT_ELLIPSE) {
615
                                                                geom=ShapeFactory.createPolygon2D(elShapeArc);
616
                                                                auxFeature.set(ID_FIELD_GEOMETRY,geom);
617
                                                                auxFeature.setDefaultGeometry(geom);
618
                                                                addFeature(auxFeature);
619
//                                                                addShape(new FPolygon2D(elShapeArc), auxRow);
620
                                                        }
621
                                                }
622

    
623
                                                // System.err.println("Entra un Arco");
624
                                                break;
625

    
626
                                        case DGNFileHeader.DGNST_TEXT:
627

    
628
                                                DGNElemText psText = (DGNElemText) elemento;
629

    
630

    
631
                                                auxFeature.set(ID_FIELD_ID,elemento.element_id);
632
//                                                auxRow[ID_FIELD_ID] = ValueFactory.createValue(elemento.element_id);
633
                                                auxFeature.set(ID_FIELD_ENTITY,"Text");
634
//                                                auxRow[ID_FIELD_ENTITY] = ValueFactory.createValue(
635
//                                                                "Text");
636
                                                auxFeature.set(ID_FIELD_LAYER,elemento.level);
637
//                                                auxRow[ID_FIELD_LAYER] = ValueFactory.createValue(elemento.level);
638
                                                auxFeature.set(ID_FIELD_COLOR,elemento.color);
639
//                                                auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(elemento.color);
640
                                                heightText = (float) psText.height_mult;
641
                                                auxFeature.set(ID_FIELD_HEIGHTTEXT,heightText);
642
//                                                auxRow[ID_FIELD_HEIGHTTEXT] = ValueFactory.createValue(heightText);
643
                                                auxFeature.set(ID_FIELD_ROTATIONTEXT,psText.rotation);
644
//                                                auxRow[ID_FIELD_ROTATIONTEXT] = ValueFactory.createValue(psText.rotation);
645
                                                auxFeature.set(ID_FIELD_TEXT,psText.string);
646
//                                                auxRow[ID_FIELD_TEXT] = ValueFactory.createValue(psText.string); // .trim();
647

    
648
                                                IGeometry geom=ShapeFactory.createPoint3D(psText.origin.x,
649
                                                                psText.origin.y, psText.origin.z);
650
                                                auxFeature.set(ID_FIELD_GEOMETRY,geom);
651
                                                auxFeature.setDefaultGeometry(geom);
652
                                                addFeature(auxFeature);
653

    
654
//                                                addShape(elShapeTxt, auxRow);
655

    
656
                                                // System.out.println("Rotaci?n texto: " + psText.rotation + "Altura Texto = " + heightText);
657

    
658
                                                /* System.out.println("  origin=(" + psText.origin.x +
659
                                                   ", " + psText.origin.y + ") rotation=" +
660
                                                   psText.rotation + "\n" + "  font=" +
661
                                                   psText.font_id + " just=" +
662
                                                   psText.justification + "length_mult=" +
663
                                                   psText.length_mult + " height_mult=" +
664
                                                   psText.height_mult + "\n" + "  string =" +
665
                                                   new String(psText.string).toString().trim() +
666
                                                   "\n"); */
667
                                                break;
668

    
669
                                        /* default:
670
                                           m_DgnReader.DGNDumpElement(m_DgnReader.getInfo(), elemento, "");
671
                                         */
672
                                } // switch
673
                        } // if
674
                } // for
675

    
676
                if (bElementoCompuesto) {
677
                        if (bInsideCell) {
678
                                auxFeature=cellFeature;
679
//                                auxRow = cellRow;
680
                        } else {
681
                                auxFeature=complexFeature;
682
//                                auxRow = complexRow;
683
                        }
684

    
685
                        // System.err.println("Entidad compuesta. bInsideCell = " + bInsideCell + " auxRow = " + auxRow[ID_FIELD_ENTITY]);
686
                        IGeometry geom=ShapeFactory.createPolyline2D(elementoCompuesto);
687
                        auxFeature.set(ID_FIELD_GEOMETRY,geom);
688
                        auxFeature.setDefaultGeometry(geom);
689
                        addFeature(auxFeature);
690

    
691
//                        addShape(new FPolyline2D(elementoCompuesto), auxRow);
692

    
693
                        if (bEsPoligono) {
694
                                if (complex_index_fill_color != -1) {
695
                                        auxFeature.set(ID_FIELD_COLOR,complex_index_fill_color);
696
//                                        auxRow[ID_FIELD_COLOR] = ValueFactory.createValue(complex_index_fill_color);
697
                                }
698
                                geom=ShapeFactory.createPolygon2D(elementoCompuesto);
699
                                auxFeature.set(ID_FIELD_GEOMETRY,geom);
700
                                auxFeature.setDefaultGeometry(geom);
701
                                addFeature(auxFeature);
702
//                                addShape(new FPolygon2D(elementoCompuesto), auxRow);
703
                        }
704
                }
705

    
706
                defaultLegend = LegendFactory.createVectorialUniqueValueLegend(FShape.MULTI);
707
                defaultLegend.setClassifyingFieldNames(new String[] {"Color"} );
708
                defaultLegend.setClassifyingFieldTypes(new int[]{Types.INTEGER,Types.VARCHAR,Types.INTEGER,Types.INTEGER,Types.FLOAT,Types.DOUBLE,Types.VARCHAR});
709

    
710

    
711
                ISymbol myDefaultSymbol = SymbologyFactory.
712
                        createDefaultSymbolByShapeType(FShape.MULTI);
713

    
714
                defaultLegend.setDefaultSymbol(myDefaultSymbol);
715

    
716

    
717
//                ISymbol theSymbol = null;
718
//
719
//                try {
720
//                        for (long j = 0; j < getFeatureCount(); j++) {
721
//                                IFeature feat=(IFeature)getFeature((int)j);
722
//                                int clave=feat.getInt(ID_FIELD_COLOR);
723
//                                if (defaultLegend.getSymbolByValue(clave) == null) {
724
//
725
//                                        Color c = m_DgnReader.DGNLookupColor(
726
//                                                        clave.getValue());
727
//                                        theSymbol =        SymbologyFactory.
728
//                                                createDefaultSymbolByShapeType(featureType.getGeometryTypes()[0], c);
729
//                                        theSymbol.setDescription(clave.toString());
730
//
731
//                                        if (theSymbol instanceof IMarkerSymbol) {
732
//                                                ((IMarkerSymbol) theSymbol).setSize(1);
733
//                                        }
734
//
735
//                                        if (theSymbol instanceof ILineSymbol) {
736
//                                                ((ILineSymbol) theSymbol).setLineWidth(1);
737
//                                        }
738
//
739
//                                        if (theSymbol instanceof IFillSymbol) {
740
//                                                ((IFillSymbol) theSymbol).getOutline().setLineColor(c);
741
//                                                ((IFillSymbol) theSymbol).getOutline().setLineWidth(1);
742
//                                                ((IFillSymbol) theSymbol).setFillColor(null);
743
//                                        }
744
//
745
//                                        // theSymbol.setStyle(FConstant.SYMBOL_STYLE_FILL_TRANSPARENT);
746
//                                        defaultLegend.addSymbol(clave, theSymbol);
747
//                                }
748
//
749
//                                if ("Text".equalsIgnoreCase(((StringValue) rs.getFieldValue(j, ID_FIELD_ENTITY)).toString())) {
750
//
751
//                                }
752
//
753
//
754
//                        } // for
755
//                } catch (ReadException e) {
756
//                        throw new OpenException(getName(),e);
757
//                }
758

    
759

    
760
                this.observable.notifyObservers(
761
                                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_OPEN)
762
                );
763

    
764
        }
765

    
766
        public void close() throws CloseException {
767
                this.observable.notifyObservers(
768
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_CLOSE)
769
            );
770
                super.close();
771
        this.observable.notifyObservers(
772
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_CLOSE)
773
            );
774

    
775
        }
776

    
777
        public void dispose() throws CloseException {
778
                this.observable.notifyObservers(
779
                new FeatureStoreNotification(this,FeatureStoreNotification.BEFORE_DISPOSE)
780
            );
781
                close();
782
        this.observable.notifyObservers(
783
                new FeatureStoreNotification(this,FeatureStoreNotification.AFTER_DISPOSE)
784
            );
785
        }
786

    
787
        public boolean isEditable() {
788
                return false;
789
        }
790

    
791
        public IMetadata getMetadata() throws BaseException {
792
                if (metadata==null){
793
                        IMetadataManager manager=MetadataManager.getManager();
794
                        metadata=manager.create(DATASTORE_NAME);
795
                        IExtent extent=getFullExtent();
796
                        metadata.set("extent",extent);
797
                        String srs=getSRS();
798
                        metadata.set("srs",srs);
799
                }
800
                if (this.alterMode){
801
                        IExtent extent=(IExtent)metadata.get("extent");
802
                        IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection();
803
                    if (spatialManager.isFullExtentDirty()){
804
                            if (!featureCollection.isEmpty()){
805
                                    Iterator featureIterator=featureCollection.iterator();
806
                                    extent = ((IFeature)featureIterator.next()).getExtent();
807
                                    while(featureIterator.hasNext()){
808
                                            IFeature feature=(IFeature)featureIterator.next();
809
                                            IExtent boundExtent=feature.getExtent();
810
                                            if (boundExtent!=null)
811
                                                    extent.add(boundExtent);
812
                                    }
813
                            }
814
                    }
815
                    metadata.set("extent",extent);
816
                }
817
                return metadata;
818
        }
819
        private String getSRS() {
820
                // TODO Auto-generated method stub
821
                return null;
822
        }
823

    
824

    
825
        public IFeaturesReader getFeatutesReader() {
826
                IFeaturesReader reader = new DGNFeaturesReader();
827
                reader.init(this);
828
                return reader;
829
        }
830
        public IFeaturesWriter getFeaturesWriter() {
831
                return null;
832
        }
833
        public IDataStoreParameters getParameters() {
834
                return parameters;
835
        }
836
}