Skip to content

Commit

Permalink
Removing geotools dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitri-justeau committed Oct 6, 2022
1 parent 9a7d167 commit ae2b663
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Java 8+ must be installed in your system to run flsgen. For the command-line int
The most straightforward way to use fslgen is the command-line interface (CLI). Once Java is installed on your system, the only thing to do is to download the CLI jar file [here](https://github.com/dimitri-justeau/flsgen/releases/download/v1.1.0/flsgen-1.1.0.jar). To test that everything is working properly, from a terminal navigate to the folder where you downloaded the jar file and use the following command:

```bash
java -jar flsgen-1.1.0.jar
java -jar flsgen-1.2.0.jar
```

You should get the following output:
Expand All @@ -40,7 +40,7 @@ Commands:
terrain Generate a fractal terrain using the Diamond-Square algorithm.
```

To avoid typing `java -jar flsgen-1.1.0.jar` each time you need to use flsgen, you can create the following alias: `alias flsgen='java -jar flsgen-1.1.0.jar'` in your `.bashrc` or `.profile` file (in Linux). The following examples assume that this step has been done. If you did not create the alias, simply replace `flsgen` by `java -jar flsgen-1.1.0.jar`.
To avoid typing `java -jar flsgen-1.2.0.jar` each time you need to use flsgen, you can create the following alias: `alias flsgen='java -jar flsgen-1.2.0.jar'` in your `.bashrc` or `.profile` file (in Linux). The following examples assume that this step has been done. If you did not create the alias, simply replace `flsgen` by `java -jar flsgen-1.2.0.jar`.

### Java API

Expand Down
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>org.flsgen</groupId>
<artifactId>flsgen</artifactId>
<version>1.1.0-SNAPSHOT</version>
<version>1.2.0-SNAPSHOT</version>

<licenses>
<license>
Expand Down Expand Up @@ -101,6 +101,7 @@
<goal>shade</goal>
</goals>
<configuration>
<!-- <minimizeJar>true</minimizeJar> -->
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
Expand Down
32 changes: 18 additions & 14 deletions src/main/java/org/flsgen/solver/LandscapeGenerator.java
Original file line number Diff line number Diff line change
Expand Up @@ -517,20 +517,7 @@ public void exportRaster(double x, double y, double resolution_x, double resolut
y - (grid.getNbRows() * resolution_y), y,
crs
);
int[] data;
if (grid instanceof PartialRegularSquareGrid) {
int noDataValue = (int) CheckLandscape.getNodataValue(structure.maskRasterPath);
data = new int[grid.getNbCols() * grid.getNbRows()];
for (int i = 0; i < data.length; i++) {
if (!((PartialRegularSquareGrid) grid).getDiscardSet().contains(i)) {
data[i] = rasterGrid[((PartialRegularSquareGrid) grid).getPartialIndex(i)];
} else {
data[i] = noDataValue;
}
}
} else {
data = IntStream.range(0, this.grid.getNbCells()).map(i -> rasterGrid[i]).toArray();
}
int[] data = getRasterData((int) CheckLandscape.getNodataValue(structure.maskRasterPath));
WritableRaster rast = RasterFactory.createBandedRaster(
DataBuffer.TYPE_INT,
grid.getNbCols(), grid.getNbRows(),
Expand All @@ -549,6 +536,23 @@ public void exportRaster(double x, double y, double resolution, String epsg, Str
exportRaster(x, y, resolution, resolution, epsg, dest);
}

public int[] getRasterData(int noDataValue) {
int[] data;
if (grid instanceof PartialRegularSquareGrid) {
data = new int[grid.getNbCols() * grid.getNbRows()];
for (int i = 0; i < data.length; i++) {
if (!((PartialRegularSquareGrid) grid).getDiscardSet().contains(i)) {
data[i] = rasterGrid[((PartialRegularSquareGrid) grid).getPartialIndex(i)];
} else {
data[i] = noDataValue;
}
}
} else {
data = IntStream.range(0, this.grid.getNbCells()).map(i -> rasterGrid[i]).toArray();
}
return data;
}

/**
* Landscape generation main algorithm
* @param terrainDependency the terrain dependency, between 0 (no terrain dependency) and 1 (only guided by terrain)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/flsgen/solver/LandscapeStructure.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ public String toJSON() {
cl.put(LandscapeStructureSolver.KEY_PD, getPatchDensity(i));
cl.put(LandscapeStructureSolver.KEY_SPI, getSmallestPatchIndex(i));
cl.put(LandscapeStructureSolver.KEY_LPI, getLargestPatchIndex(i));
cl.put(LandscapeStructureSolver.IS_SQUARE, this.isSquare[i]);
classes.add(cl);
}
json.put("classes", classes);
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/flsgen/solver/Terrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import java.awt.image.WritableRaster;
import java.io.File;
import java.io.IOException;
import java.security.SecureRandom;
import java.util.Random;
import java.util.stream.IntStream;

Expand Down Expand Up @@ -68,6 +67,13 @@ public void loadFromRaster(String rasterPath) throws IOException, FlsgenExceptio
reader.dispose();
}

public void loadFromData(double[] data, int nbRows, int nbCols) throws IOException, FlsgenException {
if (nbRows != grid.getNbRows() || nbCols != grid.getNbCols()) {
throw new FlsgenException("Input terrain raster must have the same dimensions as the landscape to generate");
}
dem = data;
}

public void generateDiamondSquare(double roughnessFactor) {
// Get the smallest power of 2 greater than of equal to the largest landscape dimension
int h = Math.max(grid.getNbRows(), grid.getNbCols());
Expand Down Expand Up @@ -159,4 +165,7 @@ public void exportRaster(double x, double y, double resolution, String epsg, Str
exportRaster(x, y, resolution, resolution, epsg, dest);
}

public double[] getData() {
return dem;
}
}

0 comments on commit ae2b663

Please sign in to comment.