Statistics
| Revision:

gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.osg / org.gvsig.gvsig3d.osg.io / src / main / java / org / gvsig / gvsig3d / osg / io / OSGStoreProvider.java @ 318

History | View | Annotate | Download (10.2 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

    
25
import java.io.File;
26
import java.util.ArrayList;
27
import java.util.Vector;
28
import org.gvsig.fmap.dal.DataStoreNotification;
29
import org.gvsig.fmap.dal.DALLocator;
30
import org.gvsig.fmap.dal.DataManager;
31
import org.gvsig.fmap.dal.DataServerExplorer;
32
import org.gvsig.fmap.dal.DataTypes;
33
import org.gvsig.fmap.dal.FileHelper;
34
import org.gvsig.fmap.dal.exception.DataException;
35
import org.gvsig.fmap.dal.exception.InitializeException;
36
import org.gvsig.fmap.dal.exception.OpenException;
37
import org.gvsig.fmap.dal.exception.ReadException;
38
import org.gvsig.fmap.dal.exception.ValidateDataParametersException;
39
import org.gvsig.fmap.dal.feature.EditableFeatureAttributeDescriptor;
40
import org.gvsig.fmap.dal.feature.EditableFeatureType;
41
import org.gvsig.fmap.dal.feature.impl.DefaultEditableFeatureType;
42
import org.gvsig.fmap.dal.feature.spi.DefaultFeatureProvider;
43
import org.gvsig.fmap.dal.feature.spi.FeatureProvider;
44
import org.gvsig.fmap.dal.feature.spi.FeatureStoreProviderServices;
45
import org.gvsig.fmap.dal.feature.spi.memory.AbstractMemoryStoreProvider;
46
import org.gvsig.fmap.dal.resource.ResourceAction;
47
import org.gvsig.fmap.dal.resource.exception.AccessResourceException;
48
import org.gvsig.fmap.dal.resource.file.FileResource;
49
import org.gvsig.fmap.dal.resource.impl.DefaultResourceNotification;
50
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer;
51
import org.gvsig.fmap.dal.resource.spi.ResourceProvider;
52
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer;
53
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters;
54
import org.gvsig.fmap.dal.spi.DataStoreProviderServices;
55
import org.gvsig.fmap.geom.GeometryLocator;
56
import org.gvsig.fmap.geom.GeometryManager;
57
import org.gvsig.fmap.geom.Geometry.SUBTYPES;
58
import org.gvsig.fmap.geom.Geometry.TYPES;
59
import org.gvsig.fmap.geom.primitive.Envelope;
60
import org.gvsig.fmap.geom.primitive.Point;
61
import org.gvsig.fmap.geom.type.GeometryType;
62
import org.gvsig.osgvp.core.osg.Node;
63
import org.gvsig.osgvp.core.osg.Vec3;
64
import org.gvsig.osgvp.core.osgdb.osgDB;
65
import org.gvsig.osgvp.exceptions.node.LoadNodeException;
66
import org.gvsig.tools.ToolsLocator;
67
import org.gvsig.tools.task.SimpleTaskStatus;
68
import org.gvsig.tools.task.TaskStatusManager;
69
import org.slf4j.Logger;
70
import org.slf4j.LoggerFactory;
71

    
72
/**
73
 * Data provider for OSG files with raster data
74
 * 
75
 * @author Rafael Gait?n (rafa.gaitan@mirage-tech.com)
76
 */
77
public class OSGStoreProvider extends AbstractMemoryStoreProvider implements
78
                ResourceConsumer {
79

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

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

    
86
        public static final String METADATA_DEFINITION_NAME = NAME;
87

    
88
        private ResourceProvider resource;
89
        private SimpleTaskStatus taskStatus;
90

    
91
        private Node _osgNode; 
92
        
93
        private long counterNewsOIDs = 0;
94
        private Envelope envelope;
95

    
96
        // An OSG store has one row with id, a point (center of bounding box) and
97
        // the entity (the filename)
98
        public static final String NAME_FIELD_ID = "ID";
99
        public static final String NAME_FIELD_GEOMETRY = "Geometry";
100
        public static final String NAME_FIELD_ENTITY = "Entity";
101
        private int ID_FIELD_ID = 0;
102
        private int ID_FIELD_GEOMETRY = 1;
103
        private int ID_FIELD_ENTITY = 2;
104

    
105
        public OSGStoreProvider(OSGStoreParameters parameters,
106
                        DataStoreProviderServices storeServices) throws InitializeException {
107
                super(parameters, storeServices, FileHelper
108
                                .newMetadataContainer(METADATA_DEFINITION_NAME));
109

    
110
                TaskStatusManager manager = ToolsLocator.getTaskStatusManager();
111
                this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG");
112

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

    
117
                counterNewsOIDs = 0;
118
                resource.addConsumer(this);
119

    
120
                this.initializeFeatureTypes();
121

    
122
        }
123

    
124
        protected void initializeFeatureTypes() throws InitializeException {
125
                try {
126
                        this.open();
127
                } catch (OpenException e) {
128
                        throw new InitializeException(this.getProviderName(), e);
129
                }
130
        }
131

    
132
        public boolean closeResourceRequested(ResourceProvider arg0) {
133
                // TODO Auto-generated method stub
134
                return false;
135
        }
136

    
137
        public void resourceChanged(ResourceProvider arg0) {
138
                // TODO Auto-generated method stub
139

    
140
        }
141

    
142
        public Object createNewOID() {
143
                return new Long(counterNewsOIDs++);
144
        }
145

    
146
        public int getOIDType() {
147
                return DataTypes.LONG;
148
        }
149

    
150
        public String getFullName() {
151
                return this.getOSGParameters().getFile().getAbsolutePath();
152
        }
153

    
154
        public String getName() {
155
                String name = this.getOSGParameters().getFile().getName();
156
                int n = name.lastIndexOf(".");
157
                if (n < 1) {
158
                        return name;
159
                }
160
                return name.substring(0, n);
161
        }
162

    
163
        private OSGStoreParameters getOSGParameters() {
164
                return (OSGStoreParameters) this.getParameters();
165
        }
166

    
167
        public String getProviderName() {
168
                return NAME;
169
        }
170

    
171
        public ResourceProvider getResource() {
172
                return resource;
173
        }
174

    
175
        public Object getSourceId() {
176
                return this.getOSGParameters().getFile();
177
        }
178

    
179
        public DataServerExplorer getExplorer() throws ReadException {
180
                DataManager manager = DALLocator.getDataManager();
181
                FilesystemServerExplorerParameters params;
182
                try {
183
                        params = (FilesystemServerExplorerParameters) manager
184
                                        .createServerExplorerParameters(FilesystemServerExplorer.NAME);
185
                        params.setRoot(this.getOSGParameters().getFile().getParent());
186
                        return manager.openServerExplorer(FilesystemServerExplorer.NAME,
187
                                        params);
188
                } catch (DataException e) {
189
                        throw new ReadException(this.getProviderName(), e);
190
                } catch (ValidateDataParametersException e) {
191
                        throw new ReadException(this.getProviderName(), e);
192
                }
193

    
194
        }
195

    
196
        public void open() throws OpenException { 
197
                if (_osgNode != null) {
198
                        return;
199
                }
200
                try {
201
                        this.taskStatus.add();
202
                        getResource().execute(new ResourceAction() {
203
                                public Object run() throws Exception {
204
                                        resource.notifyOpen();
205
                                        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;
212
                                        }
213
                                        resource.notifyClose();
214
                                        GeometryManager manager = GeometryLocator.getGeometryManager();
215
                                        EditableFeatureType featureType = store.createFeatureType();
216

    
217
                                        featureType.setHasOID(true);
218

    
219
                                        ID_FIELD_ID = featureType.add(NAME_FIELD_ID, DataTypes.INT)
220
                                                        .setDefaultValue(Integer.valueOf(0)).getIndex();
221
                                        EditableFeatureAttributeDescriptor attr = featureType.add(
222
                                                        NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY);
223
                                        //attr.setSRS(getOSGParameters().getCRS()); // say what?
224
                                        GeometryType geomType = manager.getGeometryType(TYPES.POINT, SUBTYPES.GEOM3D);
225
                                        attr.setGeometryType(geomType.getType());
226
                                        ID_FIELD_GEOMETRY = attr.getIndex();
227

    
228
                                        featureType
229
                                                        .setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY);
230

    
231
                                        ID_FIELD_ENTITY = featureType.add(NAME_FIELD_ENTITY,
232
                                                        DataTypes.STRING, 1024).setDefaultValue("")
233
                                                        .getIndex();
234
                                        
235
                                        // only one type and also the default type so :)
236
                                        ArrayList<EditableFeatureType> types = new ArrayList<EditableFeatureType>();
237
                                        types.add(featureType);
238
                                        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

    
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
 
267
                                        return null;
268
                                }
269
                        });
270
                        this.taskStatus.terminate();
271
                } catch (Exception e) {
272
                        _osgNode = null;
273
                        this.taskStatus.abort();
274
                        try {
275
                                throw new OpenException(resource.getName(), e);
276
                        } catch (AccessResourceException e1) {
277
                                throw new OpenException(getProviderName(), e);
278
                        }
279
                } finally {
280
                        this.taskStatus.remove();
281
                }
282
        }
283
        
284
        public Object getNode() throws OpenException{
285
                this.open();
286
                if (_osgNode ==null){
287
                        return null;
288
                }
289
                return _osgNode;                
290
        }
291
        
292
    public Envelope getEnvelope() throws DataException {
293
        this.open();
294
        return this.envelope;
295
    }
296
}
297