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