CityGmlParser.java 25.2 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
/*-
 *  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.parser;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
27
import java.util.ArrayList;
28
import java.util.Enumeration;
29
import java.util.Iterator;
30
31
32
33
34
35
36
37
import java.util.List;
import java.util.ServiceLoader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

import javax.xml.XMLConstants;
38
import javax.xml.bind.ValidationEventHandler;
39
40
41
42
43
44
45
46
47
48
49
50
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.citygml4j.CityGMLContext;
import org.citygml4j.builder.jaxb.CityGMLBuilder;
import org.citygml4j.builder.jaxb.CityGMLBuilderException;
import org.citygml4j.model.citygml.CityGML;
51
import org.citygml4j.model.citygml.ade.ADEComponent;
52
53
import org.citygml4j.model.citygml.ade.ADEException;
import org.citygml4j.model.citygml.ade.binding.ADEContext;
54
import org.citygml4j.model.citygml.ade.generic.ADEGenericElement;
55
import org.citygml4j.model.citygml.bridge.Bridge;
56
57
58
import org.citygml4j.model.citygml.core.AbstractCityObject;
import org.citygml4j.model.citygml.core.CityModel;
import org.citygml4j.model.citygml.core.CityObjectMember;
59
60
61
62
import org.citygml4j.model.citygml.landuse.LandUse;
import org.citygml4j.model.citygml.transportation.AbstractTransportationObject;
import org.citygml4j.model.citygml.vegetation.AbstractVegetationObject;
import org.citygml4j.model.citygml.waterbody.WaterBody;
63
64
import org.citygml4j.model.gml.feature.AbstractFeature;
import org.citygml4j.model.module.citygml.CityGMLVersion;
65
import org.citygml4j.xml.io.CityGMLInputFactory;
66
import org.citygml4j.xml.io.CityGMLOutputFactory;
67
68
69
import org.citygml4j.xml.io.reader.CityGMLReadException;
import org.citygml4j.xml.io.reader.CityGMLReader;
import org.citygml4j.xml.io.reader.FeatureReadMode;
70
71
72
73
import org.citygml4j.xml.io.reader.ParentInfo;
import org.citygml4j.xml.io.writer.CityGMLWriteException;
import org.citygml4j.xml.io.writer.CityModelInfo;
import org.citygml4j.xml.io.writer.CityModelWriter;
74
75
76
77
78
79
import org.locationtech.proj4j.BasicCoordinateTransform;
import org.locationtech.proj4j.CRSFactory;
import org.locationtech.proj4j.CoordinateReferenceSystem;
import org.locationtech.proj4j.ProjCoordinate;
import org.locationtech.proj4j.proj.Projection;
import org.locationtech.proj4j.units.Units;
80
81
82
83
84
85
86
87
88
89
90
import org.w3c.dom.DOMException;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;

import de.hft.stuttgart.citydoctor2.datastructure.CityDoctorModel;
import de.hft.stuttgart.citydoctor2.datastructure.CityObject;
import de.hft.stuttgart.citydoctor2.mapper.FeatureMapper;
import de.hft.stuttgart.citydoctor2.math.Vector3d;
Matthias Betz's avatar
Matthias Betz committed
91
import de.hft.stuttgart.citydoctor2.utils.Localization;
92
import de.hft.stuttgart.quality.QualityADEModule;
93
94
95
96
97
98
99
100
101

/**
 * Utility class to parse CityGML files.
 * 
 * @author Matthias Betz
 *
 */
public class CityGmlParser {

102
103
	private static final String WGS_84 = "EPSG:4326";

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
	private static final Logger logger = LogManager.getLogger(CityGmlParser.class);

	private static final CRSFactory CRS_FACTORY = new CRSFactory();
	// EPSG:31467
	private static final Pattern P_EPSG = Pattern.compile("^(EPSG:\\d+)$");
	// urn:ogc:def:crs,crs:EPSG:6.12:31467,crs:EPSG:6.12:5783
	// or
	// urn:ogc:def:crs,crs:EPSG::28992
	private static final Pattern P_OGC = Pattern.compile("urn:ogc:def:crs,crs:EPSG:[\\d\\.]*:([\\d]+)\\D*");

	private static final Pattern P_OGC2 = Pattern.compile("urn:ogc:def:crs:EPSG:[\\d\\.]*:([\\d]+)\\D*");

	// urn:adv:crs:DE_DHDN_3GK3*DE_DHHN92_NH
	// urn:adv:crs:ETRS89_UTM32*DE_DHHN92_NH
	private static final Pattern P_URN = Pattern.compile("urn:adv:crs:([^\\*]+)");

Matthias Betz's avatar
Matthias Betz committed
120
	private static final SAXParserFactory FACTORY;
121
122

	static {
Matthias Betz's avatar
Matthias Betz committed
123
		FACTORY = SAXParserFactory.newInstance();
124
		try {
Matthias Betz's avatar
Matthias Betz committed
125
			FACTORY.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
126
127
128
129
130
131
132
133
134
135
136
		} catch (SAXNotRecognizedException | SAXNotSupportedException | ParserConfigurationException e) {
			logger.catching(e);
		}
	}

	private CityGmlParser() {
		// only static access
	}

	public static CityDoctorModel parseCityGmlFile(String file, ParserConfiguration config)
			throws CityGmlParseException, InvalidGmlFileException {
137
		return parseCityGmlFile(file, config, null, null);
138
139
	}

140
	public static CityDoctorModel parseCityGmlFile(String file, ParserConfiguration config, ProgressListener l)
141
			throws CityGmlParseException, InvalidGmlFileException {
142
143
144
145
146
		return parseCityGmlFile(file, config, l, null);
	}

	public static CityDoctorModel parseCityGmlFile(String filePath, ParserConfiguration config, ProgressListener l,
			ValidationEventHandler handler) throws CityGmlParseException, InvalidGmlFileException {
147
148
149
		File file = new File(filePath);
		try {
			parseEpsgCodeFromFile(file, config);
150
151
152
153
154
			CityGMLBuilder builder = setupCityGmlBuilder();
			CityGMLInputFactory inputFactory = setupGmlReader(builder, config);
			if (handler != null) {
				inputFactory.setValidationEventHandler(handler);
			}
155
156
157
158
159
			// try with resources for automatic closing
			try (ObservedInputStream ois = new ObservedInputStream(file)) {
				if (l != null) {
					ois.addListener(l::updateProgress);
				}
160
				return readAndKeepFeatures(config, file, inputFactory, ois);
161
162
163
			}
		} catch (CityGMLReadException e) {
			if (e.getCause() instanceof SAXParseException) {
164
165
				throw new InvalidGmlFileException(
						Localization.getText("CityGmlParser.notValidGmlFile") + e.getCause().getMessage(), e);
166
167
168
169
170
171
172
173
174
175
			}
			throw new CityGmlParseException(e);
		} catch (IOException | CityGMLBuilderException | ParserConfigurationException | SAXException | ADEException e) {
			throw new CityGmlParseException(e);
		} catch (DOMException e) {
			// the citygml4j split per feature tries to insert an element
			// this sometimes fails because of namespace mismatch
			// fallback solution is to parse the whole file at once
			// problems with memory consumption may arise
			try {
Matthias Betz's avatar
Matthias Betz committed
176
177
178
				if (logger.isWarnEnabled()) {
					logger.warn(Localization.getText("CityGmlParser.chunkReadFailed"), e);
				}
179
180
181
182
183
184
185
				return parseCityGmlFileComplete(file, config, l);
			} catch (CityGMLBuilderException | CityGMLReadException | IOException e1) {
				throw new CityGmlParseException(e1);
			}
		}
	}

186
187
188
189
	private static CityDoctorModel readAndKeepFeatures(ParserConfiguration config, File file,
			CityGMLInputFactory inputFactory, ObservedInputStream ois) throws CityGMLReadException {
		try (CityGMLReader reader = inputFactory.createCityGMLReader(file.getAbsolutePath(), ois)) {
			FeatureMapper mapper = new FeatureMapper(config, file);
190
191
192
193
194
			// model is read in chunked mode
			// object members are replaced by href in model
			// need to remove the refs and re-add unparsed objects
			List<AbstractCityObject> acos = new ArrayList<>();
			List<ADEGenericElement> genericElements = new ArrayList<>();
195
196
197
			while (reader.hasNext()) {
				CityGML chunk = reader.nextFeature();
				if (chunk instanceof AbstractCityObject) {
198
199
200
					AbstractCityObject aco = (AbstractCityObject) chunk;
					aco.accept(mapper);
					acos.add(aco);
201
202
				} else if (chunk instanceof CityModel) {
					CityModel cModel = (CityModel) chunk;
203
204
205
206
207
208
209
210
211
212
					((CityModel) chunk).unsetCityObjectMember();
					// re-add all objects
					for (AbstractCityObject aco : acos) {
						cModel.addCityObjectMember(new CityObjectMember(aco));
					}
					// remove those that should have been parsed
					unsetParsedCityObjectMembers(cModel);
					for (ADEGenericElement ele : genericElements) {
						cModel.addGenericADEElement(ele);
					}
213
					mapper.setCityModel(cModel);
214
215
				} else if (chunk instanceof ADEGenericElement) {
					genericElements.add((ADEGenericElement) chunk);
216
217
218
219
220
221
222
223
224
225
				}
			}
			if (logger.isInfoEnabled()) {
				logger.info(Localization.getText("CityGmlParser.parsedObjects"),
						mapper.getModel().getNumberOfFeatures());
			}
			return mapper.getModel();
		}
	}

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
	private static CityDoctorModel parseCityGmlFileComplete(File file, ParserConfiguration config, ProgressListener l)
			throws CityGMLBuilderException, IOException, CityGMLReadException, CityGmlParseException {
		CityGMLContext context = CityGMLContext.getInstance();
		CityGMLBuilder builder = context.createCityGMLBuilder();
		CityGMLInputFactory inputFactory = builder.createCityGMLInputFactory();
		inputFactory.setProperty(CityGMLInputFactory.FAIL_ON_MISSING_ADE_SCHEMA, false);
		inputFactory.setProperty(CityGMLInputFactory.USE_VALIDATION, config.getValidate());

		try (ObservedInputStream ois = new ObservedInputStream(file)) {
			if (l != null) {
				ois.addListener(l::updateProgress);
			}
			try (CityGMLReader reader = inputFactory.createCityGMLReader(file.getAbsolutePath(), ois)) {
				FeatureMapper mapper = new FeatureMapper(config, file);

				CityGML chunk = reader.nextFeature();
				if (!(chunk instanceof CityModel)) {
					throw new CityGmlParseException("Did not read CityModel as first element of gml file.");
				}
				CityModel model = (CityModel) chunk;
				mapper.setCityModel(model);
				if (model.isSetCityObjectMember()) {
					for (CityObjectMember com : model.getCityObjectMember()) {
						if (com.isSetCityObject()) {
							com.getCityObject().accept(mapper);
						}
					}
				}
				model.unsetCityObjectMember();
				return mapper.getModel();
			}
		}

	}

261
	private static CityGMLInputFactory setupGmlReader(CityGMLBuilder builder, ParserConfiguration config)
262
			throws CityGMLBuilderException {
263
264
		CityGMLInputFactory inputFactory = builder.createCityGMLInputFactory();
		inputFactory.setProperty(CityGMLInputFactory.FEATURE_READ_MODE, FeatureReadMode.SPLIT_PER_FEATURE);
265
266
267
		if (config != null) {
			inputFactory.setProperty(CityGMLInputFactory.USE_VALIDATION, config.getValidate());
		}
268
269
270
271
272
273
		inputFactory.setProperty(CityGMLInputFactory.FAIL_ON_MISSING_ADE_SCHEMA, false);
		inputFactory.setProperty(CityGMLInputFactory.EXCLUDE_FROM_SPLITTING,
				new QName[] { new QName("WallSurface"), new QName("RoofSurface"), new QName("GroundSurface"),
						new QName("CeilingSurface"), new QName("ClosureSurface"), new QName("FloorSurface"),
						new QName("InteriorWallSurface"), new QName("OuterCeilingSurface"),
						new QName("OuterFloorSurface"), new QName("BuildingInstallation"), new QName("BuildingPart"),
274
						new QName("Door"), new QName("Window"), new QName("Address"), new QName("TINRelief") });
275
276
277
		return inputFactory;
	}

278
279
280
281
282
283
284
285
286
	private static CityGMLBuilder setupCityGmlBuilder() throws ADEException, CityGMLBuilderException {
		CityGMLContext context = CityGMLContext.getInstance();

		// setup energy ade stuff, so the parser doesn't crash on encountering this
		if (!context.hasADEContexts()) {
			for (ADEContext adeContext : ServiceLoader.load(ADEContext.class)) {
				context.registerADEContext(adeContext);
			}
		}
287
		return context.createCityGMLBuilder(CityGmlParser.class.getClassLoader());
288
289
290
291
	}

	public static void streamCityGml(String file, ParserConfiguration config, CityGmlConsumer cityObjectConsumer,
			String outputFile) throws CityGmlParseException {
292
		File f = new File(file);
293
		streamCityGml(f, config, null, cityObjectConsumer, outputFile);
294
295
	}

296
297
	public static void streamCityGml(File file, ParserConfiguration config, ProgressListener l,
			CityGmlConsumer cityObjectConsumer, String outputFile) throws CityGmlParseException {
298
299
300
301
302
		try {
			if (getExtension(file.getName()).equals("zip")) {
				streamZipFile(file, config);
			}
			parseEpsgCodeFromFile(file, config);
303
304
			CityGMLBuilder builder = setupCityGmlBuilder();
			startReadingCityGml(file, config, l, cityObjectConsumer, builder, outputFile);
305
306
307
308
309
		} catch (CityGMLBuilderException | IOException | ParserConfigurationException | SAXException | ADEException e) {
			throw new CityGmlParseException(e);
		}
	}

310
311
	private static void startReadingCityGml(File file, ParserConfiguration config, ProgressListener l,
			CityGmlConsumer cityObjectConsumer, CityGMLBuilder builder, String outputFile)
312
			throws CityGMLBuilderException {
313
314
315
316
317
318
319
320
321
322
323
		try (ObservedInputStream ois = new ObservedInputStream(file)) {
			if (l != null) {
				ois.addListener(l::updateProgress);
			}
			readAndDiscardFeatures(file, config, builder, ois, cityObjectConsumer, outputFile);
		} catch (IOException e) {
			logger.error(Localization.getText("CityGmlParser.errorReadingGmlFile"), e.getMessage());
			logger.catching(Level.ERROR, e);
		}
	}

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
	private static void streamZipFile(File file, ParserConfiguration config) throws CityGmlParseException {
		try (ZipFile zip = new ZipFile(file)) {
			Enumeration<? extends ZipEntry> entries = zip.entries();
			while (entries.hasMoreElements()) {
				ZipEntry ze = entries.nextElement();
				BufferedInputStream bis = new BufferedInputStream(zip.getInputStream(ze));
				parseEpsgCodeFromStream(bis, config);
			}
		} catch (IOException e) {
			throw new UncheckedIOException(e);
		} catch (ParserConfigurationException | SAXException e) {
			throw new CityGmlParseException(e);
		}
	}

	public static String getExtension(String fileName) {
		char ch;
		int len;
		if (fileName == null || (len = fileName.length()) == 0 || (ch = fileName.charAt(len - 1)) == '/' || ch == '\\'
				|| ch == '.') {
			return "";
		}
		int dotInd = fileName.lastIndexOf('.');
		int sepInd = Math.max(fileName.lastIndexOf('/'), fileName.lastIndexOf('\\'));
		if (dotInd <= sepInd)
			return "";
		else
			return fileName.substring(dotInd + 1).toLowerCase();
	}

354
355
356
	public static void streamCityGml(File file, ParserConfiguration parserConfig, CityGmlConsumer cityObjectConsumer,
			String outputFile) throws CityGmlParseException {
		streamCityGml(file, parserConfig, null, cityObjectConsumer, outputFile);
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
	}

	/**
	 * The srsName (The name by which this reference system is identified) inside
	 * the CityGML file can have multiple formats. This method tries to parse the
	 * string and detect the corresponding reference system. If it is found, it
	 * returns a proj4j.CoordinateReferenceSystem. It throws an
	 * IllegalArgumentException otherwise.
	 * 
	 * This method should be able to parse any EPSG id : e.g. "EPSG:1234". German
	 * Citygmls might also have "DE_DHDN_3GK3" or "ETRS89_UTM32" as srsName, so
	 * those are also included. It isn't guaranteed that those formats are correctly
	 * parsed, though.
	 * 
	 * The EPSG ids and parameters are defined in resources ('nad/epsg') inside
	 * proj4j-0.1.0.jar. Some EPSG ids are missing though, e.g. 7415
	 * 
	 * @param srsName
	 * @return CoordinateReferenceSystem
	 */
	private static CoordinateReferenceSystem crsFromSrsName(String srsName) {
378
		srsName = srsName.trim();
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
		Matcher mEPSG = P_EPSG.matcher(srsName);
		if (mEPSG.find()) {
			if ("EPSG:4979".contentEquals(srsName)) {
				srsName = "EPSG:4236";
			} else if ("EPSG:7415".contentEquals(srsName)) {
				return CRS_FACTORY.createFromParameters("EPSG:7415",
						"+proj=sterea +lat_0=52.15616055555555 +lon_0=5.38763888888889 +k=0.9999079 +x_0=155000 +y_0=463000 +ellps=bessel +towgs84=565.417,50.3319,465.552,-0.398957,0.343988,-1.8774,4.0725 +units=m +no_defs");
			}
			return CRS_FACTORY.createFromName(srsName);
		}

		Matcher mOGC = P_OGC.matcher(srsName);
		if (mOGC.find()) {
			return CRS_FACTORY.createFromName("EPSG:" + mOGC.group(1));
		}
		Matcher mOGC2 = P_OGC2.matcher(srsName);
		if (mOGC2.find()) {
			return CRS_FACTORY.createFromName("EPSG:" + mOGC2.group(1));
		}
		Matcher mURN = P_URN.matcher(srsName);
		// NOTE: Could use a HashMap if the switch/case becomes too long.
		if (mURN.find()) {
			switch (mURN.group(1)) {
			case "DE_DHDN_3GK2":
				return CRS_FACTORY.createFromName("EPSG:31466");
			case "DE_DHDN_3GK3":
				return CRS_FACTORY.createFromName("EPSG:31467");
			case "DE_DHDN_3GK4":
				return CRS_FACTORY.createFromName("EPSG:31468");
			case "DE_DHDN_3GK5":
				return CRS_FACTORY.createFromName("EPSG:31469");
			case "ETRS89_UTM32":
				return CRS_FACTORY.createFromName("EPSG:25832");
			default:
				return null;
			}
		}
Matthias Betz's avatar
Matthias Betz committed
416
		if (srsName.equals("http://www.opengis.net/def/crs/EPSG/0/6697")) {
417
			return CRS_FACTORY.createFromParameters("EPSG:6697", "+proj=longlat +ellps=GRS80 +no_defs +axis=neu");
Matthias Betz's avatar
Matthias Betz committed
418
		}
419
420
		return null;
	}
421

422
423
	public static CityModel parseOnlyCityModel(File inputFile) throws CityGmlParseException {
		try {
424
425
			CityGMLBuilder setupCityGmlBuilder = setupCityGmlBuilder();
			CityGMLInputFactory inputFactory = setupGmlReader(setupCityGmlBuilder, null);
426
427
428
429
430
			try (CityGMLReader reader = inputFactory.createCityGMLReader(inputFile)) {
				while (reader.hasNext()) {
					CityGML chunk = reader.nextFeature();
					if (chunk instanceof CityModel) {
						CityModel cModel = (CityModel) chunk;
431
						unsetParsedCityObjectMembers(cModel);
432
433
434
435
436
437
438
439
440
						return cModel;
					}
				}
			}
		} catch (CityGMLBuilderException | CityGMLReadException | ADEException e) {
			throw new CityGmlParseException(e);
		}
		throw new CityGmlParseException("Did not find any CityModel in CityGML file");
	}
441

442
443
444
445
446
447
448
449
450
451
452
453
454
	private static void unsetParsedCityObjectMembers(CityModel cModel) {
		Iterator<CityObjectMember> iterator = cModel.getCityObjectMember().iterator();
		while (iterator.hasNext()) {
			CityObjectMember m = iterator.next();
			AbstractCityObject aco = m.getCityObject();
			if (aco instanceof Bridge || aco instanceof org.citygml4j.model.citygml.building.Building
					|| aco instanceof AbstractTransportationObject || aco instanceof AbstractVegetationObject
					|| aco instanceof WaterBody || aco instanceof LandUse) {
				iterator.remove();
			}
		}
	}

455
456
	private static void readAndDiscardFeatures(File file, ParserConfiguration config, CityGMLBuilder builder,
			ObservedInputStream ois, CityGmlConsumer cityObjectConsumer, String outputFile)
457
			throws CityGMLBuilderException {
458
459
460
		CityGMLInputFactory inputFactory = setupGmlReader(builder, config);
		try (CityGMLReader reader = inputFactory.createCityGMLReader(file.getAbsolutePath(), ois);
				CityModelWriter writer = createCityModelWriter(builder, outputFile)) {
461
462
			FeatureMapper mapper = new FeatureMapper(config, file);
			CityDoctorModel model = mapper.getModel();
463
464
465
			boolean isInitialized = false;
			CityModelInfo cityModelInfo = null;
			while (reader.hasNext()) {
466
				CityGML chunk = reader.nextFeature();
467
468
469
470
471
472
473
				if (!isInitialized && writer != null) {
					ParentInfo parentInfo = reader.getParentInfo();
					cityModelInfo = new CityModelInfo(parentInfo);
					writer.setCityModelInfo(cityModelInfo);
					writer.writeStartDocument();
					isInitialized = true;
				}
474
475
476
				if (chunk instanceof AbstractCityObject) {
					AbstractCityObject ag = (AbstractCityObject) chunk;
					ag.accept(mapper);
477
478
					drainCityModel(model, cityObjectConsumer);
					writeAbstractCityObject(writer, ag);
479
480
481
482
				} else if (chunk instanceof CityModel) {
					CityModel cModel = (CityModel) chunk;
					cModel.unsetCityObjectMember();
					mapper.setCityModel(cModel);
483
484
485
486
					cityObjectConsumer.accept(cModel);
					writeCityModel(writer, cityModelInfo, cModel);
				} else if (chunk instanceof AbstractFeature && writer != null) {
					writer.writeFeatureMember((AbstractFeature) chunk);
487
				}
488

489
490
491
492
			}
			// end of stream
			logger.debug("End of gml file stream");
		} catch (CityGMLReadException e) {
Matthias Betz's avatar
Matthias Betz committed
493
			logger.error(Localization.getText("CityGmlParser.errorReadingGmlFile"), e.getMessage(), e);
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
		} catch (CityGMLWriteException e) {
			logger.error(Localization.getText("CityGmlParser.errorWritingGmlFile"), e.getMessage(), e);
		}
	}

	private static void writeAbstractCityObject(CityModelWriter writer, AbstractCityObject ag)
			throws CityGMLWriteException {
		if (writer != null) {
			writer.writeFeatureMember(ag);
		}
	}

	private static void writeCityModel(CityModelWriter writer, CityModelInfo cityModelInfo, CityModel cModel)
			throws CityGMLWriteException {
		if (writer != null) {
			for (ADEGenericElement genEle : cModel.getGenericADEElement()) {
				cityModelInfo.addGenericADEElement(genEle);
			}
			for (ADEComponent adeComp : cModel.getGenericApplicationPropertyOfCityModel()) {
				cityModelInfo.addGenericApplicationPropertyOfCityModel(adeComp);
			}
			writer.writeEndDocument();
		}
	}

519
520
	private static CityModelWriter createCityModelWriter(CityGMLBuilder builder, String outputFile)
			throws CityGMLWriteException {
521
522
		if (outputFile == null) {
			return null;
523
		}
524
		CityGMLOutputFactory factory = builder.createCityGMLOutputFactory();
525
		CityModelWriter writer = factory.createCityModelWriter(new File(outputFile), "UTF-8");
526
527
		writer.setPrefix("qual", QualityADEModule.NAMESPACE_URI);
		writer.setSchemaLocation(QualityADEModule.NAMESPACE_URI,
Matthias Betz's avatar
Matthias Betz committed
528
				QualityADEModule.NAMESPACE_URI + "/qualityAde.xsd");
529
530
531
532
		writer.setIndentString("  ");
		writer.setPrefixes(CityGMLVersion.DEFAULT);
		writer.setSchemaLocations(CityGMLVersion.DEFAULT);
		return writer;
533
534
535
536
537
538
539
540
541
542
543
	}

	private static void parseEpsgCodeFromFile(File file, ParserConfiguration config)
			throws IOException, ParserConfigurationException, SAXException {
		try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file))) {
			parseEpsgCodeFromStream(bis, config);
		}
	}

	private static void parseEpsgCodeFromStream(InputStream is, ParserConfiguration config)
			throws ParserConfigurationException, SAXException {
Matthias Betz's avatar
Matthias Betz committed
544
		SAXParser parser = FACTORY.newSAXParser();
545
546
547
548
549
		CityGmlHandler handler = new CityGmlHandler();
		try {
			parser.parse(new InputSource(is), handler);
		} catch (EnvelopeFoundException e) {
			try {
Matthias Betz's avatar
Matthias Betz committed
550
				parseCoordinateSystem(config, handler);
551
552
			} catch (Exception e2) {
				logger.debug("Exception while parsing for EPSG code", e2);
Matthias Betz's avatar
Matthias Betz committed
553
554
555
				if (logger.isWarnEnabled()) {
					logger.warn(Localization.getText("CityGmlParser.noEPSG"));
				}
556
557
558
			}
		} catch (Exception e) {
			logger.debug("Exception while parsing for EPSG code", e);
Matthias Betz's avatar
Matthias Betz committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
			if (logger.isWarnEnabled()) {
				logger.warn(Localization.getText("CityGmlParser.noEPSG"));
			}
		}
	}

	private static void parseCoordinateSystem(ParserConfiguration config, CityGmlHandler handler) {
		if (handler.getEpsg() == null) {
			return;
		}
		CoordinateReferenceSystem crs = crsFromSrsName(handler.getEpsg());
		if (crs == null) {
			// could not find a coordinate system for srsName
			// assuming metric system
			return;
		}
575
		if (crs.getProjection().getUnits() == Units.METRES) {
Matthias Betz's avatar
Matthias Betz committed
576
577
578
579
580
581
582
583
584
585
586
			// coordinate system is in meters, do not convert
			if (logger.isInfoEnabled()) {
				logger.info(Localization.getText("CityGmlParser.noConversionNeeded"));
			}
			return;
		}
		parseMeterConversion(config, crs);
		Vector3d low = handler.getLowerCorner();
		Vector3d up = handler.getUpperCorner();
		double centerLong = low.getX() + ((up.getX() - low.getX()) / 2);
		double centerLat = low.getY() + ((up.getY() - low.getY()) / 2);
587
		if (!crs.getName().equals(WGS_84)) {
Matthias Betz's avatar
Matthias Betz committed
588
			// need to convert coordinates first to WGS84, then find UTM Zone
589
			CoordinateReferenceSystem wgs84 = crsFromSrsName(WGS_84);
Matthias Betz's avatar
Matthias Betz committed
590
591
592
593
594
			ProjCoordinate p1 = new ProjCoordinate();
			p1.setValue(centerLong, centerLat);
			ProjCoordinate p2 = new ProjCoordinate();
			BasicCoordinateTransform bct = new BasicCoordinateTransform(crs, wgs84);
			bct.transform(p1, p2);
595
596
			centerLong = p2.x;
			centerLat = p2.y;
Matthias Betz's avatar
Matthias Betz committed
597
598
599
600
601
		}
		int zone = (int) (31 + Math.round(centerLong / 6));
		CoordinateReferenceSystem utm;
		if (centerLat < 0) {
			// south
Matthias Betz's avatar
Matthias Betz committed
602
603
			logger.info("Converting coordiante system to UTM zone {}S", zone);
			utm = CRS_FACTORY.createFromParameters("UTM", "+proj=utm +ellps=WGS84 +units=m +zone=" + zone + " +south");
Matthias Betz's avatar
Matthias Betz committed
604
605
		} else {
			// north
Matthias Betz's avatar
Matthias Betz committed
606
			logger.info("Converting coordiante system to UTM zone {}N", zone);
607
			utm = CRS_FACTORY.createFromParameters("UTM", "+proj=utm +ellps=WGS84 +units=m +zone=" + zone);
608
		}
Matthias Betz's avatar
Matthias Betz committed
609
		config.setCoordinateSystem(crs, utm);
610
611
612
613
614
615
616
617
618
619
620
621
622
	}

	private static void parseMeterConversion(ParserConfiguration config, CoordinateReferenceSystem crs) {
		Projection projection = crs.getProjection();
		double fromMetres = projection.getFromMetres();
		if (fromMetres > 0) {
			// also transform height information
			config.setFromMetres(fromMetres);
		} else {
			config.setFromMetres(1.0);
		}
	}

623
624
625
626
627
628
629
	private static void drainCityModel(CityDoctorModel model, CityGmlConsumer cityObjectConsumer) {
		drainCityObjectList(model.getBuildings(), cityObjectConsumer);
		drainCityObjectList(model.getBridges(), cityObjectConsumer);
		drainCityObjectList(model.getVegetation(), cityObjectConsumer);
		drainCityObjectList(model.getLand(), cityObjectConsumer);
		drainCityObjectList(model.getTransportation(), cityObjectConsumer);
		drainCityObjectList(model.getWater(), cityObjectConsumer);
630
631
	}

632
633
634
	private static void drainCityObjectList(List<? extends CityObject> objects, CityGmlConsumer cityObjectConsumer) {
		for (CityObject co : objects) {
			cityObjectConsumer.accept(co);
635
		}
636
		objects.clear();
637
638
	}
}