Skip to content

Commit

Permalink
Upgrade to spring boot 2.7.17 and geoscript 1.21.0
Browse files Browse the repository at this point in the history
  • Loading branch information
jericks committed Oct 28, 2023
1 parent cbf6105 commit cfa5c00
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
7 changes: 4 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.springframework.boot' version '2.6.6'
id 'org.springframework.boot' version '2.7.17'
id 'com.adarshr.test-logger' version '2.1.0'
id 'groovy'
}
Expand Down Expand Up @@ -52,8 +52,9 @@ repositories {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.codehaus.groovy:groovy'
implementation("org.geoscript:geoscript-groovy:1.18.0") {
implementation 'org.apache.groovy:groovy'
implementation 'com.twelvemonkeys.imageio:imageio-jpeg:3.10.0'
implementation("org.geoscript:geoscript-groovy:1.21.0") {
exclude module: "gt-shapefile"
exclude module: "gt-jdbc-postgis"
exclude module: "gt-jdbc-h2"
Expand Down
24 changes: 21 additions & 3 deletions src/main/groovy/org/cugos/mbtilesserver/Rest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import springfox.documentation.annotations.ApiIgnore

import javax.imageio.ImageIO
import javax.servlet.http.HttpServletResponse
import java.awt.Graphics2D
import java.awt.image.BufferedImage
import java.awt.image.RenderedImage

@Api(value = "MBTiles REST API")
Expand Down Expand Up @@ -59,7 +61,7 @@ class Rest {
String type = config.mbtiles.metadata.get("format","png") ?: "png"
Bounds b = Bounds.fromString(bounds)
b.proj = "EPSG:4326"
RenderedImage image = config.mbtiles.getRaster(b, w, h).image
BufferedImage image = config.mbtiles.getRaster(b, w, h).bufferedImage
byte[] bytes = getBytes(image, type)
createHttpEntity(bytes, type)
}
Expand All @@ -77,7 +79,7 @@ class Rest {
) throws IOException {
String type = config.mbtiles.metadata.get("format","png") ?: "png"
Point point = Projection.transform(new Point(x,y), new Projection(proj), config.mbtiles.proj)
RenderedImage image = config.mbtiles.getRaster(point, z, w, h).image
BufferedImage image = config.mbtiles.getRaster(point, z, w, h).bufferedImage
byte[] bytes = getBytes(image, type)
createHttpEntity(bytes, type)
}
Expand Down Expand Up @@ -201,10 +203,26 @@ class Rest {
new HttpEntity<byte[]>(bytes, headers)
}

protected byte[] getBytes(RenderedImage image, String type) {
protected byte[] getBytes(BufferedImage image, String type) {
if (isJpegAndHasAlpha(image, type)) {
image = removeAlpha(image)
}
ByteArrayOutputStream out = new ByteArrayOutputStream()
ImageIO.write(image, type, out)
out.close()
out.toByteArray()
}

private boolean isJpegAndHasAlpha(BufferedImage image, String type) {
type.equalsIgnoreCase("JPEG") && image.type == BufferedImage.TYPE_INT_ARGB
}

private BufferedImage removeAlpha(BufferedImage image) {
BufferedImage img = new BufferedImage(image.width, image.height, BufferedImage.TYPE_INT_RGB)
Graphics2D g2d = img.createGraphics()
g2d.drawImage(image, 0, 0, null)
g2d.dispose()
img
}

}

0 comments on commit cfa5c00

Please sign in to comment.