Skip to content

Commit 7208c48

Browse files
author
Cameron Mace
authored
added test and exception if null point exist during operations (#513)
* added test and exception if null point exist during operations * fixed checkstyle
1 parent c89a062 commit 7208c48

4 files changed

Lines changed: 89 additions & 1 deletion

File tree

mapbox/libjava-geojson/src/main/java/com/mapbox/services/commons/geojson/Point.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,31 @@ public String toJson() {
9393
gson.registerTypeAdapter(Position.class, new PositionSerializer());
9494
return gson.create().toJson(this);
9595
}
96-
}
96+
97+
@Override
98+
public boolean equals(Object object) {
99+
if (!(object instanceof Point)) {
100+
return false;
101+
}
102+
if (this == object) {
103+
return true;
104+
}
105+
Point point = (Point) object;
106+
return coordinates.equals(point.coordinates);
107+
}
108+
109+
@Override
110+
public int hashCode() {
111+
int result = type.hashCode();
112+
result = 31 * result + (coordinates != null ? coordinates.hashCode() : 0);
113+
return result;
114+
}
115+
116+
@Override
117+
public String toString() {
118+
return "Point{"
119+
+ "type='" + type + '\''
120+
+ ", coordinates=" + coordinates
121+
+ '}';
122+
}
123+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public static LineString lineSlice(Point startPt, Point stopPt, Feature line) th
5252
public static LineString lineSlice(Point startPt, Point stopPt, LineString line) throws TurfException {
5353
List<Position> coords = line.getCoordinates();
5454

55+
if (coords.size() < 2) {
56+
throw new TurfException("Turf lineSlice requires a LineString made up of at least 2 coordinates.");
57+
} else if (startPt.equals(stopPt)) {
58+
throw new TurfException("Start and stop points in Turf lineSlice cannot equal each other.");
59+
}
60+
5561
Feature startVertex = pointOnLine(startPt, coords);
5662
Feature stopVertex = pointOnLine(stopPt, coords);
5763
List<Feature> ends = new ArrayList<>();

mapbox/libjava-services/src/test/java/com/mapbox/services/api/turf/TurfMeasurementTest.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import org.junit.rules.ExpectedException;
1818

1919
import java.io.IOException;
20+
import java.util.ArrayList;
21+
import java.util.List;
2022

2123
import static org.hamcrest.Matchers.startsWith;
2224
import static org.junit.Assert.assertEquals;
@@ -72,6 +74,16 @@ public void testDistance() throws TurfException {
7274
TurfMeasurement.distance(pt1, pt2, "blah");
7375
}
7476

77+
@Test
78+
public void lineDistance_returnsZeroWhenRouteIsPoint() throws Exception {
79+
List<Position> coords = new ArrayList<>();
80+
coords.add(Position.fromCoordinates(1.0, 1.0));
81+
82+
LineString lineString = LineString.fromCoordinates(coords);
83+
double distance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS);
84+
assertEquals(0, distance, DELTA);
85+
}
86+
7587
@Test
7688
public void testLineDistanceWithGeometries() throws IOException, TurfException {
7789
Feature route1 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route1.geojson"));
@@ -171,6 +183,17 @@ public void testMidpointPositionToPoint() throws TurfException {
171183
Point.fromCoordinates(mid), TurfConstants.UNIT_MILES), DELTA);
172184
}
173185

186+
@Test
187+
public void turfAlong_returnsZeroWhenRouteIsPoint() throws Exception {
188+
List<Position> coords = new ArrayList<>();
189+
coords.add(Position.fromCoordinates(1.0, 1.0));
190+
191+
LineString lineString = LineString.fromCoordinates(coords);
192+
Point point = TurfMeasurement.along(lineString, 0, TurfConstants.UNIT_METERS);
193+
assertEquals(1.0, point.getCoordinates().getLatitude(), DELTA);
194+
assertEquals(1.0, point.getCoordinates().getLongitude(), DELTA);
195+
}
196+
174197
@Test
175198
public void testTurfAlong() throws IOException, TurfException {
176199
Feature feature = Feature.fromJson(loadJsonFixture("turf-along", "dc-line.geojson"));

mapbox/libjava-services/src/test/java/com/mapbox/services/api/turf/TurfMiscTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,51 @@
99
import com.mapbox.services.commons.geojson.Point;
1010
import com.mapbox.services.commons.models.Position;
1111

12+
import org.junit.Rule;
1213
import org.junit.Test;
14+
import org.junit.rules.ExpectedException;
1315

1416
import java.io.IOException;
1517
import java.util.ArrayList;
1618
import java.util.List;
1719

20+
import static org.hamcrest.Matchers.startsWith;
1821
import static org.junit.Assert.assertEquals;
1922
import static org.junit.Assert.assertNotEquals;
2023
import static org.junit.Assert.assertNotNull;
2124
import static org.junit.Assert.assertTrue;
2225

2326
public class TurfMiscTest extends BaseTurf {
2427

28+
@Rule
29+
public ExpectedException thrown = ExpectedException.none();
30+
31+
@Test
32+
public void lineSlice_throwsStartStopPointException() throws Exception {
33+
thrown.expect(TurfException.class);
34+
thrown.expectMessage(startsWith("Turf lineSlice requires a LineString made up of at least 2 coordinates."));
35+
36+
List<Position> coords = new ArrayList<>();
37+
coords.add(Position.fromCoordinates(1.0, 1.0));
38+
Point point = Point.fromCoordinates(new double[] {1.0, 1.0});
39+
Point point2 = Point.fromCoordinates(new double[] {2.0, 2.0});
40+
LineString lineString = LineString.fromCoordinates(coords);
41+
TurfMisc.lineSlice(point, point2, lineString);
42+
}
43+
44+
@Test
45+
public void lineSlice_throwLineMustContainTwoOrMorePoints() throws Exception {
46+
thrown.expect(TurfException.class);
47+
thrown.expectMessage(startsWith("Start and stop points in Turf lineSlice cannot equal each other."));
48+
49+
List<Position> coords = new ArrayList<>();
50+
coords.add(Position.fromCoordinates(1.0, 1.0));
51+
coords.add(Position.fromCoordinates(2.0, 2.0));
52+
Point point = Point.fromCoordinates(new double[] {1.0, 1.0});
53+
LineString lineString = LineString.fromCoordinates(coords);
54+
TurfMisc.lineSlice(point, point, lineString);
55+
}
56+
2557
@Test
2658
public void testTurfLineSliceLine1() throws IOException, TurfException {
2759
Point start = Point.fromCoordinates(Position.fromCoordinates(-97.79617309570312, 22.254624939561698));

0 commit comments

Comments
 (0)