gvsig-3d / 2.1 / trunk / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / data / DefaultLayerConverter.java @ 450
History | View | Annotate | Download (7.82 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright ? 2007-2015 gvSIG Association
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.view3d.swing.impl.data; |
26 |
|
27 |
import gov.nasa.worldwind.WorldWind; |
28 |
import gov.nasa.worldwind.avlist.AVKey; |
29 |
import gov.nasa.worldwind.avlist.AVList; |
30 |
import gov.nasa.worldwind.avlist.AVListImpl; |
31 |
import gov.nasa.worldwind.geom.Sector; |
32 |
import gov.nasa.worldwind.globes.ElevationModel; |
33 |
import gov.nasa.worldwind.layers.Layer; |
34 |
import gov.nasa.worldwind.layers.TiledImageLayer; |
35 |
import gov.nasa.worldwindx.examples.dataimport.DataInstallUtil; |
36 |
|
37 |
import java.awt.geom.Point2D; |
38 |
import java.io.File; |
39 |
import java.security.MessageDigest; |
40 |
import java.security.NoSuchAlgorithmException; |
41 |
|
42 |
import org.cresques.cts.ICoordTrans; |
43 |
import org.cresques.cts.IProjection; |
44 |
import org.slf4j.Logger; |
45 |
import org.slf4j.LoggerFactory; |
46 |
import org.w3c.dom.Document; |
47 |
|
48 |
import org.gvsig.fmap.crs.CRSFactory; |
49 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
50 |
import org.gvsig.fmap.dal.exception.DataException; |
51 |
import org.gvsig.fmap.dal.exception.ReadException; |
52 |
import org.gvsig.fmap.geom.primitive.Envelope; |
53 |
import org.gvsig.fmap.mapcontext.MapContext; |
54 |
import org.gvsig.fmap.mapcontext.ViewPort; |
55 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
56 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
57 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
58 |
import org.gvsig.view3d.swing.api.MapControl3D; |
59 |
|
60 |
/**
|
61 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
62 |
*
|
63 |
*/
|
64 |
public class DefaultLayerConverter implements LayerConverter { |
65 |
|
66 |
private static final Logger logger = LoggerFactory |
67 |
.getLogger(DefaultLayerConverter.class); |
68 |
|
69 |
public Layer convertToLayer(MapContext mapContext, FLayer layer)
|
70 |
throws DataException {
|
71 |
|
72 |
AVList params = new AVListImpl();
|
73 |
|
74 |
// Add Layer MapContext to parameters.
|
75 |
params.setValue(MapControl3D.GVSIG_MAPCONTEXT, mapContext); |
76 |
createRasterTiledImageLayerParams(layer, params); |
77 |
|
78 |
// Create configuration document from parameters.
|
79 |
Document doc =
|
80 |
TiledImageLayer.createTiledImageLayerConfigDocument(params); |
81 |
|
82 |
// Copy parameters to avoid problems
|
83 |
AVList paramsLayer = params.copy(); |
84 |
|
85 |
RasterTiledImageLayer rasterLayer = |
86 |
new RasterTiledImageLayer(doc.getDocumentElement(), paramsLayer);
|
87 |
return rasterLayer;
|
88 |
} |
89 |
|
90 |
public ElevationModel convertToElevationModel(MapContext mapContext,
|
91 |
FLayer layer) { |
92 |
// TODO Auto-generated method stub
|
93 |
return null; |
94 |
} |
95 |
|
96 |
private String getUniqueCacheId(String name) { |
97 |
// TODO Esto es insuficiente. Tener en cuenta m?s propiedades para la generaci?n
|
98 |
// del id.
|
99 |
try {
|
100 |
// Create MD5 Hash
|
101 |
MessageDigest digest =
|
102 |
java.security.MessageDigest.getInstance("MD5");
|
103 |
digest.update(name.getBytes()); |
104 |
byte messageDigest[] = digest.digest(); |
105 |
|
106 |
// Create Hex String
|
107 |
StringBuilder hexString = new StringBuilder(); |
108 |
hexString.append(name); |
109 |
hexString.append("-");
|
110 |
for (int i = 0; i < messageDigest.length; i++) |
111 |
hexString.append(Integer.toHexString(0xFF & messageDigest[i])); |
112 |
return hexString.toString();
|
113 |
|
114 |
} catch (NoSuchAlgorithmException e) { |
115 |
logger.info("MD5 algorith is not available", e);
|
116 |
return name;
|
117 |
} |
118 |
} |
119 |
|
120 |
private void createRasterTiledImageLayerParams(FLayer layer, AVList params) { |
121 |
|
122 |
// TODO Obtener las propiedades 3D de la capa mediane
|
123 |
// View3DManager.getProperties3D(layer)
|
124 |
// y rellenar los parametros con las propiedades definidas.
|
125 |
|
126 |
if (params == null) { |
127 |
params = new AVListImpl();
|
128 |
} |
129 |
|
130 |
params.setValue(AVKey.DATASET_NAME, layer.getName()); |
131 |
params.setValue(AVKey.DATA_CACHE_NAME, |
132 |
getUniqueCacheId(layer.getName())); |
133 |
|
134 |
File installLocation =
|
135 |
DataInstallUtil.getDefaultInstallLocation(WorldWind |
136 |
.getDataFileStore()); |
137 |
params.setValue(AVKey.FILE_STORE_LOCATION, |
138 |
installLocation.getAbsolutePath()); |
139 |
|
140 |
MapContext mapContext = |
141 |
(MapContext) params.getValue(MapControl3D.GVSIG_MAPCONTEXT); |
142 |
|
143 |
Sector sector = null;
|
144 |
try {
|
145 |
sector = getSector(layer, mapContext.getProjection()); |
146 |
} catch (DataException e) {
|
147 |
logger.info("Can't create sector from layer and projection", e);
|
148 |
return;
|
149 |
} |
150 |
|
151 |
params.setValue(AVKey.SECTOR, sector); |
152 |
|
153 |
if (layer instanceof FLyrRaster) { |
154 |
|
155 |
params.setValue(AVKey.WIDTH, (int) ((FLyrRaster) layer)
|
156 |
.getDataStore().getWidth()); |
157 |
params.setValue(AVKey.HEIGHT, (int) ((FLyrRaster) layer)
|
158 |
.getDataStore().getHeight()); |
159 |
|
160 |
} else if (layer instanceof FLyrVect) { |
161 |
|
162 |
ViewPort viewPort = null;
|
163 |
try {
|
164 |
viewPort = (ViewPort) mapContext.getViewPort().clone(); |
165 |
viewPort.setEnvelope(((FLyrVect) layer).getFullEnvelope()); |
166 |
// viewPort.setImageSize(new Dimension(50000, 50000));
|
167 |
} catch (CloneNotSupportedException e) { |
168 |
logger.info("Can't clone MapContext viewport: "
|
169 |
+ mapContext.getViewPort().toString(), e); |
170 |
return;
|
171 |
} catch (ReadException e) {
|
172 |
logger.info( |
173 |
"Can't get full envelope of " + layer.getInfoString(), e);
|
174 |
return;
|
175 |
} |
176 |
|
177 |
params.setValue(AVKey.WIDTH, (int) viewPort.getImageWidth());
|
178 |
params.setValue(AVKey.HEIGHT, (int) viewPort.getImageHeight());
|
179 |
} |
180 |
} |
181 |
|
182 |
private Sector getSector(FLayer layer, IProjection projection)
|
183 |
throws DataException {
|
184 |
|
185 |
ICoordTrans ct = projection.getCT(CRSFactory.getCRS("EPSG:4326"));
|
186 |
Point2D p1 = null; |
187 |
Point2D p2 = null; |
188 |
|
189 |
if (layer instanceof FLyrVect) { |
190 |
|
191 |
Envelope envelope = |
192 |
((FLyrVect) layer).getFeatureStore().getEnvelope(); |
193 |
p1 = |
194 |
ct.convert( |
195 |
new Point2D.Double(envelope.getMinimum(0), envelope |
196 |
.getMinimum(1)), null); |
197 |
p2 = |
198 |
ct.convert( |
199 |
new Point2D.Double(envelope.getMaximum(0), envelope |
200 |
.getMaximum(1)), null); |
201 |
|
202 |
} else if (layer instanceof FLyrRaster) { |
203 |
|
204 |
Extent extent = ((FLyrRaster) layer).getDataStore().getExtent(); |
205 |
p1 = |
206 |
ct.convert( |
207 |
new Point2D.Double(extent.getLLX(), extent.getLLY()), null); |
208 |
p2 = |
209 |
ct.convert( |
210 |
new Point2D.Double(extent.getURX(), extent.getURY()), null); |
211 |
} |
212 |
|
213 |
double minLatitude = p1.getY();
|
214 |
double maxLatitude = p2.getY();
|
215 |
double minLongitude = p1.getX();
|
216 |
double maxLongitude = p2.getX();
|
217 |
|
218 |
Sector sector = |
219 |
new Sector(Sector.fromDegrees(minLatitude, maxLatitude,
|
220 |
minLongitude, maxLongitude)); |
221 |
|
222 |
return sector;
|
223 |
} |
224 |
} |