Skip to content

Commit

Permalink
Fix for GEOS-8128 - SLD static legend dimensions not respected
Browse files Browse the repository at this point in the history
 - PR refactoring and improved test-case
  • Loading branch information
afabiani committed May 9, 2017
1 parent 8e40478 commit 1894c00
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.geoserver.catalog.LayerGroupInfo;
import org.geoserver.catalog.LayerInfo;
import org.geoserver.catalog.LegendInfo;
import org.geoserver.catalog.PublishedInfo;
import org.geoserver.catalog.PublishedType;
import org.geoserver.catalog.StyleInfo;
import org.geoserver.catalog.impl.LegendInfoImpl;
Expand All @@ -45,7 +44,6 @@
import org.geotools.factory.CommonFactoryFinder;
import org.geotools.factory.FactoryRegistryException;
import org.geotools.factory.GeoTools;
import org.geotools.feature.NameImpl;
import org.geotools.feature.SchemaException;
import org.geotools.resources.coverage.FeatureUtilities;
import org.geotools.styling.SLDParser;
Expand Down Expand Up @@ -247,14 +245,33 @@ private LegendRequest addLayer(LayerInfo layerInfo, GetLegendGraphicRequest requ
}
LegendInfo legendInfo = resolveLegendInfo(layerInfo.getLegend(),request);
if(legendInfo != null ){
legend.setLegendInfo( legendInfo );
configureLegendInfo(request, legend, legendInfo);
}
return legend;
} else {
throw new ServiceException("Cannot get FeatureType for Layer",
"MissingFeatureType");
}
}

/**
* Ensures the online resource is stored on the GetLegendGraphicRequest and native
* dimensions are configured if not specified on the original request.
*
* @param request GetLegendGraphicRequest original KWP Request
* @param legend LegendRequest internal Class containing references to Resources
* @param legendInfo LegendInfo used to document use external graphic
*/
private void configureLegendInfo(GetLegendGraphicRequest request, LegendRequest legend,
LegendInfo legendInfo) {
legend.setLegendInfo( legendInfo );
if (legendInfo.getHeight() > 0 && !request.getKvp().containsKey("HEIGHT")) {
request.setHeight(legendInfo.getHeight());
}
if (legendInfo.getWidth() > 0 && !request.getKvp().containsKey("WIDTH")) {
request.setWidth(legendInfo.getWidth());
}
}
/**
* Makes a copy of the provided LegendInfo resolving ExternalGraphics reference
* to local file system.
Expand Down Expand Up @@ -447,7 +464,7 @@ private void parseStyleAndRule(GetLegendGraphicRequest req, Object infoObj, Map
Name name = layerInfo.getResource().getQualifiedName();
LegendRequest legendRequest = req.getLegend(name);
if( legendRequest != null ){
legendRequest.setLegendInfo( legend );
configureLegendInfo(req, legendRequest, legend);
}
else {
LOGGER.log(Level.FINE, "Unable to set LegendInfo for "+name);
Expand All @@ -470,7 +487,7 @@ private void parseStyleAndRule(GetLegendGraphicRequest req, Object infoObj, Map
Name name = layerInfo.getResource().getQualifiedName();
LegendRequest legendRequest = req.getLegend(name);
if( legendRequest != null ){
legendRequest.setLegendInfo( legend );
configureLegendInfo(req, legendRequest, legend);
}
else {
LOGGER.log(Level.FINE, "Unable to set LegendInfo for "+name);
Expand All @@ -495,7 +512,7 @@ private void parseStyleAndRule(GetLegendGraphicRequest req, Object infoObj, Map
Name name = layerInfo.getResource().getQualifiedName();
LegendRequest legendRequest = req.getLegend(name);
if( legendRequest != null ){
legendRequest.setLegendInfo( legend );
configureLegendInfo(req, legendRequest, legend);
}
else {
LOGGER.log(Level.FINE, "Unable to set LegendInfo for "+name);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,33 @@ public void testCustomLegend() throws Exception {
BufferedImage expected = ImageIO.read( resource.file() );

assertEquals( getPixelColor(expected,10,2).getRGB(), getPixelColor(image,10,2).getRGB() );

// test external image dimensions
base = "wms?service=WMS&version=1.1.1&request=GetLegendGraphic" +
"&layer=sf:states&style=custom" +
"&format=image/png";

image = getAsImage(base, "image/png");

assertEquals( "width", image.getWidth(), expected.getWidth() );
assertEquals( "height", image.getHeight(), expected.getHeight() );

Color expectedColor = getPixelColor(expected,11,11);
Color actualColor = getPixelColor(image,11,11);
assertEquals( "red", expectedColor.getRed(), actualColor.getRed() );
assertEquals( "green",expectedColor.getGreen(), actualColor.getGreen() );
assertEquals( "blue",expectedColor.getBlue(), actualColor.getBlue() );
assertEquals( "alpha",expectedColor.getAlpha(), actualColor.getAlpha() );

// test rescale
base = "wms?service=WMS&version=1.1.1&request=GetLegendGraphic" +
"&layer=sf:states&style=custom" +
"&format=image/png&width=16&height=16";

image = getAsImage(base, "image/png");
image = getAsImage(base, "image/png");

Color expectedColor = getPixelColor(expected,11,11);
Color actualColor = getPixelColor(image,8,8);
expectedColor = getPixelColor(expected,11,11);
actualColor = getPixelColor(image,8,8);
assertEquals( "red", expectedColor.getRed(), actualColor.getRed() );
assertEquals( "green",expectedColor.getGreen(), actualColor.getGreen() );
assertEquals( "blue",expectedColor.getBlue(), actualColor.getBlue() );
Expand Down

0 comments on commit 1894c00

Please sign in to comment.