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