TravelModellingGroup/TMGToolbox

Spatial Index not finding the correct nearest nodes

PeterKucirek opened this issue · 1 comments

Ok, so this is difficult to reproduce because I cannot share my actual network. But the problem is very clearly with tmg.common.spatial_index.GridIndex.nearestToPoint().

Essentially, attempting to create centroid connectors to the nearest node in a network. But I'm seeing connectors criss-crossing the network in a completely baffling way.

Here's the code I'm using:

import inro.modeller as m
mm = m.Modeller()

spindex = mm.module('tmg.common.spatial_index')

def attach_centroids_to_nearest_node(sc, mode_ids='c', lanes=2, data1=0, data2=0, data3=0, vdf=90):
    net = sc.get_network()
    print "Loaded network"

    modes = [net.mode(c) for c in mode_ids]

    extents = spindex.get_network_extents(net)
    print "EXTENTS:", extents

    search_grid = spindex.GridIndex(extents)
    for node in net.regular_nodes():
        search_grid.insertPoint(node)
    print "Built spatial index"

    cc_count = 0
    for centroid in net.centroids():

        set_of_candidates = search_grid.nearestToPoint(centroid.x, centroid.y)

        d = float('inf')
        nearest = None
        for node in set_of_candidates:
            d0 = compute_dist(centroid.x, centroid.y, node.x, node.y)
            if d0 < d:
                d = d0
                nearest = node

        if nearest is None:
            print "ERR: Could not find node nearest to centroid %s" %centroid
            continue

        link = net.create_link(centroid.id, nearest.id, modes)
        link.length = d; link.num_lanes = lanes
        link.data1 = data1; link.data2 = data2; link.data3 = data3
        link.volume_delay_func = vdf

        link = net.create_link(nearest.id, centroid.id, modes)
        link.length = d; link.num_lanes = lanes
        link.data1 = data1; link.data2 = data2; link.data3 = data3
        link.volume_delay_func = vdf

        cc_count +=2

    print "HANDLED ALL ZONES"
    print "Created %s centorid connectors" %cc_count

My initial debugging of search_grid.nearestToPoint() shows that the code gets to line 808 when it s supposed to return on line 802 much earlier. I haven't been able to dig any deeper just yet.

Please try and reproduce and see what is going wrong!

Thanks

Fixed in
a24eae0