RegionExtractorTests.java 10.9 KB
Newer Older
1
package eu.simstadt.regionchooser;
2

Eric Duminil's avatar
Eric Duminil committed
3
4
5
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Eric Duminil's avatar
Eric Duminil committed
6
import java.io.StringWriter;
7
8
import java.nio.file.Path;
import java.nio.file.Paths;
duminil's avatar
duminil committed
9
10
import java.util.regex.Matcher;
import java.util.regex.Pattern;
Eric Duminil's avatar
Eric Duminil committed
11
import org.junit.jupiter.api.Test;
12
13


14
class RegionExtractorTests
15
{
16
	private static final String EPSG_32118 = "EPSG:32118";
Eric Duminil's avatar
Eric Duminil committed
17
18
19
20
21
	private static final String CITY_OBJECT_MEMBER_REGEX = "<(core:)?cityObjectMember";
	private static final String CITY_MODEL_HEADER = "<CityModel";
	private static final String CORE_CITY_MODEL_HEADER = "<core:CityModel";
	private static final String CITY_MODEL_FOOTER = "</CityModel";
	private static final String CORE_CITY_MODEL_FOOTER = "</core:CityModel";
Matthias Betz's avatar
Matthias Betz committed
22
	private static final Path TEST_REPOSITORY = Paths.get("src/test/resources/testdata/");
Eric Duminil's avatar
Eric Duminil committed
23

24
	static int countRegexMatches(String str, String subStr) {
duminil's avatar
duminil committed
25
26
27
28
29
30
31
		Pattern pattern = Pattern.compile(subStr);
		Matcher matcher = pattern.matcher(str);
		int count = 0;
		while (matcher.find()) {
			count++;
		}
		return count;
32
33
34
	}

	@Test
35
	void testExtract3BuildingsFromGSK3Model() throws Throwable {
36
37
		//NOTE: Small region around Martinskirche in Grünbühl
		String wktPolygon = "POLYGON((3515848.896028535 5415823.108586172,3515848.9512289143 5415803.590347393,3515829.0815150724 5415803.338023346,3515830.9784850604 5415793.437034622,3515842.0946056456 5415793.272282251,3515843.3515515197 5415766.204935087,3515864.1064344468 5415766.557899496,3515876.489172751 5415805.433782301,3515876.343844858 5415822.009293416,3515848.896028535 5415823.108586172))";
38
		Path citygmlPath = TEST_REPOSITORY.resolve("Gruenbuehl.proj/20140218_Gruenbuehl_LOD2.gml");
Eric Duminil's avatar
Eric Duminil committed
39
40
41
42
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, "EPSG:31467", gmlWriter, citygmlPath);
		String churchGMLString = gmlWriter.toString();
		assertEquals(3, count);
Eric Duminil's avatar
Eric Duminil committed
43
		assertEquals(3, countRegexMatches(churchGMLString, CITY_OBJECT_MEMBER_REGEX));
44
45
46
47
		assertTrue(churchGMLString.contains("Donaustr"));
		assertTrue(churchGMLString.contains("DEBW_LOD2_203056"));
		assertTrue(churchGMLString.contains("DEBW_LOD2_2869"));
		assertTrue(churchGMLString.contains("DEBW_LOD2_2909"));
Eric Duminil's avatar
Eric Duminil committed
48
49
		assertTrue(churchGMLString.contains(CORE_CITY_MODEL_HEADER));
		assertTrue(churchGMLString.contains(CORE_CITY_MODEL_FOOTER));
Eric Duminil's avatar
Eric Duminil committed
50
51
52
53
54
55
		assertTrue(churchGMLString.contains("<gml:Envelope srsName=\"EPSG:31467\" srsDimension=\"3\">"),
				"The exported CityGML should contain a new envelope with the correct EPSG");
		assertTrue(churchGMLString.contains("<gml:lowerCorner>3515829.0815150724 5415766.204935087 "),
				"The exported CityGML should contain a new envelope");
		assertTrue(churchGMLString.contains("<gml:upperCorner>3515876.489172751 5415823.108586172 "),
				"The exported CityGML should contain a new envelope");
56
	}
57
58

	@Test
Eric Duminil's avatar
Eric Duminil committed
59
	void testExtractBuildingsWithCommentsInBetween() throws Throwable {
60
		String wktPolygon = "POLYGON((300259.78663489706 62835.835907766595,300230.33294975647 62792.0482567884,300213.5667431851 62770.83143720031,300183.6592861123 62730.20347659383,300252.9947486632 62676.938468840905,300273.3862256562 62701.767105345614,300257.5250407747 62715.760413539596,300308.2754543957 62805.14198211394,300259.78663489706 62835.835907766595))";
61
		Path citygmlPath = TEST_REPOSITORY.resolve("NewYork.proj/ManhattanSmall.gml");
Eric Duminil's avatar
Eric Duminil committed
62
63
64
65
66
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, EPSG_32118, gmlWriter, citygmlPath);
		String archGMLString = gmlWriter.toString();
		assertEquals(2, count);
		assertEquals(2, countRegexMatches(archGMLString, CITY_OBJECT_MEMBER_REGEX));
67
68
69
		assertTrue(archGMLString.contains("WASHINGTON SQUARE"));
		assertTrue(archGMLString.contains("uuid_c0980a6e-05ea-4d09-bc83-efab226945a1"));
		assertTrue(archGMLString.contains("uuid_0985cebb-922d-4b3e-95e5-15dc6089cd28"));
Eric Duminil's avatar
Eric Duminil committed
70
71
		assertTrue(archGMLString.contains(CITY_MODEL_HEADER));
		assertTrue(archGMLString.contains(CITY_MODEL_FOOTER));
72
73
	}

74
	@Test
75
	void testExtractBuildingsAndChangeEnvelope() throws Throwable {
76
		String wktPolygon = "POLYGON((299761.8123557725 61122.68126771413,299721.46983062755 61058.11626595352,299780.84627343423 61021.99295737501,299823.9079725632 61083.3979344517,299761.8123557725 61122.68126771413))";
77
		Path citygmlPath = TEST_REPOSITORY.resolve("NewYork.proj/FamilyCourt_LOD2_with_PLUTO_attributes.gml");
Eric Duminil's avatar
Eric Duminil committed
78
79
80
81
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, EPSG_32118, gmlWriter, citygmlPath);
		String familyCourtBuilding = gmlWriter.toString();
		assertEquals(1, count);
Eric Duminil's avatar
Eric Duminil committed
82
		assertEquals(1, countRegexMatches(familyCourtBuilding, CITY_OBJECT_MEMBER_REGEX));
83
		assertTrue(familyCourtBuilding.contains("Bldg_12210021066"));
Eric Duminil's avatar
Eric Duminil committed
84
85
86
87
88
89
90
91
92
93
94
95
96
97
		assertFalse(
				familyCourtBuilding.contains(
						"<gml:lowerCorner>298393.46959639067 59277.34021543693 -11.892070104139751</gml:lowerCorner>"),
				"The exported CityGML shouldn't contain the original envelope");
		assertFalse(
				familyCourtBuilding.contains(
						"<gml:upperCorner>305641.79529639013 67101.44881543722 547.7591871983744</gml:upperCorner>"),
				"The exported CityGML shouldn't contain the original envelope");
		assertTrue(familyCourtBuilding.contains("<gml:lowerCorner>299721.46983062755 61021.99295737501 -11.89"),
				"The exported CityGML should contain a new envelope with the original altitudes");
		assertTrue(familyCourtBuilding.contains("<gml:upperCorner>299823.9079725632 61122.68126771413 547.75"),
				"The exported CityGML should contain a new envelope with the original altitudes");
		assertTrue(familyCourtBuilding.contains("<gml:Envelope srsName=\"EPSG:32118\" srsDimension=\"3\">"),
				"The exported CityGML should contain a new envelope with the correct EPSG");
98
99
	}

100
	@Test
101
	void testExtract0BuildingsWithWrongCoordinates() throws Throwable {
102
103
		//NOTE: Small region, far away from NYC
		String wktPolygon = "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))";
104
		Path citygmlPath = TEST_REPOSITORY.resolve("NewYork.proj/ManhattanSmall.gml");
Eric Duminil's avatar
Eric Duminil committed
105
106
107
108
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, EPSG_32118, gmlWriter, citygmlPath);
		String emptyGMLString = gmlWriter.toString();
		assertEquals(0, count);
Eric Duminil's avatar
Eric Duminil committed
109
		assertEquals(0, countRegexMatches(emptyGMLString, CITY_OBJECT_MEMBER_REGEX));
Eric Duminil's avatar
Eric Duminil committed
110
111
		assertTrue(emptyGMLString.contains(CITY_MODEL_HEADER));
		assertTrue(emptyGMLString.contains(CITY_MODEL_FOOTER));
112
	}
duminil's avatar
duminil committed
113

duminil's avatar
duminil committed
114
	@Test
115
	void testExtract0BuildingsFromEmptyGML() throws Throwable {
duminil's avatar
duminil committed
116
117
		//NOTE: Small region, with too many spaces between coordinates
		String wktPolygon = "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))";
118
		Path citygmlPath = TEST_REPOSITORY.resolve("NewYork.proj/empty_model.gml");
Eric Duminil's avatar
Eric Duminil committed
119
120
121
122
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, EPSG_32118, gmlWriter, citygmlPath);
		String emptyGMLString = gmlWriter.toString();
		assertEquals(0, count);
Eric Duminil's avatar
Eric Duminil committed
123
		assertEquals(0, countRegexMatches(emptyGMLString, CITY_OBJECT_MEMBER_REGEX));
Eric Duminil's avatar
Eric Duminil committed
124
125
		assertTrue(emptyGMLString.contains(CORE_CITY_MODEL_HEADER));
		assertTrue(emptyGMLString.contains(CORE_CITY_MODEL_FOOTER));
duminil's avatar
duminil committed
126
127
	}

duminil's avatar
duminil committed
128
	@Test
129
	void testExtract0BuildingsFromWeirdGML() throws Throwable {
duminil's avatar
duminil committed
130
131
		//NOTE: Small region, with too many spaces between coordinates
		String wktPolygon = "POLYGON((0 0, 0 1, 1 1, 1 0, 0 0))";
132
		Path citygmlPath = TEST_REPOSITORY.resolve("NewYork.proj/broken_nyc_lod2.gml");
Eric Duminil's avatar
Eric Duminil committed
133
134
135
136
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, EPSG_32118, gmlWriter, citygmlPath);
		String emptyGMLString = gmlWriter.toString();
		assertEquals(0, count);
Eric Duminil's avatar
Eric Duminil committed
137
		assertEquals(0, countRegexMatches(emptyGMLString, CITY_OBJECT_MEMBER_REGEX));
Eric Duminil's avatar
Eric Duminil committed
138
139
		assertTrue(emptyGMLString.contains(CORE_CITY_MODEL_HEADER));
		assertTrue(emptyGMLString.contains(CORE_CITY_MODEL_FOOTER));
duminil's avatar
duminil committed
140
	}
141
142

	@Test
143
	void testExtractBuildingsFromCitygmlWithoutZinEnvelope() throws Throwable {
144
		String wktPolygon = "POLYGON((3512683.1280912133 5404783.732132129,3512719.1608604863 5404714.627650777,3512831.40076119 5404768.344155442,3512790.239106708 5404838.614891164,3512683.1280912133 5404783.732132129))";
145
		Path citygmlPath = TEST_REPOSITORY.resolve("Stuttgart.proj/Stuttgart_LOD0_LOD1_small.gml");
Eric Duminil's avatar
Eric Duminil committed
146
147
148
149
150
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, "EPSG:31463", gmlWriter, citygmlPath);
		String twoBuildings = gmlWriter.toString();
		assertEquals(2, count);
		assertEquals(2, countRegexMatches(twoBuildings, "<bldg:Building gml:id"));
151
	}
152
153
154
155
156
157
158

	@Test
	void testExtractBuildingsFrom2Citygmls() throws Throwable {
		String wktPolygon = "POLYGON((3512984.7003764412 5405148.310572891,3513038.6360455155 5405010.072163861,3513142.7277745553 5405004.02571992,3514204.1661769524 5405563.192081669,3514399.2818417274 5405720.905457244,3514291.6158155007 5405896.706492759,3512984.7003764412 5405148.310572891))";

		Path citygml1 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stuttgart_LOD0_LOD1_small.gml");
		Path citygml2 = TEST_REPOSITORY.resolve("Stuttgart.proj/Stöckach_überarbeitete GML-NoBuildingPart.gml");
Eric Duminil's avatar
Eric Duminil committed
159
160
161
162
163
164
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, "EPSG:31463", gmlWriter, citygml1,
				citygml2);
		String gmlFromTwoGMLs = gmlWriter.toString();
		assertEquals(17 + 3, count);
		assertEquals(17 + 3, countRegexMatches(gmlFromTwoGMLs, "<bldg:Building gml:id"));
165
166
	}

167
168
169
	@Test
	void testExtractBuildingsAndRemoveOld() throws Throwable {
		String wktPolygon = "POLYGON((293229.6831819388 5623753.072371232,293681.22751166753 5623744.274551504,293668.8482257676 5623469.512992135,293197.09954629745 5623504.821467172,293229.6831819388 5623753.072371232))";
Eric Duminil's avatar
Eric Duminil committed
170
		Path citygmlPath = TEST_REPOSITORY.resolve("Others.proj/Aachen.gml");
171
		//NOTE: This file has <gml:lowerCorner srsDimension="3"> and not just <gml:lowerCorner>, which caused problems with parsing.
172

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
		StringWriter gmlWriter = new StringWriter();
		int count = RegionExtractor.selectRegionDirectlyFromCityGML(wktPolygon, "EPSG:25832", gmlWriter, citygmlPath);
		String oneAachenBuilding = gmlWriter.toString();
		System.out.println(oneAachenBuilding);
		assertEquals(1, count);
		assertEquals(1, countRegexMatches(oneAachenBuilding, CITY_OBJECT_MEMBER_REGEX));
		assertTrue(oneAachenBuilding.contains("DENW39AL10003jfi"));
		assertFalse(
				oneAachenBuilding.contains(
						"<gml:lowerCorner>298393.46959639067 59277.34021543693 -11.892070104139751</gml:lowerCorner>"),
				"The exported CityGML shouldn't contain the original envelope");
		assertFalse(
				oneAachenBuilding.contains(
						"urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH"),
				"The exported CityGML shouldn't contain the original envelope");
		assertTrue(oneAachenBuilding.contains("<gml:Envelope srsName=\"EPSG:25832\" srsDimension=\"3\">"),
				"The exported CityGML should contain a new envelope with the correct EPSG");
	}
191
}