GreenEnricher.java 27.1 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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
111
112
113
114
package de.hft.stuttgart.citygml.green.osm;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URLEncoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.citygml4j.core.model.CityGMLVersion;
import org.citygml4j.core.model.building.Building;
import org.citygml4j.core.model.core.AbstractCityObject;
import org.citygml4j.core.model.core.AbstractCityObjectProperty;
import org.citygml4j.core.model.core.AbstractFeature;
import org.citygml4j.core.model.core.CityModel;
import org.citygml4j.core.model.vegetation.PlantCover;
import org.citygml4j.core.model.vegetation.SolitaryVegetationObject;
import org.citygml4j.core.model.waterbody.WaterBody;
import org.citygml4j.core.visitor.ObjectWalker;
import org.citygml4j.xml.CityGMLContext;
import org.citygml4j.xml.CityGMLContextException;
import org.citygml4j.xml.module.citygml.CoreModule;
import org.citygml4j.xml.reader.CityGMLInputFactory;
import org.citygml4j.xml.reader.CityGMLReadException;
import org.citygml4j.xml.reader.CityGMLReader;
import org.citygml4j.xml.writer.CityGMLOutputFactory;
import org.citygml4j.xml.writer.CityGMLWriteException;
import org.citygml4j.xml.writer.CityGMLWriter;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.LineString;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Point;
import org.locationtech.jts.geom.Polygon;
import org.locationtech.proj4j.BasicCoordinateTransform;
import org.locationtech.proj4j.CRSFactory;
import org.locationtech.proj4j.CoordinateReferenceSystem;
import org.locationtech.proj4j.ProjCoordinate;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xmlobjects.gml.model.geometry.DirectPosition;
import org.xmlobjects.gml.model.geometry.DirectPositionList;
import org.xmlobjects.gml.model.geometry.Envelope;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurface;
import org.xmlobjects.gml.model.geometry.aggregates.MultiSurfaceProperty;
import org.xmlobjects.gml.model.geometry.primitives.AbstractRingProperty;
import org.xmlobjects.gml.model.geometry.primitives.LinearRing;
import org.xmlobjects.gml.model.geometry.primitives.SurfaceProperty;

public class GreenEnricher {

	private static final int BOUNDING_BOX_INCREASE_IN_M = 100;

	// in degrees
	// add around 30m ( 1" = 30m -> 0.00277 = 900m)
	private static final double BOUNDING_BOX_INCREASE = 0.00277;

	private static final String OSM_STRING = """
						[bbox:{{bbox}}];
						(
			nwr["leisure"="garden"];
			nwr["landuse"="forest"];
			nwr["natural"="tree"];
			nwr["leisure"="park"];
			nwr["natural"="tree_row"];
			nwr["natural"="wood"];
			nwr["landuse"="grass"];
			nwr["landuse"="orchard"];
			nwr["natural"="scrub"];
			nwr["landuse"="meadow"];
			nwr["landuse"="farmland"];
			nwr["landuse"="allotments"];
			nwr["natural"="water"];
			nwr["water"="river"];
			nwr["type"="waterway"];
			nwr["waterway"="stream"];
			nwr["water"="pond"];
						);
						out geom;""";

	private static final URI OVERPASS_API_URI = URI.create("http://www.overpass-api.de/api/interpreter");
	private static final CRSFactory CRS_FACTORY = new CRSFactory();
	private static CoordinateReferenceSystem sourceCRS;
	private static CoordinateReferenceSystem targetCRS = CRS_FACTORY.createFromName("EPSG:4326");
	private static BasicCoordinateTransform transform;
	private static BasicCoordinateTransform backTransform;
	public static GeometryFactory geomFactory = new GeometryFactory();

	public static void main(String[] args) throws IOException, CityGMLContextException, CityGMLReadException,
			InterruptedException, ParserConfigurationException, SAXException, CityGMLWriteException {
		System.out.println("Reading CityGML file");
		Path inFile = Paths.get(args[0]);
		CityModel cityModel = readCityGml(inFile);
115
116
		
		
117
118
119
120
121
122
123
124
125
126
127

		String epsgCode = extractEpsgCode(cityModel);
		sourceCRS = CRS_FACTORY.createFromName(epsgCode);
		transform = new BasicCoordinateTransform(sourceCRS, targetCRS);
		backTransform = new BasicCoordinateTransform(targetCRS, sourceCRS);

		OsmData osmData = new OsmData();
		String boundingBoxString = extractAndConvertBoundingBox(cityModel, epsgCode, osmData);
//		HttpResponse<String> response = getOsmData(boundingBoxString);
//		Files.write(Path.of("osm_response.xml"), response.body().getBytes(StandardCharsets.UTF_8));
//		String osmResponse = response.body();
128
		String osmResponse = Files.readString(Paths.get("data", "osm_response.xml"));
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150

		System.out.println("Parsing OSM response");
		parseOsmResponse(osmResponse, osmData);
		createBoundingBox(cityModel, osmData);

//		List<GreenArea> newGreenAreas = new ArrayList<>();	

		System.out.println("Fit data in bounding box");
		fitToBoundingBox(osmData);

		System.out.println("Filter intersecting areas");
		List<GreenArea> greenAreas = osmData.getGreenAreas();
		removeDuplicateAreas(greenAreas);

		convertGreenAreasToCityGML(cityModel, greenAreas);
		convertWaterAreasToCityGML(cityModel, osmData);

		for (Waterway waterWay : osmData.getWaterways()) {
		}

		for (TreePoint tp : osmData.getTreePoints()) {
			double trunkRadius = 0.2;
151
			double trunkHeight = 3;
152
153
154
155
			Coordinate coordinate = tp.getPoint().getCoordinate();
			if (Double.isNaN(coordinate.z)) {
				coordinate.z = 0;
			}
156
			MultiSurface generatedTree = TreeGenerator.generateTree(coordinate, trunkRadius, trunkHeight, 3, 3, 10);
157
158
159
160
161
162
163
164
165
166
167
168
169
170
			SolitaryVegetationObject cover = new SolitaryVegetationObject();
			cover.setId(UUID.randomUUID().toString());
			cover.setLod2MultiSurface(new MultiSurfaceProperty(generatedTree));
			cityModel.getCityObjectMembers().add(new AbstractCityObjectProperty(cover));
		}
//
//		for (TreeRow tr : osmData.getTreeRows()) {
//			System.out.println(tr);
//		}

		clampToGround(cityModel);

		String inputString = inFile.getFileName().toString();
		String inputPathWithoutFileEnding = inputString.substring(0, inputString.lastIndexOf('.'));
171
		Path outputPath = Paths.get("data", inputPathWithoutFileEnding + "_with_greens.gml");
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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
		System.out.println("Writing output file.");
		writeCityGML(cityModel, outputPath);
		System.out.println("Done");

	}

	private static void convertWaterAreasToCityGML(CityModel cityModel, OsmData osmData) {
		for (WaterArea waterArea : osmData.getWaterAreas()) {
			WaterBody wb = new WaterBody();
			org.xmlobjects.gml.model.geometry.primitives.Polygon poly = convertToCityGmlPoly(waterArea.getArea());
			if (poly == null) {
				System.out.println("Skipping WaterBody " + waterArea.getArea());
				continue;
			}
			MultiSurface ms = new MultiSurface();
			wb.setId(UUID.randomUUID().toString());
			ms.getSurfaceMember().add(new SurfaceProperty(poly));
			wb.setLod0MultiSurface(new MultiSurfaceProperty(ms));
			cityModel.getCityObjectMembers().add(new AbstractCityObjectProperty(wb));
		}
	}

	private static void convertGreenAreasToCityGML(CityModel cityModel, List<GreenArea> greenAreas) {
		for (GreenArea ga : greenAreas) {
			org.xmlobjects.gml.model.geometry.primitives.Polygon poly = convertToCityGmlPoly(ga.getArea());
			if (poly == null) {
				System.out.println("Skipping " + ga.getArea());
				continue;
			}
			PlantCover cover = new PlantCover();
			MultiSurface ms = new MultiSurface();
			cover.setId(UUID.randomUUID().toString());
			ms.getSurfaceMember().add(new SurfaceProperty(poly));
			cover.setLod2MultiSurface(new MultiSurfaceProperty(ms));
			cityModel.getCityObjectMembers().add(new AbstractCityObjectProperty(cover));
		}
	}

	private static void fitToBoundingBox(OsmData osmData) {
		List<GreenArea> greenAreas = osmData.getGreenAreas();
		List<GreenArea> newGreenAreas = new ArrayList<>();
		for (GreenArea greenArea : greenAreas) {
			Polygon area = greenArea.getArea();
			Geometry intersection = area.intersection(osmData.getBoundingBox());
			if (intersection instanceof MultiPolygon multi) {
				Polygon poly1 = (Polygon) multi.getGeometryN(0);
				greenArea.setArea(poly1);
				for (int k = 1; k < multi.getNumGeometries(); k++) {
					GreenArea newGreenArea = new GreenArea();
					newGreenArea.setArea((Polygon) multi.getGeometryN(k));
					newGreenAreas.add(newGreenArea);
				}
			} else {
				greenArea.setArea((Polygon) intersection);
			}
		}
		greenAreas.addAll(newGreenAreas);
		
		clipWaterAreasToBoundingBox(osmData);
		
		for (Iterator<TreePoint> iterator = osmData.getTreePoints().iterator(); iterator.hasNext();) {
			TreePoint tp = iterator.next();
			if (!osmData.getBoundingBox().contains(tp.getPoint())) {
				iterator.remove();
			}
		}
		
	}

	private static void clipWaterAreasToBoundingBox(OsmData osmData) {
		List<WaterArea> newWaterAreas = new ArrayList<>();
		for (WaterArea waterArea : osmData.getWaterAreas()) {
			Polygon area = waterArea.getArea();
			Geometry intersection = area.intersection(osmData.getBoundingBox());
			if (intersection instanceof MultiPolygon multi) {
				Polygon poly1 = (Polygon) multi.getGeometryN(0);
				waterArea.setArea(poly1);
				for (int k = 1; k < multi.getNumGeometries(); k++) {
					WaterArea newWaterArea = new WaterArea();
					newWaterArea.setArea((Polygon) multi.getGeometryN(k));
					newWaterAreas.add(newWaterArea);
				}
			} else {
				waterArea.setArea((Polygon) intersection);
			}
		}
	}

	private static void removeDuplicateAreas(List<GreenArea> greenAreas) {
		for (int i = 0; i < greenAreas.size(); i++) {
			for (int j = i + 1; j < greenAreas.size(); j++) {
				GreenArea area1 = greenAreas.get(i);
				GreenArea area2 = greenAreas.get(j);
				if (area1.getArea().intersects(area2.getArea())) {
					Geometry difference = area1.getArea().difference(area2.getArea());
					System.out.println(difference);
					if (difference instanceof MultiPolygon) {
						MultiPolygon multi = (MultiPolygon) difference;
						Polygon poly1 = (Polygon) multi.getGeometryN(0);
						area1.setArea(poly1);
						for (int k = 1; k < multi.getNumGeometries(); k++) {
							GreenArea newGreenArea = new GreenArea();
							newGreenArea.setArea((Polygon) multi.getGeometryN(k));
							greenAreas.add(newGreenArea);
						}
					} else {
						area1.setArea((Polygon) difference);
					}

				}
			}
		}
	}

	private static void createBoundingBox(CityModel cityModel, OsmData osmData) {
		// TODO Auto-generated method stub

	}

//	private static MultiSurface generateTree(Point p) {
//		double radiusTrunk = 0.2;
//		
//		MultiSurface result = new MultiSurface();
//		
//		// trunk
//		Coordinate center = p.getCoordinate();
//		List<Coordinate> coords = new ArrayList<>();
//		coords.add(new Coordinate(center.x, center.y + radiusTrunk));
//		coords.add(new Coordinate(center.x + radiusTrunk / 2, center.y + Math.cos(0.5) * radiusTrunk));
//	}

	private static void clampToGround(CityModel cityModel) {
		for (AbstractCityObjectProperty afp : cityModel.getCityObjectMembers()) {
			AbstractCityObject af = afp.getObject();
			if (af instanceof Building b) {
				Envelope envelope = b.computeEnvelope();
				Double lowestZ = envelope.getLowerCorner().getValue().get(2);
				b.accept(new ObjectWalker() {
					@Override
					public void visit(LinearRing linearRing) {
						List<Double> values = linearRing.getControlPoints().getPosList().getValue();
						for (int i = 2; i < values.size(); i = i + 3) {
							values.set(i, values.get(i) - lowestZ);
						}
					}
				});
			}
		}
	}

	public static org.xmlobjects.gml.model.geometry.primitives.Polygon convertToCityGmlPoly(Polygon area) {
		if (area.getExteriorRing().getCoordinates().length == 0) {
			return null;
		}
		org.xmlobjects.gml.model.geometry.primitives.Polygon result = new org.xmlobjects.gml.model.geometry.primitives.Polygon();
		LinearRing lr = new LinearRing();
		result.setExterior(new AbstractRingProperty(lr));
		DirectPositionList posList = new DirectPositionList();
		lr.getControlPoints().setPosList(posList);
		List<Double> values = posList.getValue();
		for (Coordinate coord : area.getExteriorRing().getCoordinates()) {
			values.add(coord.x);
			values.add(coord.y);
			if (Double.isNaN(coord.z)) {
				coord.z = 0;
			}
			values.add(coord.z);
		}

		for (int i = 0; i < area.getNumInteriorRing(); i++) {
			LinearRing innerRing = new LinearRing();
			result.getInterior().add(new AbstractRingProperty(innerRing));
			DirectPositionList innerPosList = new DirectPositionList();
			innerRing.getControlPoints().setPosList(innerPosList);
			List<Double> innerValues = innerPosList.getValue();
			Coordinate[] coordinates = area.getInteriorRingN(i).getCoordinates();
			for (Coordinate coord : coordinates) {
				innerValues.add(coord.x);
				innerValues.add(coord.y);
				if (Double.isNaN(coord.z)) {
					coord.z = 0;
				}
				innerValues.add(coord.z);
			}
		}

		return result;
	}

	private static HttpResponse<String> getOsmData(String boundingBoxString) throws IOException, InterruptedException {
		String data = OSM_STRING.replace("{{bbox}}", boundingBoxString);
		String body = URLEncoder.encode("data", StandardCharsets.UTF_8) + "="
				+ URLEncoder.encode(data, StandardCharsets.UTF_8);

		HttpClient client = HttpClient.newHttpClient();
		HttpRequest request = HttpRequest.newBuilder().uri(OVERPASS_API_URI)
				.POST(HttpRequest.BodyPublishers.ofString(body)).build();
		System.out.println("Retrieving OSM data of bounding box: " + boundingBoxString);
		HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
		if (response.statusCode() != 200) {
			System.err.println("Failed to retrieve OSM data. Return code: " + response.statusCode());
			System.exit(1);
		}
		return response;
	}

	private static void writeCityGML(CityModel cityModel, Path outputPath)
			throws CityGMLWriteException, CityGMLContextException {
		CityGMLContext context = CityGMLContext.newInstance();
		CityGMLVersion version = CityGMLVersion.v2_0;
		CityGMLOutputFactory out = context.createCityGMLOutputFactory(version);

		try (CityGMLWriter writer = out.createCityGMLWriter(outputPath, StandardCharsets.UTF_8.name())) {
			writer.withIndent("  ").withDefaultSchemaLocations().withDefaultPrefixes()
					.withDefaultNamespace(CoreModule.of(version).getNamespaceURI()).write(cityModel);
		}
	}

	private static OsmData parseOsmResponse(String osmResponse, OsmData osmData)
			throws ParserConfigurationException, SAXException, IOException {
		Document document = createDomFromOsmResponse(osmResponse);

		// root element
		Node osmItem = document.getChildNodes().item(0);
		NodeList childNodes = osmItem.getChildNodes();

		Set<String> usedIds = new HashSet<>();

		for (int i = 0; i < childNodes.getLength(); i++) {
			Node node = childNodes.item(i);
			if (parseTreeIfPossible(osmData.getTreePoints(), node, usedIds)) {
				continue;
			}
			if (parseGreenAreasIfPossible(osmData, node, usedIds)) {
				continue;
			}
			parseGreenRelationsIfPossible(osmData, node, usedIds);
		}

		return osmData;
	}

	private static boolean parseGreenRelationsIfPossible(OsmData data, Node node, Set<String> usedIds) {
		if (!"relation".equals(node.getNodeName())) {
			return false;
		}
		String nodeValue = node.getAttributes().getNamedItem("id").getNodeValue();
		System.out.println("Relation: " + nodeValue);
		NodeList childNodes = node.getChildNodes();
		List<List<Coordinate>> innerRings = new ArrayList<>();
		List<Coordinate> outerRing = new ArrayList<>();
		boolean water = false;
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node boundsOrMember = childNodes.item(i);
			if (!"member".equals(boundsOrMember.getNodeName())) {
				if ("tag".equals(boundsOrMember.getNodeName())) {
					if ("water".equals(boundsOrMember.getAttributes().getNamedItem("v").getNodeValue())) {
						water = true;
					}
				}
				continue;
			}
			Node member = boundsOrMember;
			String role = member.getAttributes().getNamedItem("role").getNodeValue();
			if (!"way".equals(member.getAttributes().getNamedItem("type").getNodeValue())) {
				continue;
			}
			String referenceId = member.getAttributes().getNamedItem("ref").getNodeValue();
			if (usedIds.contains(referenceId)) {
				System.out.println("Relation contains way that is already used " + referenceId);
				continue;
			}
			List<Coordinate> ringCoords;
			if ("inner".equals(role)) {
				ringCoords = new ArrayList<>();
				innerRings.add(ringCoords);
			} else if ("outer".equals(role)) {
				// outer ring
				outerRing.clear();
				ringCoords = outerRing;
			} else if ("main_stream".equals(role)) {
				ringCoords = outerRing;
			} else if ("side_stream".equals(role)) {
				ringCoords = outerRing;
			} else {
				System.out.println("Unknown role: " + role);
				// probably waterway
				continue;
			}
			NodeList memberNodes = member.getChildNodes();
			for (int memberNodesIndex = 0; memberNodesIndex < memberNodes.getLength(); memberNodesIndex++) {
				Node nd = memberNodes.item(memberNodesIndex);
				if ("nd".equals(nd.getNodeName())) {
					double lat = Double.parseDouble(nd.getAttributes().getNamedItem("lat").getNodeValue());
					double lon = Double.parseDouble(nd.getAttributes().getNamedItem("lon").getNodeValue());
					ProjCoordinate converted = convertCoordinatesFrom84(lon, lat);
					ringCoords.add(new Coordinate(converted.x, converted.y));
				}
			}
		}

		if (water) {
			org.locationtech.jts.geom.LinearRing outerLinearRing = geomFactory
					.createLinearRing(outerRing.toArray(new Coordinate[outerRing.size()]));
			// create the inner rings
			List<org.locationtech.jts.geom.LinearRing> innerLinearRings = new ArrayList<>();
			for (List<Coordinate> innerRing : innerRings) {
				org.locationtech.jts.geom.LinearRing innerLinearRing = geomFactory
						.createLinearRing(innerRing.toArray(new Coordinate[innerRing.size()]));
				innerLinearRings.add(innerLinearRing);
			}

			Polygon polygon = geomFactory.createPolygon(outerLinearRing,
					innerLinearRings.toArray(new org.locationtech.jts.geom.LinearRing[innerLinearRings.size()]));

			data.getWaterAreas().add(new WaterArea(polygon));
		}

//		validateRing(outerRing);
		// create the outer ring
//		org.locationtech.jts.geom.LinearRing outerLinearRing = geomFactory
//				.createLinearRing(outerRing.toArray(new Coordinate[outerRing.size()]));
//
//		// create the inner rings
//		List<org.locationtech.jts.geom.LinearRing> innerLinearRings = new ArrayList<>();
//		for (List<Coordinate> innerRing : innerRings) {
//			org.locationtech.jts.geom.LinearRing innerLinearRing = geomFactory
//					.createLinearRing(innerRing.toArray(new Coordinate[innerRing.size()]));
//			innerLinearRings.add(innerLinearRing);
//		}
//
//		if (outerRing.isEmpty()) {
//			return false;
//		}
//
//		// create the polygon
//		Polygon polygon = geomFactory.createPolygon(outerLinearRing,
//				innerLinearRings.toArray(new org.locationtech.jts.geom.LinearRing[innerLinearRings.size()]));
//
//		data.getGreenAreas().add(new GreenArea(polygon));
		return true;

	}

	private static void validateRing(List<Coordinate> outerRing) {
		if (outerRing.isEmpty()) {
			return;
		}
		if (!outerRing.get(0).equals(outerRing.get(outerRing.size() - 1))) {
			// close ring
			outerRing.add(outerRing.get(0));
		}
	}

	private static boolean parseGreenAreasIfPossible(OsmData data, Node node, Set<String> usedIds) {
		if (!"way".equals(node.getNodeName())) {
			return false;
		}

		boolean water = false;
		String id = node.getAttributes().getNamedItem("id").getNodeValue();
		if (usedIds.contains(id)) {
			System.out.println("Already used way id " + id);
			return true;
		}

		usedIds.add(id);
		NodeList childNodes = node.getChildNodes();
		List<Coordinate> coordinates = new ArrayList<>();
		boolean line = false;
		for (int i = 0; i < childNodes.getLength(); i++) {
			Node child = childNodes.item(i);
			if ("nd".equals(child.getNodeName())) {
				double lat = Double.parseDouble(getTagValue(child, "lat"));
				double lon = Double.parseDouble(getTagValue(child, "lon"));
				ProjCoordinate converted = convertCoordinatesFrom84(lon, lat);
				coordinates.add(new Coordinate(converted.x, converted.y));
			} else if ("tag".equals(child.getNodeName())) {
//				if (existTagWithValue(child, "k", "natural") && existTagWithValue(child, "v", "tree_row")) {
//					line = true;
//				}
				if ((existTagWithValue(child, "k", "natural") && existTagWithValue(child, "v", "water"))
						|| existTagWithValue(child, "k", "waterway")) {
					water = true;
				}
			}
		}

		if (!coordinates.get(0).equals(coordinates.get(coordinates.size() - 1))) {
			// assume line if start and end coordinate do not match?
			line = true;
		}

		if (water) {
			if (line) {
				LineString lineString = geomFactory
						.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
				data.getWaterways().add(new Waterway(lineString));
			} else {
				Polygon polygon = geomFactory.createPolygon(coordinates.toArray(new Coordinate[coordinates.size()]));
				data.getWaterAreas().add(new WaterArea(polygon));
			}
		} else {
			if (line) {
				LineString lineString = geomFactory
						.createLineString(coordinates.toArray(new Coordinate[coordinates.size()]));
				data.getTreeRows().add(new TreeRow(lineString));
			} else {
				Polygon polygon = geomFactory.createPolygon(coordinates.toArray(new Coordinate[coordinates.size()]));
				data.getGreenAreas().add(new GreenArea(polygon));
			}
		}

		return true;
	}

	private static boolean existTagWithValue(Node node, String key, String value) {
		return value.equals(getTagValue(node, key));
	}

	private static String getTagValue(Node node, String tagName) {
		Node namedItem = node.getAttributes().getNamedItem(tagName);
		if (namedItem == null) {
			return null;
		}
		return namedItem.getNodeValue();
	}

	private static boolean parseTreeIfPossible(List<TreePoint> treePoints, Node node, Set<String> usedIds) {
		if (!"node".equals(node.getNodeName())) {
			return false;
		}
		String id = node.getAttributes().getNamedItem("id").getNodeValue();
		if (usedIds.contains(id)) {
			System.out.println("Already used node id " + id);
			return true;
		}
		usedIds.add(id);
		double lat = Double.parseDouble(node.getAttributes().getNamedItem("lat").getNodeValue());
		double lon = Double.parseDouble(node.getAttributes().getNamedItem("lon").getNodeValue());
		ProjCoordinate converted = convertCoordinatesFrom84(lon, lat);
		Point point = geomFactory.createPoint(new Coordinate(converted.x, converted.y));
		treePoints.add(new TreePoint(point));
		return true;
	}

	private static Document createDomFromOsmResponse(String body)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		// to be compliant, completely disable DOCTYPE declaration:
		factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
		// or completely disable external entities declarations:
		factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
		factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
		// or prohibit the use of all protocols by external entities:
		factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
		factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
		DocumentBuilder builder = factory.newDocumentBuilder();
		ByteArrayInputStream input = new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8));
		return builder.parse(input);
	}

	private static CityModel readCityGml(Path inFile) throws CityGMLContextException, CityGMLReadException {
		CityGMLContext context = CityGMLContext.newInstance();
		CityGMLInputFactory in = context.createCityGMLInputFactory();
		try (CityGMLReader reader = in.createCityGMLReader(inFile)) {
			AbstractFeature feature = reader.next();
			if (feature instanceof CityModel cm) {
				return cm;
			}
		}
		throw new IllegalStateException("CityGML does not contain a CityModel feature type");
	}

	private static String extractEpsgCode(CityModel cityModel) {
		Envelope envelope = cityModel.getBoundedBy().getEnvelope();
		String srsName = envelope.getSrsName();
		return parseEpsg(srsName);
	}

	private static String parseEpsg(String srsName) {
		int utmIndex = srsName.indexOf("UTM");
		if (utmIndex > 0) {
			return "EPSG:258" + srsName.substring(utmIndex + 3, utmIndex + 5);
		}
		Pattern p = Pattern.compile("EPSG:+([0-9]+)");
		Matcher m = p.matcher(srsName);
		if (m.find()) {
			return "EPSG:" + m.group(1);
		}
		return srsName;
	}

	private static String extractAndConvertBoundingBox(CityModel cityModel, String epsgCode, OsmData osmData) {
		Envelope calculatedEnvelope = cityModel.computeEnvelope();

		DirectPosition lowerCorner = calculatedEnvelope.getLowerCorner();
		ProjCoordinate p1 = new ProjCoordinate(lowerCorner.getValue().get(0), lowerCorner.getValue().get(1));
		ProjCoordinate lowerCornerProjected = new ProjCoordinate();
		transform.transform(p1, lowerCornerProjected);

		DirectPosition upperCorner = calculatedEnvelope.getUpperCorner();
		p1.x = upperCorner.getValue().get(0);
		p1.y = upperCorner.getValue().get(1);
		ProjCoordinate upperCornerProjected = new ProjCoordinate();
		transform.transform(p1, upperCornerProjected);

		lowerCornerProjected.x -= BOUNDING_BOX_INCREASE;
		lowerCornerProjected.y -= BOUNDING_BOX_INCREASE;
		upperCornerProjected.x += BOUNDING_BOX_INCREASE;
		upperCornerProjected.y += BOUNDING_BOX_INCREASE;

		List<Coordinate> bboxCoordinates = new ArrayList<>();
		bboxCoordinates.add(new Coordinate(lowerCorner.getValue().get(0) - BOUNDING_BOX_INCREASE_IN_M,
				lowerCorner.getValue().get(1) - BOUNDING_BOX_INCREASE_IN_M));
		bboxCoordinates.add(new Coordinate(lowerCorner.getValue().get(0) - BOUNDING_BOX_INCREASE_IN_M,
				upperCorner.getValue().get(1) + BOUNDING_BOX_INCREASE_IN_M));
		bboxCoordinates.add(new Coordinate(upperCorner.getValue().get(0) + BOUNDING_BOX_INCREASE_IN_M,
				upperCorner.getValue().get(1) + BOUNDING_BOX_INCREASE_IN_M));
		bboxCoordinates.add(new Coordinate(upperCorner.getValue().get(0) + BOUNDING_BOX_INCREASE_IN_M,
				lowerCorner.getValue().get(1) - BOUNDING_BOX_INCREASE_IN_M));
		bboxCoordinates.add(new Coordinate(lowerCorner.getValue().get(0) - BOUNDING_BOX_INCREASE_IN_M,
				lowerCorner.getValue().get(1) - BOUNDING_BOX_INCREASE_IN_M));

		Polygon poly = geomFactory.createPolygon(bboxCoordinates.toArray(new Coordinate[bboxCoordinates.size()]));
		osmData.setBoundingBox(poly);

		return lowerCornerProjected.y + "," + lowerCornerProjected.x + "," + upperCornerProjected.y + ","
				+ upperCornerProjected.x;
	}

	private static ProjCoordinate convertCoordinatesFrom84(double x, double y) {
		ProjCoordinate p1 = new ProjCoordinate(x, y);
		ProjCoordinate result = new ProjCoordinate();
		backTransform.transform(p1, result);
		return result;
	}

	private static ProjCoordinate convertCoordinatesTo84(DirectPosition point) {
		double x = point.getValue().get(0);
		double y = point.getValue().get(1);
		return convertCoordinatesTo84(x, y);
	}

	private static ProjCoordinate convertCoordinatesTo84(double x, double y) {
		ProjCoordinate p1 = new ProjCoordinate(x, y);
		ProjCoordinate result = new ProjCoordinate();
		transform.transform(p1, result);
		return result;
	}

}