Revision 450
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 | ||
---|---|---|
24 | 24 |
|
25 | 25 |
package org.gvsig.view3d.swing.impl.data; |
26 | 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 |
|
|
27 | 37 |
import java.awt.geom.Point2D; |
28 | 38 |
import java.io.File; |
29 | 39 |
import java.security.MessageDigest; |
... | ... | |
35 | 45 |
import org.slf4j.LoggerFactory; |
36 | 46 |
import org.w3c.dom.Document; |
37 | 47 |
|
38 |
import gov.nasa.worldwind.WorldWind; |
|
39 |
import gov.nasa.worldwind.avlist.AVKey; |
|
40 |
import gov.nasa.worldwind.avlist.AVList; |
|
41 |
import gov.nasa.worldwind.avlist.AVListImpl; |
|
42 |
import gov.nasa.worldwind.data.TiledImageProducer; |
|
43 |
import gov.nasa.worldwind.geom.Sector; |
|
44 |
import gov.nasa.worldwind.globes.ElevationModel; |
|
45 |
import gov.nasa.worldwind.layers.Layer; |
|
46 |
import gov.nasa.worldwindx.examples.dataimport.DataInstallUtil; |
|
47 |
|
|
48 | 48 |
import org.gvsig.fmap.crs.CRSFactory; |
49 | 49 |
import org.gvsig.fmap.dal.coverage.datastruct.Extent; |
50 | 50 |
import org.gvsig.fmap.dal.exception.DataException; |
51 |
import org.gvsig.fmap.dal.exception.ReadException; |
|
51 | 52 |
import org.gvsig.fmap.geom.primitive.Envelope; |
52 | 53 |
import org.gvsig.fmap.mapcontext.MapContext; |
53 | 54 |
import org.gvsig.fmap.mapcontext.ViewPort; |
54 | 55 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
55 | 56 |
import org.gvsig.fmap.mapcontext.layers.vectorial.FLyrVect; |
56 | 57 |
import org.gvsig.raster.fmap.layers.FLyrRaster; |
58 |
import org.gvsig.view3d.swing.api.MapControl3D; |
|
57 | 59 |
|
58 | 60 |
/** |
59 | 61 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a> |
... | ... | |
64 | 66 |
private static final Logger logger = LoggerFactory |
65 | 67 |
.getLogger(DefaultLayerConverter.class); |
66 | 68 |
|
67 |
public Layer convertToLayer(MapContext mapContext, FLayer layer) throws DataException { |
|
68 |
|
|
69 |
TiledImageProducer producer = new TiledImageProducer(); |
|
69 |
public Layer convertToLayer(MapContext mapContext, FLayer layer) |
|
70 |
throws DataException { |
|
70 | 71 |
|
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(); |
|
72 | 84 |
|
73 |
String datasetName = getUniqueCacheId(layer.getName()); |
|
85 |
RasterTiledImageLayer rasterLayer = |
|
86 |
new RasterTiledImageLayer(doc.getDocumentElement(), paramsLayer); |
|
87 |
return rasterLayer; |
|
88 |
} |
|
74 | 89 |
|
75 |
params.setValue(AVKey.DATASET_NAME, datasetName); |
|
76 |
params.setValue(AVKey.DATA_CACHE_NAME, datasetName); |
|
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 |
|
|
77 | 134 |
File installLocation = |
78 | 135 |
DataInstallUtil.getDefaultInstallLocation(WorldWind |
79 | 136 |
.getDataFileStore()); |
80 | 137 |
params.setValue(AVKey.FILE_STORE_LOCATION, |
81 | 138 |
installLocation.getAbsolutePath()); |
82 | 139 |
|
83 |
producer.setStoreParameters(params); |
|
140 |
MapContext mapContext = |
|
141 |
(MapContext) params.getValue(MapControl3D.GVSIG_MAPCONTEXT); |
|
84 | 142 |
|
85 |
IProjection projection = mapContext.getProjection(); |
|
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 |
|
|
86 | 185 |
ICoordTrans ct = projection.getCT(CRSFactory.getCRS("EPSG:4326")); |
87 | 186 |
Point2D p1 = null; |
88 | 187 |
Point2D p2 = null; |
... | ... | |
120 | 219 |
new Sector(Sector.fromDegrees(minLatitude, maxLatitude, |
121 | 220 |
minLongitude, maxLongitude)); |
122 | 221 |
|
123 |
params.setValue(AVKey.SECTOR, sector); |
|
124 |
params.setValue("GVSIG_MAPCONTEXT", mapContext); |
|
125 |
|
|
126 |
if (layer instanceof FLyrRaster) { |
|
127 |
|
|
128 |
params.setValue(AVKey.WIDTH, (int) ((FLyrRaster) layer) |
|
129 |
.getDataStore().getWidth()); |
|
130 |
params.setValue(AVKey.HEIGHT, (int) ((FLyrRaster) layer) |
|
131 |
.getDataStore().getHeight()); |
|
132 |
|
|
133 |
} else if (layer instanceof FLyrVect) { |
|
134 |
|
|
135 |
ViewPort viewPort = null; |
|
136 |
try { |
|
137 |
viewPort = (ViewPort) mapContext.getViewPort().clone(); |
|
138 |
} catch (CloneNotSupportedException e) { |
|
139 |
logger.info("Can't clone MapContext viewport", e); |
|
140 |
return null; |
|
141 |
} |
|
142 |
viewPort.setEnvelope(((FLyrVect) layer).getFullEnvelope()); |
|
143 |
// viewPort.setImageSize(new Dimension(50000, 50000)); |
|
144 |
|
|
145 |
params.setValue(AVKey.WIDTH, (int) viewPort.getImageWidth()); |
|
146 |
params.setValue(AVKey.HEIGHT, (int) viewPort.getImageHeight()); |
|
147 |
} |
|
148 |
|
|
149 |
params.setValue(AVKey.PIXEL_FORMAT, AVKey.IMAGE); |
|
150 |
|
|
151 |
producer.offerDataSource(layer, params); |
|
152 |
|
|
153 |
try { |
|
154 |
producer.startProduction(); |
|
155 |
} catch (Exception e) { |
|
156 |
producer.removeProductionState(); |
|
157 |
logger.info("Can't produce tyles of ", e); |
|
158 |
return null; |
|
159 |
} |
|
160 |
|
|
161 |
Document doc = null; |
|
162 |
Iterable results = producer.getProductionResults(); |
|
163 |
if (results != null && results.iterator() != null |
|
164 |
&& results.iterator().hasNext()) { |
|
165 |
Object o = results.iterator().next(); |
|
166 |
if (o != null && o instanceof Document) { |
|
167 |
doc = (Document) o; |
|
168 |
} |
|
169 |
} |
|
170 |
|
|
171 |
AVList paramsLayer = params.copy(); |
|
172 |
|
|
173 |
RasterTiledImageLayer rasterLayer = |
|
174 |
new RasterTiledImageLayer(doc.getDocumentElement(), paramsLayer); |
|
175 |
return rasterLayer; |
|
222 |
return sector; |
|
176 | 223 |
} |
177 |
|
|
178 |
public ElevationModel convertToElevationModel(MapContext mapContext, |
|
179 |
FLayer layer) { |
|
180 |
// TODO Auto-generated method stub |
|
181 |
return null; |
|
182 |
} |
|
183 |
|
|
184 |
private String getUniqueCacheId(String name) { |
|
185 |
|
|
186 |
try { |
|
187 |
// Create MD5 Hash |
|
188 |
MessageDigest digest = |
|
189 |
java.security.MessageDigest.getInstance("MD5"); |
|
190 |
digest.update(name.getBytes()); |
|
191 |
byte messageDigest[] = digest.digest(); |
|
192 |
|
|
193 |
// Create Hex String |
|
194 |
StringBuilder hexString = new StringBuilder(); |
|
195 |
hexString.append(name); |
|
196 |
hexString.append("-"); |
|
197 |
for (int i = 0; i < messageDigest.length; i++) |
|
198 |
hexString.append(Integer.toHexString(0xFF & messageDigest[i])); |
|
199 |
return hexString.toString(); |
|
200 |
|
|
201 |
} catch (NoSuchAlgorithmException e) { |
|
202 |
// TODO |
|
203 |
e.printStackTrace(); |
|
204 |
return name; |
|
205 |
} |
|
206 |
} |
|
207 | 224 |
} |
Also available in: Unified diff