Jan 18, 2013

GPX decoding in Android, with time and ele.

Read last two article "Get Latitude and Longitude from gpx file" and "GPX decoding in Android, with custom class", it's further modified to decode "time" and "ele" also.

GPX decoding in Android, with time and ele.


package com.example.androidcodinggpx;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.location.Location;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  
  String path = Environment.getExternalStorageDirectory().toString() + "/temp/test.gpx";
  
  TextView textInfo = (TextView)findViewById(R.id.info);
  String info = "";
  
  File gpxFile = new File(path);
  info += gpxFile.getPath() +"\n\n";
  

  List<GpxNode> gpxList = decodeGPX(gpxFile);

  for(int i = 0; i < gpxList.size(); i++){
   info += gpxList.get(i).getGpsNodeInfo() + "\n";
  }
  
  textInfo.setText(info);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
 
 class GpxNode{
  
  Location location;
  String ele;
  String time;
  
  GpxNode(){
   location = null;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc){
   location = loc;
   ele = "";
   time = "";
  }
  
  GpxNode(Location loc, String e, String t){
   location = loc;
   ele = e;
   time = t;
  }
  
  void setEle(String e){
   ele = e;
  }
  
  void setTime(String t){
   time = t;
  }
  
  Location getLocation(){
   return location;
  }
  
  String getLocationString(){
   return location.getLatitude() + ":" + location.getLongitude();
  }
  
  String getGpsNodeInfo(){
   return location.getLatitude() + ":" + location.getLongitude() + "\n"
     + time + "\n"
     + ele + "\n";
  }
 }
 
 private List<GpxNode> decodeGPX(File file){
  List<GpxNode> list = new ArrayList<GpxNode>();

  DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
  try {
   DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
   FileInputStream fileInputStream = new FileInputStream(file);
   Document document = documentBuilder.parse(fileInputStream);
   Element elementRoot = document.getDocumentElement();
   
   NodeList nodelist_trkpt = elementRoot.getElementsByTagName("trkpt");

   for(int i = 0; i < nodelist_trkpt.getLength(); i++){
    
    Node node = nodelist_trkpt.item(i);
    NamedNodeMap attributes = node.getAttributes();
    
    String newLatitude = attributes.getNamedItem("lat").getTextContent();
    Double newLatitude_double = Double.parseDouble(newLatitude);
    
    String newLongitude = attributes.getNamedItem("lon").getTextContent();
    Double newLongitude_double = Double.parseDouble(newLongitude);
    
    String newLocationName = newLatitude + ":" + newLongitude;
    Location newLocation = new Location(newLocationName);
    newLocation.setLatitude(newLatitude_double);
    newLocation.setLongitude(newLongitude_double);
    
    NodeList childNodes = node.getChildNodes();
    
    String newEle = searchNodeListForTaget(childNodes, "ele");
    String newTime = searchNodeListForTaget(childNodes, "time");
    
    //insert in list
    GpxNode newGpxNode = new GpxNode(newLocation, newEle, newTime);
    list.add(newGpxNode);

   }
   
   fileInputStream.close();
   
  } catch (ParserConfigurationException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (SAXException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return list;
 }
 
 String searchNodeListForTaget(NodeList src, String target){
  
  for(int i = 0; i < src.getLength(); i++){
   Node n = src.item(i);
   if (n.getNodeName().equals(target)){
    return n.getFirstChild().getNodeValue();
   }
  }
  return "";
 }

}


No comments:

Post a Comment

Infolinks In Text Ads