From: erik Date: Tue, 8 Jul 2008 14:38:23 +0000 (+0000) Subject: track segments are now concatenated into one big track. X-Git-Tag: wamblee-utils-0.2@603~68 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=11b416ac11e1e60ea11f77c111e7464b52ab2b15;p=utils track segments are now concatenated into one big track. Based on a report of Payne Freret. --- diff --git a/trunk/gps/src/main/java/org/wamblee/gpx/GpxParser.java b/trunk/gps/src/main/java/org/wamblee/gpx/GpxParser.java index 30ced035..04379414 100644 --- a/trunk/gps/src/main/java/org/wamblee/gpx/GpxParser.java +++ b/trunk/gps/src/main/java/org/wamblee/gpx/GpxParser.java @@ -12,12 +12,13 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - */ + */ package org.wamblee.gpx; import java.io.InputStream; import java.util.Iterator; +import java.util.List; import org.dom4j.Document; import org.dom4j.Element; @@ -27,50 +28,55 @@ import org.wamblee.xml.DomUtils; import org.wamblee.xml.XMLException; /** - * Parser for GPX tracks. - * + * Parser for GPX tracks. + * * @author Erik Brakkee */ public class GpxParser { - - private static final String SCHEMA_RESOURCE = "gpx.xsd"; - - public GpxParser() { - // Empty. - } - - public Track parse(InputStream aIs) throws XMLException { - Document doc = DomUtils.convert(DomUtils.read(aIs)); - return parse(doc); - } - - /** - * @param doc - */ - public Track parse(Document doc) { - Track track = new Track(); - Element root = doc.getRootElement().element("trk").element("trkseg"); - for ( Iterator i =root.elementIterator("trkpt"); i.hasNext(); ) { - Element trkpt = (Element)i.next(); - track.addPoint(parseTrackPoint(trkpt)); - } - return track; - } - /** - * @param trkpt - */ - private TrackPoint parseTrackPoint(Element trkpt) { - //System.out.println(trkpt.asXML() + "|\n"); - double latitude = new Double(trkpt.attributeValue("lat")); - double longitude = new Double(trkpt.attributeValue("lon")); - Element ele = trkpt.element("ele"); - double elevation = 0.0; - if ( ele != null ) { - elevation = new Double(ele.getText()); - } - //System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + ele); - return new TrackPoint(latitude, longitude, elevation); - } + private static final String SCHEMA_RESOURCE = "gpx.xsd"; + + public GpxParser() { + // Empty. + } + + public Track parse(InputStream aIs) throws XMLException { + Document doc = DomUtils.convert(DomUtils.read(aIs)); + return parse(doc); + } + + /** + * @param doc + */ + public Track parse(Document doc) { + Track track = new Track(); + List segments = doc.getRootElement().element("trk").elements( + "trkseg"); + + for (Element segment : segments) { + for (Iterator i = segment.elementIterator("trkpt"); i.hasNext();) { + Element trkpt = (Element) i.next(); + track.addPoint(parseTrackPoint(trkpt)); + } + } + return track; + } + + /** + * @param trkpt + */ + private TrackPoint parseTrackPoint(Element trkpt) { + // System.out.println(trkpt.asXML() + "|\n"); + double latitude = new Double(trkpt.attributeValue("lat")); + double longitude = new Double(trkpt.attributeValue("lon")); + Element ele = trkpt.element("ele"); + double elevation = 0.0; + if (ele != null) { + elevation = new Double(ele.getText()); + } + // System.out.println(" lat = " + lat + " lon = " + lon + " ele = " + + // ele); + return new TrackPoint(latitude, longitude, elevation); + } } diff --git a/trunk/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java b/trunk/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java new file mode 100644 index 00000000..132b37bd --- /dev/null +++ b/trunk/gps/src/test/java/org/wamblee/gpx/GpxParserTest.java @@ -0,0 +1,21 @@ +package org.wamblee.gpx; + +import java.io.IOException; + +import org.wamblee.gps.track.Track; +import org.wamblee.io.ClassPathResource; +import org.wamblee.xml.XMLException; + +import junit.framework.TestCase; + +public class GpxParserTest extends TestCase { + + /** + * Tests parsing of a GPS file received by Payne Freret. + */ + public void testNoJoy() throws IOException, XMLException { + GpxParser parser = new GpxParser(); + Track track = parser.parse(new ClassPathResource("nojoy.gpx").getInputStream()); + assertEquals(468, track.size()); + } +} diff --git a/trunk/gps/src/test/resources/nojoy.gpx b/trunk/gps/src/test/resources/nojoy.gpx new file mode 100644 index 00000000..2dce3336 --- /dev/null +++ b/trunk/gps/src/test/resources/nojoy.gpx @@ -0,0 +1,1903 @@ + + + + + + + + + + ACTIVE LOG + GPS Tracklog + + + + + + 186.152 + + + + 186.633 + + + + 188.556 + + + + 192.401 + + + + 196.727 + + + + + + 199.611 + + + + 201.053 + + + + 198.650 + + + + 198.650 + + + + 198.169 + + + + 198.650 + + + + 199.130 + + + + 198.169 + + + + 200.092 + + + + 197.208 + + + + 197.208 + + + + 197.688 + + + + 199.611 + + + + 201.053 + + + + 201.534 + + + + 204.417 + + + + 204.898 + + + + 205.859 + + + + 207.782 + + + + 210.666 + + + + 210.666 + + + + 214.511 + + + + 214.511 + + + + 218.837 + + + + 229.412 + + + + 231.334 + + + + 232.776 + + + + 234.218 + + + + 238.064 + + + + 241.909 + + + + 246.715 + + + + 248.638 + + + + 251.041 + + + + 250.561 + + + + 251.522 + + + + 252.483 + + + + 252.483 + + + + 256.809 + + + + 260.655 + + + + 263.058 + + + + 262.577 + + + + 261.616 + + + + 261.616 + + + + 262.577 + + + + 262.577 + + + + + + 262.577 + + + + 285.168 + + + + 271.229 + + + + 271.229 + + + + 272.671 + + + + 272.671 + + + + 274.594 + + + + 275.555 + + + + 276.036 + + + + 276.997 + + + + 277.958 + + + + 279.400 + + + + 280.362 + + + + 283.726 + + + + 284.688 + + + + 284.688 + + + + 283.246 + + + + 282.765 + + + + 281.804 + + + + 280.362 + + + + 280.842 + + + + 280.842 + + + + 280.362 + + + + 279.881 + + + + 279.881 + + + + 279.881 + + + + 279.881 + + + + 281.804 + + + + 284.688 + + + + 287.091 + + + + 288.052 + + + + 289.975 + + + + 291.417 + + + + 291.417 + + + + 295.743 + + + + 301.030 + + + + 301.030 + + + + 301.991 + + + + 304.395 + + + + 304.395 + + + + 304.875 + + + + 304.875 + + + + 305.356 + + + + 306.317 + + + + 308.240 + + + + 308.720 + + + + 309.682 + + + + 308.240 + + + + 308.720 + + + + 310.162 + + + + 313.047 + + + + 317.853 + + + + 317.372 + + + + 314.488 + + + + 316.411 + + + + 318.334 + + + + 320.256 + + + + 323.621 + + + + 325.544 + + + + 327.466 + + + + 327.947 + + + + 328.428 + + + + 329.870 + + + + 326.986 + + + + 327.947 + + + + 331.312 + + + + 333.715 + + + + 336.118 + + + + 337.079 + + + + 338.041 + + + + 338.521 + + + + 338.521 + + + + 339.483 + + + + 339.483 + + + + 341.405 + + + + 343.809 + + + + 344.770 + + + + 343.328 + + + + 340.444 + + + + 340.444 + + + + 341.405 + + + + 343.328 + + + + 344.289 + + + + 345.731 + + + + 347.654 + + + + 349.577 + + + + 351.018 + + + + 349.577 + + + + 350.057 + + + + 347.173 + + + + 346.693 + + + + 346.693 + + + + 347.654 + + + + 347.654 + + + + 348.135 + + + + 349.096 + + + + 347.654 + + + + 346.693 + + + + 347.173 + + + + 344.770 + + + + 343.809 + + + + 345.731 + + + + 348.615 + + + + 350.538 + + + + 352.461 + + + + 354.383 + + + + 355.825 + + + + 354.864 + + + + 354.383 + + + + 355.825 + + + + 355.344 + + + + 355.825 + + + + 357.267 + + + + 356.786 + + + + 357.748 + + + + 357.748 + + + + 357.748 + + + + 358.709 + + + + 357.748 + + + + 358.228 + + + + 359.670 + + + + 360.632 + + + + 360.151 + + + + 359.190 + + + + 357.267 + + + + 356.306 + + + + 356.786 + + + + 358.228 + + + + 357.748 + + + + 357.748 + + + + 356.306 + + + + 357.748 + + + + 356.306 + + + + 360.151 + + + + 363.035 + + + + 363.996 + + + + 363.516 + + + + 362.554 + + + + 362.074 + + + + 360.632 + + + + 360.632 + + + + 360.151 + + + + 359.190 + + + + 357.267 + + + + 354.383 + + + + 353.903 + + + + 355.825 + + + + 355.344 + + + + 355.344 + + + + 350.538 + + + + 356.306 + + + + 356.786 + + + + 356.786 + + + + 356.786 + + + + 356.786 + + + + 356.786 + + + + 354.864 + + + + 354.864 + + + + 354.864 + + + + 358.228 + + + + 358.709 + + + + 360.632 + + + + 361.112 + + + + 362.554 + + + + 364.477 + + + + 365.438 + + + + 365.438 + + + + 367.842 + + + + 368.322 + + + + 369.284 + + + + 368.322 + + + + 369.284 + + + + 376.974 + + + + 376.494 + + + + 377.455 + + + + + + 379.377 + + + + 377.455 + + + + 373.129 + + + + 370.245 + + + + 368.322 + + + + 366.880 + + + + 364.958 + + + + 363.516 + + + + 363.035 + + + + 362.554 + + + + 360.632 + + + + 360.632 + + + + 361.112 + + + + 361.112 + + + + 358.228 + + + + 353.903 + + + + 353.903 + + + + 351.980 + + + + 353.903 + + + + 352.941 + + + + 353.422 + + + + 351.499 + + + + 352.461 + + + + 350.538 + + + + 348.135 + + + + 347.173 + + + + 347.173 + + + + 347.173 + + + + 348.615 + + + + 348.135 + + + + 350.538 + + + + 350.057 + + + + 350.538 + + + + 350.057 + + + + 350.538 + + + + 348.135 + + + + 344.770 + + + + 341.886 + + + + 340.925 + + + + 342.367 + + + + 341.886 + + + + 340.444 + + + + 339.963 + + + + 339.002 + + + + 339.002 + + + + 339.002 + + + + 339.483 + + + + 338.041 + + + + 336.118 + + + + 334.195 + + + + 333.715 + + + + 330.350 + + + + 328.908 + + + + 330.831 + + + + 331.312 + + + + 327.947 + + + + 326.024 + + + + 324.582 + + + + 322.660 + + + + 322.179 + + + + 322.660 + + + + 325.063 + + + + 324.582 + + + + 322.660 + + + + 316.892 + + + + 315.450 + + + + 315.930 + + + + 315.450 + + + + 313.047 + + + + 312.566 + + + + 314.008 + + + + 313.527 + + + + 311.604 + + + + 312.085 + + + + 313.527 + + + + 312.566 + + + + 311.124 + + + + 311.124 + + + + 310.643 + + + + 309.201 + + + + 308.240 + + + + 308.720 + + + + 308.720 + + + + 307.759 + + + + 307.279 + + + + 306.798 + + + + 306.317 + + + + 302.953 + + + + 301.991 + + + + 302.472 + + + + 302.953 + + + + 302.472 + + + + 301.511 + + + + 300.549 + + + + 298.627 + + + + 298.627 + + + + 295.262 + + + + 294.301 + + + + 293.820 + + + + 293.820 + + + + 293.820 + + + + 292.378 + + + + 292.859 + + + + 292.378 + + + + 292.378 + + + + 290.936 + + + + 289.975 + + + + 289.975 + + + + 288.533 + + + + 286.610 + + + + 287.091 + + + + 286.130 + + + + 284.688 + + + + 283.246 + + + + 282.284 + + + + 284.207 + + + + 280.842 + + + + 280.362 + + + + 277.478 + + + + 275.555 + + + + 273.152 + + + + 274.113 + + + + 273.152 + + + + 270.748 + + + + 271.229 + + + + 271.229 + + + + 273.152 + + + + 271.229 + + + + 271.229 + + + + 271.229 + + + + 270.268 + + + + 271.229 + + + + 271.710 + + + + 268.826 + + + + 269.787 + + + + 268.345 + + + + 267.865 + + + + 266.903 + + + + 266.903 + + + + 264.019 + + + + 264.500 + + + + 265.942 + + + + 266.422 + + + + 265.461 + + + + 265.942 + + + + 265.461 + + + + 263.539 + + + + 261.135 + + + + 260.174 + + + + 260.174 + + + + 258.251 + + + + 258.732 + + + + 260.174 + + + + 260.655 + + + + 262.097 + + + + 262.097 + + + + 263.058 + + + + 262.097 + + + + 261.616 + + + + 261.616 + + + + 261.135 + + + + 261.135 + + + + 258.251 + + + + 255.367 + + + + 258.732 + + + + 256.809 + + + + 257.290 + + + + 257.771 + + + + 256.809 + + + + 256.809 + + + + 259.213 + + + + 259.213 + + + + 257.771 + + + + 255.848 + + + + 255.367 + + + + 255.367 + + + + 255.848 + + + + 259.213 + + + + 258.732 + + + + 256.809 + + + + 253.445 + + + + 252.003 + + + + 251.522 + + + + 252.483 + + + + 251.522 + + + + 251.522 + + + + 253.445 + + + + 252.483 + + + + 252.483 + + + + 252.003 + + + + 250.080 + + + + 249.599 + + + + 249.599 + + + + 248.158 + + + + 249.599 + + + + 249.119 + + + + 248.158 + + + + 247.196 + + + + 247.196 + + + + 245.754 + + + + 243.351 + + + + 242.870 + + + + 242.390 + + + + 241.428 + + + + 239.505 + + + + 237.583 + + + + 237.583 + + + + 237.102 + + + + 235.660 + + + + 233.257 + + + + 231.815 + + + + 231.815 + + + + 232.296 + + + + 230.373 + + + + 227.970 + + + + 228.450 + + + + 225.086 + + + + 225.567 + + + + 224.125 + + + + 223.163 + + + + 221.721 + + + + 219.799 + + + + 217.395 + + + + 218.357 + + + + 217.876 + + + + 216.434 + + + + 216.434 + + + + 214.992 + + + + 214.031 + + + + 214.031 + + + + 210.666 + + + + 212.589 + + + + 211.147 + + + + 209.705 + + + + 207.782 + + + + 207.782 + + + + 206.340 + + + + 206.340 + + + + 206.340 + + + + 205.859 + + + + 203.937 + + + + 201.534 + + + + 201.534 + + + + + +