Skip to content

Commit

Permalink
Make geotools test-only dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitri-justeau committed Apr 12, 2022
1 parent c76937f commit 2b4fb1d
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 149 deletions.
10 changes: 4 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
<goal>shade</goal>
</goals>
<configuration>
<minimizeJar>true</minimizeJar>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
Expand Down Expand Up @@ -126,22 +127,19 @@
<groupId>org.geotools</groupId>
<artifactId>gt-epsg-hsql</artifactId>
<version>${geotools.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.geotools</groupId>
<artifactId>gt-geotiff</artifactId>
<version>${geotools.version}</version>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.5.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
<scope>test</scope>
</dependency>
</dependencies>
</project>
53 changes: 7 additions & 46 deletions src/main/java/org/restopt/RestoptSolution.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package org.restopt;

import org.chocosolver.solver.Model;
import org.chocosolver.solver.Solution;
import org.chocosolver.util.objects.graphs.GraphFactory;
import org.chocosolver.util.objects.graphs.UndirectedGraph;
import org.chocosolver.util.objects.setDataStructures.ISet;
import org.chocosolver.util.objects.setDataStructures.SetFactory;
import org.chocosolver.util.objects.setDataStructures.SetType;
import org.chocosolver.util.tools.ArrayUtils;
import org.restopt.choco.ConnectivityFinderSpatialGraph;
import org.restopt.choco.LandscapeIndicesUtils;
import org.restopt.exception.RestoptException;
import org.restopt.grid.regular.square.PartialRegularGroupedGrid;
import org.restopt.objectives.AbstractRestoptObjective;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.stream.IntStream;
Expand All @@ -37,6 +32,7 @@ public class RestoptSolution {
};

private final RestoptProblem problem;

private final AbstractRestoptObjective objective;
private final Solution solution;
private final Map<String, String> characteristics;
Expand Down Expand Up @@ -66,7 +62,6 @@ public void initMessages() {
}

private Map<String, String> makeCharacteristics() {
Model model = problem.getModel();
Map<String, String> solCharacteristics = new HashMap<>();
solCharacteristics.put(KEY_MIN_RESTORE, String.valueOf(getMinRestoreArea()));
solCharacteristics.put(KEY_TOTAL_RESTORABLE, String.valueOf(getTotalRestorableArea()));
Expand Down Expand Up @@ -118,46 +113,12 @@ public void printSolutionInfos() {
System.out.println();
}

public void export(String outputPath, boolean verbose) throws IOException, RestoptException {
if (problem.getData() instanceof RasterDataLoader) {
RasterDataLoader dataLoader = (RasterDataLoader) problem.getData();
PartialRegularGroupedGrid grid = problem.getGrid();
int[] sites = new int[grid.getNbUngroupedCells()];
ISet set = SetFactory.makeConstantSet(solution.getSetVal(problem.getRestoreSetVar()));
for (int i = 0; i < grid.getNbUngroupedCells(); i++) {
if (grid.getGroupIndexFromPartialIndex(i) < grid.getNbGroups()) {
sites[i] = 2;
} else if (set.contains(grid.getGroupIndexFromPartialIndex(i))) {
sites[i] = 3;
} else {
sites[i] = 1;
}
}
SolutionExporter exporter = new SolutionExporter(
problem,
sites,
dataLoader.getHabitatRasterPath(),
outputPath + ".csv",
outputPath + ".tif",
problem.getData().getNoDataValue()
);
String[][] orderedCharacteristics = new String[2][];
String[] allKeys = ArrayUtils.append(KEYS, objective.getAdditionalKeys());
orderedCharacteristics[0] = allKeys;
orderedCharacteristics[1] = new String[allKeys.length];
for (int i = 0; i < allKeys.length; i++) {
orderedCharacteristics[1][i] = characteristics.get(allKeys[i]);
}
exporter.exportCharacteristics(orderedCharacteristics);
exporter.generateRaster();
if (verbose) {
printSolutionInfos();
System.out.println("\nRaster exported at " + outputPath + ".tif");
System.out.println("Solution characteristics exported at " + outputPath + ".csv\n");
}
} else {
throw new RestoptException("The export function can only be used if the data was loaded from a raster");
}
public AbstractRestoptObjective getObjective() {
return objective;
}

public RestoptProblem getProblem() {
return problem;
}

/**
Expand Down
85 changes: 0 additions & 85 deletions src/main/java/org/restopt/SolutionExporter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import org.chocosolver.solver.Solution;
import org.chocosolver.solver.constraints.Constraint;
import org.restopt.DataLoader;
import org.restopt.RasterDataLoader;
import org.restopt.RestoptProblem;
import org.restopt.choco.LandscapeIndicesUtils;
import org.restopt.choco.PropEffectiveMeshSize;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.restopt;

import org.restopt.DataLoader;
import org.restopt.exception.RestoptException;
import org.restopt.raster.RasterReader;

Expand Down
120 changes: 120 additions & 0 deletions src/test/java/org/restopt/SolutionExporter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package org.restopt;

import org.chocosolver.util.objects.setDataStructures.ISet;
import org.chocosolver.util.objects.setDataStructures.SetFactory;
import org.chocosolver.util.tools.ArrayUtils;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.coverage.grid.GridCoverageFactory;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.geotools.gce.geotiff.GeoTiffWriter;
import org.restopt.exception.RestoptException;
import org.restopt.grid.regular.square.PartialRegularGroupedGrid;

import java.awt.*;
import java.awt.image.*;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class SolutionExporter {

public String csvDest;
public String rastDest;
public String template;
public int[] sites;
public int[] completeData;
public RestoptProblem restoptProblem;
public RestoptSolution solution;

public SolutionExporter(RestoptSolution solution, String csvDest, String rastDest, double noDataValue) {
this.solution = solution;
this.restoptProblem = solution.getProblem();
if (restoptProblem.getData() instanceof RasterDataLoader) {
RasterDataLoader dataLoader = (RasterDataLoader) restoptProblem.getData();
PartialRegularGroupedGrid grid = restoptProblem.getGrid();
sites = new int[grid.getNbUngroupedCells()];
ISet set = SetFactory.makeConstantSet(solution.getRestorationPlanningUnits());
for (int i = 0; i < grid.getNbUngroupedCells(); i++) {
if (grid.getGroupIndexFromPartialIndex(i) < grid.getNbGroups()) {
sites[i] = 2;
} else if (set.contains(grid.getGroupIndexFromPartialIndex(i))) {
sites[i] = 3;
} else {
sites[i] = 1;
}
}
this.template = dataLoader.getHabitatRasterPath();
this.csvDest = csvDest;
this.rastDest = rastDest;
completeData = new int[grid.getNbRows() * grid.getNbCols()];
for (int i = 0; i < completeData.length; i++) {
if (grid.getDiscardSet().contains(i)) {
if (restoptProblem.data.getHabitatData()[i] == 0) {
completeData[i] = 0;
} else {
completeData[i] = (int) noDataValue;
}
} else {
completeData[i] = sites[grid.getPartialIndex(i)];
}
}
}
}

public void exportCharacteristics() throws IOException {
String[][] orderedCharacteristics = new String[2][];
String[] allKeys = ArrayUtils.append(solution.KEYS, solution.getObjective().getAdditionalKeys());
orderedCharacteristics[0] = allKeys;
orderedCharacteristics[1] = new String[allKeys.length];
for (int i = 0; i < allKeys.length; i++) {
orderedCharacteristics[1][i] = solution.getCharacteristics().get(allKeys[i]);
}
BufferedWriter br = new BufferedWriter(new FileWriter(csvDest));
StringBuilder sb = new StringBuilder();
for (String[] line : orderedCharacteristics) {
int i = 0;
for (String s : line) {
i++;
sb.append(s);
if (i < line.length) {
sb.append(",");
}
}
sb.append("\n");
}
br.write(sb.toString());
br.close();
}

public void generateRaster() throws IOException {
File file = new File(template);
GeoTiffReader reader = new GeoTiffReader(file);
GridCoverage2D grid = reader.read(null);
int height = grid.getRenderedImage().getHeight();
int width = grid.getRenderedImage().getWidth();

GeoTiffWriter writer = new GeoTiffWriter(new File(rastDest));

DataBuffer buff = grid.getRenderedImage().getData().getDataBuffer();
SampleModel sm = new BandedSampleModel(DataBuffer.TYPE_INT, width, height, 1);
WritableRaster rast = Raster.createWritableRaster(sm, buff, new Point(0, 0));
rast.setPixels(0, 0, width, height, completeData);

GridCoverageFactory f = new GridCoverageFactory();
GridCoverage2D destCov = f.create("rast", rast, grid.getEnvelope());

writer.write(destCov, null);
}

public void export(boolean verbose) throws IOException, RestoptException {
exportCharacteristics();
generateRaster();
if (verbose) {
this.solution.printSolutionInfos();
System.out.println("\nRaster exported at " + rastDest + ".tif");
System.out.println("Solution characteristics exported at " + csvDest + ".csv\n");
}
}
}
11 changes: 0 additions & 11 deletions src/test/java/org/restopt/TestSolve.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import org.testng.annotations.Test;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

public class TestSolve {

Expand Down Expand Up @@ -35,10 +33,7 @@ public void testSolveMESH2() throws IOException, RestoptException {
restoptProblem.postNbComponentsConstraint(1, 1);
restoptProblem.postCompactnessConstraint(6);
restoptProblem.postRestorableConstraint(90 * 11, 110 * 11, 0.7);
Path temp = Files.createTempFile("test_mesh2", "");
RestoptSolution sol = restoptProblem.maximizeMESH(3, 30, true);
sol.export(temp.toString(), true);
Files.delete(temp);
}

@Test
Expand All @@ -52,10 +47,7 @@ public void testSolveIIC() throws IOException, RestoptException {
restoptProblem.postNbComponentsConstraint(1, 1);
restoptProblem.postCompactnessConstraint(6);
restoptProblem.postRestorableConstraint(90 * 11, 110 * 11, 0.7);
Path temp = Files.createTempFile("test_iic", "");
RestoptSolution sol = restoptProblem.maximizeIIC(3, 1, 0, true);
sol.export(temp.toString(), true);
Files.delete(temp);
}

@Test
Expand All @@ -69,9 +61,6 @@ public void testSolveIIC2() throws IOException, RestoptException {
restoptProblem.postNbComponentsConstraint(1, 1);
restoptProblem.postCompactnessConstraint(6);
restoptProblem.postRestorableConstraint(90 * 11, 110 * 11, 0.7);
Path temp = Files.createTempFile("test_iic2", "");
RestoptSolution sol = restoptProblem.maximizeIIC(3, 1, 0, true);
sol.export(temp.toString(), true);
Files.delete(temp);
}
}

0 comments on commit 2b4fb1d

Please sign in to comment.