Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,31 @@ public String toJson() {
gson.registerTypeAdapter(Position.class, new PositionSerializer());
return gson.create().toJson(this);
}
}

@Override
public boolean equals(Object object) {
if (!(object instanceof Point)) {
return false;
}
if (this == object) {
return true;
}
Point point = (Point) object;
return coordinates.equals(point.coordinates);
}

@Override
public int hashCode() {
int result = type.hashCode();
result = 31 * result + (coordinates != null ? coordinates.hashCode() : 0);
return result;
}

@Override
public String toString() {
return "Point{"
+ "type='" + type + '\''
+ ", coordinates=" + coordinates
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,12 @@ public static LineString lineSlice(Point startPt, Point stopPt, Feature line) th
public static LineString lineSlice(Point startPt, Point stopPt, LineString line) throws TurfException {
List<Position> coords = line.getCoordinates();

if (coords.size() < 2) {
throw new TurfException("Turf lineSlice requires a LineString made up of at least 2 coordinates.");
} else if (startPt.equals(stopPt)) {
throw new TurfException("Start and stop points in Turf lineSlice cannot equal each other.");
}

Feature startVertex = pointOnLine(startPt, coords);
Feature stopVertex = pointOnLine(stopPt, coords);
List<Feature> ends = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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

@Test
public void lineDistance_returnsZeroWhenRouteIsPoint() throws Exception {
List<Position> coords = new ArrayList<>();
coords.add(Position.fromCoordinates(1.0, 1.0));

LineString lineString = LineString.fromCoordinates(coords);
double distance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS);
assertEquals(0, distance, DELTA);
}

@Test
public void testLineDistanceWithGeometries() throws IOException, TurfException {
Feature route1 = Feature.fromJson(loadJsonFixture("turf-line-distance", "route1.geojson"));
Expand Down Expand Up @@ -171,6 +183,17 @@ public void testMidpointPositionToPoint() throws TurfException {
Point.fromCoordinates(mid), TurfConstants.UNIT_MILES), DELTA);
}

@Test
public void turfAlong_returnsZeroWhenRouteIsPoint() throws Exception {
List<Position> coords = new ArrayList<>();
coords.add(Position.fromCoordinates(1.0, 1.0));

LineString lineString = LineString.fromCoordinates(coords);
Point point = TurfMeasurement.along(lineString, 0, TurfConstants.UNIT_METERS);
assertEquals(1.0, point.getCoordinates().getLatitude(), DELTA);
assertEquals(1.0, point.getCoordinates().getLongitude(), DELTA);
}

@Test
public void testTurfAlong() throws IOException, TurfException {
Feature feature = Feature.fromJson(loadJsonFixture("turf-along", "dc-line.geojson"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,51 @@
import com.mapbox.services.commons.geojson.Point;
import com.mapbox.services.commons.models.Position;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static org.hamcrest.Matchers.startsWith;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

public class TurfMiscTest extends BaseTurf {

@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void lineSlice_throwsStartStopPointException() throws Exception {
thrown.expect(TurfException.class);
thrown.expectMessage(startsWith("Turf lineSlice requires a LineString made up of at least 2 coordinates."));

List<Position> coords = new ArrayList<>();
coords.add(Position.fromCoordinates(1.0, 1.0));
Point point = Point.fromCoordinates(new double[] {1.0, 1.0});
Point point2 = Point.fromCoordinates(new double[] {2.0, 2.0});
LineString lineString = LineString.fromCoordinates(coords);
TurfMisc.lineSlice(point, point2, lineString);
}

@Test
public void lineSlice_throwLineMustContainTwoOrMorePoints() throws Exception {
thrown.expect(TurfException.class);
thrown.expectMessage(startsWith("Start and stop points in Turf lineSlice cannot equal each other."));

List<Position> coords = new ArrayList<>();
coords.add(Position.fromCoordinates(1.0, 1.0));
coords.add(Position.fromCoordinates(2.0, 2.0));
Point point = Point.fromCoordinates(new double[] {1.0, 1.0});
LineString lineString = LineString.fromCoordinates(coords);
TurfMisc.lineSlice(point, point, lineString);
}

@Test
public void testTurfLineSliceLine1() throws IOException, TurfException {
Point start = Point.fromCoordinates(Position.fromCoordinates(-97.79617309570312, 22.254624939561698));
Expand Down