TreeKatasterData.java 2.61 KB
Newer Older
1
2
package de.hft.stuttgart.citygml.green.osm;

Matthias Betz's avatar
Matthias Betz committed
3
4
5
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
6
import java.util.ArrayList;
Matthias Betz's avatar
Matthias Betz committed
7
import java.util.HashMap;
8
import java.util.List;
Matthias Betz's avatar
Matthias Betz committed
9
10
11
12
13
14
15
16
17
18
import java.util.Map;

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.Point;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
19
20
21

public class TreeKatasterData {
	
Matthias Betz's avatar
Matthias Betz committed
22
23
24
	public static final double TRUNK_PERCENTAGE = 0.2;
	public static final double CROWN_PERCENTAGE = 1 - TRUNK_PERCENTAGE;
	
25
26
	private List<Tree> trees;
	
Matthias Betz's avatar
Matthias Betz committed
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
	public static TreeKatasterData parseTreeKatasterData(Path path) throws IOException {
		TreeKatasterData result = new TreeKatasterData();
		Map<String, Object> readParameters = new HashMap<>();
		readParameters.put("url", path.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();
		try (FeatureIterator<SimpleFeature> features = collection.features()) {
			while (features.hasNext()) {
				SimpleFeature feature = features.next();
				Tree tree = new Tree();

				Point p = (Point) feature.getAttribute("the_geom");
				tree.setPoint(p);

				String type = feature.getAttribute("Bezeichnun").toString();
				tree.setType(type);

				Object treeHeightObject = feature.getAttribute("Baumhöhe");
				if (treeHeightObject == null) {
					continue;
				}
				int treeHeight = Integer.parseInt(treeHeightObject.toString());
				double crownHeight = CROWN_PERCENTAGE * treeHeight;
				double trunkHeight = TRUNK_PERCENTAGE * treeHeight;
				tree.setCrownHeight(crownHeight);
				tree.setTrunkHeight(trunkHeight);

				Object crownWidth = feature.getAttribute("Kronenbrei");
				if (crownWidth == null) {
					continue;
				}
				tree.setCrownRadius(Float.parseFloat(crownWidth.toString()) / 2);
				Object trunkCirc = feature.getAttribute("Stammumfan");
				if (trunkCirc == null) {
					continue;
				}
				tree.setTrunkRadius(Integer.parseInt(trunkCirc.toString()) / (2 * Math.PI) / 100);

				result.getTrees().add(tree);
			}
		}
		return result;
	}
	
76
77
78
79
80
81
82
83
	public List<Tree> getTrees() {
		if (trees == null) {
			trees = new ArrayList<>();
		}
		return trees;
	}

}