Skip to content

Commit aa07f03

Browse files
author
Cameron Mace
authored
Split up turf methods (#366)
* split up turf methods * fixed line being to long
1 parent 7a6157c commit aa07f03

11 files changed

Lines changed: 15688 additions & 139 deletions

File tree

mapbox/libjava-services/src/main/java/com/mapbox/services/api/utils/turf/TurfMeasurement.java

Lines changed: 147 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
package com.mapbox.services.api.utils.turf;
22

3-
import com.mapbox.services.commons.geojson.Feature;
4-
import com.mapbox.services.commons.geojson.FeatureCollection;
5-
import com.mapbox.services.commons.geojson.Geometry;
63
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;
77
import com.mapbox.services.commons.geojson.Point;
8+
import com.mapbox.services.commons.geojson.Polygon;
89
import com.mapbox.services.commons.models.Position;
910

1011
import java.util.List;
@@ -101,7 +102,7 @@ public static Point destination(Point point1, double distance, double bearing, S
101102
double latitude2 = Math.asin(Math.sin(latitude1) * Math.cos(radians)
102103
+ Math.cos(latitude1) * Math.sin(radians) * Math.cos(bearingRad));
103104
double longitude2 = longitude1 + Math.atan2(Math.sin(bearingRad)
104-
* Math.sin(radians) * Math.cos(latitude1),
105+
* Math.sin(radians) * Math.cos(latitude1),
105106
Math.cos(radians) - Math.sin(latitude1) * Math.sin(latitude2));
106107

107108
return Point.fromCoordinates(
@@ -168,72 +169,81 @@ public static double distance(Point point1, Point point2, String units) throws T
168169
/**
169170
* Takes a line and measures its length in the specified units.
170171
*
171-
* @param line Line to measure.
172+
* @param lineString Line to measure.
172173
* @param units Can be degrees, radians, miles, or kilometers (defaults kilometers).
173174
* @return Length of the input line.
174175
* @throws TurfException TurfException Signals that a Turf exception of some sort has occurred.
175176
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
176177
* @since 1.2.0
177178
*/
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);
182201
}
183202
return d;
184203
}
185204

186205
/**
187206
* Takes a line and measures its length in the specified units.
188207
*
189-
* @param line Line to measure.
208+
* @param multiLineString Line to measure.
190209
* @param units Can be degrees, radians, miles, or kilometers (defaults kilometers).
191210
* @return Length of the input line.
192211
* @throws TurfException TurfException Signals that a Turf exception of some sort has occurred.
193212
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
194213
* @since 1.2.0
195214
*/
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;
198224
}
199225

200226
/**
201227
* Takes a line and measures its length in the specified units.
202228
*
203-
* @param line Line to measure.
229+
* @param multiPolygon Line to measure.
204230
* @param units Can be degrees, radians, miles, or kilometers (defaults kilometers).
205231
* @return Length of the input line.
206232
* @throws TurfException TurfException Signals that a Turf exception of some sort has occurred.
207233
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
208234
* @since 1.2.0
209235
*/
210-
public static double lineDistance(Geometry line, String units) throws TurfException {
236+
public static double lineDistance(MultiPolygon multiPolygon, String units) throws TurfException {
211237
double d;
212238

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);
230244
}
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)");
236245
}
246+
return d;
237247
}
238248

239249
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
307317

308318
return Point.fromCoordinates(coords.get(coords.size() - 1));
309319
}
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+
}
310421
}

0 commit comments

Comments
 (0)