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 @ 452
History | View | Annotate | Download (7.89 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 |
import org.gvsig.view3d.swing.impl.DefaultMapControl3D; |
60 |
|
61 |
/**
|
62 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
63 |
*
|
64 |
*/
|
65 |
public class DefaultLayerConverter implements LayerConverter { |
66 |
|
67 |
private static final Logger logger = LoggerFactory |
68 |
.getLogger(DefaultLayerConverter.class); |
69 |
|
70 |
public Layer convertToLayer(MapContext mapContext, FLayer layer)
|
71 |
throws DataException {
|
72 |
|
73 |
AVList params = new AVListImpl();
|
74 |
|
75 |
// Add Layer MapContext to parameters.
|
76 |
params.setValue(DefaultMapControl3D.GVSIG_MAPCONTEXT, mapContext); |
77 |
createRasterTiledImageLayerParams(layer, params); |
78 |
|
79 |
// Create configuration document from parameters.
|
80 |
Document doc =
|
81 |
TiledImageLayer.createTiledImageLayerConfigDocument(params); |
82 |
|
83 |
// Copy parameters to avoid problems
|
84 |
AVList paramsLayer = params.copy(); |
85 |
|
86 |
RasterTiledImageLayer rasterLayer = |
87 |
new RasterTiledImageLayer(doc.getDocumentElement(), paramsLayer);
|
88 |
return rasterLayer;
|
89 |
} |
90 |
|
91 |
public ElevationModel convertToElevationModel(MapContext mapContext,
|
92 |
FLayer layer) { |
93 |
// TODO Auto-generated method stub
|
94 |
return null; |
95 |
} |
96 |
|
97 |
private String getUniqueCacheId(String name) { |
98 |
// TODO Esto es insuficiente. Tener en cuenta m?s propiedades para la generaci?n
|
99 |
// del id.
|
100 |
try {
|
101 |
// Create MD5 Hash
|
102 |
MessageDigest digest =
|
103 |
java.security.MessageDigest.getInstance("MD5");
|
104 |
digest.update(name.getBytes()); |
105 |
byte messageDigest[] = digest.digest(); |
106 |
|
107 |
// Create Hex String
|
108 |
StringBuilder hexString = new StringBuilder(); |
109 |
hexString.append(name); |
110 |
hexString.append("-");
|
111 |
for (int i = 0; i < messageDigest.length; i++) |
112 |
hexString.append(Integer.toHexString(0xFF & messageDigest[i])); |
113 |
return hexString.toString();
|
114 |
|
115 |
} catch (NoSuchAlgorithmException e) { |
116 |
logger.info("MD5 algorith is not available", e);
|
117 |
return name;
|
118 |
} |
119 |
} |
120 |
|
121 |
private void createRasterTiledImageLayerParams(FLayer layer, AVList params) { |
122 |
|
123 |
// TODO Obtener las propiedades 3D de la capa mediane
|
124 |
// View3DManager.getProperties3D(layer)
|
125 |
// y rellenar los parametros con las propiedades definidas.
|
126 |
|
127 |
if (params == null) { |
128 |
params = new AVListImpl();
|
129 |
} |
130 |
|
131 |
params.setValue(AVKey.DATASET_NAME, layer.getName()); |
132 |
params.setValue(AVKey.DATA_CACHE_NAME, |
133 |
getUniqueCacheId(layer.getName())); |
134 |
|
135 |
File installLocation =
|
136 |
DataInstallUtil.getDefaultInstallLocation(WorldWind |
137 |
.getDataFileStore()); |
138 |
params.setValue(AVKey.FILE_STORE_LOCATION, |
139 |
installLocation.getAbsolutePath()); |
140 |
|
141 |
MapContext mapContext = |
142 |
(MapContext) params.getValue(DefaultMapControl3D.GVSIG_MAPCONTEXT); |
143 |
|
144 |
Sector sector = null;
|
145 |
try {
|
146 |
sector = getSector(layer, mapContext.getProjection()); |
147 |
} catch (DataException e) {
|
148 |
logger.info("Can't create sector from layer and projection", e);
|
149 |
return;
|
150 |
} |
151 |
|
152 |
params.setValue(AVKey.SECTOR, sector); |
153 |
|
154 |
if (layer instanceof FLyrRaster) { |
155 |
|
156 |
params.setValue(AVKey.WIDTH, (int) ((FLyrRaster) layer)
|
157 |
.getDataStore().getWidth()); |
158 |
params.setValue(AVKey.HEIGHT, (int) ((FLyrRaster) layer)
|
159 |
.getDataStore().getHeight()); |
160 |
|
161 |
} else if (layer instanceof FLyrVect) { |
162 |
|
163 |
ViewPort viewPort = null;
|
164 |
try {
|
165 |
viewPort = (ViewPort) mapContext.getViewPort().clone(); |
166 |
viewPort.setEnvelope(((FLyrVect) layer).getFullEnvelope()); |
167 |
// viewPort.setImageSize(new Dimension(50000, 50000));
|
168 |
} catch (CloneNotSupportedException e) { |
169 |
logger.info("Can't clone MapContext viewport: "
|
170 |
+ mapContext.getViewPort().toString(), e); |
171 |
return;
|
172 |
} catch (ReadException e) {
|
173 |
logger.info( |
174 |
"Can't get full envelope of " + layer.getInfoString(), e);
|
175 |
return;
|
176 |
} |
177 |
|
178 |
params.setValue(AVKey.WIDTH, (int) viewPort.getImageWidth());
|
179 |
params.setValue(AVKey.HEIGHT, (int) viewPort.getImageHeight());
|
180 |
} |
181 |
} |
182 |
|
183 |
private Sector getSector(FLayer layer, IProjection projection)
|
184 |
throws DataException {
|
185 |
|
186 |
ICoordTrans ct = projection.getCT(CRSFactory.getCRS("EPSG:4326"));
|
187 |
Point2D p1 = null; |
188 |
Point2D p2 = null; |
189 |
|
190 |
if (layer instanceof FLyrVect) { |
191 |
|
192 |
Envelope envelope = |
193 |
((FLyrVect) layer).getFeatureStore().getEnvelope(); |
194 |
p1 = |
195 |
ct.convert( |
196 |
new Point2D.Double(envelope.getMinimum(0), envelope |
197 |
.getMinimum(1)), null); |
198 |
p2 = |
199 |
ct.convert( |
200 |
new Point2D.Double(envelope.getMaximum(0), envelope |
201 |
.getMaximum(1)), null); |
202 |
|
203 |
} else if (layer instanceof FLyrRaster) { |
204 |
|
205 |
Extent extent = ((FLyrRaster) layer).getDataStore().getExtent(); |
206 |
p1 = |
207 |
ct.convert( |
208 |
new Point2D.Double(extent.getLLX(), extent.getLLY()), null); |
209 |
p2 = |
210 |
ct.convert( |
211 |
new Point2D.Double(extent.getURX(), extent.getURY()), null); |
212 |
} |
213 |
|
214 |
double minLatitude = p1.getY();
|
215 |
double maxLatitude = p2.getY();
|
216 |
double minLongitude = p1.getX();
|
217 |
double maxLongitude = p2.getX();
|
218 |
|
219 |
Sector sector = |
220 |
new Sector(Sector.fromDegrees(minLatitude, maxLatitude,
|
221 |
minLongitude, maxLongitude)); |
222 |
|
223 |
return sector;
|
224 |
} |
225 |
} |