File: GpxHandler.java

package info (click to toggle)
gpsprune 10-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,220 kB
  • ctags: 3,013
  • sloc: java: 22,662; sh: 23; makefile: 16; python: 15
file content (180 lines) | stat: -rw-r--r-- 4,798 bytes parent folder | download
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
150
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
package tim.prune.load.xml;

import java.util.ArrayList;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;

import tim.prune.data.Field;


/**
 * Class for handling specifics of parsing Gpx files
 */
public class GpxHandler extends XmlHandler
{
	private boolean _insideWaypoint = false;
	private boolean _insideName = false;
	private boolean _insideElevation = false;
	private boolean _insideTime = false;
	private boolean _insideType = false;
	private boolean _startSegment = true;
	private String _name = null, _latitude = null, _longitude = null;
	private String _elevation = null;
	private String _time = null;
	private String _type = null;
	private ArrayList<String[]> _pointList = new ArrayList<String[]>();

	/**
	 * Receive the start of a tag
	 * @see org.xml.sax.ContentHandler#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
	 */
	public void startElement(String uri, String localName, String qName,
		Attributes attributes) throws SAXException
	{
		// Read the parameters for waypoints and track points
		if (qName.equalsIgnoreCase("wpt") || qName.equalsIgnoreCase("trkpt") || qName.equalsIgnoreCase("rtept"))
		{
			_insideWaypoint = qName.equalsIgnoreCase("wpt");
			int numAttributes = attributes.getLength();
			for (int i=0; i<numAttributes; i++)
			{
				String att = attributes.getQName(i);
				if (att.equals("lat")) {_latitude = attributes.getValue(i);}
				else if (att.equals("lon")) {_longitude = attributes.getValue(i);}
			}
			_elevation = null;
			_name = null;
			_time = null;
			_type = null;
		}
		else if (qName.equalsIgnoreCase("ele"))
		{
			_insideElevation = true;
		}
		else if (qName.equalsIgnoreCase("name"))
		{
			_insideName = true;
		}
		else if (qName.equalsIgnoreCase("time"))
		{
			_insideTime = true;
		}
		else if (qName.equalsIgnoreCase("type"))
		{
			_insideType = true;
		}
		else if (qName.equalsIgnoreCase("trkseg"))
		{
			_startSegment = true;
		}
		super.startElement(uri, localName, qName, attributes);
	}


	/**
	 * Process end tag
	 * @see org.xml.sax.ContentHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void endElement(String uri, String localName, String qName)
		throws SAXException
	{
		if (qName.equalsIgnoreCase("wpt") || qName.equalsIgnoreCase("trkpt") || qName.equalsIgnoreCase("rtept"))
		{
			processPoint();
		}
		else if (qName.equalsIgnoreCase("ele"))
		{
			_insideElevation = false;
		}
		else if (qName.equalsIgnoreCase("name"))
		{
			_insideName = false;
		}
		else if (qName.equalsIgnoreCase("time"))
		{
			_insideTime = false;
		}
		else if (qName.equalsIgnoreCase("type"))
		{
			_insideType = false;
		}
		super.endElement(uri, localName, qName);
	}


	/**
	 * Process character text (inside tags or between them)
	 * @see org.xml.sax.ContentHandler#characters(char[], int, int)
	 */
	public void characters(char[] ch, int start, int length)
		throws SAXException
	{
		String value = new String(ch, start, length);
		if (_insideName && _insideWaypoint) {_name = checkCharacters(_name, value);}
		else if (_insideElevation) {_elevation = checkCharacters(_elevation, value);}
		else if (_insideTime) {_time = checkCharacters(_time, value);}
		else if (_insideType) {_type = checkCharacters(_type, value);}
		super.characters(ch, start, length);
	}


	/**
	 * Check to concatenate partially-received values, if necessary
	 * @param inVariable variable containing characters received until now
	 * @param inValue new value received
	 * @return concatenation
	 */
	private static String checkCharacters(String inVariable, String inValue)
	{
		if (inVariable == null) {return inValue;}
		return inVariable + inValue;
	}


	/**
	 * Process a point, either a waypoint or track point
	 */
	private void processPoint()
	{
		// Put the values into a String array matching the order in getFieldArray()
		String[] values = new String[7];
		values[0] = _latitude; values[1] = _longitude;
		values[2] = _elevation; values[3] = _name;
		values[4] = _time;
		if (_startSegment && !_insideWaypoint) {
			values[5] = "1";
			_startSegment = false;
		}
		values[6] = _type;
		_pointList.add(values);
	}


	/**
	 * @see tim.prune.load.xml.XmlHandler#getFieldArray()
	 */
	public Field[] getFieldArray()
	{
		final Field[] fields = {Field.LATITUDE, Field.LONGITUDE, Field.ALTITUDE,
			Field.WAYPT_NAME, Field.TIMESTAMP, Field.NEW_SEGMENT, Field.WAYPT_TYPE};
		return fields;
	}


	/**
	 * Return the parsed information as a 2d array
	 * @see tim.prune.load.xml.XmlHandler#getDataArray()
	 */
	public String[][] getDataArray()
	{
		int numPoints = _pointList.size();
		// construct data array
		String[][] result = new String[numPoints][];
		for (int i=0; i<numPoints; i++)
		{
			result[i] = _pointList.get(i);
		}
		return result;
	}
}