package com.github.davidmoten.rtree3d;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
import java.util.zip.GZIPInputStream;

import org.junit.Test;

import com.github.davidmoten.rtree3d.geometry.Box;
import com.github.davidmoten.rtree3d.geometry.Point;
import com.github.davidmoten.rx.Strings;

import rx.Observable;
import rx.Observable.Transformer;
import rx.functions.Action1;
import rx.functions.Func1;

public class RTree3DTest {

    @Test
    public void test() throws IOException {
        Observable<Entry<Object, Point>> entries = Strings
                .from(new GZIPInputStream(RTree3DTest.class
                        .getResourceAsStream("/greek-earthquakes-1964-2000-with-times.txt.gz")))
                .compose(new Transformer<String, String>() {
                    @Override
                    public Observable<String> call(Observable<String> o) {
                        return Strings.split(o, "\n");
                    }
                }).filter(new Func1<String, Boolean>() {
                    @Override
                    public Boolean call(String line) {
                        return !line.startsWith("DATE");
                    }
                }).doOnNext(new Action1<String>() {
                    @Override
                    public void call(String line) {
                        // System.out.println(line);
                    }
                }).map(new Func1<String, String>() {
                    @Override
                    public String call(String line) {
                        return line.trim();
                    }
                }).filter(new Func1<String, Boolean>() {
                    @Override
                    public Boolean call(String line) {
                        return line.length() > 0;
                    }
                }).map(new Func1<String, Entry<Object, Point>>() {
                    @Override
                    public Entry<Object, Point> call(String line) {
                        String[] items = line.split("\t");
                        SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd'T'HH:mm:ss.s");
                        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
                        try {
                            long time = sdf.parse(items[0]).getTime();
                            float lat = Float.parseFloat(items[1]);
                            float lon = Float.parseFloat(items[2]);
                            return Entry.entry(null, Point.create(lat, lon, time));
                        } catch (ParseException e) {
                            throw new RuntimeException(e);
                        }

                    }
                });
        RTree<Object, Point> tree = RTree.star().minChildren(50).maxChildren(1000).create();
        tree = tree.add(entries).last().toBlocking().single();
        System.out.println(tree.size());
        System.out.println(tree.asString());
        long t = System.currentTimeMillis();
        int count = tree.search(Box.create(39.0, 22.0, 0, 40.0, 23.0, 3.15684946E11)).count()
                .toBlocking().single();
        t = System.currentTimeMillis() - t;
        System.out.println("search=" + count + " in " + t + "ms");

    }

}

@@ -53,32 +53,60 @@
 io.IOException;%0A
+import java.io.PrintStream;%0A
 import java.text
@@ -298,16 +298,72 @@
 ry.Box;%0A
+import com.github.davidmoten.rtree3d.geometry.Geometry;%0A
 import c
@@ -2902,17 +2902,17 @@
 hildren(
-5
+1
 0).maxCh
@@ -2922,11 +2922,9 @@
 ren(
-100
+5
 0).c
@@ -3371,16 +3371,854 @@
  %22ms%22);%0A
+        PrintStream out = new PrintStream(%22target/out.txt%22);%0A        print(tree.root().get(), out);%0A        out.close();%0A    %7D%0A%0A    @SuppressWarnings(%22unchecked%22)%0A    private static %3CT extends Geometry%3E void print(Node%3CObject, T%3E node, PrintStream out) %7B%0A        if (node instanceof NonLeaf) %7B%0A            NonLeaf%3CObject, T%3E n = (NonLeaf%3CObject, T%3E) node;%0A            Box b = node.geometry().mbr();%0A            print(b, out);%0A            for (Node%3CObject, T%3E child : n.children()) %7B%0A                print(child, out);%0A            %7D%0A        %7D else if (node instanceof Leaf) %7B%0A            Leaf%3CObject, T%3E n = (Leaf%3CObject, T%3E) node;%0A            print(n.geometry().mbr(), out);%0A        %7D%0A    %7D%0A%0A    private static void print(Box b, PrintStream out) %7B%0A        out.format(%22%25s,%25s,%25s,%25s,%25s,%25s%5Cn%22, b.x1(), b.y1(), b.z1(), b.x2(), b.y2(), b.z2());
 %0A    %7D%0A%0A

write to csv