def time_tree_nearest_all_poly_python(self): # returns all input points # use an arbitrary search tolerance that seems appropriate for the density of # geometries tolerance = 200 b = shapely.buffer(self.points, tolerance, quadsegs=1) left, right = self.tree.query(b) dist = shapely.distance(self.points.take(left), self.polygons.take(right)) # sort by left, distance ix = np.lexsort((right, dist, left)) left = left[ix] right = right[ix] dist = dist[ix] run_start = np.r_[True, left[:-1] != left[1:]] run_counts = np.diff(np.r_[np.nonzero(run_start)[0], left.shape[0]]) mins = dist[run_start] # spread to rest of array so we can extract out all within each group that match all_mins = np.repeat(mins, run_counts) ix = dist == all_mins left = left[ix] right = right[ix] dist = dist[ix]
def time_tree_nearest_points_equidistant_manual_all(self): # This benchmark approximates nearest_all for equidistant results # starting from singular nearest neighbors and searching for more # within same distance. # try to find all equidistant neighbors ourselves given single nearest # result l, r = self.grid_point_tree.nearest(self.grid_points) # calculate distance to nearest neighbor dist = shapely.distance(self.grid_points.take(l), self.grid_point_tree.geometries.take(r)) # include a slight epsilon to ensure nearest are within this radius b = shapely.buffer(self.grid_points, dist + 1e-8) # query the tree for others in the same buffer distance left, right = self.grid_point_tree.query(b, predicate="intersects") dist = shapely.distance(self.grid_points.take(left), self.grid_point_tree.geometries.take(right)) # sort by left, distance ix = np.lexsort((right, dist, left)) left = left[ix] right = right[ix] dist = dist[ix] run_start = np.r_[True, left[:-1] != left[1:]] run_counts = np.diff(np.r_[np.nonzero(run_start)[0], left.shape[0]]) mins = dist[run_start] # spread to rest of array so we can extract out all within each group that match all_mins = np.repeat(mins, run_counts) ix = dist == all_mins left = left[ix] right = right[ix] dist = dist[ix]
def time_distance(self): shapely.distance(self.points, self.polygon)
def distance(self, other): """Unitless distance to other geometry (float)""" return float(shapely.distance(self, other))
def test_distance_missing(): actual = shapely.distance(point, None) assert np.isnan(actual)
def test_distance(): actual = shapely.distance(*point_polygon_testdata) expected = [2 * 2**0.5, 2**0.5, 0, 0, 0, 2**0.5] np.testing.assert_allclose(actual, expected)
def test_distance_empty(): actual = shapely.distance(point, empty) assert np.isnan(actual)