root / branches / v2_0_0_prep / libraries / libFMap_dataFile / src / org / gvsig / fmap / data / feature / file / dxf / DXFStore.java @ 23878
History | View | Annotate | Download (12.5 KB)
1 |
package org.gvsig.fmap.data.feature.file.dxf; |
---|---|
2 |
|
3 |
import java.lang.ref.WeakReference; |
4 |
import java.security.KeyException; |
5 |
import java.util.ArrayList; |
6 |
import java.util.Collection; |
7 |
import java.util.Comparator; |
8 |
import java.util.Iterator; |
9 |
import java.util.TreeSet; |
10 |
|
11 |
import org.gvsig.fmap.data.DataSet; |
12 |
import org.gvsig.fmap.data.DataExplorer; |
13 |
import org.gvsig.fmap.data.DataExplorerParameters; |
14 |
import org.gvsig.fmap.data.DataManager; |
15 |
import org.gvsig.fmap.data.DataStoreParameters; |
16 |
import org.gvsig.fmap.data.exceptions.CloseException; |
17 |
import org.gvsig.fmap.data.exceptions.DataException; |
18 |
import org.gvsig.fmap.data.exceptions.InitializeException; |
19 |
import org.gvsig.fmap.data.exceptions.OpenException; |
20 |
import org.gvsig.fmap.data.exceptions.ReadException; |
21 |
import org.gvsig.fmap.data.exceptions.WriteException; |
22 |
import org.gvsig.fmap.data.feature.AttributeDescriptor; |
23 |
import org.gvsig.fmap.data.feature.Feature; |
24 |
import org.gvsig.fmap.data.feature.FeatureAttributeDescriptor; |
25 |
import org.gvsig.fmap.data.feature.FeatureSet; |
26 |
import org.gvsig.fmap.data.feature.FeatureReference; |
27 |
import org.gvsig.fmap.data.feature.FeatureType; |
28 |
import org.gvsig.fmap.data.feature.exceptions.IsNotAttributeSettingException; |
29 |
import org.gvsig.fmap.data.feature.expressionevaluator.Filter; |
30 |
import org.gvsig.fmap.data.feature.file.FileExplorerParameters; |
31 |
import org.gvsig.fmap.data.feature.file.FileStore; |
32 |
import org.gvsig.fmap.data.feature.impl.DefaultFeatureType; |
33 |
import org.gvsig.fmap.data.feature.impl.FeaturesWriter; |
34 |
import org.gvsig.fmap.data.impl.DefaultDataManager; |
35 |
import org.gvsig.fmap.data.resource.ResourceManager; |
36 |
import org.gvsig.fmap.data.resource.impl.DefaultResourceManager; |
37 |
import org.gvsig.fmap.geom.Geometry; |
38 |
import org.gvsig.fmap.geom.primitive.Envelope; |
39 |
import org.gvsig.metadata.Metadata; |
40 |
import org.gvsig.metadata.MetadataManager; |
41 |
import org.gvsig.metadata.DefaultMetadataManager; |
42 |
import org.gvsig.tools.exception.BaseException; |
43 |
|
44 |
public class DXFStore extends FileStore { |
45 |
public static String DATASTORE_NAME = "DXFStore"; |
46 |
protected Metadata metadata;
|
47 |
|
48 |
|
49 |
private DXFStoreParameters dxfParameters;
|
50 |
|
51 |
private DXFResource dxf;
|
52 |
|
53 |
public void init(DataStoreParameters parameters) throws InitializeException { |
54 |
|
55 |
dxfParameters=(DXFStoreParameters)parameters; |
56 |
|
57 |
DXFResource tmpResource = new DXFResource(dxfParameters);
|
58 |
|
59 |
DefaultResourceManager resMan = DefaultResourceManager.getResourceManager(); |
60 |
|
61 |
try {
|
62 |
this.dxf = (DXFResource) resMan.addResource(tmpResource, this); |
63 |
// this.dxf.load();
|
64 |
} catch (DataException e1) {
|
65 |
throw new InitializeException(this.getName(),e1); |
66 |
} |
67 |
|
68 |
super.init(parameters,this.dxf); |
69 |
|
70 |
try {
|
71 |
this.defaultFeatureType = this.dxf.getFeatureType(); |
72 |
this.featureTypes = new ArrayList(); |
73 |
this.featureTypes.add(defaultFeatureType);
|
74 |
|
75 |
} catch (ReadException e) {
|
76 |
throw new InitializeException(this.getName(),e); |
77 |
} |
78 |
|
79 |
} |
80 |
|
81 |
static FeatureType newFeatureType(){
|
82 |
DefaultFeatureType fType= new DefaultFeatureType();
|
83 |
|
84 |
try{
|
85 |
AttributeDescriptor descriptorID = (AttributeDescriptor) fType.createAttributeDescriptor(); |
86 |
descriptorID.loading(); |
87 |
descriptorID.setType(FeatureAttributeDescriptor.INT); |
88 |
descriptorID.setName("ID");
|
89 |
descriptorID.setDefaultValue(new Integer(0)); |
90 |
descriptorID.stopLoading(); |
91 |
fType.add(descriptorID); |
92 |
|
93 |
AttributeDescriptor descriptorShape = (AttributeDescriptor) fType.createAttributeDescriptor(); |
94 |
descriptorShape.loading(); |
95 |
descriptorShape.setType(FeatureAttributeDescriptor.GEOMETRY); |
96 |
descriptorShape.setName("GEOMETRY");
|
97 |
descriptorShape.setDefaultValue(null);
|
98 |
descriptorShape.stopLoading(); |
99 |
fType.add(descriptorShape); |
100 |
fType.setDefaultGeometry("GEOMETRY");
|
101 |
|
102 |
AttributeDescriptor descriptorEntity = (AttributeDescriptor) fType.createAttributeDescriptor(); |
103 |
descriptorEntity.loading(); |
104 |
descriptorEntity.setType(FeatureAttributeDescriptor.STRING); |
105 |
descriptorEntity.setName("Entity");
|
106 |
descriptorEntity.setDefaultValue("Entity");
|
107 |
descriptorEntity.stopLoading(); |
108 |
fType.add(descriptorEntity); |
109 |
|
110 |
AttributeDescriptor descriptorLayer = (AttributeDescriptor) fType.createAttributeDescriptor(); |
111 |
descriptorLayer.loading(); |
112 |
descriptorLayer.setType(FeatureAttributeDescriptor.STRING); |
113 |
descriptorLayer.setName("Layer");
|
114 |
descriptorLayer.setDefaultValue("default");
|
115 |
descriptorLayer.stopLoading(); |
116 |
fType.add(descriptorLayer); |
117 |
|
118 |
AttributeDescriptor descriptorColor = (AttributeDescriptor) fType.createAttributeDescriptor(); |
119 |
descriptorColor.loading(); |
120 |
descriptorColor.setType(FeatureAttributeDescriptor.INT); |
121 |
descriptorColor.setName("Color");
|
122 |
descriptorColor.setDefaultValue(new Integer(0)); |
123 |
descriptorColor.stopLoading(); |
124 |
fType.add(descriptorColor); |
125 |
|
126 |
AttributeDescriptor descriptorElevation = (AttributeDescriptor) fType.createAttributeDescriptor(); |
127 |
descriptorElevation.loading(); |
128 |
descriptorElevation.setType(FeatureAttributeDescriptor.DOUBLE); |
129 |
descriptorElevation.setName("Elevation");
|
130 |
descriptorElevation.setDefaultValue(new Double(0)); |
131 |
descriptorElevation.stopLoading(); |
132 |
fType.add(descriptorElevation); |
133 |
|
134 |
AttributeDescriptor descriptorThickness = (AttributeDescriptor) fType.createAttributeDescriptor(); |
135 |
descriptorThickness.loading(); |
136 |
descriptorThickness.setType(FeatureAttributeDescriptor.DOUBLE); |
137 |
descriptorThickness.setName("Thickness");
|
138 |
descriptorThickness.setDefaultValue(new Double(0)); |
139 |
descriptorThickness.stopLoading(); |
140 |
fType.add(descriptorThickness); |
141 |
|
142 |
AttributeDescriptor descriptorText = (AttributeDescriptor) fType.createAttributeDescriptor(); |
143 |
descriptorText.loading(); |
144 |
descriptorText.setType(FeatureAttributeDescriptor.STRING); |
145 |
descriptorText.setName("Text");
|
146 |
descriptorText.setDefaultValue("");
|
147 |
descriptorText.stopLoading(); |
148 |
fType.add(descriptorText); |
149 |
|
150 |
AttributeDescriptor descriptorHeightText = (AttributeDescriptor) fType.createAttributeDescriptor(); |
151 |
descriptorHeightText.loading(); |
152 |
descriptorHeightText.setType(FeatureAttributeDescriptor.FLOAT); |
153 |
descriptorHeightText.setName("HeightText");
|
154 |
descriptorHeightText.setDefaultValue(new Float(10)); |
155 |
descriptorHeightText.stopLoading(); |
156 |
fType.add(descriptorHeightText); |
157 |
|
158 |
AttributeDescriptor descriptorRotationText = (AttributeDescriptor) fType.createAttributeDescriptor(); |
159 |
descriptorRotationText.loading(); |
160 |
descriptorRotationText.setType(FeatureAttributeDescriptor.DOUBLE); |
161 |
descriptorRotationText.setName("Rotation");
|
162 |
descriptorRotationText.setDefaultValue(new Double(10)); |
163 |
descriptorRotationText.stopLoading(); |
164 |
fType.add(descriptorRotationText); |
165 |
fType.setDefaultGeometry("GEOMETRY");
|
166 |
fType.setGeometryTypes(new int[]{Geometry.TYPES.GEOMETRY}); |
167 |
}catch (IsNotAttributeSettingException e) {
|
168 |
e.printStackTrace(); |
169 |
} |
170 |
|
171 |
return fType;
|
172 |
|
173 |
} |
174 |
|
175 |
|
176 |
protected void doFinishEdition() throws WriteException, ReadException { |
177 |
FeaturesWriter writer = getFeaturesWriter(); |
178 |
writer.init(this);
|
179 |
writer.updateFeatureType(defaultFeatureType); |
180 |
this.dxf.editing();
|
181 |
writer.preProcess(); |
182 |
Collection collection=getDataSet();
|
183 |
Iterator iterator=collection.iterator();
|
184 |
Feature feature; |
185 |
while (iterator.hasNext()) {
|
186 |
feature= (Feature) iterator.next(); |
187 |
writer.insertFeature(feature); |
188 |
} |
189 |
writer.postProcess(); |
190 |
this.dxf.stopEditing();
|
191 |
this.dxf.changed(this); |
192 |
|
193 |
} |
194 |
|
195 |
public DataSet getDataCollection(FeatureType type, String filter, String order) throws ReadException { |
196 |
try {
|
197 |
type = this.checkFeatureTypeForCollection(type);
|
198 |
} catch (DataException e) {
|
199 |
throw new ReadException(this.getName(), e); |
200 |
} |
201 |
|
202 |
FeatureSet coll; |
203 |
DefaultDataManager manager = DefaultDataManager.getManager(); |
204 |
|
205 |
//TODO aplicar filtro, orden e incluso y el featureType
|
206 |
Filter parser = null; |
207 |
if (filter!=null){ |
208 |
parser = manager.getExpressionParser().parseFilter(filter); |
209 |
} |
210 |
// ArrayList originalFeatures=this.dxf.getFeatures();
|
211 |
int num=0; |
212 |
if (featureManager!=null){ |
213 |
num=featureManager.getNum(); |
214 |
} |
215 |
int originalSize=this.dxf.getFeatureCount(); |
216 |
Collection allFeatures=null; |
217 |
if (order!=null){ |
218 |
Comparator comparator = manager.getExpressionParser()
|
219 |
.parseComparator(order); |
220 |
allFeatures = new TreeSet(comparator); |
221 |
}else{
|
222 |
allFeatures=new ArrayList(); |
223 |
} |
224 |
Feature auxFeautre; |
225 |
for (int i = 0; i < originalSize+num; i++) { |
226 |
Feature feature=null;
|
227 |
if (i<this.dxf.getFeatureCount()){ |
228 |
feature=this.dxf.getFeature(i);
|
229 |
}else{
|
230 |
feature=featureManager.getFeature(i-originalSize,this,type);
|
231 |
} |
232 |
if (featureManager == null || !featureManager.isDeleted(feature)){ |
233 |
if (filter == null || parser.evaluate(feature)) { |
234 |
allFeatures.add(feature.getReference()); |
235 |
} |
236 |
} |
237 |
|
238 |
} |
239 |
|
240 |
coll = new DXFFeatureCollection(this, type, allFeatures); |
241 |
this.addObserver(new WeakReference(coll)); |
242 |
return coll;
|
243 |
|
244 |
} |
245 |
|
246 |
public Feature getFeatureByID(FeatureReference id,FeatureType featureType) throws ReadException { |
247 |
if (featureType==null){ |
248 |
featureType=getDefaultFeatureType(); |
249 |
}else{
|
250 |
if (!featureType.isSubtypeOf(this.getDefaultFeatureType())){ |
251 |
throw new ReadException("invalid type",this.getName()); |
252 |
} |
253 |
} |
254 |
if (this.alterMode){ |
255 |
if (featureManager.contains(id)) {
|
256 |
return featureManager.getFeature(id,this,featureType); |
257 |
} |
258 |
} |
259 |
return id.getFeature(featureType);
|
260 |
} |
261 |
|
262 |
public boolean isWithDefaultLegend() { |
263 |
return false; |
264 |
} |
265 |
|
266 |
public Object getDefaultLegend() throws ReadException { |
267 |
return this.dxf.getDefaultLegend(); |
268 |
} |
269 |
|
270 |
public Object getDefaultLabelingStrategy() throws ReadException { |
271 |
return this.dxf.getDefaultLabelingStrategy(); |
272 |
} |
273 |
|
274 |
public boolean canAlterFeatureType() { |
275 |
return false; |
276 |
} |
277 |
|
278 |
public String getName() { |
279 |
return DATASTORE_NAME;
|
280 |
} |
281 |
|
282 |
protected void doOpen() throws OpenException { |
283 |
|
284 |
} |
285 |
|
286 |
protected void doClose() throws CloseException { |
287 |
this.dxf.close();
|
288 |
} |
289 |
|
290 |
protected void doDispose() throws CloseException { |
291 |
super.doDispose();
|
292 |
ResourceManager resMan = DefaultResourceManager.getResourceManager(); |
293 |
|
294 |
try {
|
295 |
resMan.remove(this.dxf, this); |
296 |
} catch (DataException e1) {
|
297 |
throw new CloseException(this.getName(),e1); |
298 |
} catch (KeyException e) { |
299 |
throw new CloseException(this.getName(),e); |
300 |
} |
301 |
|
302 |
this.dxf = null; |
303 |
this.defaultFeatureType=null; |
304 |
this.metadata=null; |
305 |
} |
306 |
|
307 |
public boolean isEditable() { |
308 |
return this.dxf.isEditable(); |
309 |
} |
310 |
|
311 |
public Metadata getMetadata() throws BaseException { |
312 |
if (metadata==null){ |
313 |
MetadataManager manager=DefaultMetadataManager.getManager(); |
314 |
metadata=manager.create(DATASTORE_NAME); |
315 |
Envelope extent=this.dxf.getFullExtent();
|
316 |
metadata.set("extent",extent);
|
317 |
String srs=getSRS();
|
318 |
metadata.set("srs",srs);
|
319 |
metadata.set("WithDefaultLegend", Boolean.TRUE); |
320 |
} |
321 |
if (this.alterMode){ |
322 |
Envelope extent=(Envelope)metadata.get("extent");
|
323 |
FeatureSet featureCollection=(FeatureSet)getDataSet(); |
324 |
if (spatialManager.isFullExtentDirty()){
|
325 |
if (!featureCollection.isEmpty()){
|
326 |
Iterator featureIterator=featureCollection.iterator();
|
327 |
extent = ((Feature)featureIterator.next()).getDefaultEnvelope(); |
328 |
while(featureIterator.hasNext()){
|
329 |
Feature feature=(Feature)featureIterator.next(); |
330 |
Envelope boundExtent=feature.getDefaultEnvelope(); |
331 |
if (boundExtent!=null) { |
332 |
extent.add(boundExtent); |
333 |
} |
334 |
} |
335 |
} |
336 |
} |
337 |
metadata.set("extent",extent);
|
338 |
} |
339 |
return metadata;
|
340 |
} |
341 |
private String getSRS() { |
342 |
// TODO Auto-generated method stub
|
343 |
return null; |
344 |
} |
345 |
|
346 |
|
347 |
protected FeaturesWriter getFeaturesWriter() {
|
348 |
FeaturesWriter writer = new DXFFeaturesWriter();
|
349 |
// writer.init(this);
|
350 |
return writer;
|
351 |
} |
352 |
public DataStoreParameters getParameters() {
|
353 |
return parameters;
|
354 |
} |
355 |
|
356 |
public DataExplorer getExplorer() throws InitializeException { |
357 |
DataManager dsm=DefaultDataManager.getManager(); |
358 |
DataExplorerParameters dsp = dsm.createDataExplorerParameters( |
359 |
DXFDataExplorer.DATASOURCE_NAME); |
360 |
((FileExplorerParameters)dsp).setSource(dxfParameters.getFile().getParentFile()); |
361 |
|
362 |
DataExplorer src=null;
|
363 |
try {
|
364 |
src = dsm.createDataExplorer(dsp); |
365 |
} catch (InitializeException e1) {
|
366 |
e1.printStackTrace(); |
367 |
} |
368 |
return src;
|
369 |
} |
370 |
|
371 |
/* (non-Javadoc)
|
372 |
* @see org.gvsig.fmap.data.feature.FeatureStore#doRefresh()
|
373 |
*/
|
374 |
protected void doRefresh() throws OpenException, InitializeException { |
375 |
// TODO Auto-generated method stub
|
376 |
|
377 |
} |
378 |
public boolean canWriteGeometry(int gvSIGgeometryType) { |
379 |
return getFeaturesWriter().canWriteGeometry(gvSIGgeometryType);
|
380 |
} |
381 |
|
382 |
} |