# I need to calculate shortest distance from an inputted point to the nearest lines. Nearest 4 lines

I have a huge dataset of points and roads. The point file is of Residential houses or Comercial shops/ markets. And the Line network is the roads around them. Ideally, I want to take 1 point (input) and find out its distance from nearest 4 lines (roads). The roads shapefile also includes some attributes (including name and Width). The final output should include Distance(meters) from inputted point to the nearest 4 roads and the attributes of these 4 roads.

``````roads = gpd.read_file (r'fRoads.shp')
long = float(input("Enter longitude E value ))
lat = float(input("Enter latitude N Value ))
p1 = Point((long, lat))
df = pd.DataFrame({'a':[lat,long]})
po = gpd.GeoDataFrame(geometry = [p1], crs = roads.crs)
fig, ax= plt.subplots(figsize = (15,7))
bufo.plot(ax=ax, color = 'None', edgecolor = 'red')
po.plot(ax=ax, color = 'blue')

fig, ax= plt.subplots(figsize = (15,7))
bufo.plot (ax = ax, color ='None', edgecolor = 'Black')
cliped. plot (ax = ax, color = 'red')
po.plot (ax = ax, color = 'Black')
print (cliped.length)
print (dist)
``````

I am using the Distance Function from shapely. but it gives the distance between 2 points not between a point and line.

I need something equivalent to the following code but without Arcpy and that should be Open Source.

``````    import arcpy

arcpy.env.overwriteOutput = True

long = float(input("Enter longitude N value e.g: 77.xxxxx: "))
lat = float(input("Enter latitude E Value e.g: 28.xxxxx:  "))
spatial_reference = arcpy.SpatialReference(32642)
p1 = arcpy.Point(long, lat)
pnt_geometry = arcpy.PointGeometry(p1, spatial_reference)
location = 'NO_LOCATION'
angle = 'NO_ANGLE'
out_table = "out_table.dbf"
closest = 'ALL'
closest_count = 4
fp = arcpy.SearchCursor(out_table)
for f in fp:
print f.getValue("NEAR_FID")
print f.getValue("NEAR_DIST")
``````
• Did you try to get the Points of the lines (lines consists of Points), calculate the distance of point to each one of these Points and keep the minimum one? Mar 22, 2021 at 6:27
• Distance Function from shapely. gives also the distance between a point and a line
– gene
Mar 22, 2021 at 9:17
• And the shapely distance is euclidean, so you can use it with longitude, latitude (angles)
– gene
Mar 22, 2021 at 9:33
• Can you share any example... I have tried but could not get done with that. I have seen point-to-point distance but not point to line. Mar 22, 2021 at 21:13

I think your issue is that your road geometries are using a projected coordinate system (UTM Zone 42N (EPSG Code 32642); according to your second code snippet with `arcpy`) with metre for the unit, while you are using lat/lon coordinates (in degrees) for your point. However, I was not able to run your script to double check.

If you need to use lat/lon coordinate input but have your data in a projected coordinate system, then you may be able to use `shapely.ops.transform(func, geom)`. I have added an example at the end of this answer.

Since I was not able to get your code working as well as I have no data available in the coordinate system your were using, I rewrote what I think you are looking for, using `shapely` and `geopandas`. Note that I use another dataset and projection (UTM Zone 20N) than you, neglected the user input and use a hard-coded point coordinate and buffer radius.

``````import matplotlib.pyplot as plt
import geopandas as gpd
from shapely import geometry
from shapely.geometry import Point

# Using test data from here: https://nsgi.novascotia.ca/gdd/
# NAD83(CSRS) / UTM Zone 20N (EPSG:2961)
spatial_reference = 2961

# Approx. coordinates of the Sydney Street Pub
x = 281247.02
y = 4944920.76

point = Point(x, y)

# calculate distance for all lines

# get 4 roads closest to the point
``````

Result

``````print(closest_roads)
``````

Plotting the result

``````# plot result
figure, ax = plt.subplots(figsize = (15,7))

# there is probably a more elegant and better way to plot a polygon
# without converting to a geodataframe first... ;)
# if so, use it as I am not an expert using matplotlib
buffer_gdf = gpd.GeoDataFrame(geometry = [buffer], crs = roads.crs)
buffer_gdf.plot(ax = ax, color = "white", edgecolor = "black")

clipped_roads.plot(ax = ax, color = "black")
closest_roads.plot(ax = ax, color = "red")

point_gdf = gpd.GeoDataFrame(geometry = [point], crs = roads.crs)
point_gdf.plot(ax = ax, color = "blue")

plt.savefig("result.png")
``````

Projecting Coordinates

Following an example how to project coordinates:

``````from shapely.ops import transform
from shapely.geometry import Point

x = -65.75754
y = 44.624347

wgs84 = pyproj.CRS("EPSG:4326")
utm = pyproj.CRS("EPSG:2961")

project = pyproj.Transformer.from_crs(wgs84, utm, always_xy=True).transform
point = transform(project, Point(x, y))
``````