// Path: src/main/java/com/github/davidmoten/rtree/geometry/Geometries.java
// public final class Geometries {
// 
//     private Geometries() {
//         // prevent instantiation
//     }
// 
//     public static Point point(double x, double y) {
//         return PointDouble.create(x, y);
//     }
// 
//     public static Point point(float x, float y) {
//         return PointFloat.create(x, y);
//     }
// 
//     public static Point pointGeographic(double lon, double lat) {
//         return point(normalizeLongitudeDouble(lon), lat);
//     }
// 
//     public static Point pointGeographic(float lon, float lat) {
//         return point(normalizeLongitude(lon), lat);
//     }
// 
//     public static Rectangle rectangle(double x1, double y1, double x2, double y2) {
//         return rectangleDouble(x1, y1, x2, y2);
//     }
// 
//     public static Rectangle rectangle(float x1, float y1, float x2, float y2) {
//         return RectangleFloat.create(x1, y1, x2, y2);
//     }
// 
//     public static Rectangle rectangleGeographic(double lon1, double lat1, double lon2,
//             double lat2) {
//         return rectangleGeographic((float) lon1, (float) lat1, (float) lon2, (float) lat2);
//     }
// 
//     public static Rectangle rectangleGeographic(float lon1, float lat1, float lon2, float lat2) {
//         float x1 = normalizeLongitude(lon1);
//         float x2 = normalizeLongitude(lon2);
//         if (x2 < x1) {
//             x2 += 360;
//         }
//         return rectangle(x1, lat1, x2, lat2);
//     }
// 
//     private static Rectangle rectangleDouble(double x1, double y1, double x2, double y2) {
//         return  RectangleDouble.create(x1, y1, x2, y2);
//     }
// 
//     public static Circle circle(double x, double y, double radius) {
//         return CircleDouble.create(x, y, radius);
//     }
// 
//     public static Circle circle(float x, float y, float radius) {
//         return CircleFloat.create(x, y, radius);
//     }
// 
//     public static Line line(double x1, double y1, double x2, double y2) {
//         return LineDouble.create(x1, y1, x2, y2);
//     }
// 
//     public static Line line(float x1, float y1, float x2, float y2) {
//         return LineFloat.create(x1, y1, x2, y2);
//     }
// 
//     @VisibleForTesting
//     static double normalizeLongitude(double d) {
//         return normalizeLongitude((float) d);
//     }
// 
//     private static float normalizeLongitude(float d) {
//         if (d == -180.0f)
//             return -180.0f;
//         else {
//             float sign = Math.signum(d);
//             float x = Math.abs(d) / 360;
//             float x2 = (x - (float) Math.floor(x)) * 360;
//             if (x2 >= 180)
//                 x2 -= 360;
//             return x2 * sign;
//         }
//     }
// 
//     private static double normalizeLongitudeDouble(double d) {
//         if (d == -180.0f)
//             return -180.0d;
//         else {
//             double sign = Math.signum(d);
//             double x = Math.abs(d) / 360;
//             double x2 = (x - (float) Math.floor(x)) * 360;
//             if (x2 >= 180)
//                 x2 -= 360;
//             return x2 * sign;
//         }
//     }
// 
// }
// 
// Path: src/main/java/com/github/davidmoten/rtree/geometry/Point.java
// public interface Point extends Rectangle {
// 
//     double x();
// 
//     double y();
// 
// }
// 
// Path: src/main/java/com/github/davidmoten/rtree/geometry/Rectangle.java
// public interface Rectangle extends Geometry, HasGeometry {
// 
//     double x1();
// 
//     double y1();
// 
//     double x2();
// 
//     double y2();
// 
//     double area();
// 
//     double intersectionArea(Rectangle r);
// 
//     double perimeter();
// 
//     Rectangle add(Rectangle r);
// 
//     boolean contains(double x, double y);
//     
//     boolean isDoublePrecision();
// 
// }


import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import com.github.davidmoten.grumpy.core.Position;
import com.github.davidmoten.rtree.geometry.Geometries;
import com.github.davidmoten.rtree.geometry.Point;
import com.github.davidmoten.rtree.geometry.Rectangle;
import rx.Observable;
import rx.functions.Func1;
                // get the result
                .toList().toBlocking().single();

        // should have returned Sydney only
        assertEquals(1, list.size());
        assertEquals("Sydney", list.get(0).value());
    }

    public static <T> Observable<Entry<T, Point>> search(RTree<T, Point> tree, Point lonLat,
            final double distanceKm) {
        // First we need to calculate an enclosing lat long rectangle for this
        // distance then we refine on the exact distance
        final Position from = Position.create(lonLat.y(), lonLat.x());
        Rectangle bounds = createBounds(from, distanceKm);

        return tree
                // do the first search using the bounds
                .search(bounds)
                // refine using the exact distance
                .filter(new Func1<Entry<T, Point>, Boolean>() {
                    @Override
                    public Boolean call(Entry<T, Point> entry) {
                        Point p = entry.geometry();
                        Position position = Position.create(p.y(), p.x());
                        return from.getDistanceToKm(position) < distanceKm;
                    }
                });
    }

    @Test
// Path: src/main/java/com/github/davidmoten/rtree/geometry/Geometries.java
// public final class Geometries {
// 
//     private Geometries() {
//         // prevent instantiation
//     }
// 
//     public static Point point(double x, double y) {
//         return PointDouble.create(x, y);
//     }
// 
//     public static Point point(float x, float y) {
//         return PointFloat.create(x, y);
//     }
// 
//     public static Point pointGeographic(double lon, double lat) {
//         return point(normalizeLongitudeDouble(lon), lat);
//     }
// 
//     public static Point pointGeographic(float lon, float lat) {
//         return point(normalizeLongitude(lon), lat);
//     }
// 
//     public static Rectangle rectangle(double x1, double y1, double x2, double y2) {
//         return rectangleDouble(x1, y1, x2, y2);
//     }
// 
//     public static Rectangle rectangle(float x1, float y1, float x2, float y2) {
//         return RectangleFloat.create(x1, y1, x2, y2);
//     }
// 
//     public static Rectangle rectangleGeographic(double lon1, double lat1, double lon2,
//             double lat2) {
//         return rectangleGeographic((float) lon1, (float) lat1, (float) lon2, (float) lat2);
//     }
// 
//     public static Rectangle rectangleGeographic(float lon1, float lat1, float lon2, float lat2) {
//         float x1 = normalizeLongitude(lon1);
//         float x2 = normalizeLongitude(lon2);
//         if (x2 < x1) {
//             x2 += 360;
//         }
//         return rectangle(x1, lat1, x2, lat2);
//     }
// 
//     private static Rectangle rectangleDouble(double x1, double y1, double x2, double y2) {
//         return  RectangleDouble.create(x1, y1, x2, y2);
//     }
// 
//     public static Circle circle(double x, double y, double radius) {
//         return CircleDouble.create(x, y, radius);
//     }
// 
//     public static Circle circle(float x, float y, float radius) {
//         return CircleFloat.create(x, y, radius);
//     }
// 
//     public static Line line(double x1, double y1, double x2, double y2) {
//         return LineDouble.create(x1, y1, x2, y2);
//     }
// 
//     public static Line line(float x1, float y1, float x2, float y2) {
//         return LineFloat.create(x1, y1, x2, y2);
//     }
// 
//     @VisibleForTesting
//     static double normalizeLongitude(double d) {
//         return normalizeLongitude((float) d);
//     }
// 
//     private static float normalizeLongitude(float d) {
//         if (d == -180.0f)
//             return -180.0f;
//         else {
//             float sign = Math.signum(d);
//             float x = Math.abs(d) / 360;
//             float x2 = (x - (float) Math.floor(x)) * 360;
//             if (x2 >= 180)
//                 x2 -= 360;
//             return x2 * sign;
//         }
//     }
// 
//     private static double normalizeLongitudeDouble(double d) {
//         if (d == -180.0f)
//             return -180.0d;
//         else {
//             double sign = Math.signum(d);
//             double x = Math.abs(d) / 360;
//             double x2 = (x - (float) Math.floor(x)) * 360;
//             if (x2 >= 180)
//                 x2 -= 360;
//             return x2 * sign;
//         }
//     }
// 
// }
// 
// Path: src/main/java/com/github/davidmoten/rtree/geometry/Point.java
// public interface Point extends Rectangle {
// 
//     double x();
// 
//     double y();
// 
// }
// 
// Path: src/main/java/com/github/davidmoten/rtree/geometry/Rectangle.java
// public interface Rectangle extends Geometry, HasGeometry {
// 
//     double x1();
// 
//     double y1();
// 
//     double x2();
// 
//     double y2();
// 
//     double area();
// 
//     double intersectionArea(Rectangle r);
// 
//     double perimeter();
// 
//     Rectangle add(Rectangle r);
// 
//     boolean contains(double x, double y);
//     
//     boolean isDoublePrecision();
// 
// }


// Path: src/test/java/com/github/davidmoten/rtree/LatLongExampleTest.java
import static org.junit.Assert.assertEquals;
import java.util.List;
import org.junit.Test;
import com.github.davidmoten.grumpy.core.Position;
import com.github.davidmoten.rtree.geometry.Geometries;
import com.github.davidmoten.rtree.geometry.Point;
import com.github.davidmoten.rtree.geometry.Rectangle;
import rx.Observable;
import rx.functions.Func1;

                // get the result
                .toList().toBlocking().single();

        // should have returned Sydney only
        assertEquals(1, list.size());
        assertEquals("Sydney", list.get(0).value());
    }

    public static <T> Observable<Entry<T, Point>> search(RTree<T, Point> tree, Point lonLat,
            final double distanceKm) {
        // First we need to calculate an enclosing lat long rectangle for this
        // distance then we refine on the exact distance
        final Position from = Position.create(lonLat.y(), lonLat.x());
        Rectangle bounds = createBounds(from, distanceKm);

        return tree
                // do the first search using the bounds
                .search(bounds)
                // refine using the exact distance
                .filter(new Func1<Entry<T, Point>, Boolean>() {
                    @Override
                    public Boolean call(Entry<T, Point> entry) {
                        Point p = entry.geometry();
                        Position position = Position.create(p.y(), p.x());
                        return from.getDistanceToKm(position) < distanceKm;
                    }
                });
    }

    @Test
    public void testSearchLatLongCircles() {