Skip to content

Commit

Permalink
Merge pull request #259 from ljwolf/master
Browse files Browse the repository at this point in the history
Switch snapping to Rtree package?
  • Loading branch information
jGaboardi committed May 11, 2019
2 parents 5e97b2f + 87be66f commit 59f3bd4
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
scipy>=0.11
numpy>=1.3
pandas
libpysal
esda
esda
rtree
13 changes: 6 additions & 7 deletions spaghetti/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from libpysal import cg
from libpysal.common import requires
from rtree import Rtree

try:
import geopandas as gpd
Expand Down Expand Up @@ -421,15 +422,15 @@ def snap_points_to_links(points, links):
"""

# instantiate an rtree
rtree = cg.Rtree()
rtree = Rtree()
# set the smallest possible float epsilon on machine
SMALL = np.finfo(float).eps

# initialize network vertex to link lookup
vertex_2_link = {}

# iterate over network links
for link in links:
for i,link in enumerate(links):

# extract network link (x,y) vertex coordinates
head, tail = link.vertices
Expand All @@ -452,12 +453,9 @@ def snap_points_to_links(points, links):
bx1 += SMALL
by1 += SMALL

# create a rectangle
rect = cg.Rect(bx0, by0, bx1, by1)

# insert the network link and its associated
# rectangle into the rtree
rtree.insert(link, rect)
rtree.insert(i, (bx0, by0, bx1, by1), obj=link)

# build a KDtree on link vertices
kdtree = cg.KDTree(list(vertex_2_link.keys()))
Expand All @@ -482,7 +480,8 @@ def snap_points_to_links(points, links):
# Find all links with bounding boxes that intersect
# a query rectangle centered on the point with sides
# of length dmin * dmin
candidates = [cand for cand in rtree.intersection([x0, y0, x1, y1])]
rtree_lookup = rtree.intersection([x0, y0, x1, y1], objects=True)
candidates = [cand.object for cand in rtree_lookup]
dmin += SMALL
dmin2 = dmin * dmin

Expand Down

0 comments on commit 59f3bd4

Please sign in to comment.