sebr72 commented on code in PR #890: URL: https://github.com/apache/incubator-baremaps/pull/890#discussion_r1767500472
########## baremaps-cli/src/main/java/org/apache/baremaps/cli/dem/DEM.java: ########## @@ -0,0 +1,38 @@ +/* + * 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.dem; + + + +import picocli.CommandLine; +import picocli.CommandLine.Command; + +@Command(name = "dem", description = "DEM processing commands.", + subcommands = { + Serve.class, + VectorTileContours.class + }, + sortOptions = false) +@SuppressWarnings("squid:S106") +public class DEM implements Runnable { Review Comment: Maybe you could add some javadoc explaining DEM, maybe with external links This comment applies to the Key classes (entry points of this new functionality) ########## baremaps-core/src/main/java/org/apache/baremaps/tilestore/raster/RasterTileCache.java: ########## @@ -0,0 +1,93 @@ +/* + * 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.tilestore.raster; + + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.benmanes.caffeine.cache.CaffeineSpec; +import com.github.benmanes.caffeine.cache.Weigher; +import java.awt.image.BufferedImage; +import org.apache.baremaps.tilestore.TileCoord; +import org.apache.baremaps.tilestore.TileStore; +import org.apache.baremaps.tilestore.TileStoreException; +import org.checkerframework.checker.index.qual.NonNegative; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * A {@code TileStore} decorator that uses caffeine to cache the content of tiles. + */ +public class RasterTileCache implements TileStore<BufferedImage> { + + private static final Logger logger = LoggerFactory.getLogger(RasterTileCache.class); + + private final TileStore<BufferedImage> tileStore; + + private final Cache<TileCoord, BufferedImage> cache; + + /** + * Decorates the TileStore with a cache. + * + * @param tileStore the tile store + * @param caffeineSpec the cache specification + */ + public RasterTileCache(TileStore<BufferedImage> tileStore, CaffeineSpec caffeineSpec) { + this.tileStore = tileStore; + this.cache = Caffeine.from(caffeineSpec).weigher(new Weigher<TileCoord, BufferedImage>() { + @Override + public @NonNegative int weigh(TileCoord tileCoord, BufferedImage bufferedImage) { + return bufferedImage.getData().getDataBuffer().getSize() * 4; + } + }).build(); + } + + /** {@inheritDoc} */ + @Override + public BufferedImage read(TileCoord tileCoord) throws TileStoreException { + return cache.get(tileCoord, t -> { + try { + return tileStore.read(t); + } catch (TileStoreException e) { + logger.error("Unable to read the tile.", e); + return null; + } + }); + } + + /** {@inheritDoc} */ + @Override + public void write(TileCoord tileCoord, BufferedImage bufferedImage) throws TileStoreException { + tileStore.write(tileCoord, bufferedImage); + cache.invalidate(tileCoord); + } + + /** {@inheritDoc} */ + @Override + public void delete(TileCoord tileCoord) throws TileStoreException { + tileStore.delete(tileCoord); + cache.invalidate(tileCoord); + } + + /** {@inheritDoc} */ + @Override + public void close() throws Exception { + tileStore.close(); Review Comment: If close fail. Don't we want to cleanup anyway ? ########## baremaps-dem/src/main/java/org/apache/baremaps/dem/ChaikinSmoother.java: ########## @@ -0,0 +1,126 @@ +/* + * 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.dem; + +import org.locationtech.jts.geom.*; +import org.locationtech.jts.geom.impl.CoordinateArraySequence; +import org.locationtech.jts.geom.util.GeometryTransformer; + +/** + * A geometry transformer that applies the Chaikin smoothing algorithm to the coordinates of a + * geometry. Review Comment: Maybe a link explaining why/when to use this algorithm. ########## baremaps-cli/src/main/java/org/apache/baremaps/cli/map/Serve.java: ########## @@ -95,9 +96,9 @@ public Integer call() throws Exception { try ( var tileStore = new PostgresTileStore(datasource, tileset); - var tileCache = new TileCache(tileStore, caffeineSpec)) { + var tileCache = new VectorTileCache(tileStore, caffeineSpec)) { - var tileStoreSupplier = (Supplier<TileStore>) () -> tileCache; + var tileStoreSupplier = (Supplier<TileStore<ByteBuffer>>) () -> tileCache; Review Comment: A<B<C>> = slippery slope but looks good and makes sense. ########## baremaps-core/src/main/java/org/apache/baremaps/tilestore/raster/GeoTiffReader.java: ########## @@ -0,0 +1,147 @@ +/* + * 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.tilestore.raster; + +import java.nio.file.Path; +import org.apache.baremaps.tilestore.TileCoord; +import org.apache.sis.coverage.grid.*; +import org.apache.sis.geometry.Envelopes; +import org.apache.sis.geometry.GeneralEnvelope; +import org.apache.sis.image.ImageProcessor; +import org.apache.sis.referencing.CRS; +import org.apache.sis.referencing.CommonCRS; +import org.apache.sis.storage.*; +import org.opengis.referencing.crs.CoordinateReferenceSystem; +import org.opengis.util.FactoryException; + +/** + * A reader for geotiff files based on the Apache SIS library. + */ +public class GeoTiffReader implements AutoCloseable { + + private static final CoordinateReferenceSystem WEB_MERCATOR; + + static { + try { + WEB_MERCATOR = CRS.fromWKT(""" + ProjectedCRS["WGS 84 / Pseudo-Mercator", + BaseGeodCRS["WGS 84", + Datum["World Geodetic System 1984", + Ellipsoid["WGS 84", 6378137.0, 298.257223563]], + Unit["degree", 0.017453292519943295]], + Conversion["Popular Visualisation Pseudo-Mercator", + Method["Popular Visualisation Pseudo Mercator"]], + CS[Cartesian, 2], + Axis["Easting (X)", east], + Axis["Northing (Y)", north], + Unit["metre", 1], + Scope["Certain Web mapping and visualisation applications."], + Id["EPSG", 3857]] + """); + } catch (FactoryException e) { + throw new IllegalArgumentException(e); + } + } + + private final DataStore dataStore; + + private final GridCoverage gridCoverage; + + /** + * Constructs a {@code GeoTiffReader} with the specified path. + * + * @param path the path of the geotiff file + * @throws GeoTiffException if an error occurs + */ + public GeoTiffReader(Path path) throws GeoTiffException { + try { + this.dataStore = DataStores.open(path); + var allImages = ((Aggregate) dataStore).components(); + var firstImage = (GridCoverageResource) allImages.iterator().next(); + firstImage.setLoadingStrategy(RasterLoadingStrategy.AT_GET_TILE_TIME); + gridCoverage = firstImage.read(null); + } catch (DataStoreException e) { + throw new GeoTiffException(e); + } + } + + /** + * Read the elevation data for the specified tile coordinate. + * + * @param tileCoord the tile coordinate + * @param tileSizePx the size of the tile in pixels + * @param tileBufferPx the buffer size in pixels + * @return the elevation data + * @throws GeoTiffException if an error occurs + */ + public double[] read(TileCoord tileCoord, int tileSizePx, int tileBufferPx) + throws GeoTiffException { + try { + // Compute the buffer size and expand the envelope + var imageSize = tileSizePx + 2 * tileBufferPx; + var tileEnvelope = tileCoord.envelope(); + var tileWidth = tileEnvelope.getWidth(); + var tileHeight = tileEnvelope.getHeight(); + var pixelWidth = tileWidth / tileSizePx; + var pixelHeight = tileHeight / tileSizePx; + tileEnvelope.expandBy(pixelWidth * tileBufferPx, pixelHeight * tileBufferPx); + + // Create the target grid geometry in the web mercator projection + var sourceEnvelope = new GeneralEnvelope(CommonCRS.WGS84.normalizedGeographic()); + sourceEnvelope.setRange(0, tileEnvelope.getMinX(), tileEnvelope.getMaxX()); + sourceEnvelope.setRange(1, tileEnvelope.getMinY(), tileEnvelope.getMaxY()); + var targetEnvelope = Envelopes.transform(sourceEnvelope, WEB_MERCATOR); + var targetSize = new GridExtent(imageSize, imageSize); + var targetGridGeometry = + new GridGeometry(targetSize, targetEnvelope, GridOrientation.DISPLAY); + + // Resample and render the image + var gridCoverageProcessor = new GridCoverageProcessor(); + gridCoverageProcessor.setInterpolation(new BicubicInterpolation()); + var tileGridCoverage = gridCoverageProcessor.resample(this.gridCoverage, targetGridGeometry); + var renderedImage = tileGridCoverage.render(null); + + // Convert the image to the terrarium color scale + var imageProcessor = new ImageProcessor(); + renderedImage = imageProcessor.prefetch(renderedImage, null); + + // Convert the image to a grid + var values = new double[renderedImage.getWidth() * renderedImage.getHeight()]; + renderedImage.getData().getPixels( + 0, + 0, + renderedImage.getWidth(), + renderedImage.getHeight(), + values); + + return values; + } catch (Exception e) { Review Comment: Suggestion: Limit the catch to declared exceptions (A | B |RuntimeException) ? (for consistency with line 78). This comment also applies to other places. ########## baremaps-cli/src/main/java/org/apache/baremaps/cli/dem/Serve.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.dem; + +import static org.apache.baremaps.utils.ObjectMapperUtils.objectMapper; + +import com.linecorp.armeria.common.*; +import com.linecorp.armeria.server.Server; +import com.linecorp.armeria.server.annotation.JacksonResponseConverterFunction; +import com.linecorp.armeria.server.cors.CorsService; +import com.linecorp.armeria.server.docs.DocService; +import com.linecorp.armeria.server.file.HttpFile; +import java.nio.file.Path; +import java.util.concurrent.Callable; +import org.apache.baremaps.dem.ElevationUtils; +import org.apache.baremaps.server.BufferedImageResource; +import org.apache.baremaps.server.VectorTileResource; +import org.apache.baremaps.tilestore.raster.*; +import picocli.CommandLine.Command; +import picocli.CommandLine.Option; + +@Command(name = "serve", description = "Start a tile server that serves elevation data.") +public class Serve implements Callable<Integer> { Review Comment: Maybe add a line to define purpose of the Server ########## baremaps-dem/src/test/java/org/apache/baremaps/dem/ContourRenderer.java: ########## @@ -0,0 +1,89 @@ +/* + * 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.dem; + +import java.awt.*; +import java.io.IOException; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Stream; +import javax.imageio.ImageIO; +import javax.swing.*; +import org.locationtech.jts.geom.Geometry; + +public class ContourRenderer { Review Comment: Javadoc especially important since it has a main() ########## baremaps-core/src/main/java/org/apache/baremaps/tilestore/raster/VectorContourTileStore.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.tilestore.raster; + +import java.io.ByteArrayOutputStream; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.zip.GZIPOutputStream; +import org.apache.baremaps.dem.ContourTracer; +import org.apache.baremaps.maplibre.vectortile.Feature; +import org.apache.baremaps.maplibre.vectortile.Layer; +import org.apache.baremaps.maplibre.vectortile.Tile; +import org.apache.baremaps.maplibre.vectortile.VectorTileEncoder; +import org.apache.baremaps.tilestore.TileCoord; +import org.apache.baremaps.tilestore.TileStore; +import org.apache.baremaps.tilestore.TileStoreException; +import org.locationtech.jts.geom.util.AffineTransformation; + +/** + * A {@code TileStore} that calculates vector contour tiles from elevation tiles. + */ +public class VectorContourTileStore implements TileStore<ByteBuffer> { + + private final GeoTiffReader geoTiffReader; + + /** + * Constructs a {@code VectorContourTileStore} with the specified GeoTIFF reader. + * + * @param geoTiffReader the geotiff reader + */ + public VectorContourTileStore(GeoTiffReader geoTiffReader) { + this.geoTiffReader = geoTiffReader; + } + + /** + * Read the contour data for the specified tile coordinate. + * + * @param tileCoord the tile coordinate + * @return the contour data + * @throws TileStoreException if an error occurs + */ + @Override + public ByteBuffer read(TileCoord tileCoord) throws TileStoreException { + try { + var grid = geoTiffReader.read(tileCoord, 256, 4); + + int increment = switch (tileCoord.z()) { + case 1 -> 2000; Review Comment: Maybe link (in javadoc) to some justification behind the reasoning of this mapping, rather than leaving "magic" numbers. ########## baremaps-dem/src/test/java/org/apache/baremaps/dem/MarchingSquareRenderer.java: ########## @@ -0,0 +1,109 @@ +/* + * 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.dem; + +import static org.apache.baremaps.dem.ContourTracerPolygonTest.*; + +import java.awt.*; +import java.util.List; +import javax.swing.*; +import org.locationtech.jts.geom.Coordinate; +import org.locationtech.jts.geom.Geometry; +import org.locationtech.jts.geom.LineString; +import org.locationtech.jts.geom.Polygon; +import org.locationtech.jts.geom.util.AffineTransformation; + +public class MarchingSquareRenderer extends JPanel { Review Comment: Javadoc especially since it has a main() -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
