svn-gvsig-desktop / trunk / libraries / libFMap_dataFile / src / org / gvsig / data / datastores / vectorial / file / shp / SHPStore.java @ 20744
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.FileExplorerParameters; |
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 |
try {
|
75 |
featureType.setGeometryTypes(new int[]{this.shp.getGeometryType()}); |
76 |
} catch (ReadException e) {
|
77 |
throw new InitializeException("Can't read type",this.getName(),e); |
78 |
} |
79 |
|
80 |
this.observeResource(this.shp); |
81 |
|
82 |
} |
83 |
|
84 |
|
85 |
protected void doFinishEdition() throws WriteException, ReadException { |
86 |
this.shp.editing();
|
87 |
super.doFinishEdition();
|
88 |
this.shp.stopEditing();
|
89 |
this.shp.changed(this); |
90 |
|
91 |
} |
92 |
|
93 |
public IDataCollection getDataCollection(IFeatureType type, String filter, String order) throws ReadException { |
94 |
if (type==null){ |
95 |
type=getDefaultFeatureType(); |
96 |
} |
97 |
IFeatureCollection coll; |
98 |
if (order == null){ |
99 |
if (featureManager==null){ |
100 |
coll=new SHPFeatureCollectionBitSet(this,type,filter); |
101 |
}else{
|
102 |
coll=new ShpFeatureCollection(featureManager,this,type,filter); |
103 |
} |
104 |
}else{
|
105 |
coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
106 |
} |
107 |
this.addObserver(new WeakReference(coll)); |
108 |
return coll;
|
109 |
} |
110 |
|
111 |
public IFeature getFeatureByID(IFeatureID id) throws ReadException { |
112 |
if (this.alterMode){ |
113 |
if (featureManager.contains(id)) {
|
114 |
return featureManager.getFeature(id,this); |
115 |
} |
116 |
} |
117 |
long position=((ShpFeatureID)id).getIndex();
|
118 |
ShpFeature feature=new ShpFeature(getDefaultFeatureType(),this,position); |
119 |
return feature;
|
120 |
// return getFeatureByPosition(featureType,((ShpFeatureID)id).getIndex());
|
121 |
} |
122 |
|
123 |
public boolean isWithDefaultLegend() { |
124 |
return false; |
125 |
} |
126 |
|
127 |
public Object getDefaultLegend() { |
128 |
return null; |
129 |
} |
130 |
|
131 |
public Object getDefaultLabelingStrategy() { |
132 |
return null; |
133 |
} |
134 |
|
135 |
public boolean canAlterFeatureType() { |
136 |
return true; |
137 |
} |
138 |
|
139 |
public String getName() { |
140 |
return DATASTORE_NAME;
|
141 |
} |
142 |
|
143 |
protected void doOpen() throws OpenException { |
144 |
super.doOpen();
|
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 |
((FileExplorerParameters)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 |
} |