/*
* Copyright (C) 2011 United States Government as represented by the Administrator of the
* National Aeronautics and Space Administration.
* All Rights Reserved.
*/
package org.geoforge.worldwindogc.terrain;
import gov.nasa.worldwind.avlist.AVKey;
import gov.nasa.worldwind.avlist.AVList;
import gov.nasa.worldwind.exception.WWRuntimeException;
import gov.nasa.worldwind.geom.Sector;
import gov.nasa.worldwind.terrain.WMSBasicElevationModel;
import gov.nasa.worldwind.util.Logging;
import gov.nasa.worldwind.util.TileUrlBuilder;
import gov.nasa.worldwind.util.WWIO;
import java.net.MalformedURLException;
import java.net.URL;
import org.geoforge.worldwindogc.util.GfrDataConfigurationUtils;
import org.geoforge.worldwindogc.capabilities.GfrWMSCapabilities;
/**
* @author tag
* @version $Id: WMSBasicElevationModel.java 1 2011-07-16 23:22:47Z dcollins $
*/
abstract public class GfrWMSBasicElevationModel extends WMSBasicElevationModel
{
private static final String[] formatOrderPreference = new String[]
{
"application/bil32", "application/bil16", "application/bil", "image/bil", "image/png", "image/tiff"
};
protected GfrWMSBasicElevationModel(GfrWMSCapabilities caps, AVList params)
throws
WWRuntimeException,
IllegalArgumentException,
Exception
{
super(wmsGetParamsFromCapsDoc(caps, params));
}
public static AVList getWMSElevationModelConfigParams(GfrWMSCapabilities caps, String[] formatOrderPreference,
AVList params) throws
WWRuntimeException,
IllegalArgumentException
{
if (caps == null)
{
String message = Logging.getMessage("nullValue.WMSCapabilities");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (params == null)
{
String message = Logging.getMessage("nullValue.ElevationModelConfigParams");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// Get common WMS layer parameters.
// !!!!!!!!!!!!!!!!! not getting AVList result !!!!!!!!!!!!!!!!!!!
GfrDataConfigurationUtils.s_getWMSLayerConfigParams(caps, formatOrderPreference, params);
// Attempt to extract the WMS layer names from the specified parameters.
String layerNames = params.getStringValue(AVKey.LAYER_NAMES);
if (layerNames == null || layerNames.length() == 0)
{
String message = Logging.getMessage("nullValue.WMSLayerNames");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
String[] names = layerNames.split(",");
if (names == null || names.length == 0)
{
String message = Logging.getMessage("nullValue.WMSLayerNames");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
// Get the layer's extreme elevations.
Double[] extremes = caps.getLayerExtremeElevations(caps, names);
Double d = (Double) params.getValue(AVKey.ELEVATION_MIN);
if (d == null && extremes != null && extremes[0] != null)
params.setValue(AVKey.ELEVATION_MIN, extremes[0]);
d = (Double) params.getValue(AVKey.ELEVATION_MAX);
if (d == null && extremes != null && extremes[1] != null)
params.setValue(AVKey.ELEVATION_MAX, extremes[1]);
// Compute the internal pixel type from the image format.
if (params.getValue(AVKey.DATA_TYPE) == null && params.getValue(AVKey.IMAGE_FORMAT) != null)
{
String s = WWIO.makeDataTypeForMimeType(params.getValue(AVKey.IMAGE_FORMAT).toString());
if (s != null)
params.setValue(AVKey.DATA_TYPE, s);
}
// Use the default data type.
if (params.getValue(AVKey.DATA_TYPE) == null)
params.setValue(AVKey.DATA_TYPE, AVKey.INT16);
// Use the default byte order.
if (params.getValue(AVKey.BYTE_ORDER) == null)
params.setValue(AVKey.BYTE_ORDER, AVKey.LITTLE_ENDIAN);
return params;
}
protected static AVList wmsGetParamsFromCapsDoc(GfrWMSCapabilities caps, AVList params)
{
if (caps == null)
{
String message = Logging.getMessage("nullValue.WMSCapabilities");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
if (params == null)
{
String message = Logging.getMessage("nullValue.ElevationModelConfigParams");
Logging.logger().severe(message);
throw new IllegalArgumentException(message);
}
String wmsVersion;
try
{
wmsVersion = caps.getVersion();
getWMSElevationModelConfigParams(caps, formatOrderPreference, params);
}
catch (IllegalArgumentException e)
{
String message = Logging.getMessage("WMS.MissingLayerParameters");
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
throw new IllegalArgumentException(message, e);
}
catch (WWRuntimeException e)
{
String message = Logging.getMessage("WMS.MissingCapabilityValues");
Logging.logger().log(java.util.logging.Level.SEVERE, message, e);
throw new IllegalArgumentException(message, e);
}
wmsSetFallbacks(params);
params.setValue(AVKey.TILE_URL_BUILDER, new GfrURLBuilder(wmsVersion, params));
return params;
}
// beg inner-classes
// TODO: consolidate common code in WMSTiledImageLayer.GfrURLBuilder and WMSBasicElevationModel.GfrURLBuilder
protected static class GfrURLBuilder implements TileUrlBuilder
{
protected static final String MAX_VERSION = "1.3.0";
private final String layerNames;
private final String styleNames;
private final String imageFormat;
private final String bgColor;
private final String wmsVersion;
private final String crs;
protected String URLTemplate = null;
protected GfrURLBuilder(String version, AVList params)
{
Double d = (Double) params.getValue(AVKey.MISSING_DATA_SIGNAL);
this.layerNames = params.getStringValue(AVKey.LAYER_NAMES);
this.styleNames = params.getStringValue(AVKey.STYLE_NAMES);
this.imageFormat = params.getStringValue(AVKey.IMAGE_FORMAT);
this.bgColor = (d != null) ? d.toString() : null;
if (version == null || version.compareTo(MAX_VERSION) >= 0)
{
this.wmsVersion = MAX_VERSION;
// this.crs = "&crs=CRS:84";
this.crs = "&crs=EPSG:4326"; // TODO: what's the correct CRS value for these versions?
}
else
{
this.wmsVersion = version;
this.crs = "&srs=EPSG:4326";
}
}
@Override
public URL getURL(gov.nasa.worldwind.util.Tile tile, String altImageFormat) throws MalformedURLException
{
StringBuffer sb;
if (this.URLTemplate == null)
{
sb = new StringBuffer(tile.getLevel().getService());
if (!sb.toString().toLowerCase().contains("service=wms"))
sb.append("service=WMS");
sb.append("&request=GetMap");
sb.append("&version=");
sb.append(this.wmsVersion);
sb.append(this.crs);
sb.append("&layers=");
sb.append(this.layerNames);
sb.append("&styles=");
sb.append(this.styleNames != null ? this.styleNames : "");
sb.append("&format=");
if (altImageFormat == null)
sb.append(this.imageFormat);
else
sb.append(altImageFormat);
if (this.bgColor != null)
{
sb.append("&bgColor=");
sb.append(this.bgColor);
}
this.URLTemplate = sb.toString();
}
else
{
sb = new StringBuffer(this.URLTemplate);
}
sb.append("&width=");
sb.append(tile.getWidth());
sb.append("&height=");
sb.append(tile.getHeight());
Sector s = tile.getSector();
sb.append("&bbox=");
sb.append(s.getMinLongitude().getDegrees());
sb.append(",");
sb.append(s.getMinLatitude().getDegrees());
sb.append(",");
sb.append(s.getMaxLongitude().getDegrees());
sb.append(",");
sb.append(s.getMaxLatitude().getDegrees());
sb.append("&"); // terminate the query string
return new java.net.URL(sb.toString().replace(" ", "%20"));
}
}
}