gvsig-3d / 2.1 / branches / org.gvsig.view3d_vector_and_extrusion_2.3 / org.gvsig.view3d / org.gvsig.view3d / org.gvsig.view3d.swing / org.gvsig.view3d.swing.impl / src / main / java / org / gvsig / view3d / swing / impl / data / DefaultRasterServer.java @ 708
History | View | Annotate | Download (9.13 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.WWObjectImpl; |
28 |
import gov.nasa.worldwind.WorldWind; |
29 |
import gov.nasa.worldwind.avlist.AVKey; |
30 |
import gov.nasa.worldwind.avlist.AVList; |
31 |
import gov.nasa.worldwind.cache.BasicRasterServerCache; |
32 |
import gov.nasa.worldwind.cache.MemoryCache; |
33 |
import gov.nasa.worldwind.data.BufferedImageRaster; |
34 |
import gov.nasa.worldwind.data.ByteBufferRaster; |
35 |
import gov.nasa.worldwind.data.CachedDataRaster; |
36 |
import gov.nasa.worldwind.data.DataRaster; |
37 |
import gov.nasa.worldwind.data.DataRasterReader; |
38 |
import gov.nasa.worldwind.data.DataRasterReaderFactory; |
39 |
import gov.nasa.worldwind.data.RasterServer; |
40 |
import gov.nasa.worldwind.formats.dds.DDSCompressor; |
41 |
import gov.nasa.worldwind.geom.Sector; |
42 |
import gov.nasa.worldwind.retrieve.Retriever; |
43 |
import gov.nasa.worldwind.util.ImageUtil; |
44 |
|
45 |
import java.awt.Transparency; |
46 |
import java.io.IOException; |
47 |
import java.nio.ByteBuffer; |
48 |
|
49 |
import org.gvsig.fmap.mapcontext.layers.FLayer; |
50 |
import org.gvsig.view3d.lib.api.properties.E3DLayerLoadingModes; |
51 |
import org.gvsig.view3d.swing.api.MapControl3D; |
52 |
import org.gvsig.view3d.swing.impl.DefaultMapControl3D; |
53 |
import org.slf4j.Logger; |
54 |
import org.slf4j.LoggerFactory; |
55 |
|
56 |
/**
|
57 |
* Default implementaion of {@link RasterServer}. This class representes a server
|
58 |
* that serves raster information to {@link Retriever}.
|
59 |
*
|
60 |
* @author <a href="mailto:lmarques@disid.com">Lluis Marques</a>
|
61 |
*/
|
62 |
public class DefaultRasterServer extends WWObjectImpl implements RasterServer { |
63 |
|
64 |
private static final Logger LOG = LoggerFactory |
65 |
.getLogger(DefaultRasterServer.class); |
66 |
|
67 |
protected static final MemoryCache cache = new BasicRasterServerCache(); |
68 |
|
69 |
private DataRaster dataRaster;
|
70 |
|
71 |
/**
|
72 |
* Default constructor as from params. Required params:
|
73 |
*
|
74 |
* <ul>
|
75 |
* <li>DefaultMapControl3D.GVSIG_MAPCONTROL3D</li>
|
76 |
* <li>DefaultMapControl3D.GVSIG_LAYER</li>
|
77 |
* </ul>
|
78 |
*
|
79 |
* @param rasterServerParams parameters to create <code>RasterServer</code>
|
80 |
*/
|
81 |
public DefaultRasterServer(AVList params) {
|
82 |
|
83 |
if (params == null) { |
84 |
LOG.error("Can't create raster server, raster server parameters are null");
|
85 |
throw new IllegalArgumentException(); |
86 |
} |
87 |
|
88 |
MapControl3D mapControl3D = |
89 |
(MapControl3D) params.getValue(DefaultMapControl3D.GVSIG_MAPCONTROL3D); |
90 |
|
91 |
if (mapControl3D == null) { |
92 |
LOG.error( |
93 |
"Can not create raster server, missing required parameter: {}",
|
94 |
DefaultMapControl3D.GVSIG_MAPCONTROL3D); |
95 |
throw new IllegalArgumentException(); |
96 |
} |
97 |
|
98 |
FLayer layer = (FLayer) params.getValue(E3DLayerLoadingModes.GVSIG_LAYER); |
99 |
|
100 |
if (layer == null) { |
101 |
LOG.error( |
102 |
"Can not create raster server, missing required parameter: {}",
|
103 |
E3DLayerLoadingModes.GVSIG_LAYER); |
104 |
throw new IllegalArgumentException(); |
105 |
} |
106 |
|
107 |
this.setValues(params);
|
108 |
|
109 |
DataRasterReaderFactory readerFactory = |
110 |
(DataRasterReaderFactory) WorldWind |
111 |
.createConfigurationComponent(AVKey.DATA_RASTER_READER_FACTORY_CLASS_NAME); |
112 |
|
113 |
DataRasterReader reader = |
114 |
readerFactory.findReaderFor(layer, params.copy()); |
115 |
try {
|
116 |
this.dataRaster =
|
117 |
new CachedDataRaster(layer, params.copy(), reader, cache);
|
118 |
} catch (IllegalArgumentException e) { |
119 |
LOG.error( |
120 |
"Illegal argument, cached data raster can't be created with this parameters {}",
|
121 |
new Object[] { layer.getInfoString(), params.toString(), |
122 |
reader.getDescription(), cache.getName() }); |
123 |
return;
|
124 |
} catch (IOException e) { |
125 |
LOG.error("Can't read {} with {}", layer.getInfoString(), reader.getDescription());
|
126 |
return;
|
127 |
} |
128 |
} |
129 |
|
130 |
public ByteBuffer getRasterAsByteBuffer(AVList params) { |
131 |
|
132 |
DataRaster raster = this.composeRaster(params);
|
133 |
String format = (String) params.getValue(AVKey.IMAGE_FORMAT); |
134 |
|
135 |
if (raster instanceof BufferedImageRaster) { |
136 |
|
137 |
if ("image/png".equalsIgnoreCase(format)) { |
138 |
|
139 |
return ImageUtil.asPNG(raster);
|
140 |
|
141 |
} else if ("image/jpg".equalsIgnoreCase(format) |
142 |
|| "image/jpeg".equalsIgnoreCase(format)) {
|
143 |
|
144 |
return ImageUtil.asJPEG(raster);
|
145 |
|
146 |
} else if ("image/dds".equalsIgnoreCase(format)) { |
147 |
|
148 |
return DDSCompressor
|
149 |
.compressImage(((BufferedImageRaster) raster) |
150 |
.getBufferedImage()); |
151 |
|
152 |
} |
153 |
} else if (raster instanceof ByteBufferRaster) { |
154 |
// Elevations as BIL16 or as BIL32 are stored in the simple
|
155 |
// ByteBuffer object
|
156 |
return ((ByteBufferRaster) raster).getByteBuffer();
|
157 |
} else {
|
158 |
LOG.error("Unexpected raster type {}", raster
|
159 |
.getClass().getName()); |
160 |
throw new IllegalArgumentException(); |
161 |
} |
162 |
return null; |
163 |
} |
164 |
|
165 |
public Sector getSector() {
|
166 |
return (Sector) this.getValue(AVKey.SECTOR); |
167 |
} |
168 |
|
169 |
private DataRaster composeRaster(AVList reqParams) {
|
170 |
DataRaster reqRaster; |
171 |
|
172 |
if (reqParams == null) { |
173 |
LOG.error("Can't compose raster, parameters are null");
|
174 |
throw new IllegalArgumentException(); |
175 |
} |
176 |
if (!reqParams.hasKey(AVKey.WIDTH)) {
|
177 |
LOG.error("Can't compose raster, missing required parameter: {}",
|
178 |
AVKey.WIDTH); |
179 |
throw new IllegalArgumentException(); |
180 |
} |
181 |
if (!reqParams.hasKey(AVKey.HEIGHT)) {
|
182 |
LOG.error("Can't compose raster, missing required parameter: {}",
|
183 |
AVKey.HEIGHT); |
184 |
throw new IllegalArgumentException(); |
185 |
} |
186 |
|
187 |
Object o = reqParams.getValue(AVKey.SECTOR);
|
188 |
if (null == o || !(o instanceof Sector)) { |
189 |
LOG.error("Can't compose raster, missing required parameter: {}",
|
190 |
AVKey.SECTOR); |
191 |
throw new IllegalArgumentException(); |
192 |
} |
193 |
|
194 |
Sector reqSector = (Sector) o; |
195 |
Sector rasterExtent = this.getSector();
|
196 |
if (!reqSector.intersects(rasterExtent)) {
|
197 |
LOG.error("Can't compose raster, requerided sector does not intersect with raster server sector");
|
198 |
throw new IllegalArgumentException(); |
199 |
} |
200 |
|
201 |
int reqWidth = (Integer) reqParams.getValue(AVKey.WIDTH); |
202 |
int reqHeight = (Integer) reqParams.getValue(AVKey.HEIGHT); |
203 |
|
204 |
if (!reqParams.hasKey(AVKey.BYTE_ORDER)) {
|
205 |
reqParams.setValue(AVKey.BYTE_ORDER, AVKey.BIG_ENDIAN); |
206 |
} |
207 |
|
208 |
if (AVKey.ELEVATION.equals(this.getValue(AVKey.PIXEL_FORMAT))) { |
209 |
|
210 |
reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.ELEVATION); |
211 |
|
212 |
if (!reqParams.hasKey(AVKey.DATA_TYPE)) {
|
213 |
LOG.error("Missing requerided parameter {}", AVKey.DATA_TYPE,
|
214 |
new IllegalArgumentException()); |
215 |
} |
216 |
|
217 |
String dataType = (String) reqParams.getValue(AVKey.DATA_TYPE); |
218 |
int bytesNumber = getBytesNumber(dataType);
|
219 |
ByteBuffer byteBuffer =
|
220 |
ByteBuffer.allocate(reqWidth * reqHeight * bytesNumber);
|
221 |
|
222 |
reqRaster = |
223 |
new ByteBufferRaster(reqWidth, reqHeight, reqSector,
|
224 |
byteBuffer, reqParams); |
225 |
|
226 |
} else if (AVKey.IMAGE.equals(this.getValue(AVKey.PIXEL_FORMAT))) { |
227 |
|
228 |
reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.IMAGE); |
229 |
reqRaster = |
230 |
new BufferedImageRaster(reqWidth, reqHeight,
|
231 |
Transparency.TRANSLUCENT, reqSector);
|
232 |
|
233 |
} else {
|
234 |
LOG.error("Unrecognized type of pixel format: {}",
|
235 |
this.getValue(AVKey.PIXEL_FORMAT));
|
236 |
throw new IllegalArgumentException(); |
237 |
} |
238 |
|
239 |
this.dataRaster.drawOnTo(reqRaster);
|
240 |
|
241 |
return reqRaster;
|
242 |
} |
243 |
|
244 |
private int getBytesNumber(String dataType) { |
245 |
|
246 |
if (AVKey.INT8.equals(dataType)) {
|
247 |
return 1; |
248 |
} else if (AVKey.INT16.equals(dataType)) { |
249 |
return 2; |
250 |
} else if (AVKey.INT32.equals(dataType) |
251 |
|| AVKey.FLOAT32.equals(dataType)) { |
252 |
return 4; |
253 |
} else if (AVKey.FLOAT64.equals(dataType)) { |
254 |
return 8; |
255 |
} |
256 |
|
257 |
return 1; |
258 |
} |
259 |
|
260 |
} |