# GeoPandas - compute distance to nearest road in buffer

My question is similar to this one, but I am having trouble applying the solution to my case.

I have two data sets: one contains a bunch of latitude / longitude points that I've converted into a Point geometry column. The other is a shapefile containing information on primary roads in the US from the Census website. My goal is to compute the distance between each point and the closest primary road within some buffer area.

I think there are two sources of confusion for me:

1. I'm not sure if I'm using the correct CRS or if I need to be projecting my latitude/longitude points. I find projection and when to use it a bit confusing still.
2. Am I able to create a buffer and clip the roads around each point separately?

Right now what is happening is that when I try to clip my computer terminates the process after a while. Trying on individual points I get null results.

Here is what I'm trying right now:

import geopandas as gpd
import matplotlib.pyplot as plt
import pandas as pd
from shapely import geometry
from shapely.ops import transform
from shapely.geometry import Point

# read in my points data
geo_df = gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.longitude, df.latitude))
geo_df = geo_df.set_crs('EPSG:3857')

# get buffer around each point
geo_df['buffer'] = [point.buffer(radius) for point in geo_df['geometry']]

# create new geometry of clipped roads for each point

The last line is where I'm failing to make progress.

Here is a sample data if it helps:

latitude  longitude                geometry
0  33.516935 -86.779229  POINT (-86.779 33.517)
1  32.415983 -86.295076  POINT (-86.295 32.416)
2  34.414842 -87.045289  POINT (-87.045 34.415)
3  32.300587 -86.208014  POINT (-86.208 32.301)
4  30.673394 -88.140719  POINT (-88.141 30.673)

Maybe I am misunderstanding how clip works.

UPDATE: After some tinkering around, I got the accepted answer to work for me. I ended up using 'ESRI:102003' as my CRS, and this seems to work fine for my purposes. One thing that tripped me up for a bit was that I was setting my points crs to 102003 immediately after reading in the data, which was giving me the wrong coordinates. I needed to first set crs to 'EPSG:4269' and then convert to the other crs.

• EPSG:3857 is intended to be used for web mapping and it is not good for distances. It is better to use a projected CRS such as UTM which is suitable for such analysis. You need to know the correct zone for your data before projecting the data which depends on the location of your study area. Dec 2, 2022 at 5:52
• Is there one UTM code that I could use for the entire US area? Dec 2, 2022 at 13:17

Reproject to a coordinate system of your choice. The example coordinates you are showing are in Alabama US so I choose 32613, I dont know which is best if you have data all over the US.

Buffer, intersect buffers with roads, for each point id find the closest road, merge(=join) the result back to the points:

import geopandas as gpd
import numpy as np

points = points.to_crs(32616)
points["pointid"] = np.arange(points.shape[0]) #Create an id column
points["pointgeometry"] = points["geometry"] #Save the point geometry
points["geometry"] = points.buffer(10000)

#Intersect to clip all roads with each buffer and get all road attributes

closest = []
for group, frame in clipped.groupby("pointid"): #For each pointid and all lines intersecting its buffer
#Calculate the distances to all roads intersecting the point buffer
frame["distance"] = frame["pointgeometry"].distance(frame["geometry"]) #That is why we saved the point geometry