Skip to content

Commit

Permalink
Clean code and fix issues detected by sonar
Browse files Browse the repository at this point in the history
  • Loading branch information
bchapuis committed Sep 1, 2024
1 parent 7cf7981 commit 812b75e
Show file tree
Hide file tree
Showing 20 changed files with 131 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import picocli.CommandLine.Option;

@Command(name = "vector-contours", description = "Generate vector contours from a DEM.")
@SuppressWarnings("squid:S106")
@SuppressWarnings({"squid:S106", "squid:S3864"})
public class VectorTileContours implements Callable<Integer> {

@Option(names = {"--path"}, paramLabel = "PATH", description = "The path of a geoTIFF file.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
public class TileStoreException extends Exception {

/**
* Constructs an {@code BlobStoreException} with the specified detail message.
* Constructs an {@code TileStoreException} with the specified detail message.
*
* @param message the message
*/
Expand All @@ -30,7 +30,7 @@ public TileStoreException(String message) {
}

/**
* Constructs a {@code BlobStoreException} with the specified cause.
* Constructs a {@code TileStoreException} with the specified cause.
*
* @param cause the cause
*/
Expand All @@ -39,7 +39,7 @@ public TileStoreException(Throwable cause) {
}

/**
* Constructs a {@code BlobStoreException} with the specified detail message and cause.
* Constructs a {@code TileStoreException} with the specified detail message and cause.
*
* @param message the message
* @param cause the cause
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

class BicubicInterpolation extends Interpolation {

@Override
public Dimension getSupportSize() {
return new Dimension(4, 4);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,33 @@

package org.apache.baremaps.tilestore.raster;

import static org.junit.jupiter.api.Assertions.*;
public class GeoTiffException extends Exception {

/**
* Constructs an {@code GeoTiffException} with the specified detail message.
*
* @param message the message
*/
public GeoTiffException(String message) {
super(message);
}

class VectorHillshadeTileStoreTest {

/**
* Constructs a {@code GeoTiffException} with the specified cause.
*
* @param cause the cause
*/
public GeoTiffException(Throwable cause) {
super(cause);
}

/**
* Constructs a {@code GeoTiffException} with the specified detail message and cause.
*
* @param message the message
* @param cause the cause
*/
public GeoTiffException(String message, Throwable cause) {
super(message, cause);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import java.nio.file.Path;
import org.apache.baremaps.tilestore.TileCoord;
import org.apache.baremaps.tilestore.TileStoreException;
import org.apache.sis.coverage.grid.*;
import org.apache.sis.geometry.Envelopes;
import org.apache.sis.geometry.GeneralEnvelope;
Expand Down Expand Up @@ -52,27 +51,27 @@ public class GeoTiffReader implements AutoCloseable {
Id["EPSG", 3857]]
""");
} catch (FactoryException e) {
throw new RuntimeException(e);
throw new IllegalArgumentException(e);
}
}

private final DataStore dataStore;

private final GridCoverage gridCoverage;

public GeoTiffReader(Path path) {
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 RuntimeException(e);
throw new GeoTiffException(e);
}
}

public double[] read(TileCoord tileCoord, int size, int buffer) throws TileStoreException {
public double[] read(TileCoord tileCoord, int size, int buffer) throws GeoTiffException {
try {
// Compute the buffer size and expand the envelope
var fullSize = size + 2 * buffer;
Expand All @@ -95,8 +94,8 @@ public double[] read(TileCoord tileCoord, int size, int buffer) throws TileStore
// Resample and render the image
var gridCoverageProcessor = new GridCoverageProcessor();
gridCoverageProcessor.setInterpolation(new BicubicInterpolation());
var gridCoverage = gridCoverageProcessor.resample(this.gridCoverage, targetGridGeometry);
var renderedImage = gridCoverage.render(null);
var tileGridCoverage = gridCoverageProcessor.resample(this.gridCoverage, targetGridGeometry);
var renderedImage = tileGridCoverage.render(null);

// Convert the image to the terrarium color scale
var imageProcessor = new ImageProcessor();
Expand All @@ -115,7 +114,7 @@ public double[] read(TileCoord tileCoord, int size, int buffer) throws TileStore

return values;
} catch (Exception e) {
throw new TileStoreException(e);
throw new GeoTiffException(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@ public TerrariumTileStore(GeoTiffReader geoTiffReader) {

@Override
public BufferedImage read(TileCoord tileCoord) throws TileStoreException {
var size = 256;
var grid = geoTiffReader.read(tileCoord, size, 0);
var bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
double value = (int) grid[y * size + x];
int rgb = ElevationUtils.elevationToTerrarium(value);
bufferedImage.setRGB(x, y, rgb);
try {
var size = 256;
var grid = geoTiffReader.read(tileCoord, size, 0);
var bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < size; x++) {
for (int y = 0; y < size; y++) {
double value = (int) grid[y * size + x];
int rgb = ElevationUtils.elevationToTerrarium(value);
bufferedImage.setRGB(x, y, rgb);
}
}
return bufferedImage;
} catch (Exception e) {
throw new TileStoreException(e);
}
return bufferedImage;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertNull;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import org.apache.baremaps.maplibre.tilejson.TileJSON;
Expand All @@ -35,10 +34,6 @@ public class TileSetTest {
final ObjectMapper objectMapper = ObjectMapperUtils.objectMapper();
final ConfigReader configReader = new ConfigReader();

private File resourceFile(String path) {
return new File(TileSetTest.class.getResource(path).getFile());
}

@Test
public void testBasemapJSConfig() throws IOException {
var path = Path.of("../basemap/tileset.js");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* Provides methods for generating contour lines and contour polygons from digital elevation models
* (DEMs).
*/
@SuppressWarnings({"squid:S3776", "squid:S135"})
public class ContourTracer {

private static final GeometryFactory GEOMETRY_FACTORY = new GeometryFactory();
Expand Down Expand Up @@ -222,7 +223,6 @@ private static void validateInput(double[] grid, int width, int height) {
* @param y The y-coordinate of the cell
* @return A list of line segments
*/
@SuppressWarnings("squid:S3776")
private List<LineString> processCell(double level, int x, int y) {
List<LineString> segments = new ArrayList<>();

Expand All @@ -244,7 +244,6 @@ private List<LineString> processCell(double level, int x, int y) {
double trv = grid[y * width + (x + 1)];
double brv = grid[(y + 1) * width + (x + 1)];
double blv = grid[(y + 1) * width + x];
double avg = (tlv + trv + brv + blv) / 4.0;

int index =
(tlv >= level ? 1 : 0) |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,12 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.jts.io.ParseException;

class ContourTracerTest {

@Test
@DisplayName("Test grid normalization")
void testGrid1() throws ParseException {
void testGrid1() {
var grid = new double[] {
0, 0, 0,
0, 1, 0,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.nio.file.Path;
import javax.imageio.ImageIO;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class HillShadeRenderer extends JFrame {
Expand Down Expand Up @@ -75,11 +74,9 @@ public HillShadeRenderer() throws IOException {
scaleSlider.setPaintLabels(true);

// Add listeners
ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent e) {
updateLabels();
redrawHillshade();
}
ChangeListener listener = e -> {
updateLabels();
redrawHillshade();
};
altitudeSlider.addChangeListener(listener);
azimuthSlider.addChangeListener(listener);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public static void main(String[] args) {
JFrame frame = new JFrame("Geometry Drawer");

JPanel mainPanel = new JPanel(new GridLayout(4, 4));
// mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
for (double[] c : MarchingSquareUtils.CASES) {

List<Geometry> geometries = trace(c)
Expand Down
26 changes: 13 additions & 13 deletions baremaps-gdal/src/main/java/org/apache/baremaps/gdal/Band.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,42 @@
*/
public class Band implements AutoCloseable {

private final org.gdal.gdal.Band band;
private final org.gdal.gdal.Band gdalBand;

protected Band(org.gdal.gdal.Band band) {
this.band = band;
protected Band(org.gdal.gdal.Band gdalBand) {
this.gdalBand = gdalBand;
}

public int getWidth() {
return band.getXSize();
return gdalBand.getXSize();
}

public int getHeight() {
return band.getYSize();
return gdalBand.getYSize();
}

public void read(int x, int y, int width, int height, byte[] buffer) {
band.ReadRaster(x, y, width, height, buffer);
gdalBand.ReadRaster(x, y, width, height, buffer);
}

public void read(int x, int y, int width, int height, int[] buffer) {
band.ReadRaster(x, y, width, height, buffer);
gdalBand.ReadRaster(x, y, width, height, buffer);
}

public void read(int x, int y, int width, int height, long[] buffer) {
band.ReadRaster(x, y, width, height, buffer);
gdalBand.ReadRaster(x, y, width, height, buffer);
}

public void read(int x, int y, int width, int height, double[] buffer) {
band.ReadRaster(x, y, width, height, buffer);
gdalBand.ReadRaster(x, y, width, height, buffer);
}

public BufferedImage asBufferedImage(int imageType) {
// Copy the data of the band into a byte array
int width = band.getXSize();
int height = band.getYSize();
int width = gdalBand.getXSize();
int height = gdalBand.getYSize();
double[] values = new double[height * width];
band.ReadRaster(0, 0, 256, 256, values);
gdalBand.ReadRaster(0, 0, 256, 256, values);

// Create a BufferedImage from the byte array
BufferedImage image = new BufferedImage(width, height, imageType);
Expand All @@ -77,7 +77,7 @@ public BufferedImage asBufferedImage(int imageType) {

@Override
public void close() throws Exception {
band.delete();
gdalBand.delete();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@
*/
public class Dataset implements AutoCloseable {

protected final org.gdal.gdal.Dataset dataset;
protected final org.gdal.gdal.Dataset gdalDataset;

protected Dataset(org.gdal.gdal.Dataset dataset) {
this.dataset = dataset;
protected Dataset(org.gdal.gdal.Dataset gdalDataset) {
this.gdalDataset = gdalDataset;
}

public Driver getDriver() {
return new Driver(dataset.GetDriver());
return new Driver(gdalDataset.GetDriver());
}

public int getRasterWidth() {
return dataset.getRasterXSize();
return gdalDataset.getRasterXSize();
}

public int getRasterHeight() {
return dataset.getRasterYSize();
return gdalDataset.getRasterYSize();
}

public int getRasterCount() {
return dataset.getRasterCount();
return gdalDataset.getRasterCount();
}

public Band getRasterBand(int index) {
return new Band(dataset.GetRasterBand(index));
return new Band(gdalDataset.GetRasterBand(index));
}

@Override
public void close() throws Exception {
dataset.delete();
gdalDataset.delete();
}
}
Loading

0 comments on commit 812b75e

Please sign in to comment.