2009-03-23 11 views
2

J'essaye d'analyser un GPX file. J'ai essayé avec JDOM, mais ça ne marche pas très bien.Comment analyser les fichiers GPX avec SAXReader?

SAXBuilder builder = new SAXBuilder(); 
Document document = builder.build(filename); 
Element root = document.getRootElement(); 
System.out.println("Root:\t" + root.getName()); 
List<Element> listTrks = root.getChildren("trk"); 
System.out.println("Count trk:\t" + listTrks.size()); 
for (Element tmpTrk : listTrks) { 
    List<Element> listTrkpts = tmpTrk.getChildren("trkpt"); 
    System.out.println("Count pts:\t" + listTrkpts.size()); 
    for (Element tmpTrkpt : listTrkpts) { 
    System.out.println(tmpTrkpt.getAttributeValue("lat") + ":" + tmpTrkpt.getAttributeValue("lat")); 
    } 
} 

J'ai ouvert la example file (CC-BY-SA OpenStreetMap) et la sortie est juste:

Racine: GPX
Nombre TRK: 0

Que puis-je faire? Dois-je utiliser un SAXParserFactory (javax.xml.parsers.SAXParserFactory) et implémenter une classe Handler?

Répondre

4

mon lecteur gpx. Il ignore certaines balises mais j'espère que cela aidera.

package ch.perry.rando.geocode; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
import javax.xml.parsers.ParserConfigurationException; 
import javax.xml.parsers.SAXParser; 
import javax.xml.parsers.SAXParserFactory; 
import org.xml.sax.Attributes; 
import org.xml.sax.SAXException; 
import org.xml.sax.helpers.DefaultHandler; 

/** 
* 
* @author perrym 
*/ 
public class GpxReader extends DefaultHandler { 
    private static final DateFormat TIME_FORMAT 
      = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'"); 

    private List<Trackpoint> track = new ArrayList<Trackpoint>(); 
    private StringBuffer buf = new StringBuffer(); 
    private double lat; 
    private double lon; 
    private double ele; 
    private Date time; 

    public static Trackpoint[] readTrack(InputStream in) throws IOException { 
     try { 
      SAXParserFactory factory = SAXParserFactory.newInstance(); 
      factory.setValidating(true); 
      SAXParser parser = factory.newSAXParser(); 
      GpxReader reader = new GpxReader(); 
      parser.parse(in, reader); 
      return reader.getTrack(); 
     } catch (ParserConfigurationException e) { 
      throw new IOException(e.getMessage()); 
     } catch (SAXException e) { 
      throw new IOException(e.getMessage()); 
     } 
    } 

    public static Trackpoint[] readTrack(File file) throws IOException { 
     InputStream in = new FileInputStream(file); 
     try { 
      return readTrack(in); 
     } finally { 
      in.close(); 
     } 
    } 

    @Override 
    public void startElement(String uri, String localName, String qName, 
      Attributes attributes) throws SAXException { 
     buf.setLength(0); 
     if (qName.equals("trkpt")) { 
      lat = Double.parseDouble(attributes.getValue("lat")); 
      lon = Double.parseDouble(attributes.getValue("lon")); 
     } 
    } 

    @Override 
    public void endElement(String uri, String localName, String qName) 
      throws SAXException { 
     if (qName.equals("trkpt")) { 
      track.add(Trackpoint.fromWGS84(lat, lon, ele, time)); 
     } else if (qName.equals("ele")) { 
      ele = Double.parseDouble(buf.toString()); 
     } else if (qName.equals("")) { 
      try { 
       time = TIME_FORMAT.parse(buf.toString()); 
      } catch (ParseException e) { 
       throw new SAXException("Invalid time " + buf.toString()); 
      } 
     } 
    } 

    @Override 
    public void characters(char[] chars, int start, int length) 
      throws SAXException { 
     buf.append(chars, start, length); 
    } 

    private Trackpoint[] getTrack() { 
     return track.toArray(new Trackpoint[track.size()]); 
    } 
} 
1

Pour lire les fichiers GPX facilement en Java voir: http://sourceforge.net/p/gpsanalysis/wiki/Home/
exemple:
// obtient des points d'un fichier GPX
liste finale points = GpxFileDataAccess.getPoints (new File ("/ chemin/toGpxFile. gpx "));

1

prêt à l'emploi, open source et GpxParser java entièrement fonctionnel (et beaucoup plus) ici https://sourceforge.net/projects/geokarambola/

Détails ici https://plus.google.com/u/0/communities/110606810455751902142

Avec la bibliothèque ci-dessus l'analyse d'un fichier GPX est une seule ligne:

Gpx gpx = GpxFileIo.parseIn("SomeGeoCollection.gpx") ; 

Obtenir ses points, routes ou pistes trivial aussi:

for(Point pt: gpx.getPoints()) 
    Location loc = new Location(pt.getLatitude(), pt.getLongitude()) ;