|
1 | 1 | package com.mapbox.services.api.utils.turf; |
2 | 2 |
|
3 | | -import com.mapbox.services.commons.geojson.Feature; |
4 | | -import com.mapbox.services.commons.geojson.FeatureCollection; |
5 | | -import com.mapbox.services.commons.geojson.Geometry; |
6 | 3 | import com.mapbox.services.commons.geojson.LineString; |
| 4 | +import com.mapbox.services.commons.geojson.MultiLineString; |
| 5 | +import com.mapbox.services.commons.geojson.MultiPoint; |
| 6 | +import com.mapbox.services.commons.geojson.MultiPolygon; |
7 | 7 | import com.mapbox.services.commons.geojson.Point; |
| 8 | +import com.mapbox.services.commons.geojson.Polygon; |
8 | 9 | import com.mapbox.services.commons.models.Position; |
9 | 10 |
|
10 | 11 | import java.util.List; |
@@ -101,7 +102,7 @@ public static Point destination(Point point1, double distance, double bearing, S |
101 | 102 | double latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians) |
102 | 103 | + Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad)); |
103 | 104 | double longitude2 = longitude1 + Math.atan2(Math.sin(bearingRad) |
104 | | - * Math.sin(radians) * Math.cos(latitude1), |
| 105 | + * Math.sin(radians) * Math.cos(latitude1), |
105 | 106 | Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2)); |
106 | 107 |
|
107 | 108 | return Point.fromCoordinates( |
@@ -168,72 +169,81 @@ public static double distance(Point point1, Point point2, String units) throws T |
168 | 169 | /** |
169 | 170 | * Takes a line and measures its length in the specified units. |
170 | 171 | * |
171 | | - * @param line Line to measure. |
| 172 | + * @param lineString Line to measure. |
172 | 173 | * @param units Can be degrees, radians, miles, or kilometers (defaults kilometers). |
173 | 174 | * @return Length of the input line. |
174 | 175 | * @throws TurfException TurfException Signals that a Turf exception of some sort has occurred. |
175 | 176 | * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> |
176 | 177 | * @since 1.2.0 |
177 | 178 | */ |
178 | | - public static double lineDistance(FeatureCollection line, String units) throws TurfException { |
179 | | - double d = 0; |
180 | | - for (int i = 0; i < line.getFeatures().size(); i++) { |
181 | | - d += lineDistance(line.getFeatures().get(i), units); |
| 179 | + public static double lineDistance(LineString lineString, String units) throws TurfException { |
| 180 | + List<Position> coordinates = lineString.getCoordinates(); |
| 181 | + return length(coordinates, units); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Takes a line and measures its length in the specified units. |
| 186 | + * |
| 187 | + * @param polygon Line to measure. |
| 188 | + * @param units Can be degrees, radians, miles, or kilometers (defaults kilometers). |
| 189 | + * @return Length of the input line. |
| 190 | + * @throws TurfException TurfException Signals that a Turf exception of some sort has occurred. |
| 191 | + * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> |
| 192 | + * @since 1.2.0 |
| 193 | + */ |
| 194 | + public static double lineDistance(Polygon polygon, String units) throws TurfException { |
| 195 | + double d; |
| 196 | + |
| 197 | + List<List<Position>> coordinates = polygon.getCoordinates(); |
| 198 | + d = 0; |
| 199 | + for (int i = 0; i < coordinates.size(); i++) { |
| 200 | + d += length(coordinates.get(i), units); |
182 | 201 | } |
183 | 202 | return d; |
184 | 203 | } |
185 | 204 |
|
186 | 205 | /** |
187 | 206 | * Takes a line and measures its length in the specified units. |
188 | 207 | * |
189 | | - * @param line Line to measure. |
| 208 | + * @param multiLineString Line to measure. |
190 | 209 | * @param units Can be degrees, radians, miles, or kilometers (defaults kilometers). |
191 | 210 | * @return Length of the input line. |
192 | 211 | * @throws TurfException TurfException Signals that a Turf exception of some sort has occurred. |
193 | 212 | * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> |
194 | 213 | * @since 1.2.0 |
195 | 214 | */ |
196 | | - public static double lineDistance(Feature line, String units) throws TurfException { |
197 | | - return lineDistance(line.getGeometry(), units); |
| 215 | + public static double lineDistance(MultiLineString multiLineString, String units) throws TurfException { |
| 216 | + double d; |
| 217 | + |
| 218 | + List<List<Position>> coordinates = multiLineString.getCoordinates(); |
| 219 | + d = 0; |
| 220 | + for (int i = 0; i < coordinates.size(); i++) { |
| 221 | + d += length(coordinates.get(i), units); |
| 222 | + } |
| 223 | + return d; |
198 | 224 | } |
199 | 225 |
|
200 | 226 | /** |
201 | 227 | * Takes a line and measures its length in the specified units. |
202 | 228 | * |
203 | | - * @param line Line to measure. |
| 229 | + * @param multiPolygon Line to measure. |
204 | 230 | * @param units Can be degrees, radians, miles, or kilometers (defaults kilometers). |
205 | 231 | * @return Length of the input line. |
206 | 232 | * @throws TurfException TurfException Signals that a Turf exception of some sort has occurred. |
207 | 233 | * @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a> |
208 | 234 | * @since 1.2.0 |
209 | 235 | */ |
210 | | - public static double lineDistance(Geometry line, String units) throws TurfException { |
| 236 | + public static double lineDistance(MultiPolygon multiPolygon, String units) throws TurfException { |
211 | 237 | double d; |
212 | 238 |
|
213 | | - if (line.getType().equals("LineString")) { |
214 | | - List<Position> coordinates = (List<Position>) line.getCoordinates(); |
215 | | - return length(coordinates, units); |
216 | | - } else if (line.getType().equals("Polygon") || line.getType().equals("MultiLineString")) { |
217 | | - List<List<Position>> coordinates = (List<List<Position>>) line.getCoordinates(); |
218 | | - d = 0; |
219 | | - for (int i = 0; i < coordinates.size(); i++) { |
220 | | - d += length(coordinates.get(i), units); |
221 | | - } |
222 | | - return d; |
223 | | - } else if (line.getType().equals("MultiPolygon")) { |
224 | | - List<List<List<Position>>> coordinates = (List<List<List<Position>>>) line.getCoordinates(); |
225 | | - d = 0; |
226 | | - for (int i = 0; i < coordinates.size(); i++) { |
227 | | - for (int j = 0; j < coordinates.get(i).size(); j++) { |
228 | | - d += length(coordinates.get(i).get(j), units); |
229 | | - } |
| 239 | + List<List<List<Position>>> coordinates = multiPolygon.getCoordinates(); |
| 240 | + d = 0; |
| 241 | + for (int i = 0; i < coordinates.size(); i++) { |
| 242 | + for (int j = 0; j < coordinates.get(i).size(); j++) { |
| 243 | + d += length(coordinates.get(i).get(j), units); |
230 | 244 | } |
231 | | - return d; |
232 | | - } else { |
233 | | - throw new TurfException("Input must be a LineString, MultiLineString, " |
234 | | - + "Polygon, or MultiPolygon Feature or Geometry (or a FeatureCollection " |
235 | | - + "containing only those types)"); |
236 | 245 | } |
| 246 | + return d; |
237 | 247 | } |
238 | 248 |
|
239 | 249 | private static double length(List<Position> coords, String units) throws TurfException { |
@@ -307,4 +317,105 @@ public static Point along(LineString line, double distance, String units) throws |
307 | 317 |
|
308 | 318 | return Point.fromCoordinates(coords.get(coords.size() - 1)); |
309 | 319 | } |
| 320 | + |
| 321 | + /* |
| 322 | + * Bounding box |
| 323 | + */ |
| 324 | + |
| 325 | + /** |
| 326 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 327 | + * |
| 328 | + * @param point A {@link Point} object. |
| 329 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 330 | + * @since 2.0.0 |
| 331 | + */ |
| 332 | + public static double[] bbox(Point point) { |
| 333 | + List<Position> resultCoords = TurfMeta.coordAll(point); |
| 334 | + return bboxCalculator(resultCoords); |
| 335 | + } |
| 336 | + |
| 337 | + /** |
| 338 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 339 | + * |
| 340 | + * @param lineString A {@link LineString} object. |
| 341 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 342 | + * @since 2.0.0 |
| 343 | + */ |
| 344 | + public static double[] bbox(LineString lineString) { |
| 345 | + List<Position> resultCoords = TurfMeta.coordAll(lineString); |
| 346 | + return bboxCalculator(resultCoords); |
| 347 | + } |
| 348 | + |
| 349 | + /** |
| 350 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 351 | + * |
| 352 | + * @param multiPoint A {@link MultiPoint} object. |
| 353 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 354 | + * @since 2.0.0 |
| 355 | + */ |
| 356 | + public static double[] bbox(MultiPoint multiPoint) { |
| 357 | + List<Position> resultCoords = TurfMeta.coordAll(multiPoint); |
| 358 | + return bboxCalculator(resultCoords); |
| 359 | + } |
| 360 | + |
| 361 | + /** |
| 362 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 363 | + * |
| 364 | + * @param polygon A {@link Polygon} object. |
| 365 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 366 | + * @since 2.0.0 |
| 367 | + */ |
| 368 | + public static double[] bbox(Polygon polygon) { |
| 369 | + List<Position> resultCoords = TurfMeta.coordAll(polygon, false); |
| 370 | + return bboxCalculator(resultCoords); |
| 371 | + } |
| 372 | + |
| 373 | + /** |
| 374 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 375 | + * |
| 376 | + * @param multiLineString A {@link MultiLineString} object. |
| 377 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 378 | + * @since 2.0.0 |
| 379 | + */ |
| 380 | + public static double[] bbox(MultiLineString multiLineString) { |
| 381 | + List<Position> resultCoords = TurfMeta.coordAll(multiLineString); |
| 382 | + return bboxCalculator(resultCoords); |
| 383 | + } |
| 384 | + |
| 385 | + /** |
| 386 | + * Takes a set of features, calculates the bbox of all input features, and returns a bounding box. |
| 387 | + * |
| 388 | + * @param multiPolygon A {@link MultiPolygon} object. |
| 389 | + * @return A double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}. |
| 390 | + * @since 2.0.0 |
| 391 | + */ |
| 392 | + public static double[] bbox(MultiPolygon multiPolygon) { |
| 393 | + List<Position> resultCoords = TurfMeta.coordAll(multiPolygon, false); |
| 394 | + return bboxCalculator(resultCoords); |
| 395 | + } |
| 396 | + |
| 397 | + private static double[] bboxCalculator(List<Position> resultCoords) { |
| 398 | + double[] bbox = new double[4]; |
| 399 | + |
| 400 | + bbox[0] = Double.POSITIVE_INFINITY; |
| 401 | + bbox[1] = Double.POSITIVE_INFINITY; |
| 402 | + bbox[2] = Double.NEGATIVE_INFINITY; |
| 403 | + bbox[3] = Double.NEGATIVE_INFINITY; |
| 404 | + |
| 405 | + for (Position position : resultCoords) { |
| 406 | + if (bbox[0] > position.getLongitude()) { |
| 407 | + bbox[0] = position.getLongitude(); |
| 408 | + } |
| 409 | + if (bbox[1] > position.getLatitude()) { |
| 410 | + bbox[1] = position.getLatitude(); |
| 411 | + } |
| 412 | + if (bbox[2] < position.getLongitude()) { |
| 413 | + bbox[2] = position.getLongitude(); |
| 414 | + } |
| 415 | + if (bbox[3] < position.getLatitude()) { |
| 416 | + bbox[3] = position.getLatitude(); |
| 417 | + } |
| 418 | + } |
| 419 | + return bbox; |
| 420 | + } |
310 | 421 | } |
0 commit comments