PdfStreamReporter.java 15.5 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/*-
 *  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.reporting.pdf;

import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

35
import de.hft.stuttgart.citydoctor2.check.RequirementConfiguration;
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import de.hft.stuttgart.citydoctor2.check.CheckError;
import de.hft.stuttgart.citydoctor2.check.CheckId;
import de.hft.stuttgart.citydoctor2.check.CheckResult;
import de.hft.stuttgart.citydoctor2.check.ErrorId;
import de.hft.stuttgart.citydoctor2.check.ResultStatus;
import de.hft.stuttgart.citydoctor2.check.ValidationConfiguration;
import de.hft.stuttgart.citydoctor2.checkresult.utility.CheckReportWriteException;
import de.hft.stuttgart.citydoctor2.datastructure.BoundarySurface;
import de.hft.stuttgart.citydoctor2.datastructure.BridgeObject;
import de.hft.stuttgart.citydoctor2.datastructure.Building;
import de.hft.stuttgart.citydoctor2.datastructure.BuildingPart;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.datastructure.Geometry;
import de.hft.stuttgart.citydoctor2.datastructure.LandObject;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.Opening;
import de.hft.stuttgart.citydoctor2.datastructure.Polygon;
import de.hft.stuttgart.citydoctor2.datastructure.TransportationObject;
import de.hft.stuttgart.citydoctor2.datastructure.Vegetation;
import de.hft.stuttgart.citydoctor2.datastructure.WaterObject;
import de.hft.stuttgart.citydoctor2.reporting.StreamReporter;
57
import de.hft.stuttgart.citydoctor2.utils.Localization;
58
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
102
103
104
105
106
107
108
109
110

/**
 * Reporter to create a PDF report out of a stream of feature results
 * 
 * @author Matthias Betz
 *
 */
public class PdfStreamReporter implements StreamReporter {

	private static final Logger logger = LogManager.getLogger(PdfStreamReporter.class);

	private static final String ERROR_COLOR = "red";
	@SuppressWarnings("unused")
	private static final String WARNING_COLOR = "yellow";
	private static final String OK_COLOR = "green";

	private PdfReport report;
	private OutputStream outFile;

	private Map<ErrorId, AtomicInteger> errorStatistics;
	private ValidationConfiguration config;

	private Section statistics;
	private Section vr;

	private Section buildings;
	private int numErrorBuildings;
	private int numOkBuildings;

	private Section vegetation;
	private int numErrorVegetation;
	private int numOkVegetation;

	private Section trans;
	private int numErrorTrans;
	private int numOkTrans;

	private Section bridge;
	private int numErrorBridge;
	private int numOkBridge;

	private Section water;
	private int numErrorWater;
	private int numOkWater;

	private Section land;
	private int numErrorLand;
	private int numOkLand;

	private Section globalErrors;

	private Map<String, Section> sectionMap = new HashMap<>();

Matthias Betz's avatar
Matthias Betz committed
111
	public PdfStreamReporter(OutputStream pdfOutputFile, String fileName, ValidationConfiguration config) {
112
		this.config = config;
Matthias Betz's avatar
Matthias Betz committed
113
		errorStatistics = new HashMap<>();
114
		outFile = pdfOutputFile;
Matthias Betz's avatar
Matthias Betz committed
115
		report = new PdfReport();
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
		report.writeSourceFileName(fileName);

		writeEnvironment();

		statistics = report.createSection("Statistics", true);
		statistics.addTextElement("Object distribution:");

		writeValidationPlan();

		vr = report.createSection("Validation Results", true);
	}

	private void writeValidationPlan() {
		Section vp = report.createSection("Validation Plan", true);
		vp.addTextElement("numberOfRoundingPlaces = " + config.getNumberOfRoundingPlaces());
131
		vp.addTextElement("minVertexDistance = " + config.getMinVertexDistance());
132
133
134
		if (config.getSchematronFilePath() != null && !config.getSchematronFilePath().isEmpty()) {
			vp.addTextElement("Schematron file: " + config.getSchematronFilePath());
		}
135
136
		for (Entry<String, RequirementConfiguration> e : config.getRequirements().entrySet()) {
			String text = e.getKey();
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
			String color;
			if (e.getValue().isEnabled()) {
				text = text + " = enabled";
				color = OK_COLOR;
			} else {
				text = text + " = disabled";
				color = ERROR_COLOR;
			}
			vp.addTextElement(text, color);
			for (Entry<String, String> parameter : e.getValue().getParameters().entrySet()) {
				vp.add10PtTextElement(parameter.getKey() + " = " + parameter.getValue(), 10);
			}
		}
	}

	private void writeEnvironment() {
		Section env = report.createSection("Environment");
		env.addTextElement("The checks were executed under the following environment:");
		Table t = new Table(2);
		t.setTitle("Name", "Value");
157
		t.addRow("City Doctor Version", Localization.getText(Localization.VERSION));
158
159
160
161
162
163
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
		t.addRow("Java VM-Version", System.getProperties().getProperty("java.vm.version"));
		t.addRow("Java VM-Vendor", System.getProperties().getProperty("java.vm.vendor"));
		t.addRow("Java Version", System.getProperties().getProperty("java.version"));
		t.addRow("OS Name", System.getProperties().getProperty("os.name"));
		t.addRow("OS Architecture", System.getProperties().getProperty("os.arch"));
		env.addTable(t);
	}

	@Override
	public void report(CityObject co) {
		List<CheckError> errorList = new ArrayList<>();
		co.collectContainedErrors(errorList);
		Set<CheckError> errors = new HashSet<>(errorList);
		for (CheckError e : errors) {
			AtomicInteger errorCount = errorStatistics.computeIfAbsent(e.getErrorId(), k -> new AtomicInteger(0));
			errorCount.incrementAndGet();
		}
		boolean hasError = containsError(co);
		if (co instanceof Building) {
			reportBuilding(co, hasError);
		} else if (co instanceof Vegetation) {
			reportVegetation(co, hasError);
		} else if (co instanceof TransportationObject) {
			reportTrans(co, hasError);
		} else if (co instanceof BridgeObject) {
			reportBridge(co, hasError);
		} else if (co instanceof WaterObject) {
			reportWater(co, hasError);
		} else if (co instanceof LandObject) {
			reportLand(co, hasError);
		} else {
			throw new IllegalStateException("Unknown City Object found: " + co.getClass());
		}
	}

	private void reportLand(CityObject co, boolean hasError) {
		if (land == null) {
			land = vr.createSubSection("Land Objects");
		}
		Section lSection = land.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), lSection);
		if (hasError) {
			numErrorLand++;
			lSection.setHeadlineColor(ERROR_COLOR);
		} else {
			numOkLand++;
			lSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, lSection);
	}

	private void reportWater(CityObject co, boolean hasError) {
		if (water == null) {
			water = vr.createSubSection("Water Objects");
		}
		Section wSection = water.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), wSection);
		if (hasError) {
			numErrorWater++;
			wSection.setHeadlineColor(ERROR_COLOR);
		} else {
			numOkWater++;
			wSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, wSection);
	}

	private void reportBridge(CityObject co, boolean hasError) {
		if (bridge == null) {
			bridge = vr.createSubSection("Bridge Objects");
		}
		Section bSection = bridge.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), bSection);
		if (hasError) {
			numErrorBridge++;
			bSection.setHeadlineColor(ERROR_COLOR);
		} else {
			numOkBridge++;
			bSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, bSection);
	}

	private void reportTrans(CityObject co, boolean hasError) {
		if (trans == null) {
			trans = vr.createSubSection("Transportation Objects");
		}
		Section tSection = trans.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), tSection);
		if (hasError) {
			numErrorTrans++;
			tSection.setHeadlineColor(ERROR_COLOR);
		} else {
			numOkTrans++;
			tSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, tSection);
		TransportationObject to = (TransportationObject) co;
		for (TransportationObject transO : to.getChildren()) {
			writeCheckResultForTransportationObject(transO, tSection);
		}
	}

	private void reportVegetation(CityObject co, boolean hasError) {
		if (vegetation == null) {
			vegetation = vr.createSubSection("Vegetation");
		}
		Section vSection = vegetation.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), vSection);
		if (hasError) {
			numErrorVegetation++;
			vSection.setHeadlineColor(ERROR_COLOR);
		} else {
			numOkVegetation++;
			vSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, vSection);
	}

	private void reportBuilding(CityObject co, boolean hasError) {
		if (buildings == null) {
			buildings = vr.createSubSection("Buildings");
		}
		Section bSection = buildings.createSubSection(co.getGmlId().getGmlString());
		sectionMap.put(co.getGmlId().getGmlString(), bSection);
		if (hasError) {
			bSection.setHeadlineColor(ERROR_COLOR);
		} else {
			bSection.setHeadlineColor(OK_COLOR);
		}
		writeErrorForCityObject(co, bSection);
		Building b = (Building) co;
		for (BuildingPart bp : b.getBuildingParts()) {
			sectionMap.put(bp.getGmlId().getGmlString(), bSection);
			writeCheckResultForBuildingPart(bp, bSection);
		}
		for (BoundarySurface bs : b.getBoundarySurfaces()) {
			writeCheckResultForBoundarySurface(bs, bSection);
		}
	}

	private void writeCheckResultForTransportationObject(TransportationObject to, Section root) {
		Map<CheckId, CheckResult> results = to.getAllCheckResults();
		writeCheckResults(results.values(), root);
		for (Geometry geom : to.getGeometries()) {
			writeCheckResultForGeometry(geom, root);
		}
	}

	private void writeCheckResultForBuildingPart(BuildingPart bp, Section root) {
		Map<CheckId, CheckResult> results = bp.getAllCheckResults();
		writeCheckResults(results.values(), root);
		for (Geometry geom : bp.getGeometries()) {
			writeCheckResultForGeometry(geom, root);
		}
		for (BoundarySurface bs : bp.getBoundarySurfaces()) {
			writeCheckResultForBoundarySurface(bs, root);
		}
	}

	private void writeErrorForCityObject(CityObject co, Section section) {
		Map<CheckId, CheckResult> results = co.getAllCheckResults();
		writeCheckResults(results.values(), section);
		for (Geometry geom : co.getGeometries()) {
			writeCheckResultForGeometry(geom, section);
		}
	}

	private void writeCheckResultForBoundarySurface(BoundarySurface bs, Section root) {
		Map<CheckId, CheckResult> results = bs.getAllCheckResults();
		writeCheckResults(results.values(), root);
		for (Opening o : bs.getOpenings()) {
			writeCheckResultForOpening(o, root);
		}
	}

	private void writeCheckResultForOpening(Opening o, Section root) {
		Map<CheckId, CheckResult> results = o.getAllCheckResults();
		writeCheckResults(results.values(), root);
	}

	private void writeCheckResultForGeometry(Geometry geom, Section root) {
		Map<CheckId, CheckResult> results = geom.getAllCheckResults();
		writeCheckResults(results.values(), root);
		for (Polygon poly : geom.getPolygons()) {
			writeCheckResultForPolygon(poly, root);
		}
	}

	private void writeCheckResultForPolygon(Polygon poly, Section root) {
		Map<CheckId, CheckResult> results = poly.getAllCheckResults();
		writeCheckResults(results.values(), root);
		writeCheckResultForLinearRing(poly.getExteriorRing(), root);
		for (LinearRing ring : poly.getInnerRings()) {
			writeCheckResultForLinearRing(ring, root);
		}
	}

	private void writeCheckResultForLinearRing(LinearRing ring, Section root) {
		Map<CheckId, CheckResult> results = ring.getAllCheckResults();
		writeCheckResults(results.values(), root);
	}

	private void writeCheckResults(Collection<CheckResult> results, Section root) {
		for (CheckResult cr : results) {
			if (cr.getResultStatus() == ResultStatus.ERROR) {
				writeError(root, cr.getError());
			}
		}
	}

	private void writeError(Section root, CheckError error) {
		String text = "Error " + error.getErrorId().toString() + ":";
		root.add12PtTextElement(text, 0);
		PdfErrorHandler handler = new PdfErrorHandler(root, config.getParserConfiguration());
		error.report(handler);
		root.addError(error);
	}

	private boolean containsError(CityObject co) {
		return co.containsAnyError();
	}

	@Override
	public void finishReport() throws CheckReportWriteException {
		for (Section s : buildings.getSubSections()) {
			if (!s.hasErrors()) {
				numOkBuildings++;
				// building has no errors, no table
				continue;
			}
			numErrorBuildings++;
			Table t = new Table(2);
			t.setTableColumnWidth(75, 25);
			t.setTitle("Error", "Count");
			for (Entry<String, AtomicInteger> e : s.getStats().getErrorCounts().entrySet()) {
				t.addRow(e.getKey(), e.getValue().toString());
			}
			s.addTable(1, t);
		}
		int numBuildings = numErrorBuildings + numOkBuildings;
		if (numBuildings > 0) {
			statistics.addDistributionBar("Buildings", numErrorBuildings, numOkBuildings);
		}

		int numVegetation = numErrorVegetation + numOkVegetation;
		if (numVegetation > 0) {
			statistics.addDistributionBar("Vegetation", numErrorVegetation, numOkVegetation);
		}

		int numtr = numErrorTrans + numOkTrans;
		if (numtr > 0) {
			statistics.addDistributionBar("Transportation Objects", numErrorTrans, numOkTrans);
		}

		int numbri = numErrorBridge + numOkBridge;
		if (numbri > 0) {
			statistics.addDistributionBar("Bridge Objects", numErrorBridge, numOkBridge);
		}

		int numWater = numErrorWater + numOkWater;
		if (numWater > 0) {
			statistics.addDistributionBar("Water Objects", numErrorWater, numOkWater);
		}

		int numLand = numErrorLand + numOkLand;
		if (numLand > 0) {
			statistics.addDistributionBar("Land Objects", numErrorLand, numOkLand);
		}

		statistics.addTextElement("Error Statistics:");
		for (Entry<ErrorId, AtomicInteger> e : errorStatistics.entrySet()) {
			statistics.add10PtTextElement(e.getKey().toString() + ": " + e.getValue().intValue(), 10);
		}
		report.save(outFile);
	}

	@Override
	public void reportGlobalError(CheckError err) {
		AtomicInteger errorCount = errorStatistics.computeIfAbsent(err.getErrorId(), k -> new AtomicInteger(0));
		errorCount.incrementAndGet();

		if (globalErrors == null) {
			globalErrors = vr.createSubSection("Global Errors");
		}

		String text = "Global error " + err.getErrorId();
		globalErrors.add10PtTextElement(text, 10);
446
447
		PdfErrorHandler handler = new PdfErrorHandler(globalErrors, config.getParserConfiguration());
		err.report(handler);
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
	}

	@Override
	public void addError(String gmlId, CheckError err) {
		AtomicInteger errorCount = errorStatistics.computeIfAbsent(err.getErrorId(), k -> new AtomicInteger(0));
		errorCount.incrementAndGet();
		Section section = sectionMap.get(gmlId);
		if (section == null) {
			logger.warn("No section found for gml id: {}, adding to global errors instead", gmlId);
			reportGlobalError(err);
		} else {
			section.setHeadlineColor(ERROR_COLOR);
			writeError(section, err);
			section.addError(err);
		}
	}

}