def test_two_equal_targets(self):
     geom = LineString([(0, 0), (1, 1)])
     targets1 = [Point(0, 0), Point(1, 1)]
     closest_geometry1 = common.closest(geom, targets1, 1)
     targets2 = [Point(1, 1), Point(0, 0)]
     closest_geometry2 = common.closest(geom, targets2, 1)
     self.assertNotEqual(closest_geometry1, closest_geometry2)
 def test_polygon_cresent_two_targets(self):
     geom = Polygon([(1, 1.5), (1, 2), (0, 2), (0, 0), (1, 0), (1, 0.5),
                     (1.1, 0.5), (1.1, -0.1), (-0.1, -0.1), (-0.1, 2.1),
                     (1.1, 2.1), (1.1, 1.5)])
     targets = [Point(0.5, 1), Point(1.5, 0)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(1.5, 0))
 def test_collection_two_line_targets(self):
     geom = GeometryCollection([Point(0, 0), LineString([(1, 0), (1, 1)])])
     targets = [
         LineString([(-0.5, 0), (0.5, 0)]),
         LineString([(1.1, 0), (1.1, 1)])
     ]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, LineString([(-0.5, 0), (0.5, 0)]))
Exemple #4
0
def join_points_to_lines(points, lines):
    """ Create Shapely LineStrings joining each provided point to the closest
    endpoint within the provided line geometries.

    Arguments:
        points {list} -- list of Shapely Points
        lines {list} -- list of Shapely Linestrings

    Returns:
        [list] -- list of Shapely Linestrings joining points to lines
    """

    # create an empty spatial index object
    index = rtree.index.Index()

    # populate the spatial index
    for index_id, geom in enumerate(lines):
        index.insert(index_id, geom.bounds)

    # create lines which join points to lines
    joins = []
    for search_id, geom in enumerate(points):

        # buffer out the point until we hit a line
        r = 1
        index_ids = []

        while len(index_ids) == 0:
            buffered = geom.buffer(r)
            index_ids = [int(i) for i in index.intersection(buffered.bounds)]
            r *= 2

        points_in_lines = []
        for l in [lines[ind] for ind in index_ids]:
            points_in_lines.append(Point(l.coords[0]))
            points_in_lines.append(Point(l.coords[-1]))

        closest_point_on_line = closest(geom, points_in_lines)
        joins.append(LineString((geom, closest_point_on_line)))

    return joins
 def test_polygon_outside_two_targets(self):
     geom = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)])
     targets = [Point(0.5, 0.5), Point(0.5, 1.4)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(0.5, 0.5))
 def test_point_no_targets(self):
     geom = Point(0, 0)
     targets = []
     with self.assertRaises(ValueError):
         closest_geometry = common.closest(geom, targets, 1)
 def test_linear_ring_outside_two_targets(self):
     geom = LinearRing([(0, 0), (0, 1), (1, 1), (1, 0)])
     targets = [Point(0.5, 0.5), Point(0.5, 1.4)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(0.5, 1.4))
 def test_line_point_two_targets(self):
     geom = LineString([Point(0, 0), Point(0, 10)])
     targets = [Point(2, 5), Point(1, 10)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(1, 10))
 def test_line_one_target(self):
     geom = LineString([Point(0, 0), Point(0, 1)])
     targets = [Point(1, 1)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(1, 1))
 def test_second_closest_point_two_targets(self):
     geom = Point(0, 0)
     targets = [Point(1, 1), Point(2, 2)]
     closest_geometry = common.closest(geom, targets, 2)
     self.assertEqual(closest_geometry, Point(2, 2))
 def test_point_one_target(self):
     geom = Point(0, 0)
     targets = [Point(1, 1)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(1, 1))
 def test_collection_two_targets(self):
     geom = GeometryCollection([Point(0, 0), LineString([(1, 0), (1, 1)])])
     targets = [Point(0, 0.5), Point(1.4, 0.5)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(1.4, 0.5))
 def test_polygon_with_hole_two_targets(self):
     geom = Polygon([(0, 0), (0, 1), (1, 1), (1, 0)],
                    [LinearRing([(0.5, 0), (0, 0.5), (0.5, 1), (1, 0.5)])])
     targets = [Point(0.5, 0.5), Point(0.5, 1.2)]
     closest_geometry = common.closest(geom, targets, 1)
     self.assertEqual(closest_geometry, Point(0.5, 1.2))