def dists_to_pt(pts, pt, use_great_circle=True): """ Returns a list of distances (in km) from each of the points to to the pt (uses great_circle_distance) pts: """ rpy2.robjects.numpy2ri.activate() np_pts = numpy.array(pts) np_pt = numpy.array(pt) dists_to_pt = r.spDistsN1(np_pts, np_pt, longlat=use_great_circle) return dists_to_pt
#activate r to numpy array auto-conversion rpy2.robjects.numpy2ri.activate() #load sp library r.library('sp') to_x = numpy.random.rand(100) * 100 to_y = numpy.random.rand(100) * 100 xy = zip(to_x, to_y) xy_array = numpy.array(xy) dists = r.spDists(xy_array) tree = r.hclust(r('as.dist')(dists), "average") clusts = r.cutree(tree, h=100.0) np_clusts = numpy.array(clusts) clusters = [[] for i in range(max(np_clusts))] for i in range(0, len(np_clusts)): clusters[np_clusts[i] - 1].append(xy_array[i]) cluster1 = clusters[len(clusters) - 1] cluster1_cols = numpy.array(cluster1) print cluster1_cols print cluster1[0] dists = r.spDistsN1(cluster1_cols, cluster1[0]) print dists