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

Eric Duminil's avatar
Eric Duminil committed
3
import static org.junit.jupiter.api.Assertions.assertEquals;
4
import static org.junit.jupiter.api.Assertions.assertFalse;
Eric Duminil's avatar
Eric Duminil committed
5
import static org.junit.jupiter.api.Assertions.assertTrue;
6
7
import static org.junit.jupiter.api.Assertions.fail;

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


public class ConvexHullCalculatorTests
{
	private static final GeometryFactory gf = new GeometryFactory();
Matthias Betz's avatar
Matthias Betz committed
27
	private static final Path repository = Paths.get("src/test/resources/testdata");
28
	private static final Path notExistingRepository = Paths.get("src/test/resources/surely_not_here");
29
30

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

	@Test
40
41
	public void testExtractConvexHullFromOneSmallRegion() throws IOException, XPathParseException {
		Path citygmlPath = repository.resolve("Gruenbuehl.proj/Gruenbuehl_LOD2_ALKIS_1010.gml");
42
43
44
45
		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
46
		assertTrue(hull.contains(somewhereBetweenBuildings), "Hull should contain region between buildings");
47
48
49
	}

	@Test
50
51
	public void testExtractConvexHullFromStoeckachNoBuildingPart() throws IOException, XPathParseException {
		Path citygmlPath = repository.resolve("Stuttgart.proj/Stöckach_überarbeitete GML-NoBuildingPart.gml");
52
53
54
		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
55
		assertTrue(hull.contains(somewhereBetweenBuildings), "Hull should contain region between buildings");
56
57
58
	}

	@Test
59
60
	public void testExtractConvexHullFromEveryCitygmlInRepository() throws IOException {
		int minHullCount = 6;
61
62
63
64
65
66
67
		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();
68
69
		AtomicInteger hullCount = new AtomicInteger(0);
		ConvexHullCalculator.extractHullsForEveryCityGML(repository, kmlHull -> {
Eric Duminil's avatar
Eric Duminil committed
70
71
72
73
			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");
74
75
			hullCount.getAndIncrement();
		});
Eric Duminil's avatar
Eric Duminil committed
76
77
		assertTrue(gmlCount >= minHullCount, "At least " + minHullCount + " citygmls should be present in repository");
		assertTrue(hullCount.get() >= minHullCount, "At least " + minHullCount + " hulls should have been calculated");
78
	}
79
80
81
82
83
84
85
86

	@Test
	public void testDontDoMuchWithBrokenRepo() throws IOException {
		ConvexHullCalculator.extractHullsForEveryCityGML(notExistingRepository, kmlHull -> {
			fail("I really shouldn't be called for any gml.");
		});
		assertFalse(Files.exists(notExistingRepository));
	}
Eric Duminil's avatar
todo    
Eric Duminil committed
87
88

	//TODO: Test extract in wrong folder.
89
}