/** * @param latitude in radians * @param longitude in radians * @return a point with coordinates given in radians */ public static Point fromRadians(double latitude, double longitude) { return fromDegrees( latitude * GeometricConstants.TO_DEGREES_RATIO, longitude * GeometricConstants.TO_DEGREES_RATIO ); }
public DistanceSortField(double latitude, double longitude, String fieldName, boolean reverse) { this( Point.fromDegrees( latitude, longitude ), fieldName, reverse ); this.center = Point.fromDegrees( latitude, longitude ); }
public DistanceSortField(double latitude, double longitude, String fieldName, boolean reverse) { this( Point.fromDegrees( latitude, longitude ), fieldName, reverse ); this.center = Point.fromDegrees( latitude, longitude ); }
/** * @param latitude in radians * @param longitude in radians * @return a point with coordinates given in radians */ public static Point fromRadians(double latitude, double longitude) { return fromDegrees( latitude * GeometricConstants.TO_DEGREES_RATIO, longitude * GeometricConstants.TO_DEGREES_RATIO ); }
@Override public FullTextQueryImpl setSpatialParameters(double latitude, double longitude, String fieldName) { setSpatialParameters( Point.fromDegrees( latitude, longitude ), fieldName ); return this; }
/** * Returns a Lucene Query which relies on Hibernate Search Spatial * spatial hash indexation to find candidate documents and filter its results * in radius range by a DistanceFilter * * @param latitude WGS84 latitude of the center of the search * @param longitude WGS84 longitude of the center of the search * @param radius distance max to center in km * @param fieldName name of the Lucene Field implementing Coordinates * * @return Lucene Query to be used in a search * * @see Query * @see Coordinates */ public static Query buildSpatialQueryByHash(double latitude, double longitude, double radius, String fieldName) { return SpatialQueryBuilderFromCoordinates .buildSpatialQueryByHash( Point.fromDegrees( latitude, longitude ), radius, fieldName ); }
/** * Returns a Lucene Query which relies on Hibernate Search Spatial * spatial hash indexation to find candidate documents and filter its results * in radius range by a DistanceFilter * * @param latitude WGS84 latitude of the center of the search * @param longitude WGS84 longitude of the center of the search * @param radius distance max to center in km * @param fieldName name of the Lucene Field implementing Coordinates * * @return Lucene Query to be used in a search * * @see Query * @see Coordinates */ public static Query buildSpatialQueryByHash(double latitude, double longitude, double radius, String fieldName) { return SpatialQueryBuilderFromCoordinates .buildSpatialQueryByHash( Point.fromDegrees( latitude, longitude ), radius, fieldName ); }
/** * Returns a Lucene Query which relies on Hibernate Search Spatial * double range indexation to filter document at radius and filter its results * by a fine DistanceFilter * * @param latitude WGS84 latitude of the center of the search * @param longitude WGS84 longitude of the center of the search * @param radius distance max to center in km * @param fieldName name of the Lucene Field implementing Coordinates * * @return Lucene Query to be used in a search * * @see Query * @see Coordinates */ public static Query buildSpatialQueryByRange(double latitude, double longitude, double radius, String fieldName) { return SpatialQueryBuilderFromCoordinates.buildSpatialQueryByRange( Point.fromDegrees( latitude, longitude ), radius, fieldName ); } }
@Override public SpatialTermination andLongitude(double longitude) { spatialContext.setCoordinates( Point.fromDegrees( latitude, longitude ) ); return new ConnectedSpatialQueryBuilder( spatialContext, queryCustomizer, queryContext ); } }
@Override public SpatialTermination andLongitude(double longitude) { spatialContext.setCoordinates( Point.fromDegrees( latitude, longitude ) ); return new ConnectedSpatialQueryBuilder( spatialContext, queryCustomizer, queryContext ); } }
/** * @param center the coordinates for the Point to be created * @return a Point from given Coordinates. Same instance when given a Point. */ public static Point fromCoordinates(Coordinates center) { if ( center instanceof Point ) { return (Point) center; } else { return Point.fromDegrees( center.getLatitude(), center.getLongitude() ); } }
/** * @param center the coordinates for the Point to be created * @return a Point from given Coordinates. Same instance when given a Point. */ public static Point fromCoordinates(Coordinates center) { if ( center instanceof Point ) { return (Point) center; } else { return Point.fromDegrees( center.getLatitude(), center.getLongitude() ); } }
@Test public void distanceToPoint() { Point point = Point.fromDegrees( 45, 4 ); Point point2 = Point.fromDegrees( 46, 14 ); double distance = point.getDistanceTo( point2 ); Assert.assertEquals( 786.7, distance, 0.1 ); } }
@Test public void projectedBoundingBoxCellsIdsInclusionTest() { Point center = Point.fromDegrees( 45.0d, 32.0d ); Double radius = 50.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); center = Point.fromDegrees( 0.0d, 0.0d ); radius = 100.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); center = Point.fromDegrees( 180.0d, 0.0d ); radius = 250.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); center = Point.fromDegrees( 0.0d, 90.0d ); radius = 25.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); center = Point.fromDegrees( 45.0d, 360.0d ); radius = 100.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); center = Point.fromDegrees( -147.0d, -24.0d ); radius = 73.0d; Assert.assertTrue( projectedBoundingBoxCellsIdsInclusionTest( center, radius ) ); }
@Test public void normalizeTest() { Point point = Point.fromDegrees( 45, 517 ); Assert.assertEquals( 45, point.getLatitude(), 0 ); Assert.assertEquals( 157, point.getLongitude(), 0 ); Point point2 = Point.fromDegrees( 0, -185 ); Assert.assertEquals( 175, point2.getLongitude(), 0 ); Point point3 = Point.fromDegrees( 110, 0 ); Assert.assertEquals( 70, point3.getLatitude(), 0 ); Point point4 = Point.fromDegrees( -110, 0 ); Assert.assertEquals( -70, point4.getLatitude(), 0 ); Point point5 = Point.fromDegrees( 185, 0 ); Assert.assertEquals( -5, point5.getLatitude(), 0 ); Point point6 = Point.fromDegrees( 90, 180 ); Assert.assertEquals( 90, point6.getLatitude(), 0 ); Assert.assertEquals( 180, point6.getLongitude(), 0 ); Point point7 = Point.fromDegrees( -90, -180 ); Assert.assertEquals( -90, point7.getLatitude(), 0 ); Assert.assertEquals( 180, point7.getLongitude(), 0 ); }
@Test public void computeDestinationTest() { Point point = Point.fromDegrees( 45, 4 ); Point destination = point.computeDestination( 100, 45 ); Assert.assertEquals( 0.796432523, destination.getLatitudeRad(), 0.00001 ); Assert.assertEquals( 0.08568597, destination.getLongitudeRad(), 0.00001 ); }
@Test public void getSpatialHashCellIdTest() { Point point = Point.fromDegrees( 45, 4 ); String cellId = SpatialHelper.getSpatialHashCellId( point, 5 ); Assert.assertEquals( "0|8", cellId ); String cellId2 = SpatialHelper.getSpatialHashCellId( point, 7 ); Assert.assertEquals( "1|32", cellId2 ); String cellId3 = SpatialHelper.getSpatialHashCellId( point, 14 ); Assert.assertEquals( "128|4096", cellId3 ); Point point2 = Point.fromDegrees( -12, -179 ); String cellId4 = SpatialHelper.getSpatialHashCellId( point2, 5 ); Assert.assertEquals( "-16|-3", cellId4 ); String cellId5 = SpatialHelper.getSpatialHashCellId( point2, 7 ); Assert.assertEquals( "-63|-9", cellId5 ); String cellId6 = SpatialHelper.getSpatialHashCellId( point2, 14 ); Assert.assertEquals( "-7969|-1093", cellId6 ); }
@Test public void boundingBoxTest() { Point center = Point.fromDegrees( 45, 4 ); Rectangle rectangle = Rectangle.fromBoundingCircle( center, 50 ); Assert.assertEquals( 44.550339, rectangle.getLowerLeft().getLatitude(), 0.000001d ); Assert.assertEquals( 3.359047, rectangle.getLowerLeft().getLongitude(), 0.000001d ); Assert.assertEquals( 45.449660, rectangle.getUpperRight().getLatitude(), 0.000001d ); Assert.assertEquals( 4.640952, rectangle.getUpperRight().getLongitude(), 0.000001d ); } }
@Test public void testSpatialRangeQueries() { final QueryBuilder builder = helper.queryBuilder( POI.class ); Coordinates coordinates = Point.fromDegrees( 24d, 31.5d ); Query query = builder .spatial() .onField( "location" ) .within( 51, Unit.KM ) .ofCoordinates( coordinates ) .createQuery(); helper.assertThat( query ).from( POI.class ) .matchesExactlyIds( 2 ); query = builder .spatial() .onField( "location" ) .within( 500, Unit.KM ) .ofLatitude( 48.858333d ).andLongitude( 2.294444d ) .createQuery(); helper.assertThat( query ).from( POI.class ) .matchesExactlyIds( 1 ); }
@Test public void testSpatialHashQueries() { final QueryBuilder builder = helper.queryBuilder( POIHash.class ); Coordinates coordinates = Point.fromDegrees( 24d, 31.5d ); Query query = builder .spatial() .onField( "location" ) .within( 51, Unit.KM ) .ofCoordinates( coordinates ) .createQuery(); helper.assertThat( query ).from( POIHash.class ) .matchesExactlyIds( 2 ); query = builder .spatial() .onField( "location" ) .within( 500, Unit.KM ) .ofLatitude( 48.858333d ).andLongitude( 2.294444d ) .createQuery(); helper.assertThat( query ).from( POIHash.class ) .matchesExactlyIds( 1 ); }