Skip to content

Commit 1e456b1

Browse files
author
Cameron Mace
committed
Adds nearest turf function
1 parent b855958 commit 1e456b1

5 files changed

Lines changed: 226 additions & 1 deletion

File tree

docs/turf-port.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ The helper functions are all part of the GeoJson module; this class contains uni
104104
- [ ] turf-triangle-grid
105105

106106
## Classification
107-
- [ ] turf-nearest
107+
- [x] turf-nearest
108108

109109
## Aggregation
110110
- [ ] turf-collect
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.mapbox.turf;
2+
3+
import android.support.annotation.NonNull;
4+
import com.mapbox.geojson.Point;
5+
6+
import java.util.List;
7+
8+
/**
9+
* Methods found in this class are meant to consume a set of information and classify it according
10+
* to a shared quality or characteristic.
11+
*
12+
* @since 3.0.0
13+
*/
14+
public class TurfClassification {
15+
16+
/**
17+
* Takes a reference point and a list of {@link Point} geometries and returns the point from the
18+
* set point list closest to the reference. This calculation is geodesic.
19+
*
20+
* @param targetPoint the reference point
21+
* @param points set list of points to run against the input point
22+
* @return the closest point in the set to the reference point
23+
* @since 3.0.0
24+
*/
25+
@NonNull
26+
public static Point nearest(@NonNull Point targetPoint, @NonNull List<Point> points) {
27+
if (points.isEmpty()) {
28+
return targetPoint;
29+
}
30+
Point nearestPoint = points.get(0);
31+
double minDist = Double.POSITIVE_INFINITY;
32+
for (Point point : points) {
33+
double distanceToPoint = TurfMeasurement.distance(targetPoint, point);
34+
if (distanceToPoint < minDist) {
35+
nearestPoint = point;
36+
minDist = distanceToPoint;
37+
}
38+
}
39+
return nearestPoint;
40+
}
41+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.mapbox.turf;
2+
3+
import com.mapbox.geojson.Feature;
4+
import com.mapbox.geojson.FeatureCollection;
5+
import com.mapbox.geojson.Point;
6+
import com.mapbox.services.TestUtils;
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
import java.io.IOException;
11+
import java.util.ArrayList;
12+
import java.util.List;
13+
14+
public class TurfClassificationTest extends TestUtils {
15+
16+
private static final String PT = "turf-classification/pt.json";
17+
private static final String PTS = "turf-classification/pts.json";
18+
19+
@Test
20+
public void testLineDistanceWithGeometries() throws IOException, TurfException {
21+
Point pt = (Point) Feature.fromJson(loadJsonFixture(PT)).geometry();
22+
FeatureCollection pts = FeatureCollection.fromJson(loadJsonFixture(PTS));
23+
24+
List<Point> pointList = new ArrayList<>();
25+
for (Feature feature : pts.features()) {
26+
pointList.add((Point) (feature.geometry()));
27+
}
28+
Point closestPt = TurfClassification.nearest(pt, pointList);
29+
30+
Assert.assertNotNull(closestPt);
31+
Assert.assertEquals(closestPt.type(), "Point");
32+
Assert.assertEquals(closestPt.longitude(), -75.33, DELTA);
33+
Assert.assertEquals(closestPt.latitude(), 39.44, DELTA);
34+
}
35+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"type": "Feature",
3+
"geometry": {"type": "Point", "coordinates": [-75.4, 39.4]},
4+
"properties": {
5+
"name": "Location A",
6+
"category": "Store"
7+
}
8+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
{
2+
"type": "FeatureCollection",
3+
"features": [
4+
{ "type": "Feature",
5+
"geometry": {"type": "Point", "coordinates": [-75.833, 39.284]},
6+
"properties": {
7+
"name": "Location B",
8+
"category": "House",
9+
"elevation": 25
10+
}
11+
},
12+
{ "type": "Feature",
13+
"geometry": {"type": "Point", "coordinates": [-75.6, 39.984]},
14+
"properties": {
15+
"name": "Location A",
16+
"category": "Store",
17+
"elevation": 23
18+
}
19+
},
20+
{ "type": "Feature",
21+
"geometry": {"type": "Point", "coordinates": [ -75.221, 39.125]},
22+
"properties": {
23+
"name": "Location C",
24+
"category": "Office",
25+
"elevation": 29
26+
}
27+
},
28+
{ "type": "Feature",
29+
"geometry": {"type": "Point", "coordinates": [-75.358, 39.987]},
30+
"properties": {
31+
"name": "Location A",
32+
"category": "Store",
33+
"elevation": 12
34+
}
35+
},
36+
{ "type": "Feature",
37+
"geometry": {"type": "Point", "coordinates": [-75.9221, 39.27]},
38+
"properties": {
39+
"name": "Location B",
40+
"category": "House",
41+
"elevation": 11
42+
}
43+
},
44+
{ "type": "Feature",
45+
"geometry": {"type": "Point", "coordinates": [ -75.534, 39.123]},
46+
"properties": {
47+
"name": "Location C",
48+
"category": "Office",
49+
"elevation": 49
50+
}
51+
},
52+
{ "type": "Feature",
53+
"geometry": {"type": "Point", "coordinates": [-75.21, 39.12]},
54+
"properties": {
55+
"name": "Location A",
56+
"category": "Store",
57+
"elevation": 50
58+
}
59+
},
60+
{ "type": "Feature",
61+
"geometry": {"type": "Point", "coordinates": [-75.22, 39.33]},
62+
"properties": {
63+
"name": "Location B",
64+
"category": "House",
65+
"elevation": 90
66+
}
67+
},
68+
{ "type": "Feature",
69+
"geometry": {"type": "Point", "coordinates": [ -75.44, 39.55]},
70+
"properties": {
71+
"name": "Location C",
72+
"category": "Office",
73+
"elevation": 22
74+
}
75+
},
76+
{ "type": "Feature",
77+
"geometry": {"type": "Point", "coordinates": [-75.77, 39.66]},
78+
"properties": {
79+
"name": "Location A",
80+
"category": "Store",
81+
"elevation": 99
82+
}
83+
},
84+
{ "type": "Feature",
85+
"geometry": {"type": "Point", "coordinates": [-75.44, 39.11]},
86+
"properties": {
87+
"name": "Location B",
88+
"category": "House",
89+
"elevation": 55
90+
}
91+
},
92+
{ "type": "Feature",
93+
"geometry": {"type": "Point", "coordinates": [ -75.05, 39.92]},
94+
"properties": {
95+
"name": "Location C",
96+
"category": "Office",
97+
"elevation": 41
98+
}
99+
},
100+
{ "type": "Feature",
101+
"geometry": {"type": "Point", "coordinates": [-75.88, 39.98]},
102+
"properties": {
103+
"name": "Location A",
104+
"category": "Store",
105+
"elevation": 52
106+
}
107+
},
108+
{ "type": "Feature",
109+
"geometry": {"type": "Point", "coordinates": [-75.55, 39.55]},
110+
"properties": {
111+
"name": "Location B",
112+
"category": "House",
113+
"elevation": 143
114+
}
115+
},
116+
{ "type": "Feature",
117+
"geometry": {"type": "Point", "coordinates": [ -75.33, 39.44]},
118+
"properties": {
119+
"name": "Location C",
120+
"category": "Office",
121+
"elevation": 76
122+
}
123+
},
124+
{ "type": "Feature",
125+
"geometry": {"type": "Point", "coordinates": [ -75.56, 39.24]},
126+
"properties": {
127+
"name": "Location C",
128+
"category": "Office",
129+
"elevation": 18
130+
}
131+
},
132+
{ "type": "Feature",
133+
"geometry": {"type": "Point", "coordinates": [ -75.56, 39.36]},
134+
"properties": {
135+
"name": "Location C",
136+
"category": "Office",
137+
"elevation": 52
138+
}
139+
}
140+
]
141+
}

0 commit comments

Comments
 (0)