1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.layer.gpx;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertNotNull;
|
---|
6 |
|
---|
7 | import java.io.IOException;
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.Arrays;
|
---|
11 | import java.util.Comparator;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.Map;
|
---|
14 | import java.util.Objects;
|
---|
15 | import java.util.stream.Collectors;
|
---|
16 |
|
---|
17 | import org.junit.Rule;
|
---|
18 | import org.junit.Test;
|
---|
19 | import org.openstreetmap.josm.TestUtils;
|
---|
20 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
21 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
22 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
23 | import org.openstreetmap.josm.data.osm.Node;
|
---|
24 | import org.openstreetmap.josm.data.osm.TagMap;
|
---|
25 | import org.openstreetmap.josm.gui.layer.GpxLayer;
|
---|
26 | import org.openstreetmap.josm.gui.layer.markerlayer.MarkerLayer;
|
---|
27 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
28 | import org.openstreetmap.josm.io.IllegalDataException;
|
---|
29 | import org.openstreetmap.josm.io.OsmReader;
|
---|
30 | import org.openstreetmap.josm.spi.preferences.Config;
|
---|
31 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
32 | import org.xml.sax.SAXException;
|
---|
33 |
|
---|
34 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Unit tests of {@link ConvertToDataLayerAction} class.
|
---|
38 | */
|
---|
39 | public class ConvertToDataLayerActionTest {
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Setup test.
|
---|
43 | */
|
---|
44 | @Rule
|
---|
45 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
46 | public JOSMTestRules test = new JOSMTestRules();
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Tests a conversion from a GPX marker layer to a OSM dataset
|
---|
50 | * @throws Exception if the parsing fails
|
---|
51 | */
|
---|
52 | @Test
|
---|
53 | public void testFromMarkerLayer() throws Exception {
|
---|
54 | final GpxData data = GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
|
---|
55 | final MarkerLayer markers = new MarkerLayer(data, "Markers", data.storageFile, null);
|
---|
56 | final DataSet osm = new ConvertFromMarkerLayerAction(markers).convert();
|
---|
57 | assertEquals(1, osm.getNodes().size());
|
---|
58 | assertEquals(new TagMap("name", "Schranke", "description", "Pfad", "note", "Pfad", "gpxicon", "Toll Booth"),
|
---|
59 | osm.getNodes().iterator().next().getKeys());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Tests conversions from GPX tracks to OSM datasets
|
---|
64 | * @throws Exception if the parsing fails
|
---|
65 | */
|
---|
66 | @Test
|
---|
67 | public void testFromTrack() throws Exception {
|
---|
68 | Config.getPref().put("gpx.convert-tags", "no");
|
---|
69 | testFromTrack("tracks.gpx", "tracks.osm");
|
---|
70 |
|
---|
71 | Config.getPref().put("gpx.convert-tags", "yes");
|
---|
72 | testFromTrack("tracks.gpx", "tracks-ele-time.osm");
|
---|
73 |
|
---|
74 | Config.getPref().put("gpx.convert-tags", "list");
|
---|
75 | Config.getPref().putList("gpx.convert-tags.list.yes", Arrays.asList("ele"));
|
---|
76 | Config.getPref().putList("gpx.convert-tags.list.no", Arrays.asList("time"));
|
---|
77 | testFromTrack("tracks.gpx", "tracks-ele.osm");
|
---|
78 |
|
---|
79 | Config.getPref().putList("gpx.convert-tags.list.yes", Arrays.asList("time"));
|
---|
80 | Config.getPref().putList("gpx.convert-tags.list.no", Arrays.asList("ele"));
|
---|
81 | testFromTrack("tracks.gpx", "tracks-time.osm");
|
---|
82 |
|
---|
83 | //Extension tests:
|
---|
84 | Config.getPref().put("gpx.convert-tags", "yes");
|
---|
85 | testFromTrack("tracks-extensions.gpx", "tracks-extensions.osm");
|
---|
86 |
|
---|
87 | Config.getPref().put("gpx.convert-tags", "list");
|
---|
88 | Config.getPref().putList("gpx.convert-tags.list.yes", Arrays.asList("time", "ele"));
|
---|
89 | Config.getPref().putList("gpx.convert-tags.list.no", Arrays.asList(
|
---|
90 | "gpxx:DisplayColor",
|
---|
91 | "gpxd:color",
|
---|
92 | "gpx:extension:test:tag",
|
---|
93 | "gpx:extension:test:segment:tag"));
|
---|
94 | testFromTrack("tracks-extensions.gpx", "tracks-ele-time.osm");
|
---|
95 |
|
---|
96 | }
|
---|
97 |
|
---|
98 | private static class GenericNode {
|
---|
99 | final LatLon coor;
|
---|
100 | final Map<String, String> tags;
|
---|
101 |
|
---|
102 | GenericNode(Node n) {
|
---|
103 | coor = n.getCoor().getRoundedToOsmPrecision();
|
---|
104 | tags = n.getKeys();
|
---|
105 | }
|
---|
106 |
|
---|
107 | @Override
|
---|
108 | public boolean equals(Object obj) {
|
---|
109 | if (!(obj instanceof GenericNode)) {
|
---|
110 | return false;
|
---|
111 | }
|
---|
112 | GenericNode other = (GenericNode) obj;
|
---|
113 | return coor.equals(other.coor) && tags.equals(other.tags);
|
---|
114 | }
|
---|
115 |
|
---|
116 | @Override
|
---|
117 | public int hashCode() {
|
---|
118 | return Objects.hash(coor, tags);
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | private void testFromTrack(String originalGpx, String expectedOsm) throws IOException, SAXException, IllegalDataException {
|
---|
123 | final GpxData data = GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "tracks/" + originalGpx);
|
---|
124 | final DataSet osmExpected = OsmReader.parseDataSet(Files.newInputStream(
|
---|
125 | Paths.get(TestUtils.getTestDataRoot(), "tracks/" + expectedOsm)), null);
|
---|
126 | final GpxLayer layer = new GpxLayer(data);
|
---|
127 | final DataSet osm = new ConvertFromGpxLayerAction(layer).convert();
|
---|
128 | //compare sorted nodes/ways, tags and total amount of primitives, because IDs and order will vary after reload
|
---|
129 |
|
---|
130 | List<GenericNode> nodes = osm.getNodes().stream()
|
---|
131 | .map(GenericNode::new)
|
---|
132 | .sorted(Comparator.comparing(g -> g.coor.hashCode()))
|
---|
133 | .collect(Collectors.toList());
|
---|
134 |
|
---|
135 | List<GenericNode> nodesExpected = osmExpected.getNodes().stream()
|
---|
136 | .map(GenericNode::new)
|
---|
137 | .sorted(Comparator.comparing(g -> g.coor.hashCode()))
|
---|
138 | .collect(Collectors.toList());
|
---|
139 |
|
---|
140 | assertEquals("Conversion " + originalGpx + " -> " + expectedOsm + " didn't match!", nodesExpected, nodes);
|
---|
141 |
|
---|
142 | List<String> ways = osm.getWays().stream()
|
---|
143 | .map(w -> Integer.toString(w.getNodes().size()) + ":" + w.getKeys().entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).collect(Collectors.toList()).toString())
|
---|
144 | .sorted()
|
---|
145 | .collect(Collectors.toList());
|
---|
146 |
|
---|
147 | List<String> waysExpected = osmExpected.getWays().stream()
|
---|
148 | .map(w -> Integer.toString(w.getNodes().size()) + ":" + w.getKeys().entrySet().stream().sorted((e1, e2) -> e1.getKey().compareTo(e2.getKey())).collect(Collectors.toList()).toString())
|
---|
149 | .sorted()
|
---|
150 | .collect(Collectors.toList());
|
---|
151 |
|
---|
152 | assertEquals("Conversion " + originalGpx + " -> " + expectedOsm + " didn't match!", waysExpected, ways);
|
---|
153 |
|
---|
154 | assertEquals("Conversion " + originalGpx + " -> " + expectedOsm + " didn't match!", osmExpected.allPrimitives().size(), osm.allPrimitives().size());
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/14275">#14275</a>
|
---|
159 | * @throws IOException if an error occurs during reading
|
---|
160 | * @throws SAXException if any XML error occurs
|
---|
161 | */
|
---|
162 | @Test
|
---|
163 | public void testTicket14275() throws IOException, SAXException {
|
---|
164 | assertNotNull(GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(14275, "1485101437.8189685.gpx")));
|
---|
165 | }
|
---|
166 | }
|
---|