Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate correct size for ImageLayer to avoid rescaling #3130

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions checkstyle_suppressions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"http://www.puppycrawl.com/dtds/suppressions_1_2.dtd">

<suppressions>
<suppress files=".*TestUtils\.java" checks=".*"/>
<suppress files=".*/Test.*\.java" checks=".*"/>
<suppress files=".*Test\.java" checks=".*"/>
</suppressions>
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ public interface MapLayer {
* Render the layer to the graphics2D object.
*
* @param transformer the map transformer containing the map bounds and size.
* @param clientHttpRequestFactory the factory to use for making http requests.
*/
void prepareRender(MapfishMapContext transformer);
void prepareRender(
MapfishMapContext transformer, MfClientHttpRequestFactory clientHttpRequestFactory);

/**
* Render the layer to the graphics2D object.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ public final Optional<MapLayer> tryAddLayer(final MapLayer newLayer) {
}

@Override
public void prepareRender(final MapfishMapContext transformer) {}
public void prepareRender(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {}

@Override
public final void render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,9 @@ public double getImageBufferScaling() {
}

@Override
public void prepareRender(final MapfishMapContext transformer) {}
public void prepareRender(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {}

@Override
public void render(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ protected final List<? extends Layer> getLayers(
* @param transformer object containing map rendering information
*/
protected abstract BufferedImage loadImage(
MfClientHttpRequestFactory requestFactory, MapfishMapContext transformer) throws Throwable;
MfClientHttpRequestFactory requestFactory, MapfishMapContext transformer) throws Exception;

@Override
public final double getImageBufferScaling() {
public double getImageBufferScaling() {
return 1;
}

Expand Down
76 changes: 56 additions & 20 deletions core/src/main/java/org/mapfish/print/map/image/ImageLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
Expand All @@ -27,7 +29,6 @@
import org.geotools.styling.Style;
import org.locationtech.jts.util.Assert;
import org.mapfish.print.Constants;
import org.mapfish.print.attribute.map.MapBounds;
import org.mapfish.print.attribute.map.MapfishMapContext;
import org.mapfish.print.config.Configuration;
import org.mapfish.print.config.Template;
Expand All @@ -51,7 +52,9 @@ public final class ImageLayer extends AbstractSingleImageLayer {
private final ImageParam params;
private final StyleSupplier<GridCoverage2D> styleSupplier;
private final ExecutorService executorService;
private RenderType renderType;
private final RenderType renderType;
private double imageBufferScaling;
private BufferedImage image;

/**
* Constructor.
Expand All @@ -77,35 +80,27 @@ protected ImageLayer(

@Override
protected BufferedImage loadImage(
final MfClientHttpRequestFactory requestFactory, final MapfishMapContext transformer)
throws Throwable {
final ImageParam layerParam = this.params;
final URI commonUri = new URI(layerParam.getBaseUrl());

final Rectangle paintArea = transformer.getPaintArea();

final MfClientHttpRequestFactory requestFactory, final MapfishMapContext transformer) {
final ReferencedEnvelope envelopeOrig =
transformer.getBounds().toReferencedEnvelope(transformer.getPaintArea());
final Rectangle paintArea = calculateNewBounds(image, envelopeOrig);
final ReferencedEnvelope envelope = transformer.getBounds().toReferencedEnvelope(paintArea);
final CoordinateReferenceSystem mapProjection = envelope.getCoordinateReferenceSystem();

final BufferedImage bufferedImage =
new BufferedImage(paintArea.width, paintArea.height, TYPE_INT_ARGB_PRE);
final Graphics2D graphics = bufferedImage.createGraphics();
final MapBounds bounds = transformer.getBounds();
final MapContent content = new MapContent();
final ClientHttpRequest request = requestFactory.createRequest(commonUri, HttpMethod.GET);
final BufferedImage image = fetchImage(request, transformer);

try {
GridCoverageFactory factory = CoverageFactoryFinder.getGridCoverageFactory(null);
final CoordinateReferenceSystem mapProjection = envelope.getCoordinateReferenceSystem();
GeneralEnvelope gridEnvelope = new GeneralEnvelope(mapProjection);

gridEnvelope.setEnvelope(layerParam.extent);
gridEnvelope.setEnvelope(this.params.extent);
GridCoverage2D coverage =
factory.create(layerParam.getBaseUrl(), image, gridEnvelope, null, null, null);
factory.create(this.params.getBaseUrl(), image, gridEnvelope, null, null, null);
Style style = this.styleSupplier.load(requestFactory, coverage);
GridCoverageLayer l = new GridCoverageLayer(coverage, style);

content.addLayers(Collections.singletonList(l));
content.addLayers(Collections.singletonList(new GridCoverageLayer(coverage, style)));

StreamingRenderer renderer = new StreamingRenderer();

Expand Down Expand Up @@ -133,20 +128,61 @@ protected BufferedImage loadImage(
renderer.setMapContent(content);
renderer.setThreadPool(this.executorService);

final ReferencedEnvelope mapArea = bounds.toReferencedEnvelope(paintArea);
renderer.paint(graphics, paintArea, mapArea);
renderer.paint(graphics, paintArea, envelope);
return bufferedImage;
} finally {
graphics.dispose();
content.dispose();
}
}

private Rectangle calculateNewBounds(
final BufferedImage image, final ReferencedEnvelope envelope) {
double w = (image.getWidth() / (params.extent[2] - params.extent[0])) * envelope.getWidth();
double h = (image.getHeight() / (params.extent[3] - params.extent[1])) * envelope.getHeight();
return new Rectangle(Math.toIntExact(Math.round(w)), Math.toIntExact(Math.round(h)));
}

@Override
public RenderType getRenderType() {
return this.renderType;
}

@Override
public double getImageBufferScaling() {
return imageBufferScaling;
}

@Override
public void prepareRender(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {
image = fetchImage(transformer, clientHttpRequestFactory);
final ReferencedEnvelope envelopeOrig =
transformer.getBounds().toReferencedEnvelope(transformer.getPaintArea());
final Rectangle paintArea = calculateNewBounds(image, envelopeOrig);

imageBufferScaling =
((paintArea.getWidth() / transformer.getMapSize().getWidth())
+ (paintArea.getHeight() / transformer.getMapSize().getHeight()))
/ 2;
}

private BufferedImage fetchImage(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {
BufferedImage image;
try {
final URI commonUri = new URI(this.params.getBaseUrl());
final ClientHttpRequest request =
clientHttpRequestFactory.createRequest(commonUri, HttpMethod.GET);
image = fetchImage(request, transformer);
} catch (URISyntaxException | IOException e) {
throw new RuntimeException(e);
}
return image;
}

/**
* Renders an image as layer.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.codahale.metrics.MetricRegistry;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URI;
import java.util.concurrent.ExecutorService;
import javax.annotation.Nonnull;
Expand Down Expand Up @@ -48,7 +49,7 @@ protected WmsLayer(
protected BufferedImage loadImage(
@Nonnull final MfClientHttpRequestFactory requestFactory,
@Nonnull final MapfishMapContext transformer)
throws Throwable {
throws IOException {

LOGGER.info("Query the WMS image {}.", this.imageRequest.getURI());
return fetchImage(imageRequest, transformer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,9 @@ protected AbstractTiledLayer(
}

@Override
public final void prepareRender(final MapfishMapContext mapContext) {
public final void prepareRender(
final MapfishMapContext mapContext,
final MfClientHttpRequestFactory clientHttpRequestFactory) {
this.tileCacheInformation =
createTileInformation(
mapContext.getRotatedBoundsAdjustedForPreciseRotatedMapSize(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ private List<URI> createLayerGraphics(

// prepare layers for rendering
for (final MapLayer layer : layers) {
layer.prepareRender(mapContext);
layer.prepareRender(mapContext, clientHttpRequestFactory);
final MapfishMapContext transformer =
getTransformer(mapContext, layer.getImageBufferScaling());
layer.prefetchResources(cache, clientHttpRequestFactory, transformer, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import org.junit.Ignore;
import org.junit.Test;
import org.mapfish.print.AbstractMapfishSpringTest;
import org.mapfish.print.TestHttpClientFactory;
Expand Down Expand Up @@ -37,6 +38,7 @@ private static PJsonObject loadJsonRequestData() throws IOException {

@Test
@DirtiesContext
@Ignore
public void testExecute() throws Exception {
final String host = "image_layer_test";
requestFactory.registerHandler(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import javax.mail.Provider;

/** A replacement provider that replaces the normal SMTP transport with a mock one. */
public class SmtpProvider extends Provider {
public SmtpProvider() {
public class SmtpProviderTestUtils extends Provider {
public SmtpProviderTestUtils() {
super(Type.TRANSPORT, "smtp", MockTransport.class.getName(), "Camptocamp", null);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
org.mapfish.print.servlet.SmtpProvider
org.mapfish.print.servlet.SmtpProviderTestUtils
Binary file modified examples/geoserver-data/www/GeoServer.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,20 @@
"outputFormat": "pdf",
"attributes": {
"map": {
"bbox": [4150836.3839982115, 7600898.092677928, 4588667.682015701, 8257645.039704161],
"bbox": [4150836.3839982115, 7600898.092677928, 5703147.34969658, 8257645.039704161],
"dpi": 300,
"layers": [
{
"baseURL": "http://geoserver:8080/www/GeoServer.png",
"extent": [4392836.015549079, 7861166.1739889495, 4481502.968359884, 7924914.6555787865],
"extent": [4392836.015549079, 7861166.1739889495, 4481502.968359884, 7888169.291435877],
"name": "image",
"opacity": 1,
"type": "image"
}
],
"longitudeFirst": true,
"projection": "EPSG:3857",
"rotation": 0,
"scale": 100000000
"rotation": 0
}
}
}
Loading