Statistics
| Revision:

svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / shp / SHPStore.java @ 20692

History | View | Annotate | Download (7.69 KB)

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

    
3
import java.io.File;
4
import java.lang.ref.WeakReference;
5
import java.security.KeyException;
6
import java.util.Iterator;
7
import java.util.List;
8

    
9
import org.gvsig.data.DataManager;
10
import org.gvsig.data.IDataCollection;
11
import org.gvsig.data.IDataExplorer;
12
import org.gvsig.data.IDataExplorerParameters;
13
import org.gvsig.data.IDataStoreParameters;
14
import org.gvsig.data.ResourceManager;
15
import org.gvsig.data.datastores.vectorial.IFeaturesWriter;
16
import org.gvsig.data.datastores.vectorial.file.DataExplorerFileParameters;
17
import org.gvsig.data.datastores.vectorial.file.dbf.DBFStore;
18
import org.gvsig.data.exception.CloseException;
19
import org.gvsig.data.exception.DataException;
20
import org.gvsig.data.exception.InitializeException;
21
import org.gvsig.data.exception.OpenException;
22
import org.gvsig.data.exception.ReadException;
23
import org.gvsig.data.exception.WriteException;
24
import org.gvsig.data.spatialprovisional.IExtent;
25
import org.gvsig.data.vectorial.AttributeDescriptor;
26
import org.gvsig.data.vectorial.IFeature;
27
import org.gvsig.data.vectorial.IFeatureAttributeDescriptor;
28
import org.gvsig.data.vectorial.IFeatureCollection;
29
import org.gvsig.data.vectorial.IFeatureID;
30
import org.gvsig.data.vectorial.IFeatureType;
31
import org.gvsig.data.vectorial.IsNotAttributeSettingException;
32
import org.gvsig.exceptions.BaseException;
33
import org.gvsig.metadata.IMetadata;
34
import org.gvsig.metadata.IMetadataManager;
35
import org.gvsig.metadata.MetadataManager;
36

    
37
public class SHPStore extends DBFStore{
38
        public static String DATASTORE_NAME = "SHPStore";
39
        private SHPStoreParameters shpParameters=null;
40
        private File fileShp;
41
        private SHPResource shp=null;
42

    
43

    
44
        public void init(IDataStoreParameters parameters) throws InitializeException {
45
                super.init(parameters);
46
                featureType=super.getDefaultFeatureType();
47

    
48
                AttributeDescriptor dad=new AttributeDescriptor();
49
                try {
50
                        dad.loading();
51
                        dad.setName("GEOMETRY");
52
                        dad.setType(IFeatureAttributeDescriptor.TYPE_GEOMETRY);
53
                        dad.stopLoading();
54
                } catch (IsNotAttributeSettingException e1) {
55
                        throw new InitializeException(this.getName(),e1);
56
                }
57
                featureType.add(dad);
58
                featureType.setDefaultGeometry("GEOMETRY");
59

    
60
                this.shpParameters=(SHPStoreParameters)parameters;
61

    
62
                fileShp = this.shpParameters.getSHPFile();
63

    
64
                SHPResource tmpResource = new SHPResource(this.shpParameters);
65

    
66
                ResourceManager resMan = ResourceManager.getResourceManager();
67

    
68
                 try {
69
                         this.shp = (SHPResource)resMan.addResource(tmpResource);
70
                 } catch (DataException e1) {
71
                         throw new InitializeException(this.getName(),e1);
72
                 }
73

    
74
                 this.observeResource(this.shp);
75

    
76
        }
77

    
78

    
79
        protected void doFinishEdition() throws WriteException, ReadException {
80
                this.shp.editing();
81
                super.doFinishEdition();
82
                this.shp.stopEditing();
83
                this.shp.changed(this);
84

    
85
        }
86

    
87
        public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException {
88
                if (type==null){
89
                        type=getDefaultFeatureType();
90
                }
91
                IFeatureCollection coll;
92
                if (order == null){
93
                        if (featureManager==null){
94
                                coll=new SHPFeatureCollectionBitSet(this,type,filter);
95
                        }else{
96
                                coll=new ShpFeatureCollection(featureManager,this,type,filter);
97
                        }
98
                }else{
99
                        coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order);
100
                }
101
                this.addObserver(new WeakReference(coll));
102
                return coll;
103
        }
104

    
105
        public IFeature getFeatureByID(IFeatureID id) throws ReadException {
106
                if (this.alterMode){
107
                        if (featureManager.contains(id)) {
108
                                return featureManager.getFeature(id,this);
109
                        }
110
                }
111
                long position=((ShpFeatureID)id).getIndex();
112
                ShpFeature feature=new ShpFeature(getDefaultFeatureType(),this,position);
113
                return feature;
114
//                return getFeatureByPosition(featureType,((ShpFeatureID)id).getIndex());
115
        }
116

    
117
        public boolean isWithDefaultLegend() {
118
                return false;
119
        }
120

    
121
        public Object getDefaultLegend() {
122
                return null;
123
        }
124

    
125
        public Object getDefaultLabelingStrategy() {
126
                return null;
127
        }
128

    
129
        public boolean canAlterFeatureType() {
130
                return true;
131
        }
132

    
133
        public String getName() {
134
                return DATASTORE_NAME;
135
        }
136

    
137
        protected void doOpen() throws OpenException {
138
                super.doOpen();
139
                try {
140
                        featureType.setGeometryTypes(new int[]{this.shp.getGeometryType()});
141
                } catch (ReadException e) {
142
                        throw new OpenException("Can't read type",this.getName(),e);
143
                }
144

    
145
        }
146

    
147
        protected void doClose() throws CloseException {
148
                super.doClose();
149
                try {
150
                        this.shp.close();
151
                } catch (DataException e) {
152
                        throw new CloseException(this.getName(),e);
153
                }
154

    
155

    
156
        }
157

    
158
        protected void doDispose() throws CloseException {
159
                super.doDispose();
160
                ResourceManager resMan = ResourceManager.getResourceManager();
161

    
162
            try {
163
                        resMan.remove(this.shp);
164
                } catch (DataException e1) {
165
                        throw new CloseException(this.getName(),e1);
166
                } catch (KeyException e) {
167
                        // TODO Auto-generated catch block
168
                        throw new CloseException(this.getName(),e);
169
                }
170

    
171
                this.shp = null;
172
                this.featureType=null;
173
                this.metadata=null;
174

    
175

    
176
        }
177

    
178
        public boolean isEditable() {
179
                if (super.isEditable() && this.shp.isEditable()) return true;
180
                return false;
181
        }
182

    
183
        public IMetadata getMetadata() throws BaseException {
184
                if (metadata==null){
185
                        IMetadataManager manager=MetadataManager.getManager();
186
                        metadata=manager.create(DATASTORE_NAME);
187
                        IExtent extent=this.shp.getFullExtent();
188
                        metadata.set("extent",extent);
189
                        String srs=this.shp.getSRS();
190
                        metadata.set("srs",srs);
191
                }
192
                if (this.alterMode){
193
                        IExtent extent=(IExtent)metadata.get("extent");
194
                        IFeatureCollection featureCollection=(IFeatureCollection)getDataCollection();
195
                        if (spatialManager.isFullExtentDirty()){
196
                                if (!featureCollection.isEmpty()){
197
                                        Iterator featureIterator=featureCollection.iterator();
198
                                        extent = ((IFeature)featureIterator.next()).getExtent();
199
                                        while(featureIterator.hasNext()){
200
                                                IFeature feature=(IFeature)featureIterator.next();
201
                                                IExtent boundExtent=feature.getExtent();
202
                                                if (boundExtent!=null)
203
                                                        extent.add(boundExtent);
204
                                        }
205
                                }
206
                        }
207
                        metadata.set("extent",extent);
208
                }
209
                return metadata;
210
        }
211
//        protected IFeature getFeatureByPosition(IFeatureType featureType,long position) throws ReadException {
212
//        ShpFeature feature=new ShpFeature(featureType,this,position);
213
//        feature.load(dbf, this.getGeometry(position));
214
//        return feature;
215
//        }
216

    
217
        protected IFeaturesWriter getFeaturesWriter() {
218
                IFeaturesWriter writer = new ShpFeaturesWriter();
219
//                writer.init(this);
220
                return writer;
221
        }
222

    
223
        protected long getFeatureCount() throws ReadException{
224
                return super.getFeatureCount();
225
        }
226

    
227

    
228

    
229
        public IDataExplorer getExplorer() {
230
                DataManager dsm=DataManager.getManager();
231
                IDataExplorerParameters dsp = dsm.createDataExplorerParameters(
232
                                SHPDataExplorer.DATASOURCE_NAME);
233
                ((DataExplorerFileParameters)dsp).setSource(shpParameters.getFile().getParentFile());
234

    
235
                IDataExplorer src=null;
236
                try {
237
                        src = dsm.createDataExplorer(dsp);
238
                } catch (InitializeException e1) {
239
                        e1.printStackTrace();
240
                }
241
                return src;
242
        }
243

    
244

    
245
        /* (non-Javadoc)
246
         * @see org.gvsig.data.datastores.vectorial.file.dbf.DBFStore#doRefresh()
247
         */
248
        protected void doRefresh() throws OpenException, InitializeException {
249
                // TODO Auto-generated method stub
250
                super.doRefresh();
251
        }
252

    
253

    
254
        /**
255
         * @param featureIndex
256
         * @return
257
         * @throws ReadException
258
         */
259
        protected IExtent getBoundingBox(long featureIndex) throws ReadException {
260
                return this.shp.getBoundingBox(featureIndex);
261
        }
262

    
263

    
264
        /**
265
         * @param featureIndex
266
         * @return
267
         * @throws ReadException
268
         */
269
        protected Object getGeometry(long featureIndex) throws ReadException {
270
                return this.shp.getGeometry(featureIndex);
271
        }
272

    
273

    
274

    
275
}