|
20 | 20 |
|
21 | 21 | package org.neo4j.gis.spatial.functions; |
22 | 22 |
|
| 23 | +import static org.hamcrest.MatcherAssert.assertThat; |
23 | 24 | import static org.hamcrest.Matchers.closeTo; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
24 | 26 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
25 | 27 |
|
| 28 | +import java.util.List; |
26 | 29 | import java.util.Map; |
27 | 30 | import org.hamcrest.MatcherAssert; |
| 31 | +import org.junit.jupiter.api.Nested; |
28 | 32 | import org.junit.jupiter.api.Test; |
29 | 33 | import org.neo4j.exceptions.KernelException; |
30 | 34 | import org.neo4j.gis.spatial.AbstractApiTest; |
@@ -69,4 +73,242 @@ public void literal_geometry_return() { |
69 | 73 | "WITH spatial.asGeometry({latitude: 5.0, longitude: 4.0}) AS geometry RETURN geometry", "geometry"); |
70 | 74 | assertInstanceOf(Geometry.class, geometry, "Should be Geometry type"); |
71 | 75 | } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test for all WKT types |
| 79 | + * |
| 80 | + * @see <a href="https://en.wikipedia.org/wiki/Well-known_text_representation_of_geometry">Wikipedia WKT</a> |
| 81 | + */ |
| 82 | + @Nested |
| 83 | + class WktToGeoJson { |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testPoint() { |
| 87 | + String wkt = "POINT (30 10)"; |
| 88 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 89 | + "json"); |
| 90 | + assertThat(json, equalTo(Map.of( |
| 91 | + "type", "Point", |
| 92 | + "coordinates", List.of(30., 10.) |
| 93 | + ))); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void testLineString() { |
| 98 | + String wkt = "LINESTRING (30 10, 10 30, 40 40)"; |
| 99 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 100 | + "json"); |
| 101 | + assertThat(json, equalTo(Map.of( |
| 102 | + "type", "LineString", |
| 103 | + "coordinates", List.of(List.of(30., 10.), List.of(10., 30.), List.of(40., 40.)) |
| 104 | + ))); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testPolygon() { |
| 109 | + String wkt = "POLYGON ((30 10, 40 40, 20 40, 10 20, 30 10))"; |
| 110 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 111 | + "json"); |
| 112 | + assertThat(json, equalTo(Map.of( |
| 113 | + "type", "Polygon", |
| 114 | + "coordinates", |
| 115 | + List.of( // Polygon |
| 116 | + List.of( // LineString |
| 117 | + List.of(30., 10.), |
| 118 | + List.of(40., 40.), |
| 119 | + List.of(20., 40.), |
| 120 | + List.of(10., 20.), |
| 121 | + List.of(30., 10.) |
| 122 | + ) |
| 123 | + ) |
| 124 | + ))); |
| 125 | + } |
| 126 | + |
| 127 | + @Test |
| 128 | + public void testPolygonWithHole() { |
| 129 | + String wkt = "POLYGON ((35 10, 45 45, 15 40, 10 20, 35 10),\n" |
| 130 | + + "(20 30, 35 35, 30 20, 20 30))"; |
| 131 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 132 | + "json"); |
| 133 | + assertThat(json, equalTo(Map.of( |
| 134 | + "type", "Polygon", |
| 135 | + "coordinates", |
| 136 | + List.of( // Polygon |
| 137 | + List.of( // LineString |
| 138 | + List.of(35., 10.), |
| 139 | + List.of(45., 45.), |
| 140 | + List.of(15., 40.), |
| 141 | + List.of(10., 20.), |
| 142 | + List.of(35., 10.) |
| 143 | + ), |
| 144 | + List.of( // hole |
| 145 | + List.of(20., 30.), |
| 146 | + List.of(35., 35.), |
| 147 | + List.of(30., 20.), |
| 148 | + List.of(20., 30.) |
| 149 | + ) |
| 150 | + ) |
| 151 | + ))); |
| 152 | + } |
| 153 | + |
| 154 | + @Test |
| 155 | + public void testMultiPoint() { |
| 156 | + String wkt = "MULTIPOINT ((10 40), (40 30), (20 20), (30 10))"; |
| 157 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 158 | + "json"); |
| 159 | + assertThat(json, equalTo(Map.of( |
| 160 | + "type", "MultiPoint", |
| 161 | + "coordinates", List.of( |
| 162 | + List.of(10., 40.), |
| 163 | + List.of(40., 30.), |
| 164 | + List.of(20., 20.), |
| 165 | + List.of(30., 10.) |
| 166 | + )))); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testMultiPoint2() { |
| 171 | + String wkt = "MULTIPOINT (10 40, 40 30, 20 20, 30 10)"; |
| 172 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 173 | + "json"); |
| 174 | + assertThat(json, equalTo(Map.of( |
| 175 | + "type", "MultiPoint", |
| 176 | + "coordinates", List.of( |
| 177 | + List.of(10., 40.), |
| 178 | + List.of(40., 30.), |
| 179 | + List.of(20., 20.), |
| 180 | + List.of(30., 10.) |
| 181 | + )))); |
| 182 | + } |
| 183 | + |
| 184 | + @Test |
| 185 | + public void testMultiLineString() { |
| 186 | + String wkt = "MULTILINESTRING ((10 10, 20 20, 10 40),\n" |
| 187 | + + "(40 40, 30 30, 40 20, 30 10))"; |
| 188 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 189 | + "json"); |
| 190 | + assertThat(json, equalTo(Map.of( |
| 191 | + "type", "MultiLineString", |
| 192 | + "coordinates", List.of( |
| 193 | + List.of( // LineString |
| 194 | + List.of(10., 10.), |
| 195 | + List.of(20., 20.), |
| 196 | + List.of(10., 40.) |
| 197 | + ), |
| 198 | + List.of( // LineString |
| 199 | + List.of(40., 40.), |
| 200 | + List.of(30., 30.), |
| 201 | + List.of(40., 20.), |
| 202 | + List.of(30., 10.) |
| 203 | + ) |
| 204 | + )))); |
| 205 | + } |
| 206 | + |
| 207 | + @Test |
| 208 | + public void testMultiPolygon() { |
| 209 | + String wkt = "MULTIPOLYGON (((30 20, 45 40, 10 40, 30 20)),\n" |
| 210 | + + "((15 5, 40 10, 10 20, 5 10, 15 5)))"; |
| 211 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 212 | + "json"); |
| 213 | + assertThat(json, equalTo(Map.of( |
| 214 | + "type", "MultiPolygon", |
| 215 | + "coordinates", List.of( // MultiPolygon |
| 216 | + List.of( // Polygon |
| 217 | + List.of( // LineString |
| 218 | + List.of(30., 20.), |
| 219 | + List.of(45., 40.), |
| 220 | + List.of(10., 40.), |
| 221 | + List.of(30., 20.) |
| 222 | + ) |
| 223 | + ), |
| 224 | + List.of( // Polygon |
| 225 | + List.of( // LineString |
| 226 | + List.of(15., 5.), |
| 227 | + List.of(40., 10.), |
| 228 | + List.of(10., 20.), |
| 229 | + List.of(5., 10.), |
| 230 | + List.of(15., 5.) |
| 231 | + ) |
| 232 | + ) |
| 233 | + ) |
| 234 | + ))); |
| 235 | + } |
| 236 | + |
| 237 | + @Test |
| 238 | + public void testMultiPolygon2() { |
| 239 | + String wkt = """ |
| 240 | + MULTIPOLYGON (((40 40, 20 45, 45 30, 40 40)), |
| 241 | + ((20 35, 10 30, 10 10, 30 5, 45 20, 20 35), |
| 242 | + (30 20, 20 15, 20 25, 30 20)))"""; |
| 243 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 244 | + "json"); |
| 245 | + assertThat(json, equalTo(Map.of( |
| 246 | + "type", "MultiPolygon", |
| 247 | + "coordinates", List.of( // MultiPolygon |
| 248 | + List.of( // Polygon |
| 249 | + List.of( // LineString |
| 250 | + List.of(40., 40.), |
| 251 | + List.of(20., 45.), |
| 252 | + List.of(45., 30.), |
| 253 | + List.of(40., 40.) |
| 254 | + ) |
| 255 | + ), |
| 256 | + List.of( // Polygon |
| 257 | + List.of( // LineString |
| 258 | + List.of(20., 35.), |
| 259 | + List.of(10., 30.), |
| 260 | + List.of(10., 10.), |
| 261 | + List.of(30., 5.), |
| 262 | + List.of(45., 20.), |
| 263 | + List.of(20., 35.) |
| 264 | + ), |
| 265 | + List.of( // hole |
| 266 | + List.of(30., 20.), |
| 267 | + List.of(20., 15.), |
| 268 | + List.of(20., 25.), |
| 269 | + List.of(30., 20.) |
| 270 | + ) |
| 271 | + ) |
| 272 | + ) |
| 273 | + ))); |
| 274 | + } |
| 275 | + |
| 276 | + @Test |
| 277 | + public void testGeometryCollection() { |
| 278 | + String wkt = """ |
| 279 | + GEOMETRYCOLLECTION (POINT (40 10), |
| 280 | + LINESTRING (10 10, 20 20, 10 40), |
| 281 | + POLYGON ((40 40, 20 45, 45 30, 40 40)))"""; |
| 282 | + Object json = executeObject("return spatial.wktToGeoJson($wkt) as json", Map.of("wkt", wkt), |
| 283 | + "json"); |
| 284 | + assertThat(json, equalTo(Map.of( |
| 285 | + "type", "GeometryCollection", |
| 286 | + "geometries", List.of( |
| 287 | + Map.of( // Point |
| 288 | + "type", "Point", |
| 289 | + "coordinates", List.of(40., 10.) |
| 290 | + ), |
| 291 | + Map.of( // LineString |
| 292 | + "type", "LineString", |
| 293 | + "coordinates", List.of( |
| 294 | + List.of(10., 10.), |
| 295 | + List.of(20., 20.), |
| 296 | + List.of(10., 40.) |
| 297 | + ) |
| 298 | + ), |
| 299 | + Map.of( // Polygon |
| 300 | + "type", "Polygon", |
| 301 | + "coordinates", List.of( |
| 302 | + List.of( // LineString |
| 303 | + List.of(40., 40.), |
| 304 | + List.of(20., 45.), |
| 305 | + List.of(45., 30.), |
| 306 | + List.of(40., 40.) |
| 307 | + ) |
| 308 | + ) |
| 309 | + ) |
| 310 | + ) |
| 311 | + ))); |
| 312 | + } |
| 313 | + } |
72 | 314 | } |
0 commit comments