1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.ozi;
|
---|
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.SystemOfMeasurement;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
17 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
18 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
19 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
20 | import org.openstreetmap.josm.io.IGpxReader;
|
---|
21 | import org.openstreetmap.josm.tools.Logging;
|
---|
22 | import org.xml.sax.SAXException;
|
---|
23 |
|
---|
24 | /**
|
---|
25 | * Reads an OziExplorer Waypoint file. Based on information from
|
---|
26 | * <a href="https://www.oziexplorer4.com/eng/help/fileformats.html">https://www.oziexplorer4.com</a>.
|
---|
27 | * @since 18179
|
---|
28 | */
|
---|
29 | public class OziWptReader implements IGpxReader {
|
---|
30 |
|
---|
31 | private static final int IDX_NAME = 1;
|
---|
32 | private static final int IDX_LAT = 2;
|
---|
33 | private static final int IDX_LON = 3;
|
---|
34 | private static final int IDX_DESC = 10;
|
---|
35 | private static final int IDX_ELE = 14;
|
---|
36 |
|
---|
37 | private static final int INVALID_ELE = -777;
|
---|
38 |
|
---|
39 | private final InputStream source;
|
---|
40 | private GpxData data;
|
---|
41 | private int success; // number of successfully parsed lines
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Constructs a new {@code OziWptReader}
|
---|
45 | * @param source Ozi wpt file input stream
|
---|
46 | * @throws IOException if an I/O error occurs
|
---|
47 | */
|
---|
48 | public OziWptReader(InputStream source) throws IOException {
|
---|
49 | this.source = Objects.requireNonNull(source);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override
|
---|
53 | public boolean parse(boolean tryToFinish) throws SAXException, IOException {
|
---|
54 | data = new GpxData();
|
---|
55 | Collection<Collection<WayPoint>> currentTrack = new ArrayList<>();
|
---|
56 | Collection<WayPoint> waypoints = new ArrayList<>();
|
---|
57 | try (BufferedReader rd = new BufferedReader(new InputStreamReader(source, StandardCharsets.UTF_8))) {
|
---|
58 | int linecount = 0;
|
---|
59 | String line;
|
---|
60 | do {
|
---|
61 | line = rd.readLine();
|
---|
62 | if (line != null) {
|
---|
63 | linecount++;
|
---|
64 | if (linecount == 1) {
|
---|
65 | if (!line.startsWith("OziExplorer Waypoint File")) {
|
---|
66 | throw new UnsupportedOperationException("Unsupported format: " + line);
|
---|
67 | }
|
---|
68 | } else if (linecount == 2) {
|
---|
69 | if (!"WGS 84".equals(line)) {
|
---|
70 | throw new UnsupportedOperationException("Unsupported datum: " + line);
|
---|
71 | }
|
---|
72 | } else if (linecount == 3 || linecount == 4) {
|
---|
73 | Logging.trace(line);
|
---|
74 | } else {
|
---|
75 | try {
|
---|
76 | String[] fields = line.split(",");
|
---|
77 | WayPoint currentwp = new WayPoint(new LatLon(
|
---|
78 | Double.parseDouble(fields[IDX_LAT]),
|
---|
79 | Double.parseDouble(fields[IDX_LON])));
|
---|
80 | currentwp.put(GpxConstants.GPX_NAME, fields[IDX_NAME]);
|
---|
81 | currentwp.put(GpxConstants.GPX_DESC, fields[IDX_DESC]);
|
---|
82 | String ele = fields[IDX_ELE];
|
---|
83 | if (!ele.isEmpty()) {
|
---|
84 | int eleInFeet = Integer.parseInt(ele);
|
---|
85 | if (eleInFeet != 0 && eleInFeet != INVALID_ELE) {
|
---|
86 | currentwp.put(GpxConstants.PT_ELE, eleInFeet * SystemOfMeasurement.IMPERIAL.aValue);
|
---|
87 | }
|
---|
88 | }
|
---|
89 | waypoints.add(currentwp);
|
---|
90 | success++;
|
---|
91 | } catch (IllegalArgumentException e) {
|
---|
92 | Logging.error(e);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | } while (line != null);
|
---|
97 | }
|
---|
98 | currentTrack.add(waypoints);
|
---|
99 | data.tracks.add(new GpxTrack(currentTrack, Collections.<String, Object>emptyMap()));
|
---|
100 | return true;
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public GpxData getGpxData() {
|
---|
105 | return data;
|
---|
106 | }
|
---|
107 |
|
---|
108 | @Override
|
---|
109 | public int getNumberOfCoordinates() {
|
---|
110 | return success;
|
---|
111 | }
|
---|
112 | }
|
---|