Revision 308 2.0/trunk/org.gvsig.gvsig3d.osg/org.gvsig.gvsig3d.osg.io/src/main/java/org/gvsig/gvsig3d/osg/io/OSGStoreProvider.java
OSGStoreProvider.java | ||
---|---|---|
22 | 22 |
package org.gvsig.gvsig3d.osg.io; |
23 | 23 |
|
24 | 24 |
import java.io.File; |
25 |
import java.util.ArrayList; |
|
26 |
import java.util.Vector; |
|
25 | 27 |
|
26 | 28 |
import org.gvsig.fmap.dal.DALLocator; |
27 | 29 |
import org.gvsig.fmap.dal.DataManager; |
28 | 30 |
import org.gvsig.fmap.dal.DataServerExplorer; |
29 |
import org.gvsig.fmap.dal.DataStoreParameters; |
|
30 | 31 |
import org.gvsig.fmap.dal.DataTypes; |
31 | 32 |
import org.gvsig.fmap.dal.FileHelper; |
32 | 33 |
import org.gvsig.fmap.dal.exception.DataException; |
... | ... | |
34 | 35 |
import org.gvsig.fmap.dal.exception.OpenException; |
35 | 36 |
import org.gvsig.fmap.dal.exception.ReadException; |
36 | 37 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
38 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
|
39 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
|
40 |
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType; |
|
41 |
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider; |
|
42 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
|
43 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
|
37 | 44 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
45 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
|
46 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
|
38 | 47 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
39 | 48 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
40 | 49 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
41 | 50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
42 | 51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
43 | 52 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
53 |
import org.gvsig.fmap.geom.GeometryLocator; |
|
54 |
import org.gvsig.fmap.geom.GeometryManager; |
|
55 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
|
56 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
|
57 |
import org.gvsig.fmap.geom.primitive.Envelope; |
|
58 |
import org.gvsig.fmap.geom.primitive.Point; |
|
59 |
import org.gvsig.fmap.geom.type.GeometryType; |
|
60 |
import org.gvsig.osgvp.core.osg.Node; |
|
61 |
import org.gvsig.osgvp.core.osg.Vec3; |
|
62 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
|
63 |
import org.gvsig.osgvp.exceptions.node.LoadNodeException; |
|
44 | 64 |
import org.gvsig.tools.ToolsLocator; |
45 | 65 |
import org.gvsig.tools.task.SimpleTaskStatus; |
46 | 66 |
import org.gvsig.tools.task.TaskStatusManager; |
... | ... | |
66 | 86 |
private ResourceProvider resource; |
67 | 87 |
private SimpleTaskStatus taskStatus; |
68 | 88 |
|
89 |
private Node _osgNode; |
|
90 |
|
|
91 |
private long counterNewsOIDs = 0; |
|
92 |
private Envelope envelope; |
|
93 |
|
|
94 |
// An OSG store has one row with id, a point (center of bounding box) and |
|
95 |
// the entity (the filename) |
|
96 |
public static final String NAME_FIELD_ID = "ID"; |
|
97 |
public static final String NAME_FIELD_GEOMETRY = "Geometry"; |
|
98 |
public static final String NAME_FIELD_ENTITY = "Entity"; |
|
99 |
private int ID_FIELD_ID = 0; |
|
100 |
private int ID_FIELD_GEOMETRY = 1; |
|
101 |
private int ID_FIELD_ENTITY = 2; |
|
102 |
|
|
69 | 103 |
public OSGStoreProvider(OSGStoreParameters parameters, |
70 | 104 |
DataStoreProviderServices storeServices) throws InitializeException { |
71 | 105 |
super(parameters, storeServices, FileHelper |
72 | 106 |
.newMetadataContainer(METADATA_DEFINITION_NAME)); |
73 | 107 |
|
74 | 108 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
75 |
this.taskStatus = manager.creteDefaultSimpleTaskStatus("OSG"); |
|
109 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG");
|
|
76 | 110 |
|
77 | 111 |
File file = getOSGParameters().getFile(); |
78 | 112 |
resource = this.createResource(FileResource.NAME, new Object[] { file |
79 | 113 |
.getAbsolutePath() }); |
80 | 114 |
|
115 |
counterNewsOIDs = 0; |
|
81 | 116 |
resource.addConsumer(this); |
82 | 117 |
|
118 |
this.initializeFeatureTypes(); |
|
119 |
|
|
83 | 120 |
} |
84 | 121 |
|
122 |
protected void initializeFeatureTypes() throws InitializeException { |
|
123 |
try { |
|
124 |
this.open(); |
|
125 |
} catch (OpenException e) { |
|
126 |
throw new InitializeException(this.getProviderName(), e); |
|
127 |
} |
|
128 |
} |
|
129 |
|
|
85 | 130 |
public boolean closeResourceRequested(ResourceProvider arg0) { |
86 | 131 |
// TODO Auto-generated method stub |
87 | 132 |
return false; |
... | ... | |
93 | 138 |
} |
94 | 139 |
|
95 | 140 |
public Object createNewOID() { |
96 |
return new Long(0L);
|
|
141 |
return new Long(counterNewsOIDs++);
|
|
97 | 142 |
} |
98 | 143 |
|
99 | 144 |
public int getOIDType() { |
100 |
return 0;//DataTypes.LONG;
|
|
145 |
return DataTypes.LONG; |
|
101 | 146 |
} |
102 | 147 |
|
103 | 148 |
public String getFullName() { |
... | ... | |
128 | 173 |
public Object getSourceId() { |
129 | 174 |
return this.getOSGParameters().getFile(); |
130 | 175 |
} |
131 |
|
|
132 |
public DataServerExplorer getExplorer() throws ReadException { |
|
133 |
DataManager manager = DALLocator.getDataManager(); |
|
134 |
FilesystemServerExplorerParameters params; |
|
135 |
try { |
|
136 |
params = (FilesystemServerExplorerParameters) manager |
|
137 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
|
138 |
params.setRoot(this.getOSGParameters().getFile().getParent()); |
|
139 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,params); |
|
140 |
} catch (DataException e) { |
|
141 |
throw new ReadException(this.getProviderName(), e); |
|
142 |
} catch (ValidateDataParametersException e) { |
|
143 |
throw new ReadException(this.getProviderName(), e); |
|
144 |
} |
|
145 | 176 |
|
146 |
} |
|
177 |
public DataServerExplorer getExplorer() throws ReadException { |
|
178 |
DataManager manager = DALLocator.getDataManager(); |
|
179 |
FilesystemServerExplorerParameters params; |
|
180 |
try { |
|
181 |
params = (FilesystemServerExplorerParameters) manager |
|
182 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
|
183 |
params.setRoot(this.getOSGParameters().getFile().getParent()); |
|
184 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME, |
|
185 |
params); |
|
186 |
} catch (DataException e) { |
|
187 |
throw new ReadException(this.getProviderName(), e); |
|
188 |
} catch (ValidateDataParametersException e) { |
|
189 |
throw new ReadException(this.getProviderName(), e); |
|
190 |
} |
|
147 | 191 |
|
192 |
} |
|
193 |
|
|
148 | 194 |
public void open() throws OpenException { |
149 |
// TODO Auto-generated method stub |
|
150 |
|
|
195 |
if (_osgNode != null) { |
|
196 |
return; |
|
197 |
} |
|
198 |
try { |
|
199 |
this.taskStatus.add(); |
|
200 |
getResource().execute(new ResourceAction() { |
|
201 |
public Object run() throws Exception { |
|
202 |
resource.notifyOpen(); |
|
203 |
FeatureStoreProviderServices store = getStoreServices(); |
|
204 |
try { |
|
205 |
_osgNode = osgDB.readNodeFile(getFullName()); |
|
206 |
data = new ArrayList(); // this must be initialized ??? |
|
207 |
} catch (LoadNodeException e) { |
|
208 |
logger.error("Cannot load " + getFullName() + ", please check osg plugins and library paths"); |
|
209 |
return null; |
|
210 |
} |
|
211 |
resource.notifyClose(); |
|
212 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
|
213 |
EditableFeatureType featureType = new DefaultEditableFeatureType(); |
|
214 |
|
|
215 |
featureType.setHasOID(true); |
|
216 |
|
|
217 |
ID_FIELD_ID = featureType.add(NAME_FIELD_ID, DataTypes.INT) |
|
218 |
.setDefaultValue(Integer.valueOf(0)).getIndex(); |
|
219 |
EditableFeatureAttributeDescriptor attr = featureType.add( |
|
220 |
NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY); |
|
221 |
//attr.setSRS(getOSGParameters().getCRS()); // say what? |
|
222 |
GeometryType geomType = manager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM3D); |
|
223 |
attr.setGeometryType(geomType); |
|
224 |
ID_FIELD_GEOMETRY = attr.getIndex(); |
|
225 |
|
|
226 |
featureType |
|
227 |
.setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY); |
|
228 |
|
|
229 |
ID_FIELD_ENTITY = featureType.add(NAME_FIELD_ENTITY, |
|
230 |
DataTypes.STRING, 1024).setDefaultValue("") |
|
231 |
.getIndex(); |
|
232 |
|
|
233 |
// only one type and also the default type so :) |
|
234 |
ArrayList<EditableFeatureType> types = new ArrayList<EditableFeatureType>(); |
|
235 |
types.add(featureType); |
|
236 |
store.setFeatureTypes(types, featureType); |
|
237 |
|
|
238 |
// Ok we have configured the "table" so now let's fill it with values |
|
239 |
FeatureProvider feature = new DefaultFeatureProvider(featureType.getNotEditableCopy()); |
|
240 |
|
|
241 |
feature.set(ID_FIELD_ID, Integer.valueOf(0)); |
|
242 |
feature.set(ID_FIELD_ENTITY, getFullName()); |
|
243 |
|
|
244 |
Vector<Vec3> bound = _osgNode.getBoundingBox(); |
|
245 |
Point min = manager.createPoint(bound.get(0).x(), bound.get(0).y(), SUBTYPES.GEOM3D); |
|
246 |
min.setCoordinateAt(2, bound.get(0).z()); |
|
247 |
Point max = manager.createPoint(bound.get(1).x(), bound.get(1).y(), SUBTYPES.GEOM3D); |
|
248 |
max.setCoordinateAt(2, bound.get(1).z()); |
|
249 |
envelope = manager.createEnvelope(SUBTYPES.GEOM3D); |
|
250 |
envelope.setLowerCorner(min); |
|
251 |
envelope.setUpperCorner(max); |
|
252 |
|
|
253 |
double centerx = envelope.getCenter(0); |
|
254 |
double centery = envelope.getCenter(1); |
|
255 |
double centerz = envelope.getCenter(2); |
|
256 |
Point geom = manager.createPoint(centerx, centery, SUBTYPES.GEOM3D); |
|
257 |
geom.setCoordinateAt(2, centerz); |
|
258 |
|
|
259 |
feature.set(ID_FIELD_GEOMETRY, geom); |
|
260 |
feature.setDefaultEnvelope(envelope); |
|
261 |
feature.setDefaultGeometry(geom); |
|
262 |
|
|
263 |
addFeatureProvider(feature); |
|
264 |
|
|
265 |
return null; |
|
266 |
} |
|
267 |
}); |
|
268 |
this.taskStatus.terminate(); |
|
269 |
} catch (Exception e) { |
|
270 |
_osgNode = null; |
|
271 |
this.taskStatus.abort(); |
|
272 |
try { |
|
273 |
throw new OpenException(resource.getName(), e); |
|
274 |
} catch (AccessResourceException e1) { |
|
275 |
throw new OpenException(getProviderName(), e); |
|
276 |
} |
|
277 |
} finally { |
|
278 |
this.taskStatus.remove(); |
|
279 |
} |
|
151 | 280 |
} |
281 |
|
|
282 |
public Envelope getEnvelope() throws DataException { |
|
283 |
this.open(); |
|
284 |
return this.envelope; |
|
285 |
} |
|
152 | 286 |
} |
287 |
|
Also available in: Unified diff