BuildingXmlNode.java 2.51 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
package eu.simstadt.geo.fast_xml_parser;

import com.ximpleware.AutoPilot;
import com.ximpleware.NavException;
import com.ximpleware.VTDNav;
import com.ximpleware.XPathEvalException;
import com.ximpleware.XPathParseException;


public class BuildingXmlNode
{

	private int buildingOffset;
	private int buildingLength;
	private VTDNav navigator;
	private AutoPilot coordinatesFinder;
	public Double x;
	public Double xMin;
	public Double xMax;
	public Double yMin;
	public Double yMax;
	public Double y;
	private int coordinatesCount = 0;

	public BuildingXmlNode(VTDNav navigator, int buildingOffset, int buildingLength)
			throws XPathParseException, XPathEvalException, NavException {
		this.navigator = navigator;
		this.coordinatesFinder = new AutoPilot(navigator);
		this.buildingLength = buildingLength;
		this.buildingOffset = buildingOffset;
		extractCoordinates(); //NOTE: Should it be done lazily? Is there any reason to extract a BuildingXmlNode without coordinates?
	}

	public boolean hasCoordinates() {
		return coordinatesCount > 0;
	}

	private void extractCoordinates()
			throws XPathParseException, XPathEvalException, NavException {
		double xTotal = 0;
		double yTotal = 0;
		double tempX;
		double tempY;
		double tempXMin = Double.MAX_VALUE;
		double tempXMax = Double.MIN_VALUE;
		double tempYMin = Double.MAX_VALUE;
		double tempYMax = Double.MIN_VALUE;

		coordinatesFinder.selectXPath(".//posList|.//pos");
		while (coordinatesFinder.evalXPath() != -1) {
			long offsetAndLength = navigator.getContentFragment();
			int coordinatesOffset = (int) offsetAndLength;
			int coordinatesLength = (int) (offsetAndLength >> 32);
			String posList = navigator.toRawString(coordinatesOffset, coordinatesLength);
			String[] coordinates = posList.trim().split("\\s+");
			for (int k = 0; k < coordinates.length; k = k + 3) {
				coordinatesCount++;
				tempX = Double.valueOf(coordinates[k]);
				tempY = Double.valueOf(coordinates[k + 1]);
				if (tempX < tempXMin) {
					tempXMin = tempX;
				}
				if (tempY < tempYMin) {
					tempYMin = tempY;
				}
				if (tempX > tempXMax) {
					tempXMax = tempX;
				}
				if (tempY > tempYMax) {
					tempYMax = tempY;
				}
				xTotal += tempX;
				yTotal += tempY;
			}
		}
		this.xMin = tempXMin;
		this.xMax = tempXMax;
		this.yMin = tempYMin;
		this.yMax = tempYMax;
		this.x = xTotal / coordinatesCount;
		this.y = yTotal / coordinatesCount;
	}

	public String toString() {
		try {
			return navigator.toRawString(buildingOffset, buildingLength);
		} catch (NavException ex) {
			return "";
		}
	}
}