Search in sources :

Example 1 with ShapeFactory

use of org.locationtech.spatial4j.shape.ShapeFactory in project gtfs-realtime-validator by CUTR-at-USF.

the class UtilTest method testPositionWithinShape.

@Test
public void testPositionWithinShape() {
    // Create USF Bull Runner bounding box
    ShapeFactory sf = GEO.getShapeFactory();
    ShapeFactory.MultiPointBuilder shapeBuilder = sf.multiPoint();
    shapeBuilder.pointXY(-82.438456, 28.041606);
    shapeBuilder.pointXY(-82.438456, 28.082202);
    shapeBuilder.pointXY(-82.399531, 28.082202);
    shapeBuilder.pointXY(-82.399531, 28.041606);
    Shape boundingBox = shapeBuilder.build().getBoundingBox();
    // Test utility method - USF campus location
    GtfsRealtime.Position.Builder positionBuilder = GtfsRealtime.Position.newBuilder();
    positionBuilder.setLatitude(28.0587f);
    positionBuilder.setLongitude(-82.4139f);
    boolean result = GtfsUtils.isPositionWithinShape(positionBuilder.build(), boundingBox);
    assertTrue(result);
    // Test utility method -  Downtown Tampa, FL
    positionBuilder.setLatitude(27.9482837f);
    positionBuilder.setLongitude(-82.4655826f);
    result = GtfsUtils.isPositionWithinShape(positionBuilder.build(), boundingBox);
    assertFalse(result);
}
Also used : Shape(org.locationtech.spatial4j.shape.Shape) ShapeFactory(org.locationtech.spatial4j.shape.ShapeFactory) Test(org.junit.Test)

Example 2 with ShapeFactory

use of org.locationtech.spatial4j.shape.ShapeFactory in project gtfs-realtime-validator by CUTR-at-USF.

the class GtfsUtils method isPositionWithinShape.

/**
 * Returns true if the provided vehiclePosition is within the provided shape, false if it is not
 *
 * @param vehiclePosition the vehiclePosition to test against the shape
 * @param bounds          the shape to test against the vehiclePosition
 * @return true if the provided vehiclePosition is within the provided shape, false if it is not
 */
public static boolean isPositionWithinShape(GtfsRealtime.Position vehiclePosition, Shape bounds) {
    ShapeFactory sf = GEO.getShapeFactory();
    org.locationtech.spatial4j.shape.Point p = sf.pointXY(vehiclePosition.getLongitude(), vehiclePosition.getLatitude());
    return bounds.relate(p).equals(SpatialRelation.CONTAINS);
}
Also used : ShapeFactory(org.locationtech.spatial4j.shape.ShapeFactory)

Aggregations

ShapeFactory (org.locationtech.spatial4j.shape.ShapeFactory)2 Test (org.junit.Test)1 Shape (org.locationtech.spatial4j.shape.Shape)1