Revision 346 2.0/trunk/org.gvsig.gvsig3d.osg/org.gvsig.gvsig3d.osg.io/src/main/java/org/gvsig/gvsig3d/osg/io/OSGStoreProvider.java

View differences:

OSGStoreProvider.java
19 19
 * MA  02110-1301, USA.
20 20
 *
21 21
 */
22
package org.gvsig.gvsig3d.osg.io; 
22
package org.gvsig.gvsig3d.osg.io;
23 23

  
24

  
25 24
import java.io.File;
26 25
import java.util.ArrayList;
27 26
import java.util.Vector;
......
59 58
import org.gvsig.fmap.geom.primitive.Envelope;
60 59
import org.gvsig.fmap.geom.primitive.Point;
61 60
import org.gvsig.fmap.geom.type.GeometryType;
61
import org.gvsig.osgvp.core.osg.BoundingBox;
62
import org.gvsig.osgvp.core.osg.Group;
62 63
import org.gvsig.osgvp.core.osg.Node;
63 64
import org.gvsig.osgvp.core.osg.Vec3;
64 65
import org.gvsig.osgvp.core.osgdb.osgDB;
65 66
import org.gvsig.osgvp.exceptions.node.LoadNodeException;
67
import org.gvsig.osgvp.exceptions.node.NodeException;
66 68
import org.gvsig.tools.ToolsLocator;
67 69
import org.gvsig.tools.task.SimpleTaskStatus;
68 70
import org.gvsig.tools.task.TaskStatusManager;
......
77 79
public class OSGStoreProvider extends AbstractMemoryStoreProvider implements
78 80
		ResourceConsumer {
79 81

  
80
	private static final Logger logger = LoggerFactory 
82
	private static final Logger logger = LoggerFactory
81 83
			.getLogger(OSGStoreProvider.class);
82 84

  
83 85
	public static final String NAME = "OSG";
84
	public static final String DESCRIPTION = "OSG file";  
86
	public static final String DESCRIPTION = "OSG file";
85 87

  
86 88
	public static final String METADATA_DEFINITION_NAME = NAME;
87 89

  
88 90
	private ResourceProvider resource;
89 91
	private SimpleTaskStatus taskStatus;
90 92

  
91
	private Node _osgNode; 
92
	
93
	private Node _osgNode;
94

  
93 95
	private long counterNewsOIDs = 0;
94 96
	private Envelope envelope;
95 97

  
......
97 99
	// the entity (the filename)
98 100
	public static final String NAME_FIELD_ID = "ID";
99 101
	public static final String NAME_FIELD_GEOMETRY = "Geometry";
100
	public static final String NAME_FIELD_ENTITY = "Entity";
102
	// public static final String NAME_FIELD_ENTITY = "Entity";
101 103
	private int ID_FIELD_ID = 0;
102 104
	private int ID_FIELD_GEOMETRY = 1;
103
	private int ID_FIELD_ENTITY = 2;
104 105

  
106
	// private int ID_FIELD_ENTITY = 2;
107

  
105 108
	public OSGStoreProvider(OSGStoreParameters parameters,
106 109
			DataStoreProviderServices storeServices) throws InitializeException {
107 110
		super(parameters, storeServices, FileHelper
......
111 114
		this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG");
112 115

  
113 116
		File file = getOSGParameters().getFile();
114
		resource = this.createResource(FileResource.NAME, new Object[] { file
115
				.getAbsolutePath() });
117
		resource = this.createResource(FileResource.NAME,
118
				new Object[] { file.getAbsolutePath() });
116 119

  
117 120
		counterNewsOIDs = 0;
118 121
		resource.addConsumer(this);
......
193 196

  
194 197
	}
195 198

  
196
	public void open() throws OpenException { 
199
	public void open() throws OpenException {
197 200
		if (_osgNode != null) {
198 201
			return;
199 202
		}
......
203 206
				public Object run() throws Exception {
204 207
					resource.notifyOpen();
205 208
					FeatureStoreProviderServices store = getStoreServices();
206
					try {
207
						_osgNode = osgDB.readNodeFile(getFullName());
208
						data = new ArrayList(); // this must be initialized ???
209
					} catch (LoadNodeException e) {
210
						logger.error("Cannot load " + getFullName() + ", please check osg plugins and library paths");
211
						return null;
209
					File f = new File(getFullName());
210
					Vector<Vec3> bound;
211
					if (f.exists()) {
212
						try {
213
							Node loadedNode = osgDB.readNodeFile(getFullName());
214

  
215
							if (loadedNode.getNodeName().equals("gvSIG3D")) {
216

  
217
								_osgNode = loadedNode;
218
								Vector<Vec3> bb = _osgNode.getBoundingBox();
219
								Group emptyGroup = new Group();
220
								Vector<Vec3> emptyBB = emptyGroup.getBoundingBox();
221
								if (bb.equals(emptyBB)) {
222

  
223
									bound = new Vector<Vec3>();
224
									bound.add(new Vec3(-1.0, -1.0, -1.0));
225
									bound.add(new Vec3(1.0, 1.0, 1.0));
226

  
227
								}
228
								
229
								else 
230
									bound = _osgNode.getBoundingBox();
231

  
232
							}
233

  
234
							else {
235
								_osgNode = new Group();
236
								_osgNode.setNodeName("gvSIG3D");
237
								((Group) _osgNode).addChild(loadedNode);
238
								bound = _osgNode.getBoundingBox();
239
							}
240

  
241
							data = new ArrayList(); // this must be initialized
242
													// ???
243
						} catch (LoadNodeException e) {
244
							logger.error("Cannot load "
245
									+ getFullName()
246
									+ ", please check osg plugins and library paths");
247
							return null;
248
						}
249

  
250
						catch (NodeException e) {
251
							// TODO Auto-generated catch block
252
							e.printStackTrace();
253
							return null;
254
						}
255
						// In case the file doesn't exist, we create it. Needed
256
						// to create the new OSG
257
						// layers for the edition tools
258
					} else {
259

  
260
						_osgNode = new Group();
261
						_osgNode.setNodeName("gvSIG3D");
262
						bound = new Vector<Vec3>();
263
						bound.add(new Vec3(-1.0, -1.0, -1.0));
264
						bound.add(new Vec3(1.0, 1.0, 1.0));
265
						data = new ArrayList();
266

  
212 267
					}
213 268
					resource.notifyClose();
214
					GeometryManager manager = GeometryLocator.getGeometryManager();
269
					GeometryManager manager = GeometryLocator
270
							.getGeometryManager();
215 271
					EditableFeatureType featureType = store.createFeatureType();
216 272

  
217 273
					featureType.setHasOID(true);
......
220 276
							.setDefaultValue(Integer.valueOf(0)).getIndex();
221 277
					EditableFeatureAttributeDescriptor attr = featureType.add(
222 278
							NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY);
223
					//attr.setSRS(getOSGParameters().getCRS()); // say what?
224
					GeometryType geomType = manager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM3D);
279
					// attr.setSRS(getOSGParameters().getCRS()); // say what?
280
					GeometryType geomType = manager.getGeometryType(
281
							TYPES.POINT, SUBTYPES.GEOM3D);
225 282
					attr.setGeometryType(geomType.getType());
226 283
					ID_FIELD_GEOMETRY = attr.getIndex();
227 284

  
228 285
					featureType
229 286
							.setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY);
230 287

  
231
					ID_FIELD_ENTITY = featureType.add(NAME_FIELD_ENTITY,
232
							DataTypes.STRING, 1024).setDefaultValue("")
233
							.getIndex();
234
					
288
					// ID_FIELD_ENTITY = featureType
289
					// .add(NAME_FIELD_ENTITY, DataTypes.STRING, 1024)
290
					// .setDefaultValue("").getIndex();
291

  
235 292
					// only one type and also the default type so :)
236 293
					ArrayList<EditableFeatureType> types = new ArrayList<EditableFeatureType>();
237 294
					types.add(featureType);
238 295
					store.setFeatureTypes(types, featureType);
239
					
240
					// Ok we have configured the "table" so now let's fill it with values
241
	                FeatureProvider feature = new DefaultFeatureProvider(featureType.getNotEditableCopy()); 
242
	                
243
                    feature.set(ID_FIELD_ID, Integer.valueOf(0));
244
                    feature.set(ID_FIELD_ENTITY, getFullName());
245 296

  
246
                    Vector<Vec3> bound = _osgNode.getBoundingBox();
247
                    Point min = manager.createPoint(bound.get(0).x(), bound.get(0).y(), SUBTYPES.GEOM3D);
248
                    min.setCoordinateAt(2, bound.get(0).z());
249
                    Point max = manager.createPoint(bound.get(1).x(), bound.get(1).y(), SUBTYPES.GEOM3D);
250
                    max.setCoordinateAt(2, bound.get(1).z());
251
                    envelope = manager.createEnvelope(SUBTYPES.GEOM3D);
252
                    envelope.setLowerCorner(min);
253
                    envelope.setUpperCorner(max);
254
                    
255
                    double centerx = envelope.getCenter(0);
256
                    double centery = envelope.getCenter(1);
257
                    double centerz = envelope.getCenter(2);
258
                    Point geom = manager.createPoint(centerx, centery, SUBTYPES.GEOM3D);
259
                    geom.setCoordinateAt(2, centerz);
260
                    
261
                    feature.set(ID_FIELD_GEOMETRY, geom);
262
                    feature.setDefaultEnvelope(envelope);
263
                    feature.setDefaultGeometry(geom);
264
                    
265
                    addFeatureProvider(feature);
266
 
297
					// Ok we have configured the "table" so now let's fill it
298
					// with values
299
					FeatureProvider fProvider = new DefaultFeatureProvider(
300
							featureType.getNotEditableCopy());
301

  
302
					fProvider.set(ID_FIELD_ID, Integer.valueOf(0));
303
					// feature.set(ID_FIELD_ENTITY, getFullName());
304

  
305
					Point min = manager.createPoint(bound.get(0).x(), bound
306
							.get(0).y(), SUBTYPES.GEOM3D);
307
					min.setCoordinateAt(2, bound.get(0).z());
308
					Point max = manager.createPoint(bound.get(1).x(), bound
309
							.get(1).y(), SUBTYPES.GEOM3D);
310
					max.setCoordinateAt(2, bound.get(1).z());
311
					envelope = manager.createEnvelope(SUBTYPES.GEOM3D);
312
					envelope.setLowerCorner(min);
313
					envelope.setUpperCorner(max);
314

  
315
					double centerx = envelope.getCenter(0);
316
					double centery = envelope.getCenter(1);
317
					double centerz = envelope.getCenter(2);
318
					Point geom = manager.createPoint(centerx, centery,
319
							SUBTYPES.GEOM3D);
320
					geom.setCoordinateAt(2, centerz);
321

  
322
					fProvider.set(ID_FIELD_GEOMETRY, geom);
323
					fProvider.setDefaultEnvelope(envelope);
324
					fProvider.setDefaultGeometry(geom);
325

  
326
					addFeatureProvider(fProvider);
327

  
267 328
					return null;
268 329
				}
269 330
			});
......
280 341
			this.taskStatus.remove();
281 342
		}
282 343
	}
283
	
284
	public Object getNode() throws OpenException{
344

  
345
	public Object getNode() throws OpenException {
285 346
		this.open();
286
		if (_osgNode ==null){
347
		if (_osgNode == null) {
287 348
			return null;
288 349
		}
289
		return _osgNode;		
350
		return _osgNode;
290 351
	}
291
	
292
    public Envelope getEnvelope() throws DataException {
293
        this.open();
294
        return this.envelope;
295
    }
352

  
353
	public Envelope getEnvelope() throws DataException {
354
		this.open();
355
		return this.envelope;
356
	}
296 357
}
297

  

Also available in: Unified diff