-
Notifications
You must be signed in to change notification settings - Fork 341
Expand file tree
/
Copy pathBasicRasterServer.java
More file actions
587 lines (523 loc) · 22.1 KB
/
BasicRasterServer.java
File metadata and controls
587 lines (523 loc) · 22.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/*
* Copyright (C) 2012 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package gov.nasa.worldwind.data;
import gov.nasa.worldwind.*;
import gov.nasa.worldwind.avlist.*;
import gov.nasa.worldwind.cache.*;
import gov.nasa.worldwind.exception.WWRuntimeException;
import gov.nasa.worldwind.formats.dds.DDSCompressor;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.util.*;
import javax.xml.stream.XMLStreamException;
import java.awt.*;
import java.io.File;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.*;
/**
* @author Lado Garakanidze
* @version $Id: BasicRasterServer.java 2813 2015-02-18 23:35:24Z tgaskins $
*/
/**
* BasicRasterServer maintains a list of data sources and their properties in the BasicRasterServerCache and is used to
* compose (mosaic) a data raster of the given region of interest from data sources.
*/
public class BasicRasterServer extends WWObjectImpl implements RasterServer
{
protected java.util.List<DataRaster> dataRasterList = new java.util.ArrayList<DataRaster>();
protected DataRasterReaderFactory readerFactory;
protected static final MemoryCache cache = new BasicRasterServerCache();
/**
* BasicRasterServer constructor reads a list of data raster sources from *.RasterServer.xml (the file that
* accompanies layer description XML file), reads sector of each source and maintains a list of data sources, their
* properties,
*
* @param o the RasterServer.xml source to read. May by a {@link java.io.File}, a file path, a URL or an
* {@link org.w3c.dom.Element}
* @param params optional metadata associated with the data source that might be useful to the BasicRasterServer.
*/
public BasicRasterServer(Object o, AVList params)
{
super();
if (null != params)
{
this.setValues(params);
}
try
{
this.readerFactory = (DataRasterReaderFactory) WorldWind.createConfigurationComponent(
AVKey.DATA_RASTER_READER_FACTORY_CLASS_NAME);
}
catch (Exception e)
{
this.readerFactory = new BasicDataRasterReaderFactory();
}
this.init(o);
}
/** Returns an instance of the MemoryCache that contains DataRasters and their properties
*
* @return an instance of the MemoryCache that contains DataRasters and their properties
*/
public MemoryCache getCache()
{
return cache;
}
/**
* Returns TRUE, if the DataRaster list is not empty
*
* @return true, if the DataRaster list is not empty
*/
public boolean hasDataRasters()
{
return (this.dataRasterList.size() > 0);
}
protected void init(Object o)
{
if (null == o)
{
String message = Logging.getMessage("nullValue.ObjectIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// if (null == rootElement)
// {
// String message = Logging.getMessage("generic.UnexpectedObjectType", o.getClass().getName());
// Logging.logger().severe(message);
// throw new IllegalArgumentException(message);
// }
//
// String rootElementName = rootElement.getNodeName();
// if (!"RasterServer".equals(rootElementName))
// {
// String message = Logging.getMessage("generic.InvalidDataSource", rootElementName);
// Logging.logger().severe(message);
// throw new IllegalArgumentException(message);
// }
RasterServerConfiguration config = new RasterServerConfiguration(o);
try
{
config.parse();
}
catch (XMLStreamException e)
{
String message = Logging.getMessage("generic.InvalidDataSource", "");
message += "\n" + e.getMessage();
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
this.extractProperties(config);
if( this.readRasterSources(config) )
{
// success, all raster sources are available
String message = Logging.getMessage("generic.DataSetAvailable", this.getDataSetName() );
Logging.logger().finest(message);
}
else
{
// some (or all) required source rasters are not available (either missing or unreadable)
// and therefore the dataset may not generate high resolution on-the-fly
String message = Logging.getMessage("generic.DataSetLimitedAvailability", this.getDataSetName() );
Logging.logger().severe(message);
}
config.dispose();
}
protected String getDataSetName()
{
return AVListImpl.getStringValue( this, AVKey.DATASET_NAME, "" );
}
/**
* Returns DataSet's pixel format (AVKey.IMAGE or AVKey.ELEVATION), or empty string if not set
*
* @return DataSet's pixel format (AVKey.IMAGE or AVKey.ELEVATION), or empty string if not set
*/
protected String getDataSetPixelFormat()
{
return AVListImpl.getStringValue( this, AVKey.PIXEL_FORMAT, "" );
}
protected void setDataSetPixelFormat(String pixelFormat)
{
this.setValue(AVKey.PIXEL_FORMAT, pixelFormat);
}
/**
* Extracts all <Property/> key and values from the given DOM element
*
* @param config Parsed configuration document.
*/
protected void extractProperties(RasterServerConfiguration config)
{
for (Map.Entry<String, String> prop : config.getProperties().entrySet())
{
this.setValue(prop.getKey(), prop.getValue());
}
}
/**
* Reads XML document and extracts raster sources
*
* @param config Parsed configuration document.
*
* @return TRUE, if all raster sources are available, FALSE otherwise
*
*/
protected boolean readRasterSources(RasterServerConfiguration config)
{
long startTime = System.currentTimeMillis();
boolean hasUnavailableRasterSources = false;
int numSources = 0;
Sector extent = null;
try
{
List<RasterServerConfiguration.Source> sources = config.getSources();
if (sources == null || sources.size() == 0)
{
return false;
}
numSources = sources.size();
for (RasterServerConfiguration.Source source : sources) {
Thread.yield();
try
{
String rasterSourcePath = source.getPath();
if (WWUtil.isEmpty(rasterSourcePath))
{
continue;
}
AVList rasterMetadata = new AVListImpl();
File rasterSourceFile = new File(rasterSourcePath);
// normalize
rasterSourcePath = rasterSourceFile.getAbsolutePath();
if( !rasterSourceFile.exists() )
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.FileDoesNotExists", rasterSourcePath );
Logging.logger().warning(reason);
continue;
}
if( !rasterSourceFile.canRead() )
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.FileNoReadPermission", rasterSourcePath );
Logging.logger().warning(reason);
continue;
}
DataRasterReader rasterReader = this.findDataRasterReader(rasterSourceFile, rasterMetadata);
if (null == rasterReader)
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.UnknownFileFormatOrMatchingReaderNotFound",
rasterSourcePath );
Logging.logger().warning(reason);
continue;
}
Sector sector = source.getSector();
if (null == sector)
{
rasterReader.readMetadata(rasterSourceFile, rasterMetadata);
Object o = rasterMetadata.getValue(AVKey.SECTOR);
sector = (o instanceof Sector) ? (Sector) o : null;
}
else
{
rasterMetadata.setValue(AVKey.SECTOR, sector);
}
Object rasterPixelFormat = rasterMetadata.getValue(AVKey.PIXEL_FORMAT);
String datasetPixelFormat = this.getDataSetPixelFormat();
if( !WWUtil.isEmpty(datasetPixelFormat) )
{
// verify all data rasters are the same type - we do not allow to mix elevations and imagery
if (!datasetPixelFormat.equals(rasterPixelFormat))
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.UnexpectedRasterType", rasterSourcePath );
Logging.logger().warning(reason);
continue;
}
}
else
{
if( AVKey.IMAGE.equals(rasterPixelFormat) || AVKey.ELEVATION.equals(rasterPixelFormat) )
{
this.setDataSetPixelFormat( (String)rasterPixelFormat );
}
else
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.UnknownFileFormat", rasterSourcePath );
Logging.logger().warning(reason);
continue;
}
}
if (null != sector)
{
extent = Sector.union(extent, sector);
this.dataRasterList.add(
new CachedDataRaster(rasterSourceFile, rasterMetadata, rasterReader, this.getCache())
);
}
else
{
hasUnavailableRasterSources = true;
String reason = Logging.getMessage("generic.NoSectorSpecified", rasterSourcePath );
Logging.logger().warning(reason);
}
}
catch (Throwable t)
{
String message = t.getMessage();
message = (WWUtil.isEmpty(message)) ? t.getCause().getMessage() : message;
Logging.logger().log(java.util.logging.Level.WARNING, message, t);
}
}
if (null != extent && extent.getDeltaLatDegrees() > 0d && extent.getDeltaLonDegrees() > 0d)
{
this.setValue(AVKey.SECTOR, extent);
}
}
catch (Throwable t)
{
String message = t.getMessage();
message = (WWUtil.isEmpty(message)) ? t.getCause().getMessage() : message;
Logging.logger().log(java.util.logging.Level.SEVERE, message, t);
}
finally
{
Logging.logger().finest(this.getStringValue(AVKey.DISPLAY_NAME) + ": " + numSources
+ " files in " + (System.currentTimeMillis() - startTime) + " milli-seconds");
}
return !hasUnavailableRasterSources;
}
protected DataRasterReader findDataRasterReader(Object source, AVList params)
{
if (source == null)
{
String message = Logging.getMessage("nullValue.SourceIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
params = (null == params) ? new AVListImpl() : params;
DataRasterReader reader = this.readerFactory.findReaderFor(source, params);
if (reader == null)
{
return null;
}
if (!params.hasKey(AVKey.PIXEL_FORMAT))
{
try
{
reader.readMetadata(source, params);
}
catch (Exception e)
{
// Reading the input source's metadata caused an exception. This exception does not prevent us from
// determining if the source represents elevation data, but we want to make a note of it. Therefore we
// log the exception with level FINE.
String message = Logging.getMessage("generic.ExceptionWhileReading", source);
Logging.logger().finest(message);
}
}
return reader;
}
public Sector getSector()
{
return (this.hasKey(AVKey.SECTOR)) ? (Sector) this.getValue(AVKey.SECTOR) : null;
}
/**
* Composes a DataRaster of the given width and height for the specific geographic region of interest (ROI).
*
* @param reqParams This is a required parameter, must not be null or empty; Must contain AVKey.WIDTH, AVKey.HEIGHT,
* and AVKey.SECTOR values.
* <p/>
* Optional keys are: AVKey.PIXEL_FORMAT (AVKey.ELEVATION | AVKey.IMAGE) AVKey.DATA_TYPE
* AVKey.BYTE_ORDER (AVKey.BIG_ENDIAN | AVKey.LITTLE_ENDIAN )
*
* @return a DataRaster for the requested ROI
*
* @throws gov.nasa.worldwind.exception.WWRuntimeException
* if there is no intersection of the source rasters with the requested ROI or the
* source format is unknown or not supported by currently loaded drivers
* @throws IllegalArgumentException if any of the required parameters or values are missing
*/
public DataRaster composeRaster(AVList reqParams) throws IllegalArgumentException, WWRuntimeException
{
DataRaster reqRaster;
if (null == reqParams)
{
String message = Logging.getMessage("nullValue.ParamsIsNull");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (!reqParams.hasKey(AVKey.WIDTH))
{
String message = Logging.getMessage("generic.MissingRequiredParameter", AVKey.WIDTH);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (!reqParams.hasKey(AVKey.HEIGHT))
{
String message = Logging.getMessage("generic.MissingRequiredParameter", AVKey.HEIGHT);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
Object o = reqParams.getValue(AVKey.SECTOR);
if (null == o || !(o instanceof Sector))
{
String message = Logging.getMessage("generic.MissingRequiredParameter", AVKey.SECTOR);
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
Sector reqSector = (Sector) o;
Sector rasterExtent = this.getSector();
if (!reqSector.intersects(rasterExtent))
{
String message = Logging.getMessage("generic.SectorRequestedOutsideCoverageArea", reqSector, rasterExtent);
Logging.logger().finest(message);
throw new WWRuntimeException(message);
}
try
{
int reqWidth = (Integer) reqParams.getValue(AVKey.WIDTH);
int reqHeight = (Integer) reqParams.getValue(AVKey.HEIGHT);
if (!reqParams.hasKey(AVKey.BYTE_ORDER))
{
reqParams.setValue(AVKey.BYTE_ORDER, AVKey.BIG_ENDIAN);
}
// check if this Raster Server serves elevations or imagery
if (AVKey.ELEVATION.equals(this.getStringValue(AVKey.PIXEL_FORMAT)))
{
reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.ELEVATION);
if (!reqParams.hasKey(AVKey.DATA_TYPE))
{
reqParams.setValue(AVKey.DATA_TYPE, AVKey.INT16);
}
reqRaster = new ByteBufferRaster(reqWidth, reqHeight, reqSector, reqParams);
}
else if (AVKey.IMAGE.equals(this.getStringValue(AVKey.PIXEL_FORMAT)))
{
reqParams.setValue(AVKey.PIXEL_FORMAT, AVKey.IMAGE);
reqRaster = new BufferedImageRaster(reqWidth, reqHeight, Transparency.TRANSLUCENT, reqSector);
}
else
{
String msg = Logging.getMessage("generic.UnrecognizedSourceType", this.getValue(AVKey.PIXEL_FORMAT));
Logging.logger().severe(msg);
throw new WWRuntimeException(msg);
}
int numIntersectedRasters = 0;
for (DataRaster raster : this.dataRasterList)
{
Sector rasterSector = raster.getSector();
Sector overlap = reqSector.intersection(rasterSector);
// SKIP, if not intersection, or intersects only on edges
if (null == overlap || overlap.getDeltaLatDegrees() == 0d || overlap.getDeltaLonDegrees() == 0d)
{
continue;
}
raster.drawOnTo(reqRaster);
numIntersectedRasters++;
}
if (numIntersectedRasters == 0)
{
String message = Logging.getMessage("generic.SectorRequestedOutsideCoverageArea", reqSector, "");
Logging.logger().finest(message);
throw new WWRuntimeException(message);
}
}
catch (WWRuntimeException wwe)
{
throw wwe;
}
catch (Throwable t)
{
String message = t.getMessage();
message = (WWUtil.isEmpty(message)) ? t.getCause().getMessage() : message;
Logging.logger().log(java.util.logging.Level.FINE, message, t);
throw new WWRuntimeException(message);
}
return reqRaster;
}
/**
* Composes a DataRaster of the given width and height for the specific geographic region of interest (ROI), in the
* requested file format (AVKey.IMAGE_FORMAT) and returns as a ByteBuffer
*
* @param params This is a required parameter, must not be null or empty; Must contain AVKey.WIDTH, AVKey.HEIGHT,
* AVKey.SECTOR, and AVKey.IMAGE_FORMAT (mime type) values. Supported mime types are: "image/png",
* "image/jpeg", "image/dds".
* <p/>
* Optional keys are: AVKey.PIXEL_FORMAT (AVKey.ELEVATION | AVKey.IMAGE) AVKey.DATA_TYPE
* AVKey.BYTE_ORDER (AVKey.BIG_ENDIAN | AVKey.LITTLE_ENDIAN )
*
* @return a DataRaster for the requested ROI
*
* @throws gov.nasa.worldwind.exception.WWRuntimeException
* if there is no intersection of the source rasters with the requested ROI or the
* source format is unknown or not supported by currently loaded drivers
* @throws IllegalArgumentException if any of the required parameters or values are missing
*/
public ByteBuffer getRasterAsByteBuffer(AVList params)
{
// request may contain a specific file format, different from a default file format
String format = (null != params && params.hasKey(AVKey.IMAGE_FORMAT))
? params.getStringValue(AVKey.IMAGE_FORMAT) : this.getStringValue(AVKey.IMAGE_FORMAT);
if (WWUtil.isEmpty(format))
{
String message = Logging.getMessage("generic.MissingRequiredParameter", AVKey.IMAGE_FORMAT);
Logging.logger().severe(message);
throw new WWRuntimeException(message);
}
if (this.dataRasterList.isEmpty())
{
String message = Logging.getMessage("generic.NoImagesAvailable");
Logging.logger().finest(message);
throw new WWRuntimeException(message);
}
try
{
DataRaster raster = this.composeRaster(params);
if (raster instanceof BufferedImageRaster)
{
if ("image/png".equalsIgnoreCase(format))
{
return ImageUtil.asPNG(raster);
}
else if ("image/jpeg".equalsIgnoreCase(format) || "image/jpg".equalsIgnoreCase(format))
{
return ImageUtil.asJPEG(raster);
}
if ("image/dds".equalsIgnoreCase(format))
{
return DDSCompressor.compressImage(((BufferedImageRaster) raster).getBufferedImage());
}
else
{
String msg = Logging.getMessage("generic.UnknownFileFormat", format);
Logging.logger().severe(msg);
throw new WWRuntimeException(msg);
}
}
else if (raster instanceof ByteBufferRaster)
{
// Elevations as BIL16 or as BIL32 are stored in the simple ByteBuffer object
return ((ByteBufferRaster) raster).getByteBuffer();
}
else
{
String msg = Logging.getMessage("generic.UnexpectedRasterType", raster.getClass().getName());
Logging.logger().severe(msg);
throw new WWRuntimeException(msg);
}
}
catch (WWRuntimeException wwe)
{
Logging.logger().finest(wwe.getMessage());
}
catch (Throwable t)
{
String message = t.getMessage();
message = (WWUtil.isEmpty(message)) ? t.getCause().getMessage() : message;
Logging.logger().log(java.util.logging.Level.SEVERE, message, t);
}
return null;
}
}