gvsig-3d / 2.0 / trunk / org.gvsig.gvsig3d.osg / org.gvsig.gvsig3d.osg.io / src / main / java / org / gvsig / gvsig3d / osg / io / OSGStoreProvider.java @ 308
History | View | Annotate | Download (9.94 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 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.spi.ResourceConsumer; |
49 |
import org.gvsig.fmap.dal.resource.spi.ResourceProvider; |
50 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
51 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
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; |
64 |
import org.gvsig.tools.ToolsLocator; |
65 |
import org.gvsig.tools.task.SimpleTaskStatus; |
66 |
import org.gvsig.tools.task.TaskStatusManager; |
67 |
import org.slf4j.Logger; |
68 |
import org.slf4j.LoggerFactory; |
69 |
|
70 |
/**
|
71 |
* Data provider for OSG files with raster data
|
72 |
*
|
73 |
* @author Rafael Gaitán (rafa.gaitan@mirage-tech.com)
|
74 |
*/
|
75 |
public class OSGStoreProvider extends AbstractMemoryStoreProvider implements |
76 |
ResourceConsumer { |
77 |
|
78 |
private static final Logger logger = LoggerFactory |
79 |
.getLogger(OSGStoreProvider.class); |
80 |
|
81 |
public static final String NAME = "OSG"; |
82 |
public static final String DESCRIPTION = "OSG file"; |
83 |
|
84 |
public static final String METADATA_DEFINITION_NAME = NAME; |
85 |
|
86 |
private ResourceProvider resource;
|
87 |
private SimpleTaskStatus taskStatus;
|
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 |
|
103 |
public OSGStoreProvider(OSGStoreParameters parameters,
|
104 |
DataStoreProviderServices storeServices) throws InitializeException {
|
105 |
super(parameters, storeServices, FileHelper
|
106 |
.newMetadataContainer(METADATA_DEFINITION_NAME)); |
107 |
|
108 |
TaskStatusManager manager = ToolsLocator.getTaskStatusManager(); |
109 |
this.taskStatus = manager.createDefaultSimpleTaskStatus("OSG"); |
110 |
|
111 |
File file = getOSGParameters().getFile();
|
112 |
resource = this.createResource(FileResource.NAME, new Object[] { file |
113 |
.getAbsolutePath() }); |
114 |
|
115 |
counterNewsOIDs = 0;
|
116 |
resource.addConsumer(this);
|
117 |
|
118 |
this.initializeFeatureTypes();
|
119 |
|
120 |
} |
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 |
|
130 |
public boolean closeResourceRequested(ResourceProvider arg0) { |
131 |
// TODO Auto-generated method stub
|
132 |
return false; |
133 |
} |
134 |
|
135 |
public void resourceChanged(ResourceProvider arg0) { |
136 |
// TODO Auto-generated method stub
|
137 |
|
138 |
} |
139 |
|
140 |
public Object createNewOID() { |
141 |
return new Long(counterNewsOIDs++); |
142 |
} |
143 |
|
144 |
public int getOIDType() { |
145 |
return DataTypes.LONG;
|
146 |
} |
147 |
|
148 |
public String getFullName() { |
149 |
return this.getOSGParameters().getFile().getAbsolutePath(); |
150 |
} |
151 |
|
152 |
public String getName() { |
153 |
String name = this.getOSGParameters().getFile().getName(); |
154 |
int n = name.lastIndexOf("."); |
155 |
if (n < 1) { |
156 |
return name;
|
157 |
} |
158 |
return name.substring(0, n); |
159 |
} |
160 |
|
161 |
private OSGStoreParameters getOSGParameters() {
|
162 |
return (OSGStoreParameters) this.getParameters(); |
163 |
} |
164 |
|
165 |
public String getProviderName() { |
166 |
return NAME;
|
167 |
} |
168 |
|
169 |
public ResourceProvider getResource() {
|
170 |
return resource;
|
171 |
} |
172 |
|
173 |
public Object getSourceId() { |
174 |
return this.getOSGParameters().getFile(); |
175 |
} |
176 |
|
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 |
} |
191 |
|
192 |
} |
193 |
|
194 |
public void open() throws OpenException { |
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 |
} |
280 |
} |
281 |
|
282 |
public Envelope getEnvelope() throws DataException { |
283 |
this.open();
|
284 |
return this.envelope; |
285 |
} |
286 |
} |
287 |
|