AlkisGreenEnricher.java 5.3 KB
Newer Older
Matthias Betz's avatar
Matthias Betz committed
1
2
3
4
package de.hft.stuttgart.citygml.green.alkis;

import java.io.IOException;
import java.net.MalformedURLException;
Eric Duminil's avatar
Eric Duminil committed
5
import java.net.http.HttpResponse;
Matthias Betz's avatar
Matthias Betz committed
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
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.citygml4j.core.model.core.CityModel;
import org.citygml4j.xml.CityGMLContextException;
import org.citygml4j.xml.reader.CityGMLReadException;
import org.citygml4j.xml.writer.CityGMLWriteException;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureSource;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.locationtech.jts.geom.MultiPolygon;
import org.locationtech.jts.geom.Polygon;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import de.hft.stuttgart.citygml.green.osm.GreenArea;
import de.hft.stuttgart.citygml.green.osm.GreenEnricher;
import de.hft.stuttgart.citygml.green.osm.LandUseArea;
import de.hft.stuttgart.citygml.green.osm.OsmData;
import de.hft.stuttgart.citygml.green.osm.RoadArea;
import de.hft.stuttgart.citygml.green.osm.TreeUtils;
import jakarta.xml.bind.JAXBException;

Eric Duminil's avatar
Eric Duminil committed
36
37
38
39

public class AlkisGreenEnricher
{

Matthias Betz's avatar
Matthias Betz committed
40
41
	private static Set<String> greenAreaTypes = new HashSet<>();
	private static Set<String> roadAreaTypes = new HashSet<>();
Eric Duminil's avatar
Eric Duminil committed
42

Matthias Betz's avatar
Matthias Betz committed
43
44
45
46
47
48
	static {
		greenAreaTypes.add("Wald");
		greenAreaTypes.add("Landwirtschaft");
		greenAreaTypes.add("Friedhof");
		greenAreaTypes.add("Gehölz");
		greenAreaTypes.add("Sport-, Freizeit- und Erholungsfläche");
Eric Duminil's avatar
Eric Duminil committed
49

Matthias Betz's avatar
Matthias Betz committed
50
51
52
		roadAreaTypes.add("Weg");
		roadAreaTypes.add("Straßenverkehr");
		roadAreaTypes.add("Bahnverkehr");
Eric Duminil's avatar
Eric Duminil committed
53

Matthias Betz's avatar
Matthias Betz committed
54
	}
Eric Duminil's avatar
Eric Duminil committed
55
56
57
58

	public static void main(String[] args)
			throws IOException, InterruptedException, CityGMLContextException, CityGMLReadException, JAXBException,
			CityGMLWriteException {
Matthias Betz's avatar
Matthias Betz committed
59
60
61
62
63
64
65
66
		System.out.println("Reading CityGML file");
		Path inFile = Paths.get(args[0]);
		CityModel cityModel = GreenEnricher.readCityGml(inFile);

		GreenEnricher.createTransformers(cityModel);

		OsmData osmData = new OsmData();
		String boundingBoxString = GreenEnricher.extractAndConvertBoundingBox(cityModel, osmData);
Eric Duminil's avatar
Eric Duminil committed
67
68
69
70
71
72
73
74
		String boundingBoxBasename = boundingBoxString.replace(",", "__").replace('.', '_');
		Path osmCache = Paths.get("data", "cache", "osm_response_" + boundingBoxBasename + ".xml");
		if (!Files.exists(osmCache)) {
			System.out.println("Downloading OSM data for " + boundingBoxString);
			HttpResponse<String> response = GreenEnricher.getOsmData(boundingBoxString);
			Files.write(osmCache, response.body().getBytes(StandardCharsets.UTF_8));
		}
		String osmResponse = Files.readString(osmCache);
Matthias Betz's avatar
Matthias Betz committed
75
76
77

		System.out.println("Parsing OSM response");
		GreenEnricher.parseOsmResponse(osmResponse, osmData);
Eric Duminil's avatar
Eric Duminil committed
78

Matthias Betz's avatar
Matthias Betz committed
79
80
		// ignore green areas from osm
		osmData.getGreenAreas().clear();
Eric Duminil's avatar
Eric Duminil committed
81

Matthias Betz's avatar
Matthias Betz committed
82
83
84
85
86
		parseAlkisData(osmData);


		System.out.println("Fit data in bounding box");
		GreenEnricher.fitToBoundingBox(osmData);
Eric Duminil's avatar
Eric Duminil committed
87

Matthias Betz's avatar
Matthias Betz committed
88
89
90
91
		GreenEnricher.convertGreenAreasToCityGML(cityModel, osmData.getGreenAreas());
		GreenEnricher.convertWaterAreasToCityGML(cityModel, osmData);
		GreenEnricher.convertRoadAreasToCityGML(cityModel, osmData);
		GreenEnricher.converLandUseAreasToCityGML(cityModel, osmData);
Eric Duminil's avatar
Eric Duminil committed
92

Matthias Betz's avatar
Matthias Betz committed
93
94
		TreeUtils.insertTrees(cityModel, osmData);

Eric Duminil's avatar
Eric Duminil committed
95

Matthias Betz's avatar
Matthias Betz committed
96
		GreenEnricher.clampToGround(cityModel);
Eric Duminil's avatar
Eric Duminil committed
97

Matthias Betz's avatar
Matthias Betz committed
98
99
		String inputString = inFile.getFileName().toString();
		String inputPathWithoutFileEnding = inputString.substring(0, inputString.lastIndexOf('.'));
Matthias Betz's avatar
Matthias Betz committed
100
		Path outputPath = Paths.get("data", inputPathWithoutFileEnding + "_with_alkis_greens_realistic.gml");
Matthias Betz's avatar
Matthias Betz committed
101
102
103
104
105
106
107
108
109
		System.out.println("Writing output file.");
		GreenEnricher.writeCityGML(cityModel, outputPath);
		System.out.println("Done");


	}

	private static void parseAlkisData(OsmData osmData) throws MalformedURLException, IOException {
		Path alkisDataPath = Paths.get("data", "tn_09663", "Nutzung.shp");
Eric Duminil's avatar
Eric Duminil committed
110

Matthias Betz's avatar
Matthias Betz committed
111
112
113
114
115
116
117
118
119
		Map<String, Object> readParameters = new HashMap<>();
		readParameters.put("url", alkisDataPath.toUri().toURL());
		readParameters.put("charset", StandardCharsets.UTF_8);
		DataStore dataStore = DataStoreFinder.getDataStore(readParameters);
		String typeName = dataStore.getTypeNames()[0];

		FeatureSource<SimpleFeatureType, SimpleFeature> source = dataStore.getFeatureSource(typeName);

		FeatureCollection<SimpleFeatureType, SimpleFeature> collection = source.getFeatures();
Eric Duminil's avatar
Eric Duminil committed
120

Matthias Betz's avatar
Matthias Betz committed
121
122
123
		List<GreenArea> greenAreas = osmData.getGreenAreas();
		List<RoadArea> roadAreas = osmData.getRoadAreas();
		List<LandUseArea> landUseAreas = osmData.getLandUseAreas();
Eric Duminil's avatar
Eric Duminil committed
124

Matthias Betz's avatar
Matthias Betz committed
125
126
127
		try (FeatureIterator<SimpleFeature> features = collection.features()) {
			while (features.hasNext()) {
				SimpleFeature feature = features.next();
Eric Duminil's avatar
Eric Duminil committed
128

Matthias Betz's avatar
Matthias Betz committed
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
				MultiPolygon geometry = (MultiPolygon) feature.getAttribute("the_geom");
				String nutzart = feature.getAttribute("nutzart").toString();
				if (geometry.getNumGeometries() > 1) {
					throw new IllegalStateException();
				}
				if (greenAreaTypes.contains(nutzart)) {
					greenAreas.add(new GreenArea((Polygon) geometry.getGeometryN(0)));
				} else if (roadAreaTypes.contains(nutzart)) {
					roadAreas.add(new RoadArea((Polygon) geometry.getGeometryN(0)));
				} else {
					landUseAreas.add(new LandUseArea((Polygon) geometry.getGeometryN(0)));
				}
			}
		}
	}

}