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

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

see #17829 - support RTKLib Positioning Solution files. Add new "Quality" GPX color mode

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