Skip to content

Commit

Permalink
Reorganize the project into modules
Browse files Browse the repository at this point in the history
  • Loading branch information
dimitri-justeau committed Oct 7, 2022
1 parent ae2b663 commit d1d3958
Show file tree
Hide file tree
Showing 66 changed files with 888 additions and 356 deletions.
54 changes: 34 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ To avoid typing `java -jar flsgen-1.2.0.jar` each time you need to use flsgen, y
To use the Java API, you can either download flsgen jar file [here](https://github.com/dimitri-justeau/flsgen/releases/tag/1.0b) and add it to your classpath, or clone this Github repository and install flsgen as a local Maven dependency using the following command:

```bash
mvn clean install
mvn clean install -DskipTests
```

<a name="tuto"></a>
Expand Down Expand Up @@ -101,8 +101,9 @@ You will get a fractal tif raster, e.g. :
To achieve the same result with the Java API:

```java
import org.flsgen.grid.regular.square.RegularSquareGrid;
import org.flsgen.solver.Terrain;
import org.flsgen.RegularSquareGrid;
import org.flsgen.Terrain;
import org.flsgen.RasterUtils;
import org.opengis.referencing.FactoryException;

import java.io.IOException;
Expand All @@ -113,7 +114,10 @@ public class FractalTerrainTest {
RegularSquareGrid grid = new RegularSquareGrid(200, 200);
Terrain t = new Terrain(grid);
t.generateDiamondSquare(0.4);
t.exportRaster(0, 0, 0.01, "EPSG:4326", "terrain.tif");
RasterUtils.exportDoubleRaster(
t.getData(), t.getGrid(),
0, 0, 0.01, "EPSG:4326", "terrain.tif"
);
}
}
```
Expand Down Expand Up @@ -201,19 +205,23 @@ If there exist a landscape structure satisfying these targets, the program will
To achieve the same result with the Java API:
```java
import org.flsgen.solver.LandscapeStructureSolver;
import org.flsgen.solver.LandscapeStructure;
import org.flsgen.LandscapeStructureSolver;
import org.flsgen.LandscapeStructure;
import org.flsgen.FlsgenException;
import org.flsgen.solver.*;
import com.github.cliftonlabs.json_simple.JsonException;
import org.flsgen.exception.FlsgenException;
import org.apache.commons.io.IOUtils;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class StructureTest {

public static void main(String[] args) throws IOException, JsonException, FlsgenException { {
LandscapeStructureSolver ls = LandscapeStructureSolver.readFromJSON(new FileReader("target.json"));
public static void main(String[] args) throws IOException, JsonException, FlsgenException {
LandscapeStructureSolver ls = LandscapeStructureSolverFactory.readFromJSON(
IOUtils.toString(new FileReader("target.json"))
);
ls.build();
LandscapeStructure struct = ls.findSolution();
FileWriter writer = new FileWriter("struct_target.json");
Expand Down Expand Up @@ -308,30 +316,36 @@ To achieve the same with the Java API:
```java
import com.github.cliftonlabs.json_simple.JsonException;
import org.flsgen.exception.FlsgenException;
import org.flsgen.grid.neighborhood.INeighborhood;
import org.flsgen.grid.neighborhood.Neighborhoods;
import org.flsgen.grid.regular.square.RegularSquareGrid;
import org.flsgen.solver.LandscapeStructure;
import org.flsgen.solver.LandscapeGenerator
import org.flsgen.solver.Terrain;;
import ors.flsgen.RasterUtils;
import org.flsgen.FlsgenException;
import org.flsgen.INeighborhood;
import org.flsgen.Neighborhoods;
import org.flsgen.RegularSquareGrid;
import org.flsgen.LandscapeStructure;
import org.flsgen.LandscapeGenerator;
import org.flsgen.Terrain;
import org.opengis.referencing.FactoryException;
import org.apache.commons.io.IOUtils;
import java.io.FileReader;
import java.io.IOException;
public class GenerateTest {
public static void main(String[] args) throws IOException, JsonException, FlsgenException, FactoryException {
LandscapeStructure structure = LandscapeStructure.fromJSON(new FileReader("struct_target.json"));
LandscapeStructure structure = LandscapeStructureSolverFactory.fromJSON(
IOUtils.toString(new FileReader("struct_target.json"))
);
RegularSquareGrid grid = new RegularSquareGrid(200, 200);
Terrain terrain = new Terrain(grid);
terrain.loadFromRaster("terrain.tif");
double[] terrainData = RasterUtils.loadDoubleDataFromRaster("terrain.tif", terrain.getGrid());
terrain.loadFromData(terrainData);
INeighborhood neighborhood = Neighborhoods.FOUR_CONNECTED;
INeighborhood distance = Neighborhoods.TWO_WIDE_FOUR_CONNECTED;
LandscapeGenerator generator = new LandscapeGenerator(structure, neighborhood, distance, terrain);
generator.generate(0.5, 10,10);
generator.exportRaster(0, 0, 0.001, "EPSG:4326", "landscape_struct_target.tif");
int[] rasterData = landscapeGenerator.getRasterData(noDataValue);
generator.exportRaster(rasterData, generator.getGrid(), 0, 0, 0.001, "EPSG:4326", "landscape_struct_target.tif");
}
}
```
Expand All @@ -342,7 +356,7 @@ It is possible to use a mask raster, whose NO_DATA cell will be unavailable for
```json
{
"maskRasterPath": "mask.tif"
"maskRasterPath": "mask.tif",
"classes" : [
{
"name" : "Class A",
Expand Down
138 changes: 138 additions & 0 deletions flsgen-cli/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2021, Dimitri Justeau-Allaire
~
~ Institut Agronomique neo-Caledonien (IAC), 98800 Noumea, New Caledonia
~ AMAP, Univ Montpellier, CIRAD, CNRS, INRA, IRD, Montpellier, France
~
~ This file is part of flsgen.
~
~ flsgen is free software: you can redistribute it and/or modify
~ it under the terms of the GNU General Public License as published by
~ the Free Software Foundation, either version 3 of the License, or
~ (at your option) any later version.
~
~ flsgen is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of
~ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
~ GNU General Public License for more details.
~
~ You should have received a copy of the GNU General Public License
~ along with flsgen. If not, see <https://www.gnu.org/licenses/>.
-->

<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/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>flsgen</artifactId>
<groupId>org.flsgen</groupId>
<version>1.2.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>flsgen-cli</artifactId>

<licenses>
<license>
<name>GNU GENERAL PUBLIC LICENSE, Version 3</name>
<url>https://www.gnu.org/licenses/gpl-3.0.html</url>
<distribution>repo</distribution>
</license>
</licenses>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.2</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<manifestEntries>
<Main-Class>org.flsgen.cli.Main</Main-Class>
<Specification-Title>flsgen</Specification-Title>
<Specification-Version>${project.version}</Specification-Version>
<Specification-Vendor>IAC/UMR AMAP</Specification-Vendor>
<Implementation-Vendor>IAC/UMR AMAP</Implementation-Vendor>
<Implementation-Title>${project.artifactId}</Implementation-Title>
<Implementation-Version>${project.version}</Implementation-Version>
<Implementation-Vendor-Id>${project.groupId}</Implementation-Vendor-Id>
</manifestEntries>
</transformer>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
</transformers>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
</execution>
</executions>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<path>
<groupId>info.picocli</groupId>
<artifactId>picocli-codegen</artifactId>
<version>4.5.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.2.0</version>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
<version>4.5.0</version>
</dependency>
<dependency>
<groupId>org.flsgen</groupId>
<artifactId>flsgen-raster</artifactId>
<version>1.2.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>

<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>

</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@

package org.flsgen.cli;

import org.flsgen.RasterUtils;
import org.flsgen.grid.regular.square.RegularSquareGrid;
import org.flsgen.solver.Terrain;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import org.flsgen.utils.ANSIColors;
import picocli.CommandLine;

import java.io.File;
import java.io.IOException;

@CommandLine.Command(
Expand Down Expand Up @@ -112,21 +111,17 @@ public void run() {
RegularSquareGrid grid = new RegularSquareGrid(nbRows, nbCols);
Terrain terrain = new Terrain(grid);
terrain.generateDiamondSquare(roughnessFactor);
terrain.exportRaster(x, y, resolution, srs, output);
RasterUtils.exportDoubleRaster(terrain.getData(), grid, x, y, resolution, srs, output);
} catch (Exception e) {
e.printStackTrace();
}
}

public void initRasterMetadataFromTemplate(String input) throws IOException {
File file = new File(input);
GeoTiffReader reader = new GeoTiffReader(file);
GridCoverage2D gridCov = reader.read(null);
resolution = gridCov.getEnvelope2D().getHeight() / gridCov.getRenderedImage().getHeight();
srs = gridCov.getEnvelope2D().getCoordinateReferenceSystem().getIdentifiers().iterator().next().toString();
x = gridCov.getEnvelope2D().getMinX();
y = gridCov.getEnvelope2D().getMinY();
gridCov.dispose(true);
reader.dispose();
srs = RasterUtils.getSrs(input);
double[] xyres = RasterUtils.getXYRes(input);
x = xyres[0];
y = xyres[1];
resolution = xyres[2];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@
*/

package org.flsgen.cli;

import org.apache.commons.io.FilenameUtils;

import org.apache.commons.io.IOUtils;
import org.flsgen.RasterUtils;
import org.flsgen.grid.neighborhood.INeighborhood;
import org.flsgen.grid.neighborhood.Neighborhoods;
import org.flsgen.grid.regular.square.RegularSquareGrid;
import org.flsgen.solver.LandscapeGenerator;
import org.flsgen.solver.LandscapeStructure;
import org.flsgen.solver.LandscapeStructureFactory;
import org.flsgen.solver.Terrain;
import org.flsgen.utils.ANSIColors;
import org.geotools.coverage.grid.GridCoverage2D;
import org.geotools.gce.geotiff.GeoTiffReader;
import picocli.CommandLine;
Expand Down Expand Up @@ -213,14 +217,15 @@ public void run() {
} else {
reader = new FileReader(jsonPaths[i]);
}
LandscapeStructure s = LandscapeStructure.fromJSON(reader);
LandscapeStructure s = LandscapeStructureFactory.readFromJSON(IOUtils.toString(reader));
reader.close();
// Generate landscape
Terrain terrain = new Terrain(new RegularSquareGrid(s.getNbRows(), s.getNbCols()));
if (terrainInput.equals("")) {
terrain.generateDiamondSquare(roughnessFactor);
} else {
terrain.loadFromRaster(terrainInput);
double[] rasterData = RasterUtils.loadDoubleDataFromRaster(terrainInput, terrain.getGrid());
terrain.loadFromData(rasterData);
}
INeighborhood bufferNeighborhood;
if (maxMinDistance > 1) {
Expand Down Expand Up @@ -250,7 +255,12 @@ public void run() {
System.out.println("FAIL");
} else {
System.out.println("Feasible landscape found after " + landscapeGenerator.getNbTry() + " tries");
landscapeGenerator.exportRaster(x, y, resolution, srs, outputPrefix + "_" + structNames[i] + ".tif");
int noDataValue = -1;
if (s.getMaskRasterPath() != null) {
noDataValue = (int) RasterUtils.getNodataValue(s.getMaskRasterPath());
}
int[] rasterData = landscapeGenerator.getRasterData(noDataValue);
RasterUtils.exportIntRaster(rasterData, landscapeGenerator.getGrid(), x, y, resolution, srs, outputPrefix + "_" + structNames[i] + ".tif");
}
} else { // Several landscapes case
int n = 0;
Expand All @@ -260,14 +270,20 @@ public void run() {
System.out.println("Failed to generate landscape " + (n + 1));
} else {
System.out.println("Feasible landscape " + (n + 1) + " found after " + landscapeGenerator.getNbTry() + " tries");
landscapeGenerator.exportRaster(x, y, resolution, srs, outputPrefix + "_" + structNames[i] + "_" + (n + 1) + ".tif");
int noDataValue = -1;
if (s.getMaskRasterPath() != null) {
noDataValue = (int) RasterUtils.getNodataValue(s.getMaskRasterPath());
}
int[] rasterData = landscapeGenerator.getRasterData(noDataValue);
RasterUtils.exportIntRaster(rasterData, landscapeGenerator.getGrid(), x, y, resolution, srs, outputPrefix + "_" + structNames[i] + "_" + (n + 1) + ".tif");
}
n++;
landscapeGenerator.init();
}
}
if (!terrainOutput.equals("")) {
landscapeGenerator.getTerrain().exportRaster(x, y, resolution, srs, terrainOutput);
int[] rasterData = landscapeGenerator.getRasterData((int) RasterUtils.getNodataValue(s.getMaskRasterPath()));
RasterUtils.exportIntRaster(rasterData, landscapeGenerator.getGrid(), x, y, resolution, srs, terrainOutput);
}
}
} catch (Exception e) {
Expand Down
Loading

0 comments on commit d1d3958

Please sign in to comment.