CityObjectMember.java 2.76 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
{
17
18
19
	//TODO: Check it's the only correct possibility.
	//FIXME: BuildingPart too!
	static final String XPATH_PATTERN = "/CityModel/cityObjectMember[Building or SolitaryVegetationObject or PlantCover]";
20

21
22
	private int nodeOffset;
	private int nodeLength;
23
24
25
26
27
28
29
30
31
32
	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;

33
	public CityObjectMember(VTDNav navigator, int nodeOffset, int nodeLength)
34
35
36
			throws XPathParseException, XPathEvalException, NavException {
		this.navigator = navigator;
		this.coordinatesFinder = new AutoPilot(navigator);
37
38
		this.nodeLength = nodeLength;
		this.nodeOffset = nodeOffset;
Eric Duminil's avatar
Notes.    
Eric Duminil committed
39
		extractCoordinates();
40
41
		//TODO: Get Node ID too, in order to avoid duplicates?
		//TODO: Now that "Building" can be Vegetation too, define a method to check class?
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
	}

	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 {
96
			return navigator.toRawString(nodeOffset, nodeLength);
97
98
99
100
101
		} catch (NavException ex) {
			return "";
		}
	}
}