Code Monkey home page Code Monkey logo

Comments (9)

davidmoten avatar davidmoten commented on August 24, 2024

I didn't quite understand what you are trying to do and what the problem was. Can you make a failing unit test please that demonstrates the problem. The test should be as succinct as possible focusing on the unexpected behaviour, not your use case. Thanks.

from rtree.

mghildiy avatar mghildiy commented on August 24, 2024
Rectangle mbb1 = Geometries.rectangleGeographic(72.96089172363281,19.17504474158168,72.96230792999268,19.190122840904767);
Rectangle mbb2 = Geometries.rectangleGeographic(72.96083271503448,19.173942709431888,72.96088099479675,19.174720466220187);
RTree<String, Rectangle> tree = RTree.create();
Map<String, Rectangle> nameByBoundingBox = new HashMap();
tree = tree.add("bb1", mbb1);
tree = tree.add("bb2", mbb2);
nameByBoundingBox.put("bb1", mbb1);
nameByBoundingBox.put("bb2", mbb2);

Map<String, String> pairs = new HashMap<>();
for(Map.Entry<String, Rectangle> entry : nameByBoundingBox.entrySet()) {
    tree.search(entry.getValue(), new Func2<Rectangle, Rectangle, Boolean>() {
            public Boolean call(Rectangle g, Rectangle r) {
                   //  find those items which are within 5 metres
                   return 0.0 <= g.distance(r) && g.distance(r) <= 5.0;
                }
            })
            .forEach((Entry<String, Rectangle> rectangle) -> {
                // don't pair up with itself
                if(!rectangle.value().equals(entry.getKey())) {
                    pairs.put(entry.getKey(), rectangle.value());
                }
            });
            // remove this node from rtree so that its not paired up again
            tree = tree.delete(entry.getKey(), nameByBoundingBox.get(entry.getKey()));
}

So I am basically looking to pair up those rectangles which are within 5 metres distance. I expect that for every iteration of for loop, number of times 'call' is invoked is number of times items left in rtree. So if we start with 2 rectangles in rtree, for first entry 'call' is called 2 times. Then I remove that entry using 'delete' method. So now 1 item is left. So for second entry, 'call' is invoked 1 time. So total 3 times 'call' must be invoked. But I observe that 'call' is invoked only twice.

from rtree.

davidmoten avatar davidmoten commented on August 24, 2024

Thanks but please isolate further. I want to see one call to search that doesn't give what you expect or a call to delete that doesn't give you what you expect.

from rtree.

mghildiy avatar mghildiy commented on August 24, 2024

So I have structured code a bit. I wanted to modify a counter inside 'call' method which I could then apply assert on. But it seems its not possible. So what I have done is that I have put a print out statement inside 'call'. I expect "Inside call" to be printed 3 times, but you would see only 2 times, and each time g and r are similar to each other, meaning 'call' is not invoked for comparing different g and r.
PS: I have changed criteria in 'call' and then used assert on pairs.

class Test {
     public static void main(String[] args) {
        Rectangle mbb1 = Geometries.rectangleGeographic(72.96089172363281, 19.17504474158168, 72.96230792999268, 19.190122840904767);
        Rectangle mbb2 = Geometries.rectangleGeographic(72.96083271503448, 19.173942709431888, 72.96088099479675, 19.174720466220187);
        RTree<String, Rectangle> tree = RTree.create();
        Map<String, Rectangle> nameByBoundingBox = new HashMap();
        tree = tree.add("bb1", mbb1);
        tree = tree.add("bb2", mbb2);
        nameByBoundingBox.put("bb1", mbb1);
        nameByBoundingBox.put("bb2", mbb2);

        Map<String, String> pairs = new HashMap<>();
        for (Map.Entry<String, Rectangle> entry : nameByBoundingBox.entrySet()) {
            tree.search(entry.getValue(), new Func2<Rectangle, Rectangle, Boolean>() {
                public Boolean call(Rectangle g, Rectangle r) {
                    System.out.println("Inside call");
                    return 0.0 <= g.distance(r) && g.distance(r) <= 5000.0;
                }
            })
                    .forEach((Entry<String, Rectangle> rectangle) -> {
                        // don't pair up with itself
                        if (!rectangle.value().equals(entry.getKey())) {
                            pairs.put(entry.getKey(), rectangle.value());
                        }
                    });
            // remove this node from rtree so that its not paired up again
            tree = tree.delete(entry.getKey(), nameByBoundingBox.get(entry.getKey()));
        }

        Assert.equals(1, pairs.size());
    }
}

Please let me know if you need more inputs or action on my side.

from rtree.

davidmoten avatar davidmoten commented on August 24, 2024

Hi, I'm trying to avoid some diagnostic work here. If there is a problem with the library then one call to tree.search or tree.delete is doing the wrong thing. Can you step through this three times yourself to see when something unexpected happens and then isolate so you have one tree object that shows the problem. No loops, just add and delete stuff from a tree and show it ends up in an unexpected state.

from rtree.

mghildiy avatar mghildiy commented on August 24, 2024

So I removed delete part and loop, and now my code is:

tree.search(nameByBoundingBox.get("bb1"), new Func2<Rectangle, Rectangle, Boolean>() {
            public Boolean call(Rectangle g, Rectangle r) {
                //  find those items which are within 5 metres
                System.out.println("Inside call");
                return 0.0 <= g.distance(r) && g.distance(r) <= 5000.0;
            }
        })
                .forEach((Entry<String, Rectangle> rectangle) -> {
                    // don't pair up with itself
                    if (!rectangle.value().equals("bb1")) {
                        pairs.put("bb1", rectangle.value());
                    }
                });

When I run this code, 'call' is still invoked only once, even though there are 2 rectangles in tree.
I also output tree image at start and end, and both are same, as below.

image

I don't see any modification in tree.

from rtree.

mghildiy avatar mghildiy commented on August 24, 2024

Another observation I made. With following code,

tree.search(nameByBoundingBox.get("bb1"), 5000.0)
                .forEach((Entry<String, Rectangle> rectangle) -> {
                    // don't pair up with itself
                    System.out.println("Inside foreach");
                    if (!rectangle.value().equals("bb1")) {
                        pairs.put("bb1", rectangle.value());
                    }
                });

I see 'Inside foreach' printed twice, as expected. So only when I pass callback function to search method, code is not working as expected.

from rtree.

davidmoten avatar davidmoten commented on August 24, 2024

Wait a second, you shouldn't expect the call to be made once for every item in the tree. If it always did then it would be a pretty useless index wouldn't it! The distance check might be made on a bounding box around two items. If it fails then the distance check will fail for the two items as well and the check doesn't need to be made on them individually. If there is a problem here you need to show me via the results of the search not the number of times the distance check is called.

from rtree.

davidmoten avatar davidmoten commented on August 24, 2024

Closing as non-issue.

from rtree.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.