Skip to content

Commit

Permalink
Fix on image layer with error
Browse files Browse the repository at this point in the history
- simplify the too big images generation
- manage the download error
  • Loading branch information
sbrunner committed Jan 11, 2024
1 parent 9b6b581 commit 2ad3a98
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 16 deletions.
59 changes: 44 additions & 15 deletions core/src/main/java/org/mapfish/print/map/image/ImageLayer.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import org.mapfish.print.map.geotools.StyleSupplier;
import org.mapfish.print.parser.HasDefaultValue;
import org.opengis.referencing.crs.CoordinateReferenceSystem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpRequest;
Expand All @@ -50,11 +52,14 @@
*/
public final class ImageLayer extends AbstractSingleImageLayer {
private final ImageParam params;
private final boolean failOnError;
private final StyleSupplier<GridCoverage2D> styleSupplier;
private final ExecutorService executorService;
private final RenderType renderType;
private double imageBufferScaling;
private BufferedImage image;
private boolean imageLoadError = false;
private static final Logger LOGGER = LoggerFactory.getLogger(ImageLayer.class);

/**
* Constructor.
Expand All @@ -73,6 +78,8 @@ protected ImageLayer(
@Nonnull final MetricRegistry registry) {
super(executorService, styleSupplier, params, registry, configuration);
this.params = params;
this.failOnError = params.failOnError;
params.failOnError = true;
this.styleSupplier = styleSupplier;
this.executorService = executorService;
this.renderType = RenderType.fromMimeType(params.imageFormat);
Expand All @@ -83,7 +90,12 @@ protected BufferedImage loadImage(
final MfClientHttpRequestFactory requestFactory, final MapfishMapContext transformer) {
final ReferencedEnvelope envelopeOrig =
transformer.getBounds().toReferencedEnvelope(transformer.getPaintArea());
final Rectangle paintArea = calculateNewBounds(image, envelopeOrig);
final Rectangle paintArea;
if (imageLoadError) {
paintArea = transformer.getPaintArea();
} else {
paintArea = calculateNewBounds(image, envelopeOrig);
}
final ReferencedEnvelope envelope = transformer.getBounds().toReferencedEnvelope(paintArea);
final BufferedImage bufferedImage =
new BufferedImage(paintArea.width, paintArea.height, TYPE_INT_ARGB_PRE);
Expand Down Expand Up @@ -157,30 +169,47 @@ public double getImageBufferScaling() {
public void prepareRender(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {
image = fetchImage(transformer, clientHttpRequestFactory);
try {
image = fetchImage(transformer, clientHttpRequestFactory);
} catch (Exception e) {
if (failOnError) {
throw new RuntimeException(e);
} else {
LOGGER.error("Error while fetching image", e);
image = createErrorImage(new Rectangle(1, 1));
imageLoadError = true;
imageBufferScaling = 1;
return;
}
}
imageLoadError = false;

final ReferencedEnvelope envelopeOrig =
transformer.getBounds().toReferencedEnvelope(transformer.getPaintArea());
final Rectangle paintArea = calculateNewBounds(image, envelopeOrig);

double widthImageBufferScaling = paintArea.getWidth() / transformer.getMapSize().getWidth();
double heightImageBufferScaling = paintArea.getHeight() / transformer.getMapSize().getHeight();
imageBufferScaling =
((paintArea.getWidth() / transformer.getMapSize().getWidth())
+ (paintArea.getHeight() / transformer.getMapSize().getHeight()))
/ 2;
Math.sqrt(
(Math.pow(widthImageBufferScaling, 2) + Math.pow(heightImageBufferScaling, 2)) / 2);
if (imageBufferScaling >= 2) {
// Simplify the image by multiple of 2
imageBufferScaling =
imageBufferScaling
/ Math.pow((int) (Math.floor(Math.log(imageBufferScaling) / Math.log(2))), 2);
}
}

private BufferedImage fetchImage(
final MapfishMapContext transformer,
final MfClientHttpRequestFactory clientHttpRequestFactory) {
final MfClientHttpRequestFactory clientHttpRequestFactory)
throws URISyntaxException, IOException {
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;
final URI commonUri = new URI(this.params.getBaseUrl());
final ClientHttpRequest request =
clientHttpRequestFactory.createRequest(commonUri, HttpMethod.GET);
return fetchImage(request, transformer);
}

/**
Expand Down
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
@@ -0,0 +1,22 @@
{
"layout": "A4 landscape",
"outputFormat": "pdf",
"attributes": {
"map": {
"bbox": [4150836.3839982115, 7600898.092677928, 5703147.34969658, 8257645.039704161],
"dpi": 300,
"layers": [
{
"baseURL": "http://geoserver:8080/does-not-exist.png",
"extent": [4392836.015549079, 7861166.1739889495, 4481502.968359884, 7888169.291435877],
"name": "image",
"opacity": 1,
"type": "image"
}
],
"longitudeFirst": true,
"projection": "EPSG:3857",
"rotation": 0
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dpi": 300,
"layers": [
{
"baseURL": "http://geoserver:8080/geoserver/GeoServer.png",
"baseURL": "http://geoserver:8080/geoserver/www/GeoServer.png",
"extent": [4392836.015549079, 7861166.1739889495, 4481502.968359884, 7888169.291435877],
"name": "image",
"opacity": 1,
Expand Down

0 comments on commit 2ad3a98

Please sign in to comment.