Statistics
| Revision:

svn-gvsig-desktop / branches / Mobile_Compatible_Hito_1 / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / dxf / DXFStore.java @ 22125

History | View | Annotate | Download (12.8 KB)

1 19844 vcaballero
package org.gvsig.data.datastores.vectorial.file.dxf;
2 19737 vcaballero
3 20130 jmvivo
import java.lang.ref.WeakReference;
4 20692 jmvivo
import java.security.KeyException;
5 19737 vcaballero
import java.util.ArrayList;
6
import java.util.Collection;
7 20130 jmvivo
import java.util.Comparator;
8 19737 vcaballero
import java.util.Iterator;
9
import java.util.List;
10 20130 jmvivo
import java.util.TreeSet;
11 19737 vcaballero
12 20971 vcaballero
import org.gvsig.data.CloseException;
13 21177 vcaballero
import org.gvsig.data.DataCollection;
14 20971 vcaballero
import org.gvsig.data.DataException;
15 21045 jmvivo
import org.gvsig.data.DataExplorer;
16
import org.gvsig.data.DataExplorerParameters;
17 21177 vcaballero
import org.gvsig.data.DataManager;
18 21045 jmvivo
import org.gvsig.data.DataStoreParameters;
19 20971 vcaballero
import org.gvsig.data.InitializeException;
20
import org.gvsig.data.OpenException;
21
import org.gvsig.data.ReadException;
22 20692 jmvivo
import org.gvsig.data.ResourceManager;
23 20971 vcaballero
import org.gvsig.data.WriteException;
24 21045 jmvivo
import org.gvsig.data.datastores.vectorial.FeaturesWriter;
25 20744 jmvivo
import org.gvsig.data.datastores.vectorial.file.FileExplorerParameters;
26 21177 vcaballero
import org.gvsig.data.vectorial.AbstractFeatureStore;
27 20607 vcaballero
import org.gvsig.data.vectorial.AttributeDescriptor;
28 21045 jmvivo
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 20607 vcaballero
import org.gvsig.data.vectorial.FeatureType;
34 20421 vcaballero
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
35 20414 vcaballero
import org.gvsig.data.vectorial.IsNotFeatureSettingException;
36 19737 vcaballero
import org.gvsig.data.vectorial.MemoryFeature;
37 20452 jmvivo
import org.gvsig.data.vectorial.expressionevaluator.FeatureComparator;
38 21571 jcarrasco
//import org.gvsig.data.vectorial.expressionevaluator.FeatureFilter;
39 19737 vcaballero
import org.gvsig.exceptions.BaseException;
40 21177 vcaballero
import org.gvsig.fmap.geom.primitive.Envelope;
41 19737 vcaballero
import org.gvsig.metadata.IMetadata;
42
import org.gvsig.metadata.IMetadataManager;
43
import org.gvsig.metadata.MetadataManager;
44 21571 jcarrasco
import org.opengis.filter.Filter;
45
import org.opengis.filter.sort.SortBy;
46 19737 vcaballero
47 21045 jmvivo
public class DXFStore extends AbstractFeatureStore{
48 19737 vcaballero
        public static String DATASTORE_NAME = "DXFStore";
49 20692 jmvivo
        private List featureTypes = new ArrayList();
50
        protected IMetadata metadata;
51 19737 vcaballero
52 20719 jmvivo
53 20692 jmvivo
        private DXFStoreParameters dxfParameters;
54 19737 vcaballero
55 20692 jmvivo
        private DXFResource dxf;
56 19737 vcaballero
57 21045 jmvivo
        public void init(DataStoreParameters parameters) throws InitializeException {
58 19737 vcaballero
59 20692 jmvivo
                dxfParameters=(DXFStoreParameters)parameters;
60 19737 vcaballero
61 20692 jmvivo
                DXFResource tmpResource = new DXFResource(dxfParameters);
62 19737 vcaballero
63 20692 jmvivo
                ResourceManager resMan = ResourceManager.getResourceManager();
64 19737 vcaballero
65 20692 jmvivo
                try {
66
                        this.dxf = (DXFResource)resMan.addResource(tmpResource);
67
                } catch (DataException e1) {
68
                        throw new InitializeException(this.getName(),e1);
69
                }
70 19737 vcaballero
71 20692 jmvivo
                super.init(parameters,this.dxf);
72 19737 vcaballero
73 20692 jmvivo
                try {
74 20850 jmvivo
                        this.defaultFeatureType = this.dxf.getFeatureType();
75 20692 jmvivo
                } catch (ReadException e) {
76
                        throw new InitializeException(this.getName(),e);
77 19737 vcaballero
                }
78
79 20692 jmvivo
        }
80
81 21045 jmvivo
         static FeatureType newFeatureType(){
82
                        DefaultFeatureType fType= new DefaultFeatureType();
83 19737 vcaballero
84 20421 vcaballero
                        try{
85 20909 jmvivo
                        AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor();
86 20421 vcaballero
                        descriptorID.loading();
87 21045 jmvivo
                        descriptorID.setType(FeatureAttributeDescriptor.TYPE_INT);
88 19737 vcaballero
                        descriptorID.setName("ID");
89
                        descriptorID.setDefaultValue(new Integer(0));
90 20421 vcaballero
                        descriptorID.stopLoading();
91 20049 jmvivo
                        fType.add(descriptorID);
92 19737 vcaballero
93 20909 jmvivo
                        AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor();
94 20421 vcaballero
                        descriptorShape.loading();
95 21045 jmvivo
                        descriptorShape.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY);
96 19737 vcaballero
                        descriptorShape.setName("GEOMETRY");
97
                        descriptorShape.setDefaultValue(null);
98 20421 vcaballero
                        descriptorShape.stopLoading();
99 20049 jmvivo
                        fType.add(descriptorShape);
100
                        fType.setDefaultGeometry("GEOMETRY");
101 19737 vcaballero
102 20909 jmvivo
                        AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor();
103 20421 vcaballero
                        descriptorEntity.loading();
104 21045 jmvivo
                        descriptorEntity.setType(FeatureAttributeDescriptor.TYPE_STRING);
105 19737 vcaballero
                        descriptorEntity.setName("Entity");
106
                        descriptorEntity.setDefaultValue("Entity");
107 20421 vcaballero
                        descriptorEntity.stopLoading();
108 20049 jmvivo
                        fType.add(descriptorEntity);
109 19737 vcaballero
110 20909 jmvivo
                        AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor();
111 20421 vcaballero
                        descriptorLayer.loading();
112 21045 jmvivo
                        descriptorLayer.setType(FeatureAttributeDescriptor.TYPE_STRING);
113 19737 vcaballero
                        descriptorLayer.setName("Layer");
114
                        descriptorLayer.setDefaultValue("default");
115 20421 vcaballero
                        descriptorLayer.stopLoading();
116 20049 jmvivo
                        fType.add(descriptorLayer);
117 19737 vcaballero
118 20909 jmvivo
                        AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor();
119 20421 vcaballero
                        descriptorColor.loading();
120 21045 jmvivo
                        descriptorColor.setType(FeatureAttributeDescriptor.TYPE_INT);
121 19737 vcaballero
                        descriptorColor.setName("Color");
122
                        descriptorColor.setDefaultValue(new Integer(0));
123 20421 vcaballero
                        descriptorColor.stopLoading();
124 20049 jmvivo
                        fType.add(descriptorColor);
125 19737 vcaballero
126 20909 jmvivo
                        AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor();
127 20421 vcaballero
                        descriptorElevation.loading();
128 21045 jmvivo
                        descriptorElevation.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
129 19737 vcaballero
                        descriptorElevation.setName("Elevation");
130
                        descriptorElevation.setDefaultValue(new Double(0));
131 20421 vcaballero
                        descriptorElevation.stopLoading();
132 20049 jmvivo
                        fType.add(descriptorElevation);
133 19737 vcaballero
134 20909 jmvivo
                        AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor();
135 20421 vcaballero
                        descriptorThickness.loading();
136 21045 jmvivo
                        descriptorThickness.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
137 19737 vcaballero
                        descriptorThickness.setName("Thickness");
138
                        descriptorThickness.setDefaultValue(new Double(0));
139 20421 vcaballero
                        descriptorThickness.stopLoading();
140 20049 jmvivo
                        fType.add(descriptorThickness);
141 19737 vcaballero
142 20909 jmvivo
                        AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor();
143 20421 vcaballero
                        descriptorText.loading();
144 21045 jmvivo
                        descriptorText.setType(FeatureAttributeDescriptor.TYPE_STRING);
145 19737 vcaballero
                        descriptorText.setName("Text");
146
                        descriptorText.setDefaultValue("");
147 20421 vcaballero
                        descriptorText.stopLoading();
148 20049 jmvivo
                        fType.add(descriptorText);
149 19737 vcaballero
150 20909 jmvivo
                        AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor();
151 20421 vcaballero
                        descriptorHeightText.loading();
152 21045 jmvivo
                        descriptorHeightText.setType(FeatureAttributeDescriptor.TYPE_FLOAT);
153 19737 vcaballero
                        descriptorHeightText.setName("HeightText");
154
                        descriptorHeightText.setDefaultValue(new Float(10));
155 20421 vcaballero
                        descriptorHeightText.stopLoading();
156 20049 jmvivo
                        fType.add(descriptorHeightText);
157 19737 vcaballero
158 20909 jmvivo
                        AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor();
159 20421 vcaballero
                        descriptorRotationText.loading();
160 21045 jmvivo
                        descriptorRotationText.setType(FeatureAttributeDescriptor.TYPE_DOUBLE);
161 19737 vcaballero
                        descriptorRotationText.setName("Rotation");
162
                        descriptorRotationText.setDefaultValue(new Double(10));
163 20421 vcaballero
                        descriptorRotationText.stopLoading();
164 20049 jmvivo
                        fType.add(descriptorRotationText);
165 20421 vcaballero
                        }catch (IsNotAttributeSettingException e) {
166
                                e.printStackTrace();
167
                        }
168 19737 vcaballero
169 20049 jmvivo
                        return fType;
170
171
         }
172
173 19737 vcaballero
174 20421 vcaballero
        protected void doFinishEdition() throws WriteException, ReadException {
175 21045 jmvivo
                FeaturesWriter writer = getFeaturesWriter();
176 19737 vcaballero
        writer.init(this);
177 20850 jmvivo
        writer.updateFeatureType(defaultFeatureType);
178 20719 jmvivo
        this.dxf.editing();
179 19737 vcaballero
        writer.preProcess();
180
        Collection collection=getDataCollection();
181
        Iterator iterator=collection.iterator();
182 21045 jmvivo
        Feature feature;
183 19737 vcaballero
        while (iterator.hasNext()) {
184 21045 jmvivo
                feature= (Feature) iterator.next();
185 19737 vcaballero
                        writer.insertFeature(feature);
186
                }
187
        writer.postProcess();
188 20719 jmvivo
        this.dxf.stopEditing();
189 20692 jmvivo
        this.dxf.changed(this);
190 19737 vcaballero
191
        }
192
193 21571 jcarrasco
        public DataCollection getDataCollection(FeatureType type, Filter filter, SortBy[] order) throws ReadException {
194 19737 vcaballero
                if (type==null){
195
                        type=getDefaultFeatureType();
196 21069 jmvivo
                } else {
197
                        if (!type.isSubtypeOf(this.getDefaultFeatureType())){
198
                                throw new ReadException("invalid type",this.getName());
199
                        }
200 19737 vcaballero
                }
201 21045 jmvivo
                FeatureCollection coll;
202 20130 jmvivo
203
                //TODO aplicar filtro, orden e incluso y el featureType
204 21571 jcarrasco
                /*FeatureFilter parser = null;
205 20130 jmvivo
                if (filter!=null){
206 20452 jmvivo
                        parser = new FeatureFilter(filter,type);
207 21571 jcarrasco
                }*/
208 20692 jmvivo
//                ArrayList originalFeatures=this.dxf.getFeatures();
209 20130 jmvivo
                int num=0;
210
                if (featureManager!=null){
211
                        num=featureManager.getNum();
212
                }
213 20692 jmvivo
                int originalSize=this.dxf.getFeatureCount();
214 20130 jmvivo
                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 21045 jmvivo
                        Feature feature=null;
223 20692 jmvivo
                        if (i<this.dxf.getFeatureCount()){
224
                                feature=this.dxf.getFeature(i);
225 20130 jmvivo
                        }else{
226 21069 jmvivo
                                feature=featureManager.getFeature(i-originalSize,this,type);
227 20130 jmvivo
                        }
228
                        if (featureManager == null || !featureManager.isDeleted(feature)){
229
                                try {
230 20598 jmvivo
                                                MemoryFeature auxfeature=new DXFFeature(type,false);
231 20414 vcaballero
                                                auxfeature.loading();
232 20130 jmvivo
//                                                auxfeature.setDefaultGeometry(feature.getDefaultGeometry());
233 20414 vcaballero
                                                Iterator iterator=type.iterator();
234
                                                while (iterator.hasNext()) {
235 21045 jmvivo
                                                        FeatureAttributeDescriptor fad = (FeatureAttributeDescriptor) iterator.next();
236 20607 vcaballero
                                                        int index=((AttributeDescriptor)fad).originalPosition();
237 20598 jmvivo
238 21045 jmvivo
                                                        if (fad.getDataType().equals(FeatureAttributeDescriptor.TYPE_GEOMETRY)){
239 20598 jmvivo
                                                                auxfeature.setGeometry(index,feature.get(index));
240
                                                        } else {
241
                                                                auxfeature.set(index,feature.get(index));
242
                                                        }
243 20130 jmvivo
                                                }
244 21571 jcarrasco
                                                if (filter==null || filter.evaluate(auxfeature)){
245 20130 jmvivo
                                                        allFeatures.add(auxfeature);
246
                                                }
247 20414 vcaballero
                                                auxfeature.stopLoading();
248 20130 jmvivo
249 20598 jmvivo
                                } catch (IsNotFeatureSettingException e) {
250
                                        throw new ReadException(this.getName(),e);
251 20130 jmvivo
                                }
252
                        }
253
254
                }
255
256
                coll=new DXFFeatureCollection(allFeatures);
257
                this.addObserver(new WeakReference(coll));
258
                return coll;
259
260 19737 vcaballero
        }
261
262 21069 jmvivo
        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 21177 vcaballero
                }
270 19737 vcaballero
                if (this.alterMode){
271
                    if (featureManager.contains(id)) {
272 21069 jmvivo
                            return featureManager.getFeature(id,this,featureType);
273 19737 vcaballero
                    }
274
            }
275 21069 jmvivo
                return id.getFeature(featureType);
276 19737 vcaballero
        }
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 20719 jmvivo
                return false;
297 19737 vcaballero
        }
298
299
        public String getName() {
300
                return DATASTORE_NAME;
301
        }
302
303 20626 jmvivo
        protected void doOpen() throws OpenException {
304 19737 vcaballero
305
        }
306
307 20626 jmvivo
        protected void doClose() throws CloseException {
308 20692 jmvivo
                this.dxf.close();
309 19737 vcaballero
        }
310
311 20626 jmvivo
        protected void doDispose() throws CloseException {
312
                super.doDispose();
313 20692 jmvivo
                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 20850 jmvivo
                this.defaultFeatureType=null;
325 20692 jmvivo
                this.metadata=null;
326 19737 vcaballero
        }
327
328
        public boolean isEditable() {
329 20719 jmvivo
                return this.dxf.isEditable();
330 19737 vcaballero
        }
331
332
        public IMetadata getMetadata() throws BaseException {
333
                if (metadata==null){
334
                        IMetadataManager manager=MetadataManager.getManager();
335
                        metadata=manager.create(DATASTORE_NAME);
336 21177 vcaballero
                        Envelope extent=this.dxf.getFullExtent();
337 19737 vcaballero
                        metadata.set("extent",extent);
338
                        String srs=getSRS();
339
                        metadata.set("srs",srs);
340
                }
341
                if (this.alterMode){
342 21177 vcaballero
                        Envelope extent=(Envelope)metadata.get("extent");
343 21045 jmvivo
                        FeatureCollection featureCollection=(FeatureCollection)getDataCollection();
344 19737 vcaballero
                    if (spatialManager.isFullExtentDirty()){
345
                            if (!featureCollection.isEmpty()){
346
                                    Iterator featureIterator=featureCollection.iterator();
347 21045 jmvivo
                                    extent = ((Feature)featureIterator.next()).getExtent();
348 19737 vcaballero
                                    while(featureIterator.hasNext()){
349 21045 jmvivo
                                            Feature feature=(Feature)featureIterator.next();
350 21177 vcaballero
                                            Envelope boundExtent=feature.getExtent();
351 19737 vcaballero
                                            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 21045 jmvivo
        protected FeaturesWriter getFeaturesWriter() {
367
                FeaturesWriter writer = new DXFFeaturesWriter();
368 19940 vcaballero
//                writer.init(this);
369 19737 vcaballero
                return writer;
370
        }
371 21045 jmvivo
        public DataStoreParameters getParameters() {
372 19737 vcaballero
                return parameters;
373
        }
374 19907 vcaballero
375 21045 jmvivo
        public DataExplorer getExplorer() {
376 20424 vcaballero
                DataManager dsm=DataManager.getManager();
377 21045 jmvivo
                DataExplorerParameters dsp = dsm.createDataExplorerParameters(
378 20424 vcaballero
                                DXFDataExplorer.DATASOURCE_NAME);
379 20744 jmvivo
                ((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile());
380 20424 vcaballero
381 21045 jmvivo
                DataExplorer src=null;
382 20424 vcaballero
                try {
383
                        src = dsm.createDataExplorer(dsp);
384
                } catch (InitializeException e1) {
385
                        e1.printStackTrace();
386
                }
387
                return src;
388 19907 vcaballero
        }
389 20660 jmvivo
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 19737 vcaballero
}