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