|
17 | 17 | import java.io.IOException; |
18 | 18 | import java.util.Arrays; |
19 | 19 |
|
| 20 | +import static org.hamcrest.CoreMatchers.startsWith; |
20 | 21 | import static org.junit.Assert.assertEquals; |
| 22 | +import static org.junit.Assert.assertNotNull; |
21 | 23 |
|
22 | 24 | public class TurfConversionTest extends TestUtils { |
23 | 25 |
|
@@ -78,6 +80,130 @@ public void convertDistance() throws TurfException { |
78 | 80 | TurfConstants.UNIT_CENTIMETERS), DELTA); |
79 | 81 | } |
80 | 82 |
|
| 83 | + @Test |
| 84 | + public void combinePointsToMultiPoint() throws Exception { |
| 85 | + FeatureCollection pointFeatureCollection = |
| 86 | + FeatureCollection.fromFeatures( |
| 87 | + new Feature[]{ |
| 88 | + Feature.fromGeometry(Point.fromLngLat(-2.46, |
| 89 | + 27.6835)), |
| 90 | + Feature.fromGeometry(Point.fromLngLat(41.83, |
| 91 | + 7.3624)), |
| 92 | + }); |
| 93 | + |
| 94 | + FeatureCollection featureCollectionWithNewMultiPointObject = TurfConversion.combine(pointFeatureCollection); |
| 95 | + assertNotNull(featureCollectionWithNewMultiPointObject); |
| 96 | + |
| 97 | + MultiPoint multiPoint = (MultiPoint) featureCollectionWithNewMultiPointObject.features().get(0).geometry(); |
| 98 | + assertNotNull(multiPoint); |
| 99 | + |
| 100 | + assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA); |
| 101 | + assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA); |
| 102 | + assertEquals(41.83, multiPoint.coordinates().get(1).longitude(), DELTA); |
| 103 | + assertEquals(7.3624, multiPoint.coordinates().get(1).latitude(), DELTA); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void combineLineStringToMultiLineString() throws Exception { |
| 108 | + FeatureCollection lineStringFeatureCollection = |
| 109 | + FeatureCollection.fromFeatures( |
| 110 | + new Feature[]{ |
| 111 | + Feature.fromGeometry(LineString.fromLngLats( |
| 112 | + Arrays.asList(Point.fromLngLat(-11.25, 55.7765), |
| 113 | + Point.fromLngLat(41.1328, 22.91792)))), |
| 114 | + Feature.fromGeometry(LineString.fromLngLats( |
| 115 | + Arrays.asList(Point.fromLngLat(3.8671, 19.3111), |
| 116 | + Point.fromLngLat(20.742, -20.3034)))) |
| 117 | + }); |
| 118 | + |
| 119 | + FeatureCollection featureCollectionWithNewMultiLineStringObject = TurfConversion.combine(lineStringFeatureCollection); |
| 120 | + assertNotNull(featureCollectionWithNewMultiLineStringObject); |
| 121 | + |
| 122 | + MultiLineString multiLineString = (MultiLineString) featureCollectionWithNewMultiLineStringObject.features().get(0).geometry(); |
| 123 | + assertNotNull(multiLineString); |
| 124 | + |
| 125 | + // Checking the first LineString in the MultiLineString |
| 126 | + assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA); |
| 127 | + assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA); |
| 128 | + |
| 129 | + // Checking the second LineString in the MultiLineString |
| 130 | + assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA); |
| 131 | + assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void combinePolygonToMultiPolygon() throws Exception { |
| 136 | + FeatureCollection polygonFeatureCollection = |
| 137 | + FeatureCollection.fromFeatures( |
| 138 | + new Feature[]{ |
| 139 | + Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList( |
| 140 | + Arrays.asList( |
| 141 | + Point.fromLngLat(61.938950426660604, 5.9765625), |
| 142 | + Point.fromLngLat(52.696361078274485, 33.046875), |
| 143 | + Point.fromLngLat(69.90011762668541, 28.828124999999996), |
| 144 | + Point.fromLngLat(61.938950426660604, 5.9765625))))), |
| 145 | + Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList( |
| 146 | + Arrays.asList( |
| 147 | + Point.fromLngLat(11.42578125, 16.636191878397664), |
| 148 | + Point.fromLngLat(7.91015625, -9.102096738726443), |
| 149 | + Point.fromLngLat(31.113281249999996, 17.644022027872726), |
| 150 | + Point.fromLngLat(11.42578125, 16.636191878397664) |
| 151 | + )))) |
| 152 | + }); |
| 153 | + |
| 154 | + FeatureCollection featureCollectionWithNewMultiPolygonObject = TurfConversion.combine(polygonFeatureCollection); |
| 155 | + assertNotNull(featureCollectionWithNewMultiPolygonObject); |
| 156 | + |
| 157 | + MultiPolygon multiPolygon = (MultiPolygon) featureCollectionWithNewMultiPolygonObject.features().get(0).geometry(); |
| 158 | + assertNotNull(multiPolygon); |
| 159 | + |
| 160 | + // Checking the first Polygon in the MultiPolygon |
| 161 | + |
| 162 | + // Checking the first Point |
| 163 | + assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA); |
| 164 | + assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA); |
| 165 | + |
| 166 | + // Checking the second Point |
| 167 | + assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA); |
| 168 | + assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA); |
| 169 | + |
| 170 | + // Checking the second Polygon in the MultiPolygon |
| 171 | + |
| 172 | + // Checking the first Point |
| 173 | + assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA); |
| 174 | + assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA); |
| 175 | + |
| 176 | + // Checking the second Point |
| 177 | + assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA); |
| 178 | + assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA); |
| 179 | + } |
| 180 | + |
| 181 | + // TODO: Add test that checks Feature amount |
| 182 | + @Test |
| 183 | + public void geometryTypeMixThrowsException() throws Exception { |
| 184 | + thrown.expect(TurfException.class); |
| 185 | + thrown.expectMessage(startsWith("Your FeatureCollection must be of all of " + |
| 186 | + "the same geometry type.")); |
| 187 | + |
| 188 | + // Create a FeatureCollection with a Point Feature and a Polygon Feature |
| 189 | + FeatureCollection pointAndPolygonFeatureCollection = |
| 190 | + FeatureCollection.fromFeatures( |
| 191 | + new Feature[]{ |
| 192 | + Feature.fromGeometry(Point.fromLngLat(-2.46, |
| 193 | + 27.6835)), |
| 194 | + Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList( |
| 195 | + Arrays.asList( |
| 196 | + Point.fromLngLat(11.42578125, 16.636191878397664), |
| 197 | + Point.fromLngLat(7.91015625, -9.102096738726443), |
| 198 | + Point.fromLngLat(31.113281249999996, 17.644022027872726), |
| 199 | + Point.fromLngLat(11.42578125, 16.636191878397664) |
| 200 | + )))) |
| 201 | + }); |
| 202 | + |
| 203 | + // Building a geometry with this FeatureCollection should through an error |
| 204 | + FeatureCollection newMultiPolygonObject = TurfConversion.combine(pointAndPolygonFeatureCollection); |
| 205 | + } |
| 206 | + |
81 | 207 | @Test |
82 | 208 | public void explodePointSingleFeature() throws IOException, NullPointerException { |
83 | 209 | Point point = Point.fromLngLat(102, 0.5); |
|
0 commit comments