svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / datastores / feature / file / shp / SHPStore.java @ 22360
History | View | Annotate | Download (8 KB)
1 |
package org.gvsig.fmap.data.datastores.feature.file.shp; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.security.KeyException; |
5 |
import java.util.Iterator; |
6 |
|
7 |
import org.gvsig.exceptions.BaseException; |
8 |
import org.gvsig.fmap.data.CloseException; |
9 |
import org.gvsig.fmap.data.DataCollection; |
10 |
import org.gvsig.fmap.data.DataException; |
11 |
import org.gvsig.fmap.data.DataExplorer; |
12 |
import org.gvsig.fmap.data.DataExplorerParameters; |
13 |
import org.gvsig.fmap.data.DataManager; |
14 |
import org.gvsig.fmap.data.DataStoreParameters; |
15 |
import org.gvsig.fmap.data.InitializeException; |
16 |
import org.gvsig.fmap.data.OpenException; |
17 |
import org.gvsig.fmap.data.ReadException; |
18 |
import org.gvsig.fmap.data.ResourceManager; |
19 |
import org.gvsig.fmap.data.WriteException; |
20 |
import org.gvsig.fmap.data.datastores.feature.file.FileExplorerParameters; |
21 |
import org.gvsig.fmap.data.datastores.feature.file.dbf.DBFFeature; |
22 |
import org.gvsig.fmap.data.datastores.feature.file.dbf.DBFFeatureID; |
23 |
import org.gvsig.fmap.data.datastores.feature.file.dbf.DBFStore; |
24 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
25 |
import org.gvsig.fmap.data.feature.DefaultFeatureType; |
26 |
import org.gvsig.fmap.data.feature.Feature; |
27 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
28 |
import org.gvsig.fmap.data.feature.FeatureCollection; |
29 |
import org.gvsig.fmap.data.feature.FeatureID; |
30 |
import org.gvsig.fmap.data.feature.FeatureType; |
31 |
import org.gvsig.fmap.data.feature.FeaturesWriter; |
32 |
import org.gvsig.fmap.data.feature.IsNotAttributeSettingException; |
33 |
import org.gvsig.fmap.geom.primitive.Envelope; |
34 |
import org.gvsig.metadata.IMetadata; |
35 |
import org.gvsig.metadata.IMetadataManager; |
36 |
import org.gvsig.metadata.MetadataManager; |
37 |
|
38 |
public class SHPStore extends DBFStore{ |
39 |
public static String DATASTORE_NAME = "SHPStore"; |
40 |
private SHPStoreParameters shpParameters=null; |
41 |
private SHPResource shp=null; |
42 |
|
43 |
|
44 |
public void init(DataStoreParameters parameters) throws InitializeException { |
45 |
super.init(parameters);
|
46 |
defaultFeatureType=super.getDefaultFeatureType();
|
47 |
|
48 |
AttributeDescriptor dad=(AttributeDescriptor) ((DefaultFeatureType)defaultFeatureType).createAttributeDescriptor(); |
49 |
try {
|
50 |
dad.loading(); |
51 |
dad.setName("GEOMETRY");
|
52 |
dad.setType(FeatureAttributeDescriptor.TYPE_GEOMETRY); |
53 |
dad.stopLoading(); |
54 |
} catch (IsNotAttributeSettingException e1) {
|
55 |
throw new InitializeException(this.getName(),e1); |
56 |
} |
57 |
defaultFeatureType.add(dad); |
58 |
defaultFeatureType.setDefaultGeometry("GEOMETRY");
|
59 |
|
60 |
this.shpParameters=(SHPStoreParameters)parameters;
|
61 |
|
62 |
SHPResource tmpResource = new SHPResource(this.shpParameters); |
63 |
|
64 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
65 |
|
66 |
try {
|
67 |
this.shp = (SHPResource)resMan.addResource(tmpResource);
|
68 |
} catch (DataException e1) {
|
69 |
throw new InitializeException(this.getName(),e1); |
70 |
} |
71 |
|
72 |
try {
|
73 |
defaultFeatureType.setGeometryTypes(new int[]{this.shp.getGeometryType()}); |
74 |
} catch (ReadException e) {
|
75 |
throw new InitializeException("Can't read type",this.getName(),e); |
76 |
} |
77 |
|
78 |
this.observeResource(this.shp); |
79 |
|
80 |
} |
81 |
|
82 |
|
83 |
protected void doFinishEdition() throws WriteException, ReadException { |
84 |
this.shp.editing();
|
85 |
super.doFinishEdition();
|
86 |
this.shp.stopEditing();
|
87 |
this.shp.changed(this); |
88 |
|
89 |
} |
90 |
|
91 |
public DataCollection getDataCollection(FeatureType type, String filter, String order) throws ReadException { |
92 |
if (type==null){ |
93 |
type=getDefaultFeatureType(); |
94 |
} |
95 |
FeatureCollection coll; |
96 |
if (order == null){ |
97 |
if (featureManager==null){ |
98 |
coll=new SHPFeatureCollectionBitSet(this,type,filter); |
99 |
}else{
|
100 |
coll=new ShpFeatureCollection(featureManager,this,type,filter); |
101 |
} |
102 |
}else{
|
103 |
coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
104 |
} |
105 |
this.addObserver(new WeakReference(coll)); |
106 |
return coll;
|
107 |
} |
108 |
|
109 |
public Feature getFeatureByID(FeatureID id,FeatureType featureType) throws ReadException { |
110 |
if (featureType==null){ |
111 |
featureType=getDefaultFeatureType(); |
112 |
}else{
|
113 |
if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){ |
114 |
throw new ReadException("invalid type",this.getName()); |
115 |
} |
116 |
} |
117 |
if (this.alterMode){ |
118 |
if (featureManager.contains(id)) {
|
119 |
return featureManager.getFeature(id,this,featureType); |
120 |
} |
121 |
} |
122 |
long position=((ShpFeatureID)id).getIndex();
|
123 |
ShpFeature feature=new ShpFeature(featureType,this,position); |
124 |
return feature;
|
125 |
} |
126 |
|
127 |
public boolean isWithDefaultLegend() { |
128 |
return false; |
129 |
} |
130 |
|
131 |
public Object getDefaultLegend() { |
132 |
return null; |
133 |
} |
134 |
|
135 |
public Object getDefaultLabelingStrategy() { |
136 |
return null; |
137 |
} |
138 |
|
139 |
public boolean canAlterFeatureType() { |
140 |
return true; |
141 |
} |
142 |
|
143 |
public String getName() { |
144 |
return DATASTORE_NAME;
|
145 |
} |
146 |
|
147 |
protected void doOpen() throws OpenException { |
148 |
super.doOpen();
|
149 |
} |
150 |
|
151 |
protected void doClose() throws CloseException { |
152 |
super.doClose();
|
153 |
try {
|
154 |
this.shp.close();
|
155 |
} catch (DataException e) {
|
156 |
throw new CloseException(this.getName(),e); |
157 |
} |
158 |
|
159 |
|
160 |
} |
161 |
|
162 |
protected void doDispose() throws CloseException { |
163 |
super.doDispose();
|
164 |
ResourceManager resMan = ResourceManager.getResourceManager(); |
165 |
|
166 |
try {
|
167 |
resMan.remove(this.shp);
|
168 |
} catch (DataException e1) {
|
169 |
throw new CloseException(this.getName(),e1); |
170 |
} catch (KeyException e) { |
171 |
// TODO Auto-generated catch block
|
172 |
throw new CloseException(this.getName(),e); |
173 |
} |
174 |
|
175 |
this.shp = null; |
176 |
this.metadata=null; |
177 |
|
178 |
|
179 |
} |
180 |
|
181 |
public boolean isEditable() { |
182 |
if (super.isEditable() && this.shp.isEditable()) return true; |
183 |
return false; |
184 |
} |
185 |
|
186 |
public IMetadata getMetadata() throws BaseException { |
187 |
if (metadata==null){ |
188 |
IMetadataManager manager=MetadataManager.getManager(); |
189 |
metadata=manager.create(DATASTORE_NAME); |
190 |
Envelope extent=this.shp.getFullExtent();
|
191 |
metadata.set("extent",extent);
|
192 |
String srs=this.shp.getSRS(); |
193 |
metadata.set("srs",srs);
|
194 |
} |
195 |
if (this.alterMode){ |
196 |
Envelope extent=(Envelope)metadata.get("extent");
|
197 |
FeatureCollection featureCollection=(FeatureCollection)getDataCollection(); |
198 |
if (spatialManager.isFullExtentDirty()){
|
199 |
if (!featureCollection.isEmpty()){
|
200 |
Iterator featureIterator=featureCollection.iterator();
|
201 |
extent = ((Feature)featureIterator.next()).getExtent(); |
202 |
while(featureIterator.hasNext()){
|
203 |
Feature feature=(Feature)featureIterator.next(); |
204 |
Envelope boundExtent=feature.getExtent(); |
205 |
if (boundExtent!=null) |
206 |
extent.add(boundExtent); |
207 |
} |
208 |
} |
209 |
} |
210 |
metadata.set("extent",extent);
|
211 |
} |
212 |
return metadata;
|
213 |
} |
214 |
// protected Feature getFeatureByPosition(FeatureType featureType,long position) throws ReadException {
|
215 |
// ShpFeature feature=new ShpFeature(featureType,this,position);
|
216 |
// feature.load(dbf, this.getGeometry(position));
|
217 |
// return feature;
|
218 |
// }
|
219 |
|
220 |
protected FeaturesWriter getFeaturesWriter() {
|
221 |
FeaturesWriter writer = new ShpFeaturesWriter();
|
222 |
// writer.init(this);
|
223 |
return writer;
|
224 |
} |
225 |
|
226 |
protected long getFeatureCount() throws ReadException{ |
227 |
return super.getFeatureCount(); |
228 |
} |
229 |
|
230 |
|
231 |
|
232 |
public DataExplorer getExplorer() throws InitializeException { |
233 |
DataManager dsm=DataManager.getManager(); |
234 |
DataExplorerParameters dsp = dsm.createDataExplorerParameters( |
235 |
SHPDataExplorer.DATASOURCE_NAME); |
236 |
((FileExplorerParameters)dsp).setSource(shpParameters.getFile().getParentFile()); |
237 |
|
238 |
DataExplorer src=null;
|
239 |
try {
|
240 |
src = dsm.createDataExplorer(dsp); |
241 |
} catch (InitializeException e1) {
|
242 |
e1.printStackTrace(); |
243 |
} |
244 |
return src;
|
245 |
} |
246 |
|
247 |
|
248 |
/* (non-Javadoc)
|
249 |
* @see org.gvsig.fmap.data.datastores.feature.file.dbf.DBFStore#doRefresh()
|
250 |
*/
|
251 |
protected void doRefresh() throws OpenException, InitializeException { |
252 |
// TODO Auto-generated method stub
|
253 |
super.doRefresh();
|
254 |
} |
255 |
|
256 |
|
257 |
/**
|
258 |
* @param featureIndex
|
259 |
* @return
|
260 |
* @throws ReadException
|
261 |
*/
|
262 |
protected Envelope getBoundingBox(long featureIndex) throws ReadException { |
263 |
return this.shp.getBoundingBox(featureIndex); |
264 |
} |
265 |
|
266 |
|
267 |
/**
|
268 |
* @param featureIndex
|
269 |
* @return
|
270 |
* @throws ReadException
|
271 |
*/
|
272 |
protected Object getGeometry(long featureIndex) throws ReadException { |
273 |
return this.shp.getGeometry(featureIndex); |
274 |
} |
275 |
} |