CityObjectMember.java 2.88 KB
Newer Older
1
package eu.simstadt.regionchooser.fast_xml_parser;
2
3
4
5
6
7
8
9

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


10
11
12
13
14
15
/**
 * XML Node representing a CityObjectMember
 * 
 */

public class CityObjectMember
16
{
Eric Duminil's avatar
Eric Duminil committed
17
	static final String XPATH_PATTERN = "/CityModel/cityObjectMember[Building or SolitaryVegetationObject or PlantCover or LandUse or Road]";
18

19
20
	private int nodeOffset;
	private int nodeLength;
21
22
23
24
25
26
27
28
29
	private VTDNav navigator;
	public Double x;
	public Double xMin;
	public Double xMax;
	public Double yMin;
	public Double yMax;
	public Double y;
	private int coordinatesCount = 0;

30
	public CityObjectMember(VTDNav navigator, int nodeOffset, int nodeLength)
31
32
			throws XPathParseException, XPathEvalException, NavException {
		this.navigator = navigator;
33
34
		this.nodeLength = nodeLength;
		this.nodeOffset = nodeOffset;
Eric Duminil's avatar
Notes.    
Eric Duminil committed
35
		extractCoordinates();
36
37
38
39
40
41
	}

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

42
43
44
45
46
47
48
49
50
51
52
53
54
	public boolean isBuilding() {
		try {
			this.navigator.push();
			AutoPilot checkBuilding = new AutoPilot(this.navigator);
			checkBuilding.selectXPath("./Building");
			return checkBuilding.evalXPath() != -1;
		} catch (XPathEvalException | NavException | XPathParseException ex) {
			return false;
		} finally {
			this.navigator.pop();
		}
	}

55
56
57
58
59
60
61
62
63
64
65
	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;

66
67
68

		AutoPilot coordinatesFinder = new AutoPilot(navigator);

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
		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 {
106
			return navigator.toRawString(nodeOffset, nodeLength);
107
108
109
110
111
		} catch (NavException ex) {
			return "";
		}
	}
}