ConvexHullCalculatorTests.java 3.91 KB
Newer Older
1
package eu.simstadt.regionchooser.fast_xml_parser;
2

Eric Duminil's avatar
Eric Duminil committed
3
4
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
5
import java.io.File;
6
import java.io.IOException;
7
import java.nio.file.Files;
8
9
import java.nio.file.Path;
import java.nio.file.Paths;
10
import java.util.Comparator;
11
import java.util.concurrent.atomic.AtomicInteger;
Eric Duminil's avatar
Eric Duminil committed
12
import org.junit.jupiter.api.Test;
13
14
15
16
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;
17
import com.ximpleware.XPathParseException;
18
import eu.simstadt.regionchooser.RegionChooserUtils;
19
20
21
22
23


public class ConvexHullCalculatorTests
{
	private static final GeometryFactory gf = new GeometryFactory();
Matthias Betz's avatar
Matthias Betz committed
24
	private static final Path repository = Paths.get("src/test/resources/testdata");
25
26

	@Test
27
28
	public void testExtractConvexHullFromOneBuilding() throws IOException, XPathParseException {
		Path citygmlPath = repository.resolve("Gruenbuehl.proj/20140218_Gruenbuehl_LOD2_1building.gml");
29
		Geometry hull = ConvexHullCalculator.calculateFromCityGML(citygmlPath);
Eric Duminil's avatar
Eric Duminil committed
30
		assertEquals(4 + 1, hull.getCoordinates().length); // Convex hull of a building should be a closed rectangle
31
		Point someBuildingPoint = gf.createPoint(new Coordinate(9.216845, 48.878196)); // WGS84
Eric Duminil's avatar
Eric Duminil committed
32
		assertTrue(hull.contains(someBuildingPoint), "Hull should contain every building point");
33
34
35
	}

	@Test
36
37
	public void testExtractConvexHullFromOneSmallRegion() throws IOException, XPathParseException {
		Path citygmlPath = repository.resolve("Gruenbuehl.proj/Gruenbuehl_LOD2_ALKIS_1010.gml");
38
39
40
41
		Geometry hull = ConvexHullCalculator.calculateFromCityGML(citygmlPath);
		assertTrue(hull.getCoordinates().length > 4); // Convex hull should have at least 4 corners
		//		Point somewhereBetweenBuildings = gf.createPoint(new Coordinate(3515883.6668538367, 5415843.300640578)); // Original coordinates, GSK3
		Point somewhereBetweenBuildings = gf.createPoint(new Coordinate(9.21552249084, 48.87980446)); // WGS84
Eric Duminil's avatar
Eric Duminil committed
42
		assertTrue(hull.contains(somewhereBetweenBuildings), "Hull should contain region between buildings");
43
44
45
	}

	@Test
46
47
	public void testExtractConvexHullFromStoeckachNoBuildingPart() throws IOException, XPathParseException {
		Path citygmlPath = repository.resolve("Stuttgart.proj/Stöckach_überarbeitete GML-NoBuildingPart.gml");
48
49
50
		Geometry hull = ConvexHullCalculator.calculateFromCityGML(citygmlPath);
		assertTrue(hull.getCoordinates().length > 4); // Convex hull should have at least 4 corners
		Point somewhereBetweenBuildings = gf.createPoint(new Coordinate(9.195212, 48.789062)); // WGS84
Eric Duminil's avatar
Eric Duminil committed
51
		assertTrue(hull.contains(somewhereBetweenBuildings), "Hull should contain region between buildings");
52
53
54
	}

	@Test
55
56
	public void testExtractConvexHullFromEveryCitygmlInRepository() throws IOException {
		int minHullCount = 6;
57
58
59
60
61
62
63
		Path cachePath = repository.resolve(".cache/hulls");
		if (Files.exists(cachePath)) {
			// In order to make sure that hulls are calculated during the test, not just read from Cache
			Files.walk(cachePath).sorted(Comparator.reverseOrder()).map(Path::toFile).forEach(File::delete);
		}

		long gmlCount = RegionChooserUtils.everyCityGML(repository).count();
64
65
		AtomicInteger hullCount = new AtomicInteger(0);
		ConvexHullCalculator.extractHullsForEveryCityGML(repository, kmlHull -> {
Eric Duminil's avatar
Eric Duminil committed
66
67
68
69
			assertTrue(kmlHull.contains("Data name=\"project\""), "KML hull should contain project name");
			assertTrue(kmlHull.contains("Data name=\"srsName\""), "KML hull should contain srs name");
			assertTrue(kmlHull.contains("<value>EPSG:"), "KML hull should contain epsg id");
			assertTrue(kmlHull.contains("<coordinates>"), "KML hull should contain coordinates");
70
71
			hullCount.getAndIncrement();
		});
Eric Duminil's avatar
Eric Duminil committed
72
73
		assertTrue(gmlCount >= minHullCount, "At least " + minHullCount + " citygmls should be present in repository");
		assertTrue(hullCount.get() >= minHullCount, "At least " + minHullCount + " hulls should have been calculated");
74
75
	}
}