Statistics
| Revision:

root / branches / Mobile_Compatible_Hito_1 / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dxf / DXFStore.java @ 21571

History | View | Annotate | Download (12.8 KB)

1
package org.gvsig.data.datastores.vectorial.file.dxf;
2

    
3
import java.lang.ref.WeakReference;
4
import java.security.KeyException;
5
import java.util.ArrayList;
6
import java.util.Collection;
7
import java.util.Comparator;
8
import java.util.Iterator;
9
import java.util.List;
10
import java.util.TreeSet;
11

    
12
import org.gvsig.data.CloseException;
13
import org.gvsig.data.DataCollection;
14
import org.gvsig.data.DataException;
15
import org.gvsig.data.DataExplorer;
16
import org.gvsig.data.DataExplorerParameters;
17
import org.gvsig.data.DataManager;
18
import org.gvsig.data.DataStoreParameters;
19
import org.gvsig.data.InitializeException;
20
import org.gvsig.data.OpenException;
21
import org.gvsig.data.ReadException;
22
import org.gvsig.data.ResourceManager;
23
import org.gvsig.data.WriteException;
24
import org.gvsig.data.datastores.vectorial.FeaturesWriter;
25
import org.gvsig.data.datastores.vectorial.file.FileExplorerParameters;
26
import org.gvsig.data.vectorial.AbstractFeatureStore;
27
import org.gvsig.data.vectorial.AttributeDescriptor;
28
import org.gvsig.data.vectorial.DefaultFeatureType;
29
import org.gvsig.data.vectorial.Feature;
30
import org.gvsig.data.vectorial.FeatureAttributeDescriptor;
31
import org.gvsig.data.vectorial.FeatureCollection;
32
import org.gvsig.data.vectorial.FeatureID;
33
import org.gvsig.data.vectorial.FeatureType;
34
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
35
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
36
import org.gvsig.data.vectorial.MemoryFeature;
37
import org.gvsig.data.vectorial.expressionevaluator.FeatureComparator;
38
//import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
39
import org.gvsig.exceptions.BaseException;
40
import org.gvsig.fmap.geom.primitive.Envelope;
41
import org.gvsig.metadata.IMetadata;
42
import org.gvsig.metadata.IMetadataManager;
43
import org.gvsig.metadata.MetadataManager;
44
import org.opengis.filter.Filter;
45
import org.opengis.filter.sort.SortBy;
46

    
47
public class DXFStore extends AbstractFeatureStore{
48
        public static String DATASTORE_NAME = "DXFStore";
49
        private List featureTypes = new ArrayList();
50
        protected IMetadata metadata;
51

    
52

    
53
        private DXFStoreParameters dxfParameters;
54

    
55
        private DXFResource dxf;
56

    
57
        public void init(DataStoreParameters parameters) throws InitializeException {
58

    
59
                dxfParameters=(DXFStoreParameters)parameters;
60

    
61
                DXFResource tmpResource = new DXFResource(dxfParameters);
62

    
63
                ResourceManager resMan = ResourceManager.getResourceManager();
64

    
65
                try {
66
                        this.dxf = (DXFResource)resMan.addResource(tmpResource);
67
                } catch (DataException e1) {
68
                        throw new InitializeException(this.getName(),e1);
69
                }
70

    
71
                super.init(parameters,this.dxf);
72

    
73
                try {
74
                        this.defaultFeatureType = this.dxf.getFeatureType();
75
                } catch (ReadException e) {
76
                        throw new InitializeException(this.getName(),e);
77
                }
78

    
79
        }
80

    
81
         static FeatureType newFeatureType(){
82
                        DefaultFeatureType fType= new DefaultFeatureType();
83

    
84
                        try{
85
                        AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
86
                        descriptorID.loading();
87
                        descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
88
                        descriptorID.setName("ID");
89
                        descriptorID.setDefaultValue(new Integer(0));
90
                        descriptorID.stopLoading();
91
                        fType.add(descriptorID);
92

    
93
                        AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
94
                        descriptorShape.loading();
95
                        descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
96
                        descriptorShape.setName("GEOMETRY");
97
                        descriptorShape.setDefaultValue(null);
98
                        descriptorShape.stopLoading();
99
                        fType.add(descriptorShape);
100
                        fType.setDefaultGeometry("GEOMETRY");
101

    
102
                        AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor();
103
                        descriptorEntity.loading();
104
                        descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
105
                        descriptorEntity.setName("Entity");
106
                        descriptorEntity.setDefaultValue("Entity");
107
                        descriptorEntity.stopLoading();
108
                        fType.add(descriptorEntity);
109

    
110
                        AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
111
                        descriptorLayer.loading();
112
                        descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
113
                        descriptorLayer.setName("Layer");
114
                        descriptorLayer.setDefaultValue("default");
115
                        descriptorLayer.stopLoading();
116
                        fType.add(descriptorLayer);
117

    
118
                        AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor();
119
                        descriptorColor.loading();
120
                        descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
121
                        descriptorColor.setName("Color");
122
                        descriptorColor.setDefaultValue(new Integer(0));
123
                        descriptorColor.stopLoading();
124
                        fType.add(descriptorColor);
125

    
126
                        AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor();
127
                        descriptorElevation.loading();
128
                        descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
129
                        descriptorElevation.setName("Elevation");
130
                        descriptorElevation.setDefaultValue(new Double(0));
131
                        descriptorElevation.stopLoading();
132
                        fType.add(descriptorElevation);
133

    
134
                        AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor();
135
                        descriptorThickness.loading();
136
                        descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
137
                        descriptorThickness.setName("Thickness");
138
                        descriptorThickness.setDefaultValue(new Double(0));
139
                        descriptorThickness.stopLoading();
140
                        fType.add(descriptorThickness);
141

    
142
                        AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor();
143
                        descriptorText.loading();
144
                        descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
145
                        descriptorText.setName("Text");
146
                        descriptorText.setDefaultValue("");
147
                        descriptorText.stopLoading();
148
                        fType.add(descriptorText);
149

    
150
                        AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
151
                        descriptorHeightText.loading();
152
                        descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
153
                        descriptorHeightText.setName("HeightText");
154
                        descriptorHeightText.setDefaultValue(new Float(10));
155
                        descriptorHeightText.stopLoading();
156
                        fType.add(descriptorHeightText);
157

    
158
                        AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
159
                        descriptorRotationText.loading();
160
                        descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
161
                        descriptorRotationText.setName("Rotation");
162
                        descriptorRotationText.setDefaultValue(new Double(10));
163
                        descriptorRotationText.stopLoading();
164
                        fType.add(descriptorRotationText);
165
                        }catch (IsNotAttributeSettingException e) {
166
                                e.printStackTrace();
167
                        }
168

    
169
                        return fType;
170

    
171
         }
172

    
173

    
174
        protected void doFinishEdition() throws WriteException, ReadException {
175
                FeaturesWriter writer = getFeaturesWriter();
176
        writer.init(this);
177
        writer.updateFeatureType(defaultFeatureType);
178
        this.dxf.editing();
179
        writer.preProcess();
180
        Collection collection=getDataCollection();
181
        Iterator iterator=collection.iterator();
182
        Feature feature;
183
        while (iterator.hasNext()) {
184
                feature= (Feature) iterator.next();
185
                        writer.insertFeature(feature);
186
                }
187
        writer.postProcess();
188
        this.dxf.stopEditing();
189
        this.dxf.changed(this);
190

    
191
        }
192

    
193
        public DataCollection getDataCollection(FeatureType type, Filter filter, SortBy[] order) throws ReadException {
194
                if (type==null){
195
                        type=getDefaultFeatureType();
196
                } else {
197
                        if (!type.isSubtypeOf(this.getDefaultFeatureType())){
198
                                throw new ReadException("invalid type",this.getName());
199
                        }
200
                }
201
                FeatureCollection coll;
202

    
203
                //TODO aplicar filtro, orden e incluso y el featureType
204
                /*FeatureFilter parser = null;
205
                if (filter!=null){
206
                        parser = new FeatureFilter(filter,type);
207
                }*/
208
//                ArrayList originalFeatures=this.dxf.getFeatures();
209
                int num=0;
210
                if (featureManager!=null){
211
                        num=featureManager.getNum();
212
                }
213
                int originalSize=this.dxf.getFeatureCount();
214
                Collection allFeatures=null;
215
                if (order!=null){
216
                        Comparator comparator = new FeatureComparator(type,order);
217
                        allFeatures = new TreeSet(comparator);
218
                }else{
219
                        allFeatures=new ArrayList();
220
                }
221
                for (int i = 0; i < originalSize+num; i++) {
222
                        Feature feature=null;
223
                        if (i<this.dxf.getFeatureCount()){
224
                                feature=this.dxf.getFeature(i);
225
                        }else{
226
                                feature=featureManager.getFeature(i-originalSize,this,type);
227
                        }
228
                        if (featureManager == null || !featureManager.isDeleted(feature)){
229
                                try {
230
                                                MemoryFeature auxfeature=new DXFFeature(type,false);
231
                                                auxfeature.loading();
232
//                                                auxfeature.setDefaultGeometry(feature.getDefaultGeometry());
233
                                                Iterator iterator=type.iterator();
234
                                                while (iterator.hasNext()) {
235
                                                        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
236
                                                        int index=((AttributeDescriptor)fad).originalPosition();
237

    
238
                                                        if (fad.getDataType().equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
239
                                                                auxfeature.setGeometry(index,feature.get(index));
240
                                                        } else {
241
                                                                auxfeature.set(index,feature.get(index));
242
                                                        }
243
                                                }
244
                                                if (filter==null || filter.evaluate(auxfeature)){
245
                                                        allFeatures.add(auxfeature);
246
                                                }
247
                                                auxfeature.stopLoading();
248

    
249
                                } catch (IsNotFeatureSettingException e) {
250
                                        throw new ReadException(this.getName(),e);
251
                                }
252
                        }
253

    
254
                }
255

    
256
                coll=new DXFFeatureCollection(allFeatures);
257
                this.addObserver(new WeakReference(coll));
258
                return coll;
259

    
260
        }
261

    
262
        public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException {
263
                if (featureType==null){
264
                        featureType=getDefaultFeatureType();
265
                }else{
266
                        if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){
267
                                throw new ReadException("invalid type",this.getName());
268
                        }
269
                }
270
                if (this.alterMode){
271
                    if (featureManager.contains(id)) {
272
                            return featureManager.getFeature(id,this,featureType);
273
                    }
274
            }
275
                return id.getFeature(featureType);
276
        }
277

    
278
        public List getFeatureTypes() {
279
                featureTypes.set(0,getDefaultFeatureType());
280
        return featureTypes;
281
        }
282

    
283
        public boolean isWithDefaultLegend() {
284
                return false;
285
        }
286

    
287
        public Object getDefaultLegend() {
288
                return null;
289
        }
290

    
291
        public Object getDefaultLabelingStrategy() {
292
                return null;
293
        }
294

    
295
        public boolean canAlterFeatureType() {
296
                return false;
297
        }
298

    
299
        public String getName() {
300
                return DATASTORE_NAME;
301
        }
302

    
303
        protected void doOpen() throws OpenException {
304

    
305
        }
306

    
307
        protected void doClose() throws CloseException {
308
                this.dxf.close();
309
        }
310

    
311
        protected void doDispose() throws CloseException {
312
                super.doDispose();
313
                ResourceManager resMan = ResourceManager.getResourceManager();
314

    
315
            try {
316
                        resMan.remove(this.dxf);
317
                } catch (DataException e1) {
318
                        throw new CloseException(this.getName(),e1);
319
                } catch (KeyException e) {
320
                        throw new CloseException(this.getName(),e);
321
                }
322

    
323
                this.dxf = null;
324
                this.defaultFeatureType=null;
325
                this.metadata=null;
326
        }
327

    
328
        public boolean isEditable() {
329
                return this.dxf.isEditable();
330
        }
331

    
332
        public IMetadata getMetadata() throws BaseException {
333
                if (metadata==null){
334
                        IMetadataManager manager=MetadataManager.getManager();
335
                        metadata=manager.create(DATASTORE_NAME);
336
                        Envelope extent=this.dxf.getFullExtent();
337
                        metadata.set("extent",extent);
338
                        String srs=getSRS();
339
                        metadata.set("srs",srs);
340
                }
341
                if (this.alterMode){
342
                        Envelope extent=(Envelope)metadata.get("extent");
343
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
344
                    if (spatialManager.isFullExtentDirty()){
345
                            if (!featureCollection.isEmpty()){
346
                                    Iterator featureIterator=featureCollection.iterator();
347
                                    extent = ((Feature)featureIterator.next()).getExtent();
348
                                    while(featureIterator.hasNext()){
349
                                            Feature feature=(Feature)featureIterator.next();
350
                                            Envelope boundExtent=feature.getExtent();
351
                                            if (boundExtent!=null)
352
                                                    extent.add(boundExtent);
353
                                    }
354
                            }
355
                    }
356
                    metadata.set("extent",extent);
357
                }
358
                return metadata;
359
        }
360
        private String getSRS() {
361
                // TODO Auto-generated method stub
362
                return null;
363
        }
364

    
365

    
366
        protected FeaturesWriter getFeaturesWriter() {
367
                FeaturesWriter writer = new DXFFeaturesWriter();
368
//                writer.init(this);
369
                return writer;
370
        }
371
        public DataStoreParameters getParameters() {
372
                return parameters;
373
        }
374

    
375
        public DataExplorer getExplorer() {
376
                DataManager dsm=DataManager.getManager();
377
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
378
                                DXFDataExplorer.DATASOURCE_NAME);
379
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
380

    
381
                DataExplorer src=null;
382
                try {
383
                        src = dsm.createDataExplorer(dsp);
384
                } catch (InitializeException e1) {
385
                        e1.printStackTrace();
386
                }
387
                return src;
388
        }
389

    
390
        /* (non-Javadoc)
391
         * @see org.gvsig.data.vectorial.FeatureStore#doRefresh()
392
         */
393
        protected void doRefresh() throws OpenException, InitializeException {
394
                // TODO Auto-generated method stub
395

    
396
        }
397
}