gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.osg / org.gvsig.gvsig3d.osg.io / src / main / java / org / gvsig / gvsig3d / osg / io / OSGStoreProvider.java @ 348
History | View | Annotate | Download (13.4 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
package org.gvsig.gvsig3d.osg.io; |
23 |
|
24 |
import java.io.File; |
25 |
import java.util.ArrayList; |
26 |
import java.util.Vector; |
27 |
|
28 |
import javax.swing.JOptionPane; |
29 |
|
30 |
import org.gvsig.fmap.dal.DALLocator; |
31 |
import org.gvsig.fmap.dal.DataManager; |
32 |
import org.gvsig.fmap.dal.DataServerExplorer; |
33 |
import org.gvsig.fmap.dal.DataTypes; |
34 |
import org.gvsig.fmap.dal.FileHelper; |
35 |
import org.gvsig.fmap.dal.exception.DataException; |
36 |
import org.gvsig.fmap.dal.exception.InitializeException; |
37 |
import org.gvsig.fmap.dal.exception.OpenException; |
38 |
import org.gvsig.fmap.dal.exception.ReadException; |
39 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
40 |
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor; |
41 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
42 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
43 |
import org.gvsig.fmap.dal.feature.spi.AbstractFeatureStoreProvider; |
44 |
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider; |
45 |
import org.gvsig.fmap.dal.feature.spi.FeatureProvider; |
46 |
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices; |
47 |
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider; |
48 |
import org.gvsig.fmap.dal.resource.ResourceAction; |
49 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
50 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
51 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
52 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
53 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
54 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
55 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
56 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
57 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
58 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
59 |
import org.gvsig.fmap.geom.GeometryLocator; |
60 |
import org.gvsig.fmap.geom.GeometryManager; |
61 |
import org.gvsig.fmap.geom.exception.CreateEnvelopeException; |
62 |
import org.gvsig.fmap.geom.exception.CreateGeometryException; |
63 |
import org.gvsig.fmap.geom.primitive.Envelope; |
64 |
import org.gvsig.fmap.geom.primitive.Point; |
65 |
import org.gvsig.fmap.geom.type.GeometryType; |
66 |
import org.gvsig.fmap.geom.type.GeometryTypeNotSupportedException; |
67 |
import org.gvsig.fmap.geom.type.GeometryTypeNotValidException; |
68 |
import org.gvsig.osgvp.core.osg.Group; |
69 |
import org.gvsig.osgvp.core.osg.Node; |
70 |
import org.gvsig.osgvp.core.osg.Vec3; |
71 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
72 |
import org.gvsig.osgvp.exceptions.node.ChildIndexOutOfBoundsExceptions; |
73 |
import org.gvsig.osgvp.exceptions.node.LoadNodeException; |
74 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
75 |
import org.gvsig.tools.ToolsLocator; |
76 |
import org.gvsig.tools.task.SimpleTaskStatus; |
77 |
import org.gvsig.tools.task.TaskStatusManager; |
78 |
import org.slf4j.Logger; |
79 |
import org.slf4j.LoggerFactory; |
80 |
|
81 |
/**
|
82 |
* Data provider for OSG files with raster data
|
83 |
*
|
84 |
* @author Rafael Gait?n (rafa.gaitan@mirage-tech.com)
|
85 |
*/
|
86 |
public class OSGStoreProvider extends AbstractMemoryStoreProvider implements |
87 |
ResourceConsumer { |
88 |
|
89 |
private static final Logger logger = LoggerFactory |
90 |
.getLogger(OSGStoreProvider.class); |
91 |
|
92 |
public static final String NAME = "OSG"; |
93 |
public static final String DESCRIPTION = "OSG file"; |
94 |
|
95 |
public static final String METADATA_DEFINITION_NAME = NAME; |
96 |
|
97 |
private ResourceProvider resource;
|
98 |
private SimpleTaskStatus taskStatus;
|
99 |
|
100 |
private Node _osgNode;
|
101 |
|
102 |
private long counterNewsOIDs = 0; |
103 |
private Envelope envelope;
|
104 |
private int counterFeatures = 0; |
105 |
|
106 |
// An OSG store has one row with id, a point (center of bounding box) and
|
107 |
// the entity (the filename)
|
108 |
public static final String NAME_FIELD_ID = "ID"; |
109 |
public static final String NAME_FIELD_GEOMETRY = "Geometry"; |
110 |
// public static final String NAME_FIELD_ENTITY = "Entity";
|
111 |
private int ID_FIELD_ID = 0; |
112 |
private int ID_FIELD_GEOMETRY = 1; |
113 |
|
114 |
private EditableFeatureType featureType;
|
115 |
|
116 |
// private int ID_FIELD_ENTITY = 2;
|
117 |
|
118 |
public OSGStoreProvider(OSGStoreParameters parameters,
|
119 |
DataStoreProviderServices storeServices) throws InitializeException {
|
120 |
super(parameters, storeServices, FileHelper
|
121 |
.newMetadataContainer(METADATA_DEFINITION_NAME)); |
122 |
|
123 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
124 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG"); |
125 |
|
126 |
File file = getOSGParameters().getFile();
|
127 |
resource = this.createResource(FileResource.NAME,
|
128 |
new Object[] { file.getAbsolutePath() }); |
129 |
|
130 |
counterNewsOIDs = 0;
|
131 |
resource.addConsumer(this);
|
132 |
|
133 |
this.initializeFeatureTypes();
|
134 |
|
135 |
} |
136 |
|
137 |
protected void initializeFeatureTypes() throws InitializeException { |
138 |
try {
|
139 |
this.open();
|
140 |
} catch (OpenException e) {
|
141 |
throw new InitializeException(this.getProviderName(), e); |
142 |
} |
143 |
} |
144 |
|
145 |
public boolean closeResourceRequested(ResourceProvider arg0) { |
146 |
// TODO Auto-generated method stub
|
147 |
return false; |
148 |
} |
149 |
|
150 |
public void resourceChanged(ResourceProvider arg0) { |
151 |
// TODO Auto-generated method stub
|
152 |
|
153 |
} |
154 |
|
155 |
public Object createNewOID() { |
156 |
return new Long(counterNewsOIDs++); |
157 |
} |
158 |
|
159 |
public int getOIDType() { |
160 |
return DataTypes.LONG;
|
161 |
} |
162 |
|
163 |
public String getFullName() { |
164 |
return this.getOSGParameters().getFile().getAbsolutePath(); |
165 |
} |
166 |
|
167 |
public String getName() { |
168 |
String name = this.getOSGParameters().getFile().getName(); |
169 |
int n = name.lastIndexOf("."); |
170 |
if (n < 1) { |
171 |
return name;
|
172 |
} |
173 |
return name.substring(0, n); |
174 |
} |
175 |
|
176 |
private OSGStoreParameters getOSGParameters() {
|
177 |
return (OSGStoreParameters) this.getParameters(); |
178 |
} |
179 |
|
180 |
public String getProviderName() { |
181 |
return NAME;
|
182 |
} |
183 |
|
184 |
public ResourceProvider getResource() {
|
185 |
return resource;
|
186 |
} |
187 |
|
188 |
public Object getSourceId() { |
189 |
return this.getOSGParameters().getFile(); |
190 |
} |
191 |
|
192 |
public DataServerExplorer getExplorer() throws ReadException { |
193 |
DataManager manager = DALLocator.getDataManager(); |
194 |
FilesystemServerExplorerParameters params; |
195 |
try {
|
196 |
params = (FilesystemServerExplorerParameters) manager |
197 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
198 |
params.setRoot(this.getOSGParameters().getFile().getParent());
|
199 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,
|
200 |
params); |
201 |
} catch (DataException e) {
|
202 |
throw new ReadException(this.getProviderName(), e); |
203 |
} catch (ValidateDataParametersException e) {
|
204 |
throw new ReadException(this.getProviderName(), e); |
205 |
} |
206 |
|
207 |
} |
208 |
|
209 |
public void open() throws OpenException { |
210 |
if (_osgNode != null) { |
211 |
return;
|
212 |
} |
213 |
try {
|
214 |
this.taskStatus.add();
|
215 |
getResource().execute(new ResourceAction() {
|
216 |
public Object run() throws Exception { |
217 |
resource.notifyOpen(); |
218 |
File f = new File(getFullName()); |
219 |
|
220 |
if (f.exists()) {
|
221 |
try {
|
222 |
Node loadedNode = osgDB.readNodeFile(getFullName()); |
223 |
|
224 |
if (loadedNode == null) { |
225 |
|
226 |
JOptionPane.showMessageDialog(null, |
227 |
"Can't open the file");
|
228 |
return null; |
229 |
|
230 |
} |
231 |
|
232 |
// Open existing gvSIG3D layer
|
233 |
if (loadedNode.getNodeName().equals("gvSIG3D")) { |
234 |
processLayerGvSIG(loadedNode); |
235 |
configureFeatures(); |
236 |
addFeatures(); |
237 |
} |
238 |
// Open non gvSIG3D layer
|
239 |
else {
|
240 |
processLayerNoGvSIG(loadedNode); |
241 |
configureFeatures(); |
242 |
addFeatures(); |
243 |
} |
244 |
|
245 |
} catch (LoadNodeException e) {
|
246 |
logger.error("Cannot load "
|
247 |
+ getFullName() |
248 |
+ ", please check osg plugins and library paths");
|
249 |
return null; |
250 |
} |
251 |
// Create a new gvSIG3D layer
|
252 |
} else {
|
253 |
|
254 |
processNewLayerGvSIG(); |
255 |
configureFeatures(); |
256 |
addFeatures(); |
257 |
|
258 |
} |
259 |
|
260 |
return null; |
261 |
} |
262 |
}); |
263 |
this.taskStatus.terminate();
|
264 |
} catch (Exception e) { |
265 |
_osgNode = null;
|
266 |
this.taskStatus.abort();
|
267 |
try {
|
268 |
throw new OpenException(resource.getName(), e); |
269 |
} catch (AccessResourceException e1) {
|
270 |
throw new OpenException(getProviderName(), e); |
271 |
} |
272 |
} finally {
|
273 |
this.taskStatus.remove();
|
274 |
} |
275 |
} |
276 |
|
277 |
protected void configureFeatures() throws ResourceNotifyCloseException, |
278 |
GeometryTypeNotSupportedException, GeometryTypeNotValidException { |
279 |
|
280 |
FeatureStoreProviderServices store = getStoreServices(); |
281 |
data = new ArrayList(); // this must be initialized |
282 |
resource.notifyClose(); |
283 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
284 |
featureType = store.createFeatureType(); |
285 |
|
286 |
featureType.setHasOID(true);
|
287 |
|
288 |
ID_FIELD_ID = featureType.add(NAME_FIELD_ID, DataTypes.INT) |
289 |
.setDefaultValue(Integer.valueOf(0)).getIndex(); |
290 |
EditableFeatureAttributeDescriptor attr = featureType.add( |
291 |
NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY); |
292 |
// attr.setSRS(getOSGParameters().getCRS()); // say what?
|
293 |
GeometryType geomType = manager.getGeometryType(TYPES.POINT, |
294 |
SUBTYPES.GEOM3D); |
295 |
attr.setGeometryType(geomType.getType()); |
296 |
ID_FIELD_GEOMETRY = attr.getIndex(); |
297 |
|
298 |
featureType.setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY); |
299 |
|
300 |
// ID_FIELD_ENTITY = featureType
|
301 |
// .add(NAME_FIELD_ENTITY, DataTypes.STRING, 1024)
|
302 |
// .setDefaultValue("").getIndex();
|
303 |
|
304 |
// only one type and also the default type so :)
|
305 |
ArrayList<EditableFeatureType> types = new ArrayList<EditableFeatureType>(); |
306 |
types.add(featureType); |
307 |
store.setFeatureTypes(types, featureType); |
308 |
// Ok we have configured the "table" so now let's fill it
|
309 |
// with values
|
310 |
|
311 |
} |
312 |
|
313 |
protected void addFeatures() { |
314 |
|
315 |
Group parent;
|
316 |
// Create a new group if the scene has been loaded as a node
|
317 |
if (!(_osgNode instanceof Group)) |
318 |
try {
|
319 |
parent = new Group(_osgNode.getCPtr()); |
320 |
} catch (NodeException e) {
|
321 |
return;
|
322 |
} |
323 |
else
|
324 |
parent = (Group) _osgNode;
|
325 |
|
326 |
if (parent.getNumChildren() == 0) { |
327 |
|
328 |
// Vector<Vec3> bound = new Vector<Vec3>();
|
329 |
// bound.add(new Vec3(-1.0, -1.0, -1.0));
|
330 |
// bound.add(new Vec3(1.0, 1.0, 1.0));
|
331 |
// addOneFeature(bound);
|
332 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
333 |
if (envelope == null) |
334 |
try {
|
335 |
Point min = manager.createPoint(-1.0, -1.0, SUBTYPES.GEOM3D); |
336 |
min.setCoordinateAt(2, 0.0); |
337 |
Point max = manager.createPoint(1.0, 1.0, SUBTYPES.GEOM3D); |
338 |
max.setCoordinateAt(2, 0.0); |
339 |
|
340 |
envelope = manager.createEnvelope(SUBTYPES.GEOM3D); |
341 |
|
342 |
envelope.setLowerCorner(min); |
343 |
envelope.setUpperCorner(max); |
344 |
|
345 |
} catch (CreateEnvelopeException e) {
|
346 |
// TODO Auto-generated catch block
|
347 |
e.printStackTrace(); |
348 |
} catch (CreateGeometryException e) {
|
349 |
// TODO Auto-generated catch block
|
350 |
e.printStackTrace(); |
351 |
} |
352 |
|
353 |
} |
354 |
|
355 |
for (int i = 0; i < parent.getNumChildren(); i++) { |
356 |
|
357 |
try {
|
358 |
Vector<Vec3> bound = parent.getChild(i).getBoundingBox();
|
359 |
addOneFeature(bound); |
360 |
} catch (ChildIndexOutOfBoundsExceptions e) {
|
361 |
// TODO Auto-generated catch block
|
362 |
e.printStackTrace(); |
363 |
} |
364 |
|
365 |
} |
366 |
|
367 |
} |
368 |
|
369 |
protected void addOneFeature(Vector<Vec3> bound) { |
370 |
|
371 |
GeometryManager manager = GeometryLocator.getGeometryManager(); |
372 |
|
373 |
FeatureProvider fProvider = new DefaultFeatureProvider(
|
374 |
featureType.getNotEditableCopy()); |
375 |
|
376 |
fProvider.set(ID_FIELD_ID, Integer.valueOf(counterFeatures));
|
377 |
counterFeatures++; |
378 |
|
379 |
Point min;
|
380 |
try {
|
381 |
min = manager.createPoint(bound.get(0).x(), bound.get(0).y(), |
382 |
SUBTYPES.GEOM3D); |
383 |
min.setCoordinateAt(2, bound.get(0).z()); |
384 |
Point max = manager.createPoint(bound.get(1).x(), bound.get(1).y(), |
385 |
SUBTYPES.GEOM3D); |
386 |
max.setCoordinateAt(2, bound.get(1).z()); |
387 |
|
388 |
if (envelope == null) |
389 |
envelope = manager.createEnvelope(SUBTYPES.GEOM3D); |
390 |
|
391 |
Envelope featureEnvelope = manager.createEnvelope(SUBTYPES.GEOM3D); |
392 |
featureEnvelope.setLowerCorner(min); |
393 |
featureEnvelope.setUpperCorner(max); |
394 |
|
395 |
double centerx = featureEnvelope.getCenter(0); |
396 |
double centery = featureEnvelope.getCenter(1); |
397 |
double centerz = featureEnvelope.getCenter(2); |
398 |
Point geom = manager.createPoint(centerx, centery, SUBTYPES.GEOM3D);
|
399 |
geom.setCoordinateAt(2, centerz);
|
400 |
|
401 |
fProvider.set(ID_FIELD_GEOMETRY, geom); |
402 |
envelope.add(featureEnvelope); |
403 |
// fProvider.getDefaultEnvelope().add(envelope);
|
404 |
// fProvider.setDefaultEnvelope(envelope);
|
405 |
fProvider.setDefaultGeometry(geom); |
406 |
addFeatureProvider(fProvider); |
407 |
} catch (CreateGeometryException e) {
|
408 |
// TODO Auto-generated catch block
|
409 |
e.printStackTrace(); |
410 |
} catch (CreateEnvelopeException e) {
|
411 |
// TODO Auto-generated catch block
|
412 |
e.printStackTrace(); |
413 |
} |
414 |
|
415 |
} |
416 |
|
417 |
protected void processNewLayerGvSIG() { |
418 |
_osgNode = new Group(); |
419 |
_osgNode.setNodeName("gvSIG3D");
|
420 |
|
421 |
} |
422 |
|
423 |
protected void processLayerNoGvSIG(Node node) { |
424 |
_osgNode = new Group(); |
425 |
_osgNode.setNodeName("gvSIG3D");
|
426 |
try {
|
427 |
((Group) _osgNode).addChild(node);
|
428 |
} catch (NodeException e) {
|
429 |
// TODO Auto-generated catch block
|
430 |
e.printStackTrace(); |
431 |
} |
432 |
|
433 |
} |
434 |
|
435 |
protected void processLayerGvSIG(Node node) { |
436 |
_osgNode = node; |
437 |
} |
438 |
|
439 |
public Object getNode() throws OpenException { |
440 |
this.open();
|
441 |
if (_osgNode == null) { |
442 |
return null; |
443 |
} |
444 |
return _osgNode;
|
445 |
} |
446 |
|
447 |
public Envelope getEnvelope() throws DataException { |
448 |
this.open();
|
449 |
return this.envelope; |
450 |
} |
451 |
} |