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