-
Notifications
You must be signed in to change notification settings - Fork 70
Raster processing #884
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
Closed
Closed
Raster processing #884
Changes from 7 commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
acebe4c
Port the MARTINI algorithm to java
bchapuis b034bf2
Fix minor formatting issues
bchapuis a24f898
use double instead of float values
bchapuis 6104fb9
Format code
bchapuis ebc0f43
Add hillshade and isolines algorithms
bchapuis 4eb2d23
Compute hillshades and contour
bchapuis 4c0c5bb
Refactor the code and improve javadoc
bchapuis d1f8069
Improve contour tracer
bchapuis ba21a24
Do some cleanup
bchapuis c818573
Improve API
bchapuis e3994b3
Systematically use SRI
bchapuis f94eb34
Add Chaikin smoother and improve hillshade and contour server
bchapuis e4543b2
Improve hillshade demonstration
bchapuis 9ba78e7
Fix sonar issues
bchapuis 80a92db
Fix issues detected by sonar
bchapuis 25cc892
Do some cleaning
bchapuis a144428
Refactor and clean
bchapuis 65204df
Fix polygonization
bchapuis 7d51799
Fix unit tests
bchapuis 2db80a1
Improve naming
bchapuis 31e2ef7
Do some cleanup
bchapuis fc95956
Read wkt
bchapuis 533c32f
Format code
bchapuis 2ee536a
Improve hillshade and contour map
bchapuis ead787e
Improve hillshade and add gdal api
bchapuis 7856a00
Move the gdal classes in a dedicated module
bchapuis 1444236
Avoid falling into the extremes when interpolating
bchapuis 2c4f443
Remove intermediary line merge
bchapuis 73d163c
Add conversion method
bchapuis 6f82ad1
Refactor and cleanup the code
bchapuis 68ff7ea
Clean, format and document
bchapuis 0e0c6fa
Use Apache SIS instead of GDAL to read tiff files
bchapuis 5c4d04c
Remove printstacktrace call
bchapuis 71bc83d
Add failing polygonization test
bchapuis a4d96ec
Fix polygonization
bchapuis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
256 changes: 256 additions & 0 deletions
256
baremaps-cli/src/main/java/org/apache/baremaps/cli/raster/HillShade.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,256 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.baremaps.cli.raster; | ||
|
||
import static com.google.common.net.HttpHeaders.ACCESS_CONTROL_ALLOW_ORIGIN; | ||
import static com.google.common.net.HttpHeaders.CONTENT_TYPE; | ||
import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper; | ||
|
||
import com.github.benmanes.caffeine.cache.Caffeine; | ||
import com.github.benmanes.caffeine.cache.LoadingCache; | ||
import com.linecorp.armeria.common.*; | ||
import com.linecorp.armeria.server.Server; | ||
import com.linecorp.armeria.server.annotation.Blocking; | ||
import com.linecorp.armeria.server.annotation.Get; | ||
import com.linecorp.armeria.server.annotation.JacksonResponseConverterFunction; | ||
import com.linecorp.armeria.server.annotation.Param; | ||
import com.linecorp.armeria.server.cors.CorsService; | ||
import com.linecorp.armeria.server.docs.DocService; | ||
import java.awt.*; | ||
import java.awt.image.BufferedImage; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.net.URL; | ||
import java.nio.ByteBuffer; | ||
import java.util.concurrent.Callable; | ||
import java.util.function.Function; | ||
import java.util.function.Supplier; | ||
import javax.imageio.ImageIO; | ||
import org.apache.baremaps.raster.elevation.ElevationUtils; | ||
import org.apache.baremaps.tilestore.TileCoord; | ||
import org.apache.baremaps.tilestore.TileStore; | ||
import org.apache.baremaps.tilestore.TileStoreException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import picocli.CommandLine.Command; | ||
import picocli.CommandLine.Option; | ||
|
||
@Command(name = "hillshade", description = "Start a tile server that computes hillshades.") | ||
public class HillShade implements Callable<Integer> { | ||
|
||
@Option(names = {"--host"}, paramLabel = "HOST", description = "The host of the server.") | ||
private String host = "localhost"; | ||
|
||
@Option(names = {"--port"}, paramLabel = "PORT", description = "The port of the server.") | ||
private int port = 9000; | ||
|
||
@Override | ||
public Integer call() throws Exception { | ||
|
||
var serverBuilder = Server.builder(); | ||
serverBuilder.http(port); | ||
|
||
var objectMapper = objectMapper(); | ||
var jsonResponseConverter = new JacksonResponseConverterFunction(objectMapper); | ||
var tileStore = new HillShadeTileStore(); | ||
|
||
serverBuilder.annotatedService(new HillShadeTileResource(() -> tileStore), | ||
jsonResponseConverter); | ||
|
||
serverBuilder.decorator(CorsService.builderForAnyOrigin() | ||
.allowAllRequestHeaders(true) | ||
.allowRequestMethods( | ||
HttpMethod.GET, | ||
HttpMethod.POST, | ||
HttpMethod.PUT, | ||
HttpMethod.DELETE, | ||
HttpMethod.OPTIONS, | ||
HttpMethod.HEAD) | ||
.allowCredentials() | ||
.exposeHeaders(HttpHeaderNames.LOCATION) | ||
.newDecorator()); | ||
|
||
serverBuilder.serviceUnder("/docs", new DocService()); | ||
|
||
serverBuilder.disableServerHeader(); | ||
serverBuilder.disableDateHeader(); | ||
|
||
var server = serverBuilder.build(); | ||
|
||
var startFuture = server.start(); | ||
startFuture.join(); | ||
|
||
var shutdownFuture = server.closeOnJvmShutdown(); | ||
shutdownFuture.join(); | ||
|
||
return 0; | ||
} | ||
|
||
public static class HillShadeTileStore implements TileStore { | ||
|
||
// private String url = "https://s3.amazonaws.com/elevation-tiles-prod/geotiff/{z}/{x}/{y}.tif"; | ||
// private String url = "https://demotiles.maplibre.org/terrain-tiles/{z}/{x}/{y}.png"; | ||
private String url = "https://s3.amazonaws.com/elevation-tiles-prod/terrarium/{z}/{x}/{y}.png"; | ||
|
||
private final LoadingCache<TileCoord, BufferedImage> cache = Caffeine.newBuilder() | ||
.maximumSize(1000) | ||
.build(this::getImage); | ||
|
||
public HillShadeTileStore() { | ||
// Default constructor | ||
} | ||
|
||
public BufferedImage getImage(TileCoord tileCoord) throws IOException { | ||
var tileUrl = new URL(this.url | ||
.replace("{z}", String.valueOf(tileCoord.z())) | ||
.replace("{x}", String.valueOf(tileCoord.x())) | ||
.replace("{y}", String.valueOf(tileCoord.y()))); | ||
return ImageIO.read(tileUrl); | ||
} | ||
|
||
public BufferedImage getKernel(TileCoord tileCoord, Function<TileCoord, BufferedImage> provider) | ||
throws IOException { | ||
BufferedImage z1 = | ||
provider.apply(new TileCoord(tileCoord.x() - 1, tileCoord.y() - 1, tileCoord.z())); | ||
BufferedImage z2 = | ||
provider.apply(new TileCoord(tileCoord.x(), tileCoord.y() - 1, tileCoord.z())); | ||
BufferedImage z3 = | ||
provider.apply(new TileCoord(tileCoord.x() + 1, tileCoord.y() - 1, tileCoord.z())); | ||
BufferedImage z4 = | ||
provider.apply(new TileCoord(tileCoord.x() - 1, tileCoord.y(), tileCoord.z())); | ||
BufferedImage z5 = provider.apply(tileCoord); | ||
BufferedImage z6 = | ||
provider.apply(new TileCoord(tileCoord.x() + 1, tileCoord.y(), tileCoord.z())); | ||
BufferedImage z7 = | ||
provider.apply(new TileCoord(tileCoord.x() - 1, tileCoord.y() + 1, tileCoord.z())); | ||
BufferedImage z8 = | ||
provider.apply(new TileCoord(tileCoord.x(), tileCoord.y() + 1, tileCoord.z())); | ||
BufferedImage z9 = | ||
provider.apply(new TileCoord(tileCoord.x() + 1, tileCoord.y() + 1, tileCoord.z())); | ||
int kernelSize = z5.getWidth() * 3; | ||
BufferedImage kernel = new BufferedImage(kernelSize, kernelSize, z5.getType()); | ||
for (int y = 0; y < z5.getHeight(); y++) { | ||
for (int x = 0; x < z5.getWidth(); x++) { | ||
kernel.setRGB(x, y, z1.getRGB(x, y)); | ||
kernel.setRGB(x + z5.getWidth(), y, z2.getRGB(x, y)); | ||
kernel.setRGB(x + 2 * z5.getWidth(), y, z3.getRGB(x, y)); | ||
kernel.setRGB(x, y + z5.getHeight(), z4.getRGB(x, y)); | ||
kernel.setRGB(x + z5.getWidth(), y + z5.getHeight(), z5.getRGB(x, y)); | ||
kernel.setRGB(x + 2 * z5.getWidth(), y + z5.getHeight(), z6.getRGB(x, y)); | ||
kernel.setRGB(x, y + 2 * z5.getHeight(), z7.getRGB(x, y)); | ||
kernel.setRGB(x + z5.getWidth(), y + 2 * z5.getHeight(), z8.getRGB(x, y)); | ||
kernel.setRGB(x + 2 * z5.getWidth(), y + 2 * z5.getHeight(), z9.getRGB(x, y)); | ||
} | ||
} | ||
return kernel; | ||
} | ||
|
||
@Override | ||
public ByteBuffer read(TileCoord tileCoord) throws TileStoreException { | ||
try { | ||
|
||
var image = cache.get(tileCoord); | ||
var kernel = getKernel(tileCoord, cache::get); | ||
var buffer = kernel.getSubimage( | ||
image.getWidth() - 1, | ||
image.getHeight() - 1, | ||
image.getWidth() + 2, | ||
image.getHeight() + 2); | ||
|
||
var grid = ElevationUtils.imageToGrid(buffer); | ||
var hillshade = org.apache.baremaps.raster.elevation.HillShade.hillShade(grid, | ||
buffer.getWidth(), buffer.getHeight(), 45, 315); | ||
|
||
// Create an output image | ||
BufferedImage hillshadeImage = | ||
new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_GRAY); | ||
for (int y = 0; y < image.getHeight(); y++) { | ||
for (int x = 0; x < image.getWidth(); x++) { | ||
int value = (int) hillshade[(y + 1) * buffer.getHeight() + x + 1]; | ||
hillshadeImage.setRGB(x, y, new Color(value, value, value).getRGB()); | ||
} | ||
} | ||
|
||
try (var baos = new ByteArrayOutputStream()) { | ||
ImageIO.write(hillshadeImage, "png", baos); | ||
baos.flush(); | ||
return ByteBuffer.wrap(baos.toByteArray()); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
@Override | ||
public void write(TileCoord tileCoord, ByteBuffer blob) throws TileStoreException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void delete(TileCoord tileCoord) throws TileStoreException { | ||
throw new UnsupportedOperationException(); | ||
} | ||
|
||
@Override | ||
public void close() throws Exception { | ||
// Do nothing | ||
} | ||
} | ||
|
||
public class HillShadeTileResource { | ||
|
||
private static final Logger logger = | ||
LoggerFactory.getLogger(org.apache.baremaps.server.TileResource.class); | ||
|
||
// public static final String TILE_ENCODING = "gzip"; | ||
|
||
public static final String TILE_TYPE = "image/png"; | ||
|
||
private final Supplier<TileStore> tileStoreSupplier; | ||
|
||
public HillShadeTileResource(Supplier<TileStore> tileStoreSupplier) { | ||
this.tileStoreSupplier = tileStoreSupplier; | ||
} | ||
|
||
@Get("regex:^/tiles/(?<z>[0-9]+)/(?<x>[0-9]+)/(?<y>[0-9]+).png") | ||
@Blocking | ||
public HttpResponse tile(@Param("z") int z, @Param("x") int x, @Param("y") int y) { | ||
TileCoord tileCoord = new TileCoord(x, y, z); | ||
try { | ||
TileStore tileStore = tileStoreSupplier.get(); | ||
ByteBuffer blob = tileStore.read(tileCoord); | ||
if (blob != null) { | ||
var headers = ResponseHeaders.builder(200) | ||
.add(CONTENT_TYPE, TILE_TYPE) | ||
// .add(CONTENT_ENCODING, TILE_ENCODING) | ||
.add(ACCESS_CONTROL_ALLOW_ORIGIN, "*") | ||
.build(); | ||
byte[] bytes = new byte[blob.remaining()]; | ||
blob.get(bytes); | ||
HttpData data = HttpData.wrap(bytes); | ||
return HttpResponse.of(headers, data); | ||
} else { | ||
return HttpResponse.of(204); | ||
} | ||
} catch (TileStoreException ex) { | ||
logger.error("Error while reading tile.", ex); | ||
return HttpResponse.of(404); | ||
} | ||
} | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
baremaps-cli/src/main/java/org/apache/baremaps/cli/raster/Raster.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to you under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.baremaps.cli.raster; | ||
|
||
|
||
|
||
import picocli.CommandLine; | ||
import picocli.CommandLine.Command; | ||
|
||
@Command(name = "raster", description = "Raster processing commands.", | ||
subcommands = {HillShade.class}, | ||
sortOptions = false) | ||
public class Raster implements Runnable { | ||
|
||
@Override | ||
public void run() { | ||
CommandLine.usage(this, System.out); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.baremaps</groupId> | ||
<artifactId>baremaps</artifactId> | ||
<version>0.7.4-SNAPSHOT</version> | ||
</parent> | ||
<artifactId>baremaps-raster</artifactId> | ||
<dependencies> | ||
<dependency> | ||
<groupId>com.twelvemonkeys.imageio</groupId> | ||
<artifactId>imageio-tiff</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.locationtech.jts</groupId> | ||
<artifactId>jts-core</artifactId> | ||
</dependency> | ||
</dependencies> | ||
</project> |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.