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