Point center = Point.fromCoordinates( centerCoordinates ); if ( radius > center.getDistanceTo( GeometricConstants.NORTH_POLE ) ) { maximumLatitude = GeometricConstants.LATITUDE_DEGREE_MAX; maximumLatitude = center.computeDestination( radius, GeometricConstants.HEADING_NORTH ).getLatitude(); if ( radius > center.getDistanceTo( GeometricConstants.SOUTH_POLE ) ) { minimumLatitude = GeometricConstants.LATITUDE_DEGREE_MIN; minimumLatitude = center.computeDestination( radius, GeometricConstants.HEADING_SOUTH ).getLatitude(); Point referencePoint = Point.fromDegrees( Math.max( Math.abs( minimumLatitude ), Math.abs( maximumLatitude ) ), center.getLongitude() ); maximumLongitude = referencePoint.computeDestination( radius, GeometricConstants.HEADING_EAST ) .getLongitude(); minimumLongitude = referencePoint.computeDestination( radius, GeometricConstants.HEADING_WEST ) .getLongitude(); Point.fromDegreesInclusive( minimumLatitude, minimumLongitude ), Point.fromDegreesInclusive( maximumLatitude, maximumLongitude ) );
/** * Compute distance point and other location given by its latitude and longitude in decimal degrees * * @param latitude in decimal degrees * @param longitude in decimal degrees * @return the distance between the points * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Distance haversine formula</a> */ public double getDistanceTo(final double latitude,final double longitude) { double destinationLatitudeRadians = normalizeLatitude( latitude ) * GeometricConstants.TO_RADIANS_RATIO; double destinationLongitudeRadians = normalizeLongitude( longitude ) * GeometricConstants.TO_RADIANS_RATIO; final double dLat = ( destinationLatitudeRadians - getLatitudeRad() ) / 2.0d; final double dLon = ( destinationLongitudeRadians - getLongitudeRad() ) / 2.0d; final double a = Math.pow( Math.sin( dLat ), 2 ) + Math.pow( Math.sin( dLon ), 2 ) * Math.cos( getLatitudeRad() ) * Math.cos( destinationLatitudeRadians ); final double c = 2.0d * Math.atan2( Math.sqrt( a ), Math.sqrt( 1.0d - a ) ); return c * GeometricConstants.EARTH_MEAN_RADIUS_KM; }
/** * @param latitude in degrees * @param longitude in degrees * @return a point with coordinates given in degrees */ public static Point fromDegreesInclusive(double latitude, double longitude) { return new Point( normalizeLatitude( latitude ), normalizeLongitudeInclusive( longitude ) ); }
/** * Compute distance between two points * * @param other a {@link org.hibernate.search.spatial.impl.Point} object. * @return the distance between points * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Distance haversine formula</a> */ public double getDistanceTo(Point other) { return getDistanceTo( other.getLatitude(), other.getLongitude() ); }
/** * @param latitude in degrees * @param longitude in degrees * @return a point with coordinates given in degrees */ public static Point fromDegrees(double latitude, double longitude) { return new Point( normalizeLatitude( latitude ), normalizeLongitude( longitude ) ); }
@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 ); }
for(Point pt: gpx.getPoints( )) Location loc = new Location( pt.getLatitude( ), pt.getLongitude( ) ) ;
/** * @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 ); }
double lowerLeftLatitude = boundingBox.getLowerLeft().getLatitude(); double lowerLeftLongitude = boundingBox.getLowerLeft().getLongitude(); double upperRightLatitude = boundingBox.getUpperRight().getLatitude(); double upperRightLongitude = boundingBox.getUpperRight().getLongitude(); Point.fromDegreesInclusive( lowerLeftLatitude, lowerLeftLongitude ), Point.fromDegreesInclusive( upperRightLatitude, GeometricConstants.LONGITUDE_DEGREE_RANGE / 2 ), spatialHashLevel ); spatialHashCellsIds.addAll( getSpatialHashCellsIds( Point.fromDegreesInclusive( lowerLeftLatitude, -GeometricConstants.LONGITUDE_DEGREE_RANGE / 2 ), Point.fromDegreesInclusive( upperRightLatitude, upperRightLongitude ), spatialHashLevel Point.fromDegreesInclusive( lowerLeftLatitude, lowerLeftLongitude ), Point.fromDegreesInclusive( upperRightLatitude, upperRightLongitude ), spatialHashLevel );
Point center = Point.fromCoordinates( centerCoordinates ); Rectangle boundingBox = Rectangle.fromBoundingCircle( center, radius ); latitudeFieldName, boundingBox.getLowerLeft().getLatitude(), boundingBox.getUpperRight().getLatitude(), true, true ); if ( boundingBox.getLowerLeft().getLongitude() <= boundingBox.getUpperRight().getLongitude() ) { longQuery = NumericRangeQuery.newDoubleRange( longitudeFieldName, boundingBox.getLowerLeft().getLongitude(), boundingBox.getUpperRight().getLongitude(), true, true ); .add( NumericRangeQuery.newDoubleRange( longitudeFieldName, boundingBox.getLowerLeft().getLongitude(), 180.0, true, true ), BooleanClause.Occur.SHOULD ) .add( NumericRangeQuery.newDoubleRange( longitudeFieldName, -180.0, boundingBox.getUpperRight().getLongitude(), true, true ), BooleanClause.Occur.SHOULD ) .build();
Gpx gpx = GpxFileIo.parseIn( "SomeGeoCollection.gpx" ) ; for (Point pt: gpx.getPoints( ) ) { Location loc = new Location( pt.getLatitude( ), pt.getLongitude( ) ) ; if (pt.getElevation( ) != null) loc.setElevation( pt.getElevation( ) ) ; }
@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 ); }
pointToAdd.setX(p.getLongitude()); pointToAdd.setY(p.getLatitude()); psSE.setString(1, p.getDescription()); psSE.setObject(2, new org.postgis.PGgeometry(pointToAdd)); psSE.setString(3, "marina");
/** * Calculate end of travel point * * @param distance to travel * @param heading of travel in decimal degree * @return arrival point * @see <a href="http://www.movable-type.co.uk/scripts/latlong.html">Compute destination</a> */ public Point computeDestination(double distance, double heading) { double headingRadian = heading * GeometricConstants.TO_RADIANS_RATIO; double destinationLatitudeRadian = Math.asin( Math.sin( getLatitudeRad() ) * Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) + Math.cos( getLatitudeRad() ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( headingRadian ) ); double destinationLongitudeRadian = getLongitudeRad() + Math.atan2( Math.sin( headingRadian ) * Math.sin( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) * Math.cos( getLatitudeRad() ), Math.cos( distance / GeometricConstants.EARTH_MEAN_RADIUS_KM ) - Math.sin( getLatitudeRad() ) * Math.sin( destinationLatitudeRadian ) ); return fromRadians( destinationLatitudeRadian, destinationLongitudeRadian ); }
@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 ); } }
/** * Project a degree latitude/longitude point into a sinusoidal projection planar space for spatial hash cell ids * computation * * @param point * point to be projected * @return array of projected coordinates */ public static double[] projectToIndexSpace(Point point) { double[] projectedCoordinates = new double[2]; projectedCoordinates[0] = point.getLongitudeRad() * Math.cos( point.getLatitudeRad() ); projectedCoordinates[1] = point.getLatitudeRad(); return projectedCoordinates; }
@Override Double getDistance(final Point center) { return center.getDistanceTo( latitude, longitude ); } }
public boolean projectedBoundingBoxCellsIdsInclusionTest( Point center, Double radius) { Integer spatialHashLevel = SpatialHelper.findBestSpatialHashLevelForSearchRange( radius ); List<String> cellsIds = SpatialHelper.getSpatialHashCellsIds( center, radius, spatialHashLevel ); Point edge = null; boolean validated = true; for ( int heading = 0; heading < 360; heading++ ) { edge = center.computeDestination( radius, heading ); String cellId = SpatialHelper.getSpatialHashCellId( edge, spatialHashLevel ); validated &= cellsIds.contains( cellId ); } return validated; }
@Override public boolean equals(Object obj) { if ( obj == this ) { return true; } if ( obj instanceof DistanceQuery ) { DistanceQuery other = (DistanceQuery) obj; return Float.floatToIntBits( getBoost() ) == Float.floatToIntBits( other.getBoost() ) && approximationQuery.equals( other.approximationQuery ) && center.equals( other.center ) && radius == other.radius && Objects.equals( coordinatesField, other.coordinatesField ) && Objects.equals( latitudeField, other.latitudeField ) && Objects.equals( longitudeField, other.longitudeField ); } return false; }
); double[] projectedLowerRight = projectToIndexSpace( Point.fromDegrees( lowerLeft.getLatitude(), upperRight.getLongitude() ) ); int lowerRightXIndex = getCellIndex( projectedLowerRight[0], ); double[] projectedUpperLeft = projectToIndexSpace( Point.fromDegrees( upperRight.getLatitude(), lowerLeft.getLongitude() ) ); int upperLeftXIndex = getCellIndex( projectedUpperLeft[0],