Newer
Older
adjustgpx-core / src / osm / jp / gpx / ElementMapTRKSEG.java
@haya4 haya4 on 18 Aug 2019 4 KB Java11 - AdjustTime2
  1. package osm.jp.gpx;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5. import java.text.ParseException;
  6. import java.util.Date;
  7. import java.util.TreeMap;
  8.  
  9. import javax.xml.parsers.DocumentBuilder;
  10. import javax.xml.parsers.DocumentBuilderFactory;
  11. import javax.xml.parsers.ParserConfigurationException;
  12.  
  13. import org.w3c.dom.*;
  14. import org.xml.sax.SAXException;
  15.  
  16. @SuppressWarnings("serial")
  17. public class ElementMapTRKSEG extends TreeMap<Date, ElementMapTRKPT> {
  18. /**
  19. * TESTing
  20. * @param argv
  21. * @throws ParseException
  22. * @throws ParserConfigurationException
  23. * @throws IOException
  24. * @throws SAXException
  25. * @throws DOMException
  26. */
  27. public static void main(String[] argv) throws DOMException, SAXException, IOException, ParserConfigurationException, ParseException {
  28. ElementMapTRKSEG mapTRKSEG = null;
  29. mapTRKSEG = new ElementMapTRKSEG();
  30. mapTRKSEG.parse(new File("testdata/cameradata/separate.gpx"));
  31. mapTRKSEG.printinfo();
  32. }
  33. public ElementMapTRKSEG() {
  34. super(new TimeComparator());
  35. }
  36.  
  37. /**
  38. * GPXファイルをパースする
  39. * @code{
  40. * <gpx>
  41. * <trk>
  42. * <trkseg>
  43. * <trkpt lat="35.32123832" lon="139.56965631">
  44. * <ele>47.20000076293945</ele>
  45. * <time>2012-06-15T03:00:29Z</time>
  46. * <hdop>0.5</hdop>
  47. * </trkpt>
  48. * </trkseg>
  49. * </trk>
  50. * </gpx>
  51. * }
  52. *
  53. * @param gpxFile
  54. * @return Document
  55. * @throws SAXException
  56. * @throws IOException
  57. * @throws ParserConfigurationException
  58. * @throws DOMException
  59. * @throws ParseException
  60. */
  61. public Document parse(File gpxFile) throws SAXException, IOException, ParserConfigurationException, DOMException, ParseException {
  62. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  63. DocumentBuilder builder = factory.newDocumentBuilder();
  64. factory.setIgnoringElementContentWhitespace(true);
  65. factory.setIgnoringComments(true);
  66. factory.setValidating(true);
  67. Node gpx = builder.parse(gpxFile).getFirstChild();
  68. Document document = gpx.getOwnerDocument();
  69. NodeList nodes = gpx.getChildNodes();
  70. for (int i=0; i < nodes.getLength(); i++) {
  71. Node node2 = nodes.item(i);
  72. if (node2.getNodeName().equals("trk")) {
  73. Element trk = (Element) node2;
  74. NodeList nodes1 = trk.getChildNodes();
  75. for (int i1=0; i1 < nodes1.getLength(); i1++) {
  76. Node nodeTRKSEG = nodes1.item(i1);
  77. if (nodeTRKSEG.getNodeName().equals("trkseg")) {
  78. this.put(nodeTRKSEG);
  79. }
  80. }
  81. }
  82. }
  83. return document;
  84. }
  85.  
  86. /**
  87. * @code{
  88. * 拡張put value:Node<TRKSEG>をputするとNode<TRKSEG>内のNode<TRKSPT>を put(key,value)する。
  89. * }
  90. * @param nodeTRKSEG
  91. * @throws ParseException
  92. * @throws DOMException
  93. */
  94. public void put(Node nodeTRKSEG) throws DOMException, ParseException {
  95. if (nodeTRKSEG.getNodeName().equals("trkseg")) {
  96. NodeList nodes2 = nodeTRKSEG.getChildNodes();
  97. ElementMapTRKPT mapTRKPT = new ElementMapTRKPT();
  98. for (int i2 = 0; i2 < nodes2.getLength(); i2++) {
  99. Node nodeTRKPT = nodes2.item(i2);
  100. if (nodeTRKPT.getNodeName().equals("trkpt")) {
  101. if (ImportPicture.param_GpxNoFirstNode && (i2 == 0)) {
  102. continue;
  103. }
  104. mapTRKPT.put(new TagTrkpt((Element)nodeTRKPT));
  105. }
  106. }
  107. this.put(mapTRKPT);
  108. }
  109. }
  110. /**
  111. * 拡張put value:ElementMapTRKPTをputするとElementMapTRKPT内の最初のエントリのtimeを読み取ってkeyとしてthis.put(key,value)する。
  112. * @param value
  113. * @throws DOMException
  114. */
  115. public void put(ElementMapTRKPT value) {
  116. for (Date key : value.keySet()) {
  117. this.put(key, value);
  118. return;
  119. }
  120. }
  121. public void printinfo() {
  122. System.out.println(" +--------------------+--------------------|");
  123. System.out.println(" GPS logging time | First Time | Last Time |");
  124. System.out.println("|--------------------------------+--------------------+--------------------|");
  125. for (java.util.Map.Entry<Date, ElementMapTRKPT> map : this.entrySet()) {
  126. ElementMapTRKPT mapTRKPT = map.getValue();
  127. mapTRKPT.printinfo();
  128. }
  129. System.out.println("|--------------------------------+--------------------+--------------------|");
  130. System.out.println();
  131. }
  132. }