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