# Find Properties of Nearest Line Segments to Point in Python

I have been trying to find a workable Python approach to finding nearest line segments to one (or more) points, and came accross @gene's solution to @RustyMagoo's problem here:

``````from shapely.geometry import Point, LineString, mapping, shape
from shapely import affinity
import fiona

sweep_res = 10  # sweep resolution (degrees)
focal_pt = Point(0, 0)  # radial sweep centre point

# create the radial sweep lines
line = LineString([(focal_pt.x,focal_pt.y), \

sweep_lines = [affinity.rotate(line, i, (focal_pt.x, focal_pt.y)) \
for i in range(0, 360, sweep_res)]

# load the input lines and combine them into one geometry
input_lines = fiona.open("input_lines.shp")
input_shapes = [shape(f['geometry']) for f in input_lines]

perimeter = []
# traverse each radial sweep line and check for intersection with input lines

if inter.type == "MultiPoint":
# radial line intersects at multiple points
inter_dict = {}
for inter_pt in inter:
inter_dict[focal_pt.distance(inter_pt)] = inter_pt
# save the nearest intersected point to the sweep centre point
perimeter.append(inter_dict[min(inter_dict.keys())])

if inter.type == "Point":
# radial line intersects at one point only
perimeter.append(inter)

if inter.type == "GeometryCollection":
# radial line doesn't intersect, so skip
pass

# combine the nearest perimeter points into one geometry