| 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.util.ArrayList;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.Collections;
|
|---|
| 12 | import java.util.Objects;
|
|---|
| 13 |
|
|---|
| 14 | import org.openstreetmap.josm.data.coor.LatLon;
|
|---|
| 15 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
|---|
| 16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
|---|
| 17 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
|---|
| 18 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
|---|
| 19 | import org.openstreetmap.josm.io.IGpxReader;
|
|---|
| 20 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 21 | import org.openstreetmap.josm.tools.date.DateUtils;
|
|---|
| 22 | import 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 | */
|
|---|
| 30 | public 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 | @Override
|
|---|
| 114 | public int getNumberOfCoordinates() {
|
|---|
| 115 | return success;
|
|---|
| 116 | }
|
|---|
| 117 | }
|
|---|