📄 getmapkvpreader.java
字号:
}
if (!matches) {
continue; // this layer is fitered out
}
}
}
}
// handle no styles -- use default
if ((layerStyles == null) || (layerStyles.length == 0)) {
layers.add(currLayer);
styles.add(currLayer.getDefaultStyle());
return;
}
final int length = layerStyles.length;
Style s;
for (int t = 0; t < length; t++) {
if (layerStyles[t] instanceof NamedStyle) {
layers.add(currLayer);
s = findStyle(request, ((NamedStyle) layerStyles[t]).getName());
if (s == null) {
throw new WmsException("couldnt find style named '"
+ ((NamedStyle) layerStyles[t]).getName() + "'");
}
styles.add(s);
} else {
layers.add(currLayer);
styles.add(layerStyles[t]);
}
}
}
/**
* Finds the style for <code>layer</code> in <code>styledLayers</code>
* or the layer's default style if <code>styledLayers</code> has no a
* UserLayer or a NamedLayer with the same name than <code>layer</code>
* <p>
* This method is used to parse the style of a layer for SLD and SLD_BODY
* parameters, both in library and literal mode. Thus, once the declared
* style for the given layer is found, it is checked for validity of
* appliance for that layer (i.e., whether the featuretype contains the
* attributes needed for executing the style filters).
* </p>
*
* @param request
* used to find out an internally configured style when
* referenced by name by a NamedLayer
*
* @param layer
* one of the internal FeatureType that was requested through the
* LAYERS parameter or through and SLD document when the request
* is in literal mode.
* @param styledLayers
* a set of StyledLayers from where to find the SLD layer with
* the same name as <code>layer</code> and extract the style to
* apply.
*
* @return the Style applicable to <code>layer</code> extracted from
* <code>styledLayers</code>.
*
* @throws RuntimeException
* if one of the StyledLayers is neither a UserLayer nor a
* NamedLayer. This shuoldn't happen, since the only allowed
* subinterfaces of StyledLayer are NamedLayer and UserLayer.
* @throws WmsException
*/
private Style findStyleOf(GetMapRequest request, FeatureTypeInfo layer,
StyledLayer[] styledLayers) throws WmsException {
Style style = null;
String layerName = layer.getName();
StyledLayer sl;
for (int i = 0; i < styledLayers.length; i++) {
sl = styledLayers[i];
if (layerName.equals(sl.getName())) {
if (sl instanceof UserLayer) {
Style[] styles = ((UserLayer) sl).getUserStyles();
if ((null != styles) && (0 < styles.length)) {
style = styles[0];
}
} else if (sl instanceof NamedLayer) {
Style[] styles = ((NamedLayer) sl).getStyles();
if ((null != styles) && (0 < styles.length)) {
style = styles[0];
}
if (style instanceof NamedStyle) {
style = findStyle(request, style.getName());
}
} else {
throw new RuntimeException("Unknown layer type: " + sl);
}
break;
}
}
if (null == style) {
style = layer.getDefaultStyle();
}
FeatureType type;
try {
type = layer.getFeatureType();
} catch (IOException ioe) {
throw new RuntimeException(
"Error getting FeatureType, this should never happen!");
}
checkStyle(style, type);
return style;
}
/**
* Parses a list of layers in a layer grouping.
*
* @param layerGroup The layer group.
*
* @return List of String.
*/
public static List parseLayerGroup( String layerGroup ) {
return readFlat( layerGroup, INNER_DELIMETER );
}
/**
* Parses the requested layers given by the LAYERS request parameter and
* looks up their corresponding FeatureTypeInfo objects in the server.
*
* @param request
*
* @return
*
* @throws WmsException
*/
protected MapLayerInfo[] parseLayersParam(GetMapRequest request)
throws WmsException {
MapLayerInfo[] layers;
String layersParam = getValue("LAYERS");
List layerNames = readFlat(layersParam, INNER_DELIMETER);
List realLayerNames = new ArrayList();
// expand base layers, if there is any
WMS wms = request.getWMS();
if (wms.getBaseMapLayers() != null) {
for (int i = 0; i < layerNames.size(); i++) {
String layerGroup = (String) wms.getBaseMapLayers().get(
layerNames.get(i));
if (layerGroup != null) {
List layerGroupExpanded = parseLayerGroup(layerGroup);
layerNames.remove(i);
layerNames.addAll(i, layerGroupExpanded);
}
}
}
String layerName = null;
Data catalog = request.getWMS().getData();
String rawStyles = getValue("STYLES");
List styleNames = readFlat(rawStyles, INNER_DELIMETER);
int l_counter = 0;
int s_counter = styleNames.size();
// //
// Expand the eventually WMS grouped layers into the same WMS Path
// element
// //
for (Iterator it = layerNames.iterator(); it.hasNext();) {
layerName = (String) it.next();
Integer layerType = catalog.getLayerType(layerName);
if (layerType == null) {
// //
// Search for grouped layers (attention: heavy process)
// //
String catalogLayerName = null;
for (Iterator c_keys = catalog.getLayerNames().iterator(); c_keys
.hasNext();) {
catalogLayerName = (String) c_keys.next();
try {
FeatureTypeInfo ftype = findFeatureLayer(request,
catalogLayerName);
String wmsPath = ftype.getWmsPath();
if ((wmsPath != null)
&& wmsPath.matches(".*/" + layerName)) {
realLayerNames.add(catalogLayerName);
l_counter++;
if (l_counter > s_counter) {
rawStyles = ((rawStyles.length() > 0) ? (rawStyles + ",")
: rawStyles)
+ ftype.getDefaultStyle().getName();
}
}
} catch (WmsException e_1) {
try {
CoverageInfo cv = findCoverageLayer(request,
catalogLayerName);
String wmsPath = cv.getWmsPath();
if ((wmsPath != null)
&& wmsPath.matches(".*/" + layerName)) {
realLayerNames.add(catalogLayerName);
l_counter++;
if (l_counter > s_counter) {
rawStyles = ((rawStyles.length() > 0) ? (rawStyles + ",")
: rawStyles)
+ cv.getDefaultStyle().getName();
}
}
} catch (WmsException e_2) {
}
}
}
} else {
realLayerNames.add(layerName);
l_counter++;
}
}
// JD: only set if non-null since if the layer is an actual layer
// (ie. not something matching wms path) with no style specified we
// dont want to create an empty "STYLES" entry
if ((rawStyles != null) && !"".equals(rawStyles.trim())) {
kvpPairs.put("STYLES", rawStyles);
}
int layerCount = realLayerNames.size();
if (layerCount == 0) {
throw new WmsException("No LAYERS has been requested", getClass()
.getName());
}
layers = new MapLayerInfo[layerCount];
for (int i = 0; i < layerCount; i++) {
layerName = (String) layerNames.get(i);
layers[i] = new MapLayerInfo();
try {
FeatureTypeInfo ftype = findFeatureLayer(request, layerName);
layers[i].setFeature(ftype);
} catch (WmsException e) {
CoverageInfo cv = findCoverageLayer(request, layerName);
layers[i].setCoverage(cv);
}
}
return layers;
}
/**
* DOCUMENT ME!
*
* @param request
* @param layerName
*
* @return
*
* @throws WmsException
* DOCUMENT ME!
*/
public static FeatureTypeInfo findFeatureLayer(GetMapRequest request,
String layerName) throws WmsException {
Data catalog = request.getWMS().getData();
FeatureTypeInfo ftype = null;
Integer layerType = catalog.getLayerType(layerName);
if (Data.TYPE_VECTOR != layerType) {
throw new WmsException(new StringBuffer(layerName).append(
": no such layer on this server").toString(),
"LayerNotDefined");
} else {
ftype = catalog.getFeatureTypeInfo(layerName);
}
return ftype;
}
public static CoverageInfo findCoverageLayer(GetMapRequest request,
String layerName) throws WmsException {
Data catalog = request.getWMS().getData();
CoverageInfo cv = null;
Integer layerType = catalog.getLayerType(layerName);
if (Data.TYPE_RASTER != layerType) {
throw new WmsException(new StringBuffer(layerName).append(
": no such layer on this server").toString(),
"LayerNotDefined");
} else {
cv = catalog.getCoverageInfo(layerName);
}
return cv;
}
/**
* This method gets the correct input stream for a URL. If the URL is a
* http/https connection, the Accept-Encoding: gzip, deflate is added. It
* the paramter is added, the response is checked to see if the response is
* encoded in gzip, deflate or plain bytes. The correct input stream wrapper
* is then selected and returned.
*
* This method was added as part of GEOS-420
*
* @param sldUrl
* The url to the sld file
* @return The InputStream used to validate and parse the SLD xml.
* @throws IOException
*/
private InputStream getInputStream(URL sldUrl) throws IOException {
// Open the connection
URLConnection conn = sldUrl.openConnection();
// If it is the http or https scheme, then ask for gzip if the server
// supports it.
if (conn instanceof HttpURLConnection) {
// Send the requested encoding to the remote server.
conn.setRequestProperty("Accept-Encoding", "gzip, deflate");
}
// Conect to get the response headers
conn.connect();
// Return the correct inputstream
// If the connection is a url, connection, check the response encoding.
if (conn instanceof HttpURLConnection) {
// Get the content encoding of the server response
String encoding = conn.getContentEncoding();
// If null, set it to a emtpy string
if (encoding == null) {
encoding = "";
}
if (encoding.equalsIgnoreCase("gzip")) {
// For gzip input stream, use a GZIPInputStream
return new GZIPInputStream(conn.getInputStream());
} else if (encoding.equalsIgnoreCase("deflate")) {
// If it is encoded as deflate, then select the inflater
// inputstream.
return new InflaterInputStream(conn.getInputStream(),
new Inflater(true));
} else {
// Else read the raw bytes
return conn.getInputStream();
}
} else {
// Else read the raw bytes.
return conn.getInputStream();
}
}
/**
* Filters the layers and styles if the user specified "layers=basemap".
*
*
* @param layers
* @param styles
*/
public void filterBaseMap(Map layers, Map styles) {
List currentLayers = null;
try {
// read flat can return non modifiable lists
currentLayers = new ArrayList(readFlat(getValue("LAYERS"), ","));
} catch (NullPointerException e) {
// No layers defined. This is either wrong or they are listing the
// layers
// in an SLD document specified with the SLD= parameter
LOGGER
.fine("No layers defined. This is either wrong or they are listing the layers"
+ " in an SLD document specified with the SLD= parameter");
return; // just continue ignoring the basemap option
}
List currentStyles = null;
try {
currentStyles = new ArrayList(readFlat(getValue("STYLES"), ","));
} catch (NullPointerException e) {
currentStyles = new ArrayList();
}
while (currentStyles.size() < currentLayers.size())
currentStyles.add("");
String[] baseLayers = (String[]) layers.keySet().toArray(new String[0]);
boolean replacedOne = false;
for (int i = 0; i < baseLayers.length; i++) {
String blTitle = baseLayers[i];
int index = currentLayers.indexOf(blTitle);
if (index > -1) {
replacedOne = true;
LOGGER.info("Using BASEMAP layer: " + baseLayers[i]);
// remove the 'basemap layer' from the currentLayers list
currentLayers.remove(index);
List blLayers = new ArrayList(readFlat((String) layers
.get(blTitle), ","));
currentLayers.addAll(index, blLayers);
List blStyles = new ArrayList(readFlat((String) styles
.get(blTitle), ","));
while (blStyles.size() < blLayers.size())
blStyles.add("");
currentStyles.remove(index);
currentStyles.addAll(index, blStyles);
}
}
if (replacedOne) {
kvpPairs.remove("LAYERS");
kvpPairs.put("LAYERS", toStringList(currentLayers, ","));
kvpPairs.remove("STYLES");
kvpPairs.put("STYLES", toStringList(currentStyles, ","));
}
}
private String toStringList(List currentLayers, String separator) {
StringBuffer sb = new StringBuffer();
for (Iterator it = currentLayers.iterator(); it.hasNext();) {
String item = (String) it.next();
sb.append(item);
if (it.hasNext()) {
sb.append(separator);
}
}
return sb.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -