Citygml3GeometryMapper.java 5.81 KB
Newer Older
Matthias Betz's avatar
Matthias Betz committed
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*-
 *  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.mapper.citygml3;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.locationtech.proj4j.BasicCoordinateTransform;
import org.locationtech.proj4j.ProjCoordinate;
import org.xmlobjects.gml.model.geometry.primitives.AbstractRing;
import org.xmlobjects.gml.model.geometry.primitives.AbstractRingProperty;
import org.xmlobjects.gml.model.geometry.primitives.Polygon;
import org.xmlobjects.gml.model.geometry.primitives.Ring;
import org.xmlobjects.gml.visitor.GeometryWalker;

import de.hft.stuttgart.citydoctor2.datastructure.ConcretePolygon;
import de.hft.stuttgart.citydoctor2.datastructure.GmlId;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing;
import de.hft.stuttgart.citydoctor2.datastructure.LinearRing.LinearRingType;
import de.hft.stuttgart.citydoctor2.datastructure.Vertex;
import de.hft.stuttgart.citydoctor2.parser.ParserConfiguration;
import de.hft.stuttgart.citydoctor2.utils.Localization;

public class Citygml3GeometryMapper extends GeometryWalker {
	
	private static final Logger logger = LogManager.getLogger(Citygml3GeometryMapper.class);
	
	private List<ConcretePolygon> polygons = new ArrayList<>();
	private LinearRing currentRing = null;
	private ParserConfiguration config;
	private ProjCoordinate p1 = new ProjCoordinate();
	private ProjCoordinate p2 = new ProjCoordinate();
	private Map<Vertex, Vertex> vertexMap;
	
	public Citygml3GeometryMapper(ParserConfiguration config, Map<Vertex, Vertex> vertices) {
		this.config = config;
		this.vertexMap = vertices;
	}
	
	@Override
	public void visit(Polygon polygon) {
		parsePolygon(polygon.getId(), polygon.getExterior(), polygon.getInterior());
		if (polygon.getExterior() == null) {
			System.out.println("No exterior: " + polygon.getId());
		}
	}
	
	private void parsePolygon(String id, AbstractRingProperty exterior, List<AbstractRingProperty> interior) {
		if (exterior == null || exterior.getObject() == null) {
			if (logger.isWarnEnabled()) {
				logger.warn(Localization.getText("GeometryMapper.emptyPolygon"));
			}
			return;
		}
		ConcretePolygon cdPoly = new ConcretePolygon();
		polygons.add(cdPoly);
		
		if (id != null) {
			cdPoly.setGmlId(new GmlId(id));
		}
		
		currentRing = new LinearRing(LinearRingType.EXTERIOR);
		exterior.getObject().accept(this);
		cdPoly.setExteriorRing(currentRing);
		
		for (AbstractRingProperty interiorGmlRing : interior) {
			AbstractRing gmlRing = interiorGmlRing.getObject();
			if (gmlRing == null) {
				continue;
			}
			currentRing = new LinearRing(LinearRingType.INTERIOR);
			gmlRing.accept(this);
			cdPoly.addInteriorRing(currentRing);
		}
	}
	
	@Override
	public void visit(org.xmlobjects.gml.model.geometry.primitives.LinearRing gmlRing) {
		if (gmlRing.getSrsDimension() != null && gmlRing.getSrsDimension() != 3) {
			logger.warn("Cannot handle geometry with srsDimension not 3");
			return;
		}
		if (gmlRing.getId() != null) {
			currentRing.setGmlId(new GmlId(gmlRing.getId()));
		}
		List<Double> coordinates = gmlRing.toCoordinateList3D();
		handleCoordinateList(coordinates);
	}
	
	@Override
	public void visit(Ring ring) {
		if (ring.getSrsDimension() != null && ring.getSrsDimension() != 3) {
			logger.warn("Cannot handle geometry with srsDimension not 3");
			return;
		}
		if (ring.getId() != null) {
			currentRing.setGmlId(new GmlId(ring.getId()));
		}
		List<Double> coordinates = ring.toCoordinateList3D();
		handleCoordinateList(coordinates);
	}
	
	private void handleCoordinateList(List<Double> coordinates) {
		if (coordinates.size() % 3 != 0) {
			String id;
			if (currentRing.hasExistingGmlId()) {
				id = currentRing.getGmlId().getGmlString();
			} else {
				id = "";
			}
			logger.warn("The number of coordinates for linear ring {} do not result in fully 3D points", id);
		}
		for (int i = 0; i < coordinates.size(); i = i + 3) {
			double x = coordinates.get(i + 0);
			double y = coordinates.get(i + 1);
			double z = coordinates.get(i + 2);
			createVertex(x, y, z);
		}
	}
	
	private void createVertex(double x, double y, double z) {
		// transform into utm, if available
		BasicCoordinateTransform trans = config.getTargetTransform();
		if (trans != null) {
			p1.setValue(x, y);
			trans.transform(p1, p2);
			x = p2.x;
			y = p2.y;
150
			z = z / config.getFromMeters();
Matthias Betz's avatar
Matthias Betz committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
		}
		x = round(x, config.getNumberOfRoundingPlaces());
		y = round(y, config.getNumberOfRoundingPlaces());
		z = round(z, config.getNumberOfRoundingPlaces());
		Vertex v = new Vertex(x, y, z);
		Vertex duplicate = vertexMap.get(v);
		if (duplicate == null) {
			vertexMap.put(v, v);
		} else {
			v = duplicate;
		}

		currentRing.addVertex(v);
	}
	
	private double round(double value, int places) {
		if (places < 0) {
			throw new IllegalArgumentException();
		}

		BigDecimal bd = BigDecimal.valueOf(value);
		bd = bd.setScale(places, RoundingMode.HALF_UP);
		return bd.doubleValue();
	}
	
	public List<ConcretePolygon> getPolygons() {
		return polygons;
	}

}