public LegendInfo build() { // let's see if we need to really on the default values for width, height and format Integer finalWidth = width == null ? defaultWidth : width; Integer finalHeight = height == null ? defaultHeight : height; String finalFormat = format == null ? defaultFormat : format; // checking mandatory format value checkNotNull(finalFormat, "A legend image format is mandatory."); // default styles can have a NULL name String finalStyleName = styleName == null ? "" : styleName; // building the legend url String finalUrl = buildFinalUrl(finalStyleName, finalWidth, finalHeight, finalFormat); return new LegendInfo( finalStyleName, finalWidth, finalHeight, finalFormat, finalUrl, minScale, maxScale); }
/** * Build the concrete legend information using the provided layer information and defaults * values. */ public LegendInfo getLegendInfo( String layerName, String layerUrl, Integer defaultWidth, Integer defaultHeight, String defaultFormat) { return new LegendInfoBuilder() .withLayerName(layerName) .withLayerUrl(layerUrl) .withDefaultWidth(defaultWidth) .withDefaultHeight(defaultHeight) .withDefaultFormat(defaultFormat) .withStyleName(style) .withWidth(width) .withHeight(height) .withFormat(format) .withUrl(url) .withCompleteUrl(completeUrl) .withMinScale(minScale) .withMaxScale(maxScale) .build(); }
@Override public void marshal( Object source, HierarchicalStreamWriter writer, MarshallingContext context) { LegendsRawInfo legendsRawInfo = (LegendsRawInfo) source; // encode default values writer.addAttribute("defaultWidth", String.valueOf(legendsRawInfo.getDefaultWidth())); writer.addAttribute("defaultHeight", String.valueOf(legendsRawInfo.getDefaultHeight())); writer.addAttribute("defaultFormat", String.valueOf(legendsRawInfo.getDefaultFormat())); // encode legends information for (LegendRawInfo legendRawInfo : legendsRawInfo.getLegendsRawInfo()) { writer.startNode("legend"); writer.addAttribute("style", legendRawInfo.getStyle()); encodeAttribute(writer, "width", legendRawInfo.getWidth()); encodeAttribute(writer, "height", legendRawInfo.getHeight()); encodeAttribute(writer, "format", legendRawInfo.getFormat()); encodeAttribute(writer, "url", legendRawInfo.getUrl()); encodeAttribute(writer, "completeUrl", legendRawInfo.getCompleteUrl()); encodeAttribute(writer, "minScale", legendRawInfo.getMinScale()); encodeAttribute(writer, "maxScale", legendRawInfo.getMaxScale()); writer.endNode(); } }
@Test public void testWithValues() { LegendInfo legendInfo = new LegendInfoBuilder() .withLayerName("layer1") .withLayerUrl("http://localhost:8080/geoserver") .withDefaultWidth(50) .withDefaultHeight(100) .withDefaultFormat("image/png") .withStyleName("style1") .withWidth(150) .withHeight(200) .withFormat("image/gif") .withMinScale(1000.55) .withMaxScale(2000.655) .build(); assertThat(legendInfo.getWidth(), is(150)); assertThat(legendInfo.getHeight(), is(200)); assertThat(legendInfo.getFormat(), is("image/gif")); assertThat(legendInfo.getStyleName(), is("style1")); assertThat(legendInfo.getMinScale(), is(1000.55)); assertThat(legendInfo.getMaxScale(), is(2000.655)); assertThat( legendInfo.getLegendUrl(), is( "http://localhost:8080/geoserver?" + "service=WMS&request=GetLegendGraphic&format=image/gif&width=150&height=200&layer=layer1&style=style1")); }
@Test public void testWithCompleteUrl() { LegendInfo legendInfo = new LegendInfoBuilder() .withLayerName("layer1") .withLayerUrl("http://localhost:8080/geoserver") .withDefaultWidth(50) .withDefaultHeight(100) .withDefaultFormat("image/png") .withStyleName("style1") .withWidth(150) .withHeight(200) .withFormat("image/gif") .withCompleteUrl("http://my.server.com/image.gif") .build(); assertThat(legendInfo.getWidth(), is(150)); assertThat(legendInfo.getHeight(), is(200)); assertThat(legendInfo.getFormat(), is("image/gif")); assertThat(legendInfo.getStyleName(), is("style1")); assertThat(legendInfo.getLegendUrl(), is("http://my.server.com/image.gif")); }
LegendsRawInfo legendsRawInfo = new LegendsRawInfo(); legendsRawInfo.setDefaultWidth(50); legendsRawInfo.setDefaultHeight(100); legendsRawInfo.setDefaultFormat("image/png"); LegendRawInfo legendRawInfoA = new LegendRawInfo(); legendRawInfoA.setStyle("polygon"); legendRawInfoA.setWidth(75); legendRawInfoA.setHeight(125); legendRawInfoA.setFormat("image/jpeg"); legendRawInfoA.setUrl("http://url"); legendRawInfoA.setMinScale(5000D); legendRawInfoA.setMaxScale(10000D); LegendRawInfo legendRawInfoB = new LegendRawInfo(); legendRawInfoB.setStyle("point"); legendRawInfoB.setCompleteUrl("http://url"); LegendRawInfo legendRawInfoC = new LegendRawInfo(); legendRawInfoC.setStyle(""); legendsRawInfo.addLegendRawInfo(legendRawInfoA); legendsRawInfo.addLegendRawInfo(legendRawInfoB); legendsRawInfo.addLegendRawInfo(legendRawInfoC); layer.setLegends(legendsRawInfo); assertThat(l.getLegends().getDefaultWidth(), is(50)); assertThat(l.getLegends().getDefaultHeight(), is(100)); assertThat(l.getLegends().getDefaultFormat(), is("image/png"));
LegendsRawInfo legendsRawInfo = new LegendsRawInfo(); legendsRawInfo.setDefaultWidth(50); legendsRawInfo.setDefaultHeight(100); legendsRawInfo.setDefaultFormat("image/png"); LegendRawInfo legendRawInfoA = new LegendRawInfo(); legendRawInfoA.setStyle("polygon"); legendRawInfoA.setWidth(75); legendRawInfoA.setHeight(125); legendRawInfoA.setFormat("image/jpeg"); legendRawInfoA.setUrl("http://url"); legendRawInfoA.setMinScale(5000D); legendRawInfoA.setMaxScale(10000D); LegendRawInfo legendRawInfoB = new LegendRawInfo(); legendRawInfoB.setStyle("point"); legendRawInfoB.setCompleteUrl("http://url"); LegendRawInfo legendRawInfoC = new LegendRawInfo(); legendRawInfoC.setStyle(""); legendsRawInfo.addLegendRawInfo(legendRawInfoA); legendsRawInfo.addLegendRawInfo(legendRawInfoB); legendsRawInfo.addLegendRawInfo(legendRawInfoC); layer.setLegends(legendsRawInfo);
/** Helper method that extracts from a legend url the width, height and format parameters. */ private LegendsRawInfo extractLegendsInfo(List<StyleImpl> styles) { LegendsRawInfo legendsRawInfo = new LegendsRawInfo(); // setting some acceptable default values legendsRawInfo.setDefaultWidth(20); legendsRawInfo.setDefaultHeight(20); legendsRawInfo.setDefaultFormat("image/png"); for (StyleImpl style : styles) { // extracting legend information from each style LegendRawInfo legendRawInfo = new LegendRawInfo(); legendRawInfo.setStyle(style.getName()); List legendUrls = style.getLegendURLs(); if (legendUrls != null && !legendUrls.isEmpty()) { String legendUrl = (String) legendUrls.get(0); // let's see if we can extract width, height and format from the style legend url legendRawInfo.setWidth(extractIntegerParameter(legendUrl, LEGEND_WIDTH_PATTERN)); legendRawInfo.setHeight(extractIntegerParameter(legendUrl, LEGEND_HEIGHT_PATTERN)); legendRawInfo.setFormat(extractParameter(legendUrl, LEGEND_FORMAT_PATTERN)); // setting the complete legend url legendRawInfo.setCompleteUrl(legendUrl); } legendsRawInfo.addLegendRawInfo(legendRawInfo); } return legendsRawInfo; }
@Test public void testWithOnlyDefaults() { LegendInfo legendInfo = new LegendInfoBuilder() .withLayerName("layer1") .withLayerUrl("http://localhost:8080/geoserver") .withDefaultWidth(50) .withDefaultHeight(100) .withDefaultFormat("image/png") .build(); assertThat(legendInfo.getWidth(), is(50)); assertThat(legendInfo.getHeight(), is(100)); assertThat(legendInfo.getFormat(), is("image/png")); assertThat(legendInfo.getStyleName(), is("")); assertThat(legendInfo.getMinScale(), is(nullValue())); assertThat(legendInfo.getMaxScale(), is(nullValue())); assertThat( legendInfo.getLegendUrl(), is( "http://localhost:8080/geoserver?" + "service=WMS&request=GetLegendGraphic&format=image/png&width=50&height=100&layer=layer1&style=")); }
/** * Helper method that get layer legends info by merging deprecated legends info objects with the * new ones. */ private Map<String, LegendInfo> getLegendsInfo(TileLayer layer) { Map<String, LegendInfo> legendsInfo = new HashMap<>(); for (Map.Entry<String, TileLayer.LegendInfo> entry : layer.getLegendsInfo().entrySet()) { // convert deprecated model to new model legendsInfo.put( entry.getKey(), new LegendInfoBuilder() .withWidth(entry.getValue().width) .withHeight(entry.getValue().height) .withFormat(entry.getValue().format) .withCompleteUrl(entry.getValue().legendUrl) .withStyleName(entry.getKey()) .build()); } // add the new legend info model objects legendsInfo.putAll(layer.getLayerLegendsInfo()); return legendsInfo; }
LegendRawInfo legendRawInfo = new LegendRawInfo(); legendRawInfo.setStyle(reader.getAttribute("style")); while (reader.hasMoreChildren()) { reader.moveDown(); switch (reader.getNodeName()) { case "width": legendRawInfo.setWidth(toInteger(reader.getValue())); break; case "height": legendRawInfo.setHeight(toInteger(reader.getValue())); break; case "format": legendRawInfo.setFormat(reader.getValue()); break; case "url": legendRawInfo.setUrl(reader.getValue()); break; case "completeUrl": legendRawInfo.setCompleteUrl(reader.getValue()); break; case "minScale": legendRawInfo.setMinScale(toDouble(reader.getValue())); break; case "maxScale": legendRawInfo.setMaxScale(toDouble(reader.getValue())); break;
@Override public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) { LegendsRawInfo legendsRawInfo = new LegendsRawInfo(); // setting the defaults based on legends element attributes legendsRawInfo.setDefaultWidth(toInteger(reader.getAttribute("defaultWidth"))); legendsRawInfo.setDefaultHeight(toInteger(reader.getAttribute("defaultHeight"))); legendsRawInfo.setDefaultFormat(reader.getAttribute("defaultFormat")); // parsing all legends information while (reader.hasMoreChildren()) { reader.moveDown(); legendsRawInfo.addLegendRawInfo(parseLegendRawInfo(reader)); reader.moveUp(); } return legendsRawInfo; }
checkNotNull(legendInfo.getFormat(), "Legend format is mandatory in WMTS."); checkNotNull(legendInfo.getLegendUrl(), "Legend URL is mandatory in WMTS."); xml.attribute("format", legendInfo.getFormat()); xml.attribute("xlink:href", legendInfo.getLegendUrl()); if (legendInfo.getWidth() != null) { xml.attribute("width", String.valueOf(legendInfo.getWidth())); if (legendInfo.getHeight() != null) { xml.attribute("height", String.valueOf(legendInfo.getHeight())); if (legendInfo.getMinScale() != null) { xml.attribute("minScaleDenominator", String.valueOf(legendInfo.getMinScale())); if (legendInfo.getMaxScale() != null) { xml.attribute("maxScaleDenominator", String.valueOf(legendInfo.getMaxScale()));
legendInfo.getWidth(), "Legend with is mandatory in WMS (1.1.0, 1.1.1 and 1.3.0)."); checkNotNull( legendInfo.getHeight(), "Legend height is mandatory in WMS (1.1.0, 1.1.1 and 1.3.0)."); checkNotNull( legendInfo.getFormat(), "Legend format is mandatory in WMS (1.1.0, 1.1.1 and 1.3.0)."); checkNotNull( legendInfo.getLegendUrl(), "Legend URL is mandatory in WMS (1.1.0, 1.1.1 and 1.3.0)."); xml.indentElement("LegendURL"); xml.attribute("width", String.valueOf(legendInfo.getWidth())); xml.attribute("height", String.valueOf(legendInfo.getHeight())); xml.simpleElement("Format", legendInfo.getFormat(), true); xml.attribute("xlink:href", legendInfo.getLegendUrl()); xml.endElement("OnlineResource");
/** * Builds the concrete legends info for each raw legend info using the provided layer * information. The returned map index the legend info per layer. */ public Map<String, LegendInfo> getLegendsInfo(String layerName, String layerUrl) { Map<String, LegendInfo> legendsInfo = new HashMap<>(); for (LegendRawInfo legendRawInfo : legendsRawInfo) { legendsInfo.put( legendRawInfo.getStyle(), legendRawInfo.getLegendInfo( layerName, layerUrl, defaultWidth, defaultHeight, defaultFormat)); } return legendsInfo; } }
@Override public Map<String, org.geowebcache.config.legends.LegendInfo> getLayerLegendsInfo() { String layerName = wmsLayers == null ? getName() : wmsLayers; return legends == null ? super.getLayerLegendsInfo() : legends.getLegendsInfo( layerName, wmsUrl != null && wmsUrl.length > 0 ? wmsUrl[0] : null); }
/** Helper method that builds the legend get url using the available info. */ private String buildFinalUrl( String finalStyleName, Integer finalWidth, Integer finalHeight, String finalFormat) { if (completeUrl != null) { // we have a complete url so let's just return it return completeUrl; } String finalUrl = url == null ? layerUrl : url; // check mandatory values checkNotNull(finalWidth, "A legend width is mandatory."); checkNotNull(finalHeight, "A legend height is mandatory."); checkNotNull(finalUrl, "A legend url is mandatory."); checkNotNull(layerName, "A layer name is mandatory."); return finalUrl + addQuoteMark(finalUrl) + "service=WMS&request=GetLegendGraphic" + "&format=" + finalFormat + "&width=" + finalWidth + "&height=" + finalHeight + "&layer=" + ServletUtils.URLEncode(layerName) + "&style=" + ServletUtils.URLEncode(finalStyleName); }
xs.registerConverter(new LegendsRawInfoConverter()); xs.alias("legends", LegendsRawInfo.class);
@Test public void testWithValuesNoDefaults() { LegendInfo legendInfo = new LegendInfoBuilder() .withLayerName("layer1") .withLayerUrl("http://localhost:8080/geoserver") .withStyleName("style1") .withWidth(150) .withHeight(200) .withFormat("image/gif") .withMinScale(50.5) .withMaxScale(80.5) .build(); assertThat(legendInfo.getWidth(), is(150)); assertThat(legendInfo.getHeight(), is(200)); assertThat(legendInfo.getFormat(), is("image/gif")); assertThat(legendInfo.getStyleName(), is("style1")); assertThat(legendInfo.getMinScale(), is(50.5)); assertThat(legendInfo.getMaxScale(), is(80.5)); assertThat( legendInfo.getLegendUrl(), is( "http://localhost:8080/geoserver?" + "service=WMS&request=GetLegendGraphic&format=image/gif&width=150&height=200&layer=layer1&style=style1")); } }
@Test public void testWithUrl() { LegendInfo legendInfo = new LegendInfoBuilder() .withLayerName("layer1") .withLayerUrl("http://localhost:8080/geoserver") .withDefaultWidth(50) .withDefaultHeight(100) .withDefaultFormat("image/png") .withStyleName("style1") .withWidth(150) .withHeight(200) .withFormat("image/gif") .withUrl("http://localhost:9090/image.gif?") .build(); assertThat(legendInfo.getWidth(), is(150)); assertThat(legendInfo.getHeight(), is(200)); assertThat(legendInfo.getFormat(), is("image/gif")); assertThat(legendInfo.getStyleName(), is("style1")); assertThat( legendInfo.getLegendUrl(), is( "http://localhost:9090/image.gif?" + "service=WMS&request=GetLegendGraphic&format=image/gif&width=150&height=200&layer=layer1&style=style1")); }