gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.osg / org.gvsig.gvsig3d.osg.io / src / main / java / org / gvsig / gvsig3d / osg / io / OSGStoreProvider.java @ 346
History | View | Annotate | Download (11.3 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 |
import org.gvsig.fmap.dal.DataStoreNotification; |
28 |
import org.gvsig.fmap.dal.DALLocator; |
29 |
import org.gvsig.fmap.dal.DataManager; |
30 |
import org.gvsig.fmap.dal.DataServerExplorer; |
31 |
import org.gvsig.fmap.dal.DataTypes; |
32 |
import org.gvsig.fmap.dal.FileHelper; |
33 |
import org.gvsig.fmap.dal.exception.DataException; |
34 |
import org.gvsig.fmap.dal.exception.InitializeException; |
35 |
import org.gvsig.fmap.dal.exception.OpenException; |
36 |
import org.gvsig.fmap.dal.exception.ReadException; |
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; |
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; |
47 |
import org.gvsig.fmap.dal.resource.file.FileResource; |
48 |
import org.gvsig.fmap.dal.resource.impl.DefaultResourceNotification; |
49 |
import org.gvsig.fmap.dal.resource.spi.ResourceConsumer; |
50 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
52 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
53 |
import org.gvsig.fmap.dal.spi.DataStoreProviderServices; |
54 |
import org.gvsig.fmap.geom.GeometryLocator; |
55 |
import org.gvsig.fmap.geom.GeometryManager; |
56 |
import org.gvsig.fmap.geom.Geometry.SUBTYPES; |
57 |
import org.gvsig.fmap.geom.Geometry.TYPES; |
58 |
import org.gvsig.fmap.geom.primitive.Envelope; |
59 |
import org.gvsig.fmap.geom.primitive.Point; |
60 |
import org.gvsig.fmap.geom.type.GeometryType; |
61 |
import org.gvsig.osgvp.core.osg.BoundingBox; |
62 |
import org.gvsig.osgvp.core.osg.Group; |
63 |
import org.gvsig.osgvp.core.osg.Node; |
64 |
import org.gvsig.osgvp.core.osg.Vec3; |
65 |
import org.gvsig.osgvp.core.osgdb.osgDB; |
66 |
import org.gvsig.osgvp.exceptions.node.LoadNodeException; |
67 |
import org.gvsig.osgvp.exceptions.node.NodeException; |
68 |
import org.gvsig.tools.ToolsLocator; |
69 |
import org.gvsig.tools.task.SimpleTaskStatus; |
70 |
import org.gvsig.tools.task.TaskStatusManager; |
71 |
import org.slf4j.Logger; |
72 |
import org.slf4j.LoggerFactory; |
73 |
|
74 |
/**
|
75 |
* Data provider for OSG files with raster data
|
76 |
*
|
77 |
* @author Rafael Gait?n (rafa.gaitan@mirage-tech.com)
|
78 |
*/
|
79 |
public class OSGStoreProvider extends AbstractMemoryStoreProvider implements |
80 |
ResourceConsumer { |
81 |
|
82 |
private static final Logger logger = LoggerFactory |
83 |
.getLogger(OSGStoreProvider.class); |
84 |
|
85 |
public static final String NAME = "OSG"; |
86 |
public static final String DESCRIPTION = "OSG file"; |
87 |
|
88 |
public static final String METADATA_DEFINITION_NAME = NAME; |
89 |
|
90 |
private ResourceProvider resource;
|
91 |
private SimpleTaskStatus taskStatus;
|
92 |
|
93 |
private Node _osgNode;
|
94 |
|
95 |
private long counterNewsOIDs = 0; |
96 |
private Envelope envelope;
|
97 |
|
98 |
// An OSG store has one row with id, a point (center of bounding box) and
|
99 |
// the entity (the filename)
|
100 |
public static final String NAME_FIELD_ID = "ID"; |
101 |
public static final String NAME_FIELD_GEOMETRY = "Geometry"; |
102 |
// public static final String NAME_FIELD_ENTITY = "Entity";
|
103 |
private int ID_FIELD_ID = 0; |
104 |
private int ID_FIELD_GEOMETRY = 1; |
105 |
|
106 |
// private int ID_FIELD_ENTITY = 2;
|
107 |
|
108 |
public OSGStoreProvider(OSGStoreParameters parameters,
|
109 |
DataStoreProviderServices storeServices) throws InitializeException {
|
110 |
super(parameters, storeServices, FileHelper
|
111 |
.newMetadataContainer(METADATA_DEFINITION_NAME)); |
112 |
|
113 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
114 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG"); |
115 |
|
116 |
File file = getOSGParameters().getFile();
|
117 |
resource = this.createResource(FileResource.NAME,
|
118 |
new Object[] { file.getAbsolutePath() }); |
119 |
|
120 |
counterNewsOIDs = 0;
|
121 |
resource.addConsumer(this);
|
122 |
|
123 |
this.initializeFeatureTypes();
|
124 |
|
125 |
} |
126 |
|
127 |
protected void initializeFeatureTypes() throws InitializeException { |
128 |
try {
|
129 |
this.open();
|
130 |
} catch (OpenException e) {
|
131 |
throw new InitializeException(this.getProviderName(), e); |
132 |
} |
133 |
} |
134 |
|
135 |
public boolean closeResourceRequested(ResourceProvider arg0) { |
136 |
// TODO Auto-generated method stub
|
137 |
return false; |
138 |
} |
139 |
|
140 |
public void resourceChanged(ResourceProvider arg0) { |
141 |
// TODO Auto-generated method stub
|
142 |
|
143 |
} |
144 |
|
145 |
public Object createNewOID() { |
146 |
return new Long(counterNewsOIDs++); |
147 |
} |
148 |
|
149 |
public int getOIDType() { |
150 |
return DataTypes.LONG;
|
151 |
} |
152 |
|
153 |
public String getFullName() { |
154 |
return this.getOSGParameters().getFile().getAbsolutePath(); |
155 |
} |
156 |
|
157 |
public String getName() { |
158 |
String name = this.getOSGParameters().getFile().getName(); |
159 |
int n = name.lastIndexOf("."); |
160 |
if (n < 1) { |
161 |
return name;
|
162 |
} |
163 |
return name.substring(0, n); |
164 |
} |
165 |
|
166 |
private OSGStoreParameters getOSGParameters() {
|
167 |
return (OSGStoreParameters) this.getParameters(); |
168 |
} |
169 |
|
170 |
public String getProviderName() { |
171 |
return NAME;
|
172 |
} |
173 |
|
174 |
public ResourceProvider getResource() {
|
175 |
return resource;
|
176 |
} |
177 |
|
178 |
public Object getSourceId() { |
179 |
return this.getOSGParameters().getFile(); |
180 |
} |
181 |
|
182 |
public DataServerExplorer getExplorer() throws ReadException { |
183 |
DataManager manager = DALLocator.getDataManager(); |
184 |
FilesystemServerExplorerParameters params; |
185 |
try {
|
186 |
params = (FilesystemServerExplorerParameters) manager |
187 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
188 |
params.setRoot(this.getOSGParameters().getFile().getParent());
|
189 |
return manager.openServerExplorer(FilesystemServerExplorer.NAME,
|
190 |
params); |
191 |
} catch (DataException e) {
|
192 |
throw new ReadException(this.getProviderName(), e); |
193 |
} catch (ValidateDataParametersException e) {
|
194 |
throw new ReadException(this.getProviderName(), e); |
195 |
} |
196 |
|
197 |
} |
198 |
|
199 |
public void open() throws OpenException { |
200 |
if (_osgNode != null) { |
201 |
return;
|
202 |
} |
203 |
try {
|
204 |
this.taskStatus.add();
|
205 |
getResource().execute(new ResourceAction() {
|
206 |
public Object run() throws Exception { |
207 |
resource.notifyOpen(); |
208 |
FeatureStoreProviderServices store = getStoreServices(); |
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 |
|
267 |
} |
268 |
resource.notifyClose(); |
269 |
GeometryManager manager = GeometryLocator |
270 |
.getGeometryManager(); |
271 |
EditableFeatureType featureType = store.createFeatureType(); |
272 |
|
273 |
featureType.setHasOID(true);
|
274 |
|
275 |
ID_FIELD_ID = featureType.add(NAME_FIELD_ID, DataTypes.INT) |
276 |
.setDefaultValue(Integer.valueOf(0)).getIndex(); |
277 |
EditableFeatureAttributeDescriptor attr = featureType.add( |
278 |
NAME_FIELD_GEOMETRY, DataTypes.GEOMETRY); |
279 |
// attr.setSRS(getOSGParameters().getCRS()); // say what?
|
280 |
GeometryType geomType = manager.getGeometryType( |
281 |
TYPES.POINT, SUBTYPES.GEOM3D); |
282 |
attr.setGeometryType(geomType.getType()); |
283 |
ID_FIELD_GEOMETRY = attr.getIndex(); |
284 |
|
285 |
featureType |
286 |
.setDefaultGeometryAttributeName(NAME_FIELD_GEOMETRY); |
287 |
|
288 |
// ID_FIELD_ENTITY = featureType
|
289 |
// .add(NAME_FIELD_ENTITY, DataTypes.STRING, 1024)
|
290 |
// .setDefaultValue("").getIndex();
|
291 |
|
292 |
// only one type and also the default type so :)
|
293 |
ArrayList<EditableFeatureType> types = new ArrayList<EditableFeatureType>(); |
294 |
types.add(featureType); |
295 |
store.setFeatureTypes(types, featureType); |
296 |
|
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 |
|
328 |
return null; |
329 |
} |
330 |
}); |
331 |
this.taskStatus.terminate();
|
332 |
} catch (Exception e) { |
333 |
_osgNode = null;
|
334 |
this.taskStatus.abort();
|
335 |
try {
|
336 |
throw new OpenException(resource.getName(), e); |
337 |
} catch (AccessResourceException e1) {
|
338 |
throw new OpenException(getProviderName(), e); |
339 |
} |
340 |
} finally {
|
341 |
this.taskStatus.remove();
|
342 |
} |
343 |
} |
344 |
|
345 |
public Object getNode() throws OpenException { |
346 |
this.open();
|
347 |
if (_osgNode == null) { |
348 |
return null; |
349 |
} |
350 |
return _osgNode;
|
351 |
} |
352 |
|
353 |
public Envelope getEnvelope() throws DataException { |
354 |
this.open();
|
355 |
return this.envelope; |
356 |
} |
357 |
} |