1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.InputStream;
|
---|
8 | import java.nio.file.Files;
|
---|
9 | import java.nio.file.Paths;
|
---|
10 | import java.util.Arrays;
|
---|
11 |
|
---|
12 | import org.junit.BeforeClass;
|
---|
13 | import org.junit.Test;
|
---|
14 | import org.openstreetmap.josm.JOSMFixture;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
17 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
18 | import org.openstreetmap.josm.data.osm.Node;
|
---|
19 | import org.openstreetmap.josm.data.osm.Way;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Unit tests of {@link GeoJSONWriter} class.
|
---|
23 | */
|
---|
24 | public class GeoJSONWriterTest {
|
---|
25 |
|
---|
26 | /**
|
---|
27 | * Setup test.
|
---|
28 | */
|
---|
29 | @BeforeClass
|
---|
30 | public static void setUp() {
|
---|
31 | JOSMFixture.createUnitTestFixture().init();
|
---|
32 | }
|
---|
33 |
|
---|
34 | /**
|
---|
35 | * Unit test for Point
|
---|
36 | */
|
---|
37 | @Test
|
---|
38 | public void testPoint() {
|
---|
39 | final Node node = new Node(new LatLon(12.3, 4.56));
|
---|
40 | node.put("name", "foo");
|
---|
41 | node.put("source", "code");
|
---|
42 | final DataSet ds = new DataSet();
|
---|
43 | ds.addPrimitive(node);
|
---|
44 | final GeoJSONWriter writer = new GeoJSONWriter(ds);
|
---|
45 | assertEquals(("" +
|
---|
46 | "{\n" +
|
---|
47 | " 'type': 'FeatureCollection',\n" +
|
---|
48 | " 'generator': 'JOSM',\n" +
|
---|
49 | " 'features': [\n" +
|
---|
50 | " {\n" +
|
---|
51 | " 'type': 'Feature',\n" +
|
---|
52 | " 'properties': {\n" +
|
---|
53 | " 'name': 'foo',\n" +
|
---|
54 | " 'source': 'code'\n" +
|
---|
55 | " },\n" +
|
---|
56 | " 'geometry': {\n" +
|
---|
57 | " 'type': 'Point',\n" +
|
---|
58 | " 'coordinates': [\n" +
|
---|
59 | " 4.56000000000,\n" +
|
---|
60 | " 12.30000000000\n" +
|
---|
61 | " ]\n" +
|
---|
62 | " }\n" +
|
---|
63 | " }\n" +
|
---|
64 | " ]\n" +
|
---|
65 | "}").replace("'", "\""), writer.write().trim());
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Unit test for LineString
|
---|
70 | */
|
---|
71 | @Test
|
---|
72 | public void testLineString() {
|
---|
73 | final DataSet ds = new DataSet();
|
---|
74 | final Node n1 = new Node(new LatLon(12.3, 4.56));
|
---|
75 | final Node n2 = new Node(new LatLon(12.4, 4.57));
|
---|
76 | ds.addPrimitive(n1);
|
---|
77 | ds.addPrimitive(n2);
|
---|
78 | final Way way = new Way();
|
---|
79 | way.put("highway", "footway");
|
---|
80 | way.setNodes(Arrays.asList(n1, n2));
|
---|
81 | ds.addPrimitive(way);
|
---|
82 | final GeoJSONWriter writer = new GeoJSONWriter(ds);
|
---|
83 | assertEquals(("" +
|
---|
84 | "{\n" +
|
---|
85 | " 'type': 'FeatureCollection',\n" +
|
---|
86 | " 'generator': 'JOSM',\n" +
|
---|
87 | " 'features': [\n" +
|
---|
88 | " {\n" +
|
---|
89 | " 'type': 'Feature',\n" +
|
---|
90 | " 'properties': {\n" +
|
---|
91 | " 'highway': 'footway'\n" +
|
---|
92 | " },\n" +
|
---|
93 | " 'geometry': {\n" +
|
---|
94 | " 'type': 'LineString',\n" +
|
---|
95 | " 'coordinates': [\n" +
|
---|
96 | " [\n" +
|
---|
97 | " 4.56000000000,\n" +
|
---|
98 | " 12.30000000000\n" +
|
---|
99 | " ],\n" +
|
---|
100 | " [\n" +
|
---|
101 | " 4.57000000000,\n" +
|
---|
102 | " 12.40000000000\n" +
|
---|
103 | " ]\n" +
|
---|
104 | " ]\n" +
|
---|
105 | " }\n" +
|
---|
106 | " }\n" +
|
---|
107 | " ]\n" +
|
---|
108 | "}").replace("'", "\""), writer.write().trim());
|
---|
109 | }
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Unit test for multipolygon
|
---|
113 | * @throws Exception if an error occurs
|
---|
114 | */
|
---|
115 | @Test
|
---|
116 | public void testMultipolygon() throws Exception {
|
---|
117 | try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "multipolygon.osm"))) {
|
---|
118 | DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
119 | final GeoJSONWriter writer = new GeoJSONWriter(ds);
|
---|
120 | assertTrue(writer.write().contains("MultiPolygon"));
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /**
|
---|
125 | * Unit test for exporting invalid multipolygons, see #13827
|
---|
126 | * @throws Exception if an error occurs
|
---|
127 | */
|
---|
128 | @Test
|
---|
129 | public void testMultipolygonRobustness() throws Exception {
|
---|
130 | try (InputStream in = Files.newInputStream(Paths.get("nodist/data/multipolygon.osm"))) {
|
---|
131 | DataSet ds = OsmReader.parseDataSet(in, null);
|
---|
132 | final GeoJSONWriter writer = new GeoJSONWriter(ds);
|
---|
133 | assertTrue(writer.write().contains("MultiPolygon"));
|
---|
134 | }
|
---|
135 | }
|
---|
136 | }
|
---|