svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / shp / SHPStore.java @ 24081
History | View | Annotate | Download (7.98 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.fmap.data.DataSet; |
8 |
import org.gvsig.fmap.data.DataExplorer; |
9 |
import org.gvsig.fmap.data.DataExplorerParameters; |
10 |
import org.gvsig.fmap.data.DataManager; |
11 |
import org.gvsig.fmap.data.DataStoreParameters; |
12 |
import org.gvsig.fmap.data.exceptions.CloseException; |
13 |
import org.gvsig.fmap.data.exceptions.DataException; |
14 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
15 |
import org.gvsig.fmap.data.exceptions.OpenException; |
16 |
import org.gvsig.fmap.data.exceptions.ReadException; |
17 |
import org.gvsig.fmap.data.exceptions.WriteException; |
18 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
19 |
import org.gvsig.fmap.data.feature.Feature; |
20 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
21 |
import org.gvsig.fmap.data.feature.FeatureSet; |
22 |
import org.gvsig.fmap.data.feature.FeatureType; |
23 |
import org.gvsig.fmap.data.feature.exceptions.IsNotAttributeSettingException; |
24 |
import org.gvsig.fmap.data.feature.file.FileExplorerParameters; |
25 |
import org.gvsig.fmap.data.feature.file.dbf.DBFStore; |
26 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType; |
27 |
import org.gvsig.fmap.data.feature.impl.FeaturesWriter; |
28 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
29 |
import org.gvsig.fmap.data.resource.ResourceManager; |
30 |
import org.gvsig.fmap.data.resource.impl.DefaultResourceManager; |
31 |
import org.gvsig.fmap.geom.primitive.Envelope; |
32 |
import org.gvsig.metadata.Metadata; |
33 |
import org.gvsig.metadata.MetadataManager; |
34 |
import org.gvsig.metadata.DefaultMetadataManager; |
35 |
import org.gvsig.tools.exception.BaseException; |
36 |
|
37 |
public class SHPStore extends DBFStore{ |
38 |
public static String DATASTORE_NAME = "SHPStore"; |
39 |
private SHPStoreParameters shpParameters=null; |
40 |
private SHPResource shp=null; |
41 |
|
42 |
|
43 |
public void init(DataStoreParameters parameters) throws InitializeException { |
44 |
super.init(parameters);
|
45 |
defaultFeatureType=super.getDefaultFeatureType();
|
46 |
|
47 |
AttributeDescriptor dad=(AttributeDescriptor) ((DefaultFeatureType)defaultFeatureType).createAttributeDescriptor(); |
48 |
try {
|
49 |
dad.loading(); |
50 |
dad.setName("GEOMETRY");
|
51 |
dad.setType(FeatureAttributeDescriptor.GEOMETRY); |
52 |
dad.stopLoading(); |
53 |
} catch (IsNotAttributeSettingException e1) {
|
54 |
throw new InitializeException(this.getName(),e1); |
55 |
} |
56 |
defaultFeatureType.add(dad); |
57 |
defaultFeatureType.setDefaultGeometryAttributeName("GEOMETRY");
|
58 |
|
59 |
this.shpParameters=(SHPStoreParameters)parameters;
|
60 |
|
61 |
SHPResource tmpResource = new SHPResource(this.shpParameters); |
62 |
|
63 |
DefaultResourceManager resMan = DefaultResourceManager.getResourceManager(); |
64 |
|
65 |
try {
|
66 |
this.shp = (SHPResource) resMan.addResource(tmpResource, this); |
67 |
} catch (DataException e1) {
|
68 |
throw new InitializeException(this.getName(),e1); |
69 |
} |
70 |
|
71 |
try {
|
72 |
defaultFeatureType.setGeometryTypes(new int[]{this.shp.getGeometryType()}); |
73 |
} catch (ReadException e) {
|
74 |
throw new InitializeException("Can't read type",this.getName(),e); |
75 |
} |
76 |
|
77 |
this.observeResource(this.shp); |
78 |
|
79 |
} |
80 |
|
81 |
|
82 |
protected void doFinishEdition() throws WriteException, ReadException { |
83 |
this.shp.editing();
|
84 |
super.doFinishEdition();
|
85 |
this.shp.stopEditing();
|
86 |
this.shp.changed(this); |
87 |
|
88 |
} |
89 |
|
90 |
public DataSet getDataCollection(FeatureType type, String filter, String order) throws ReadException { |
91 |
if (type==null){ |
92 |
type=getDefaultFeatureType(); |
93 |
} |
94 |
FeatureSet coll; |
95 |
if (order == null){ |
96 |
//if (featureManager==null){
|
97 |
// coll=new SHPFeatureCollectionBitSet(this,type,filter);
|
98 |
//}else{
|
99 |
coll=new ShpFeatureCollection(featureManager,this,type,filter); |
100 |
//}
|
101 |
}else{
|
102 |
coll=new ShpFeatureCollectionWithFeatureID(featureManager,this,type,filter,order); |
103 |
} |
104 |
this.addObserver(new WeakReference(coll)); |
105 |
return coll;
|
106 |
} |
107 |
|
108 |
/**
|
109 |
*
|
110 |
* NOT supported in Alter Mode
|
111 |
*
|
112 |
* @param index
|
113 |
* @param featureType
|
114 |
* @return
|
115 |
* @throws ReadException
|
116 |
*/
|
117 |
protected Feature getFeatureByIndex(long index, FeatureType featureType) |
118 |
throws ReadException {
|
119 |
if (index >= this.getFeatureCount()) { |
120 |
throw new ReadException(this.getName(), |
121 |
"Unsupported operation for not persistented features");
|
122 |
} |
123 |
ShpFeature feature = new ShpFeature(featureType, this, index); |
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 = DefaultResourceManager.getResourceManager(); |
165 |
|
166 |
try {
|
167 |
resMan.remove(this.shp, this); |
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 allowWrite() { |
182 |
if (super.allowWrite() && this.shp.isEditable()) { |
183 |
return true; |
184 |
} |
185 |
return false; |
186 |
} |
187 |
|
188 |
public Metadata getMetadata() throws BaseException { |
189 |
if (metadata==null){ |
190 |
MetadataManager manager=DefaultMetadataManager.getManager(); |
191 |
metadata=manager.create(DATASTORE_NAME); |
192 |
Envelope extent=this.shp.getFullExtent();
|
193 |
metadata.set("extent",extent);
|
194 |
String srs=this.shp.getSRS(); |
195 |
metadata.set("srs",srs);
|
196 |
} |
197 |
if (this.alterMode){ |
198 |
Envelope extent=null;
|
199 |
FeatureSet featureCollection=(FeatureSet)getDataSet(); |
200 |
if (spatialManager.isFullExtentDirty()){
|
201 |
if (!featureCollection.isEmpty()){
|
202 |
Iterator featureIterator=featureCollection.iterator();
|
203 |
|
204 |
while(featureIterator.hasNext()){
|
205 |
if (extent==null){ |
206 |
extent = ((Feature)featureIterator.next()).getDefaultEnvelope(); |
207 |
}else{
|
208 |
Feature feature=(Feature)featureIterator.next(); |
209 |
Envelope boundExtent=feature.getDefaultEnvelope(); |
210 |
if (boundExtent!=null) { |
211 |
extent.add(boundExtent); |
212 |
} |
213 |
} |
214 |
} |
215 |
} |
216 |
metadata.set("extent",extent);
|
217 |
} |
218 |
|
219 |
} |
220 |
return metadata;
|
221 |
} |
222 |
// protected Feature getFeatureByPosition(FeatureType featureType,long position) throws ReadException {
|
223 |
// ShpFeature feature=new ShpFeature(featureType,this,position);
|
224 |
// feature.load(dbf, this.getGeometry(position));
|
225 |
// return feature;
|
226 |
// }
|
227 |
|
228 |
protected FeaturesWriter getFeaturesWriter() {
|
229 |
FeaturesWriter writer = new ShpFeaturesWriter();
|
230 |
// writer.init(this);
|
231 |
return writer;
|
232 |
} |
233 |
|
234 |
protected long getFeatureCount() throws ReadException{ |
235 |
return super.getFeatureCount(); |
236 |
} |
237 |
|
238 |
|
239 |
|
240 |
public DataExplorer getExplorer() throws InitializeException { |
241 |
DataManager dsm=DefaultDataManager.getManager(); |
242 |
DataExplorerParameters dsp = dsm.createExplorerParameters( |
243 |
SHPDataExplorer.DATASOURCE_NAME); |
244 |
((FileExplorerParameters)dsp).setSource(shpParameters.getFile().getParentFile()); |
245 |
|
246 |
DataExplorer src=null;
|
247 |
try {
|
248 |
src = dsm.createExplorer(dsp); |
249 |
} catch (InitializeException e1) {
|
250 |
e1.printStackTrace(); |
251 |
} |
252 |
return src;
|
253 |
} |
254 |
|
255 |
|
256 |
/* (non-Javadoc)
|
257 |
* @see org.gvsig.fmap.data.feature.file.dbf.DBFStore#doRefresh()
|
258 |
*/
|
259 |
protected void doRefresh() throws OpenException, InitializeException { |
260 |
// TODO Auto-generated method stub
|
261 |
super.doRefresh();
|
262 |
} |
263 |
|
264 |
|
265 |
/**
|
266 |
* @param featureIndex
|
267 |
* @return
|
268 |
* @throws ReadException
|
269 |
*/
|
270 |
protected Envelope getBoundingBox(long featureIndex) throws ReadException { |
271 |
return this.shp.getBoundingBox(featureIndex); |
272 |
} |
273 |
|
274 |
|
275 |
/**
|
276 |
* @param featureIndex
|
277 |
* @return
|
278 |
* @throws ReadException
|
279 |
*/
|
280 |
protected Object getGeometry(long featureIndex) throws ReadException { |
281 |
return this.shp.getGeometry(featureIndex); |
282 |
} |
283 |
} |