source: josm/trunk/src/org/openstreetmap/josm/io/rtklib/RtkLibPosReader.java@ 18048

Last change on this file since 18048 was 18048, checked in by Don-vip, 4 years ago

fix #20677 - fix wrong parsing of RTKLIB .pos files

  • Property svn:eol-style set to native
File size: 5.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io.rtklib;
3
4import java.io.BufferedReader;
5import java.io.IOException;
6import java.io.InputStream;
7import java.io.InputStreamReader;
8import java.nio.charset.StandardCharsets;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.Objects;
13
14import org.openstreetmap.josm.data.coor.LatLon;
15import org.openstreetmap.josm.data.gpx.GpxConstants;
16import org.openstreetmap.josm.data.gpx.GpxData;
17import org.openstreetmap.josm.data.gpx.GpxTrack;
18import org.openstreetmap.josm.data.gpx.WayPoint;
19import org.openstreetmap.josm.io.IGpxReader;
20import org.openstreetmap.josm.tools.Logging;
21import org.openstreetmap.josm.tools.date.DateUtils;
22import org.xml.sax.SAXException;
23
24/**
25 * Reads a RTKLib Positioning Solution file.
26 * <p>
27 * See <a href="https://github.com/tomojitakasu/RTKLIB/blob/rtklib_2.4.3/doc/manual_2.4.2.pdf">RTKLIB Manual</a>.
28 * @since 15247
29 */
30public class RtkLibPosReader implements IGpxReader {
31
32 private static final int IDX_DATE = 0;
33 private static final int IDX_TIME = 1;
34 private static final int IDX_LAT = 2;
35 private static final int IDX_LON = 3;
36 private static final int IDX_HEIGHT = 4;
37 private static final int IDX_Q = 5;
38 private static final int IDX_NS = 6;
39 private static final int IDX_SDN = 7;
40 private static final int IDX_SDE = 8;
41 private static final int IDX_SDU = 9;
42 private static final int IDX_SDNE = 10;
43 private static final int IDX_SDEU = 11;
44 private static final int IDX_SDUN = 12;
45 private static final int IDX_AGE = 13;
46 private static final int IDX_RATIO = 14;
47
48 private final InputStream source;
49 private GpxData data;
50 private int success; // number of successfully parsed lines
51
52 /**
53 * Constructs a new {@code RtkLibPosReader}
54 * @param source RTKLib .pos file input stream
55 * @throws IOException if an I/O error occurs
56 */
57 public RtkLibPosReader(InputStream source) throws IOException {
58 this.source = Objects.requireNonNull(source);
59 }
60
61 @Override
62 public boolean parse(boolean tryToFinish) throws SAXException, IOException {
63 data = new GpxData();
64 Collection<Collection<WayPoint>> currentTrack = new ArrayList<>();
65 Collection<WayPoint> waypoints = new ArrayList<>();
66 try (BufferedReader rd = new BufferedReader(new InputStreamReader(source, StandardCharsets.UTF_8))) {
67 String line;
68 do {
69 line = rd.readLine();
70 if (line != null) {
71 if (line.startsWith("% ref pos :")) {
72 // TODO add marker
73 } else if (!line.startsWith("%")) {
74 try {
75 String[] fields = line.split("[ ]+", -1);
76 WayPoint currentwp = new WayPoint(new LatLon(
77 Double.parseDouble(fields[IDX_LAT]),
78 Double.parseDouble(fields[IDX_LON])));
79 currentwp.put(GpxConstants.PT_ELE, fields[IDX_HEIGHT]);
80 currentwp.setInstant(DateUtils.parseInstant(fields[IDX_DATE]+" "+fields[IDX_TIME]));
81 currentwp.put(GpxConstants.RTKLIB_Q, Integer.parseInt(fields[IDX_Q]));
82 currentwp.put(GpxConstants.PT_SAT, fields[IDX_NS]);
83 currentwp.put(GpxConstants.RTKLIB_SDN, fields[IDX_SDN]);
84 currentwp.put(GpxConstants.RTKLIB_SDE, fields[IDX_SDE]);
85 currentwp.put(GpxConstants.RTKLIB_SDU, fields[IDX_SDU]);
86 currentwp.put(GpxConstants.RTKLIB_SDNE, fields[IDX_SDNE]);
87 currentwp.put(GpxConstants.RTKLIB_SDEU, fields[IDX_SDEU]);
88 currentwp.put(GpxConstants.RTKLIB_SDUN, fields[IDX_SDUN]);
89 currentwp.put(GpxConstants.RTKLIB_AGE, fields[IDX_AGE]);
90 currentwp.put(GpxConstants.RTKLIB_RATIO, fields[IDX_RATIO]);
91 double sdn = Double.parseDouble(fields[IDX_SDN]);
92 double sde = Double.parseDouble(fields[IDX_SDE]);
93 currentwp.put(GpxConstants.PT_HDOP, (float) Math.sqrt(sdn*sdn + sde*sde));
94 waypoints.add(currentwp);
95 success++;
96 } catch (IllegalArgumentException e) {
97 Logging.error(e);
98 }
99 }
100 }
101 } while (line != null);
102 }
103 currentTrack.add(waypoints);
104 data.tracks.add(new GpxTrack(currentTrack, Collections.<String, Object>emptyMap()));
105 return true;
106 }
107
108 @Override
109 public GpxData getGpxData() {
110 return data;
111 }
112
113 /**
114 * Returns the number of coordinates that have been successfully read.
115 * @return the number of coordinates that have been successfully read
116 */
117 public int getNumberOfCoordinates() {
118 return success;
119 }
120}
Note: See TracBrowser for help on using the repository browser.