FeatureMapperTest.java 7.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*-
 *  Copyright 2020 Beuth Hochschule für Technik Berlin, Hochschule für Technik Stuttgart
 * 
 *  This file is part of CityDoctor2.
 *
 *  CityDoctor2 is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  CityDoctor2 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 Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with CityDoctor2.  If not, see <https://www.gnu.org/licenses/>.
 */
package de.hft.stuttgart.citydoctor2.mapper;

import static org.junit.Assert.assertEquals;
Matthias Betz's avatar
Matthias Betz committed
22
import static org.junit.Assert.assertNull;
23
import static org.junit.Assert.fail;
Matthias Betz's avatar
Matthias Betz committed
24
import static org.mockito.Mockito.mock;
25

Matthias Betz's avatar
Matthias Betz committed
26
import java.io.File;
27
28
import java.io.IOException;

Matthias Betz's avatar
Matthias Betz committed
29
import org.citygml4j.model.citygml.vegetation.PlantCover;
30
31
32
33
34
35
36
import org.citygml4j.model.citygml.waterbody.WaterBody;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurface;
import org.citygml4j.model.gml.geometry.aggregates.MultiSurfaceProperty;
import org.citygml4j.model.gml.geometry.primitives.Coord;
import org.citygml4j.model.gml.geometry.primitives.Exterior;
import org.citygml4j.model.gml.geometry.primitives.LinearRing;
import org.citygml4j.model.gml.geometry.primitives.Polygon;
Matthias Betz's avatar
Matthias Betz committed
37
import org.citygml4j.model.gml.geometry.primitives.SurfaceProperty;
38
39
40
41
42
43
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
Matthias Betz's avatar
Matthias Betz committed
44
45
import de.hft.stuttgart.citydoctor2.datastructure.GeometryType;
import de.hft.stuttgart.citydoctor2.datastructure.Lod;
46
47
48
49
50
51
52
53
54
55
56
57
58
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.datastructure.WaterObject;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;

/**
 * 
 * @author Matthias Betz
 *
 */
public class FeatureMapperTest {

	@Rule
	public TemporaryFolder folder = new TemporaryFolder();
Matthias Betz's avatar
Matthias Betz committed
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
	
	@Test
	public void testVisitWaterBody() {
		WaterBody body = new WaterBody();
		FeatureMapper mapper = new FeatureMapper(mock(ParserConfiguration.class), new File(""));
		mapper.visit(body);
		assertEquals(1, mapper.getModel().getWater().size());
		body.setId("test1");
		mapper.visit(body);
		assertEquals(2, mapper.getModel().getWater().size());
		WaterObject waterObject = mapper.getModel().getWater().get(1);
		assertEquals("test1", waterObject.getGmlId().getGmlString());
		
		body.setLod1MultiSurface(createDummyMsp());
		mapper.visit(body);
		assertEquals(3, mapper.getModel().getWater().size());
		assertNull(body.getLod1MultiSurface());
		Geometry geometry = mapper.getModel().getWater().get(2).getGeometries().get(0);
		assertEquals(1, geometry.getPolygons().size());
		assertEquals(Lod.LOD1, geometry.getLod());
		assertEquals(GeometryType.MULTI_SURFACE, geometry.getType());
	}
	
	@Test
	public void testVisitPlantCover() {
		PlantCover cover = new PlantCover();
		FeatureMapper mapper = new FeatureMapper(mock(ParserConfiguration.class), new File(""));
		mapper.visit(cover);
	}
	
	private MultiSurfaceProperty createDummyMsp() {
		LinearRing ext = new LinearRing();
		ext.addCoord(createCoord(0, 0, 0));
		ext.addCoord(createCoord(10, 0, 0));
		ext.addCoord(createCoord(10, 10, 0));
		ext.addCoord(createCoord(0, 10, 0));
		ext.addCoord(createCoord(0, 0, 0));
		Polygon p = new Polygon();
		p.setExterior(new Exterior(ext));
		MultiSurface ms = new MultiSurface();
		ms.addSurfaceMember(new SurfaceProperty(p));
		return new MultiSurfaceProperty(ms);
	}
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127

	@Test
	public void testNeighboringVertices() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p = new Polygon();
		LinearRing ring = new LinearRing();
		Coord coord = createCoord(0, 0.0000000049, 0);
		ring.addCoord(coord);
		p.setExterior(new Exterior(ring));

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		coord = createCoord(0, 0.0000000050, 0);
		ring2.addCoord(coord);
		p2.setExterior(new Exterior(ring2));
		MultiSurface ms = new MultiSurface(p, p2);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
128
		assertEquals(1, geometry.getVertices().size());
129
		Vertex vertex = geometry.getVertices().get(0);
130
		assertEquals(new Vertex(0, 0, 0), vertex);
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
	}

	@Test
	public void testNeighboringVertices2() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p = new Polygon();
		LinearRing ring = new LinearRing();
		Coord coord = createCoord(0, 0.0000000049, 0);
		ring.addCoord(coord);

		coord = createCoord(0, 0.0000000150, 0);
		ring.addCoord(coord);
		p.setExterior(new Exterior(ring));
		MultiSurface ms = new MultiSurface(p);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
		assertEquals(2, geometry.getVertices().size());
		Vertex vertex = geometry.getVertices().get(0);
157
158
159
160
161
162
163
		if (vertex.equals(new Vertex(0, 0, 0))) {
			assertEquals(new Vertex(0, 0.00000002, 0), geometry.getVertices().get(1));
		} else if (vertex.equals(new Vertex(0, 0.00000002, 0))) {
			assertEquals(new Vertex(0, 0, 0), geometry.getVertices().get(1));
		} else {
			fail("Did not find both vertices again");
		}
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
	}

	@Test
	public void testNeighboringVerticesWithCompleteGeometry() throws IOException {
		WaterBody body = new WaterBody();
		Polygon p1 = new Polygon();
		LinearRing ring1 = new LinearRing();
		p1.setExterior(new Exterior(ring1));
		ring1.addCoord(createCoord(0, 0, 0));
		ring1.addCoord(createCoord(1, 0, 0));
		ring1.addCoord(createCoord(1, 1, 0));
		ring1.addCoord(createCoord(1, 2, 0));
		ring1.addCoord(createCoord(0, 2, 0));
		ring1.addCoord(createCoord(0, 0, 0));

		Polygon p2 = new Polygon();
		LinearRing ring2 = new LinearRing();
		p2.setExterior(new Exterior(ring2));
		ring2.addCoord(createCoord(1, 0, 0));
		ring2.addCoord(createCoord(2, 0, 0));
		ring2.addCoord(createCoord(2, 2, 0));
		ring2.addCoord(createCoord(1, 2, 0));
		ring2.addCoord(createCoord(1, 1.0000000050, 0));
		ring2.addCoord(createCoord(1, 0, 0));

		MultiSurface ms = new MultiSurface(p1, p2);
		body.setLod1MultiSurface(new MultiSurfaceProperty(ms));

		ParserConfiguration config = new ParserConfiguration(8, false);
		FeatureMapper mapper = new FeatureMapper(config, folder.newFile());
		mapper.visit(body);

		CityDoctorModel model = mapper.getModel();
		assertEquals(1, model.getWater().size());
		WaterObject waterObject = model.getWater().get(0);
		Geometry geometry = waterObject.getGeometries().get(0);
200
		assertEquals(7, geometry.getVertices().size());
201
202
203
204
205
206
207
208
209
210
211
212
		assertEquals(8, geometry.getEdges().size());
	}

	private Coord createCoord(double x, double y, double z) {
		Coord coord = new Coord();
		coord.setX(x);
		coord.setY(y);
		coord.setZ(z);
		return coord;
	}

}