# Finding closest point to shapefile coastline Python

I have a dataset consisting of 2,2 million houses in a Norway, and I want to find the distance to the coast for each dwelling. The location of the house is made up by latitude and longitude coordinates, and I have downloaded a shapefile of Europe with all the coastlines from (Could not find a shapefile for only Norway): https://www.eea.europa.eu/data-and-maps/data/eea-coastline-for-analysis-1/gis-data/europe-coastline-shapefile

Which have the coastline in both MULTILINESTRING and POLYGON format.

Then I try using the nearest point function from the shapely library, but it only assign the same value for each point, and it takes too long to run.

``````import geopandas as gpd
from shapely.geometry import Point
from shapely.ops import nearest_points

def findClosestCoastline(df):

#assign Point from the coordinates of dwelling
df['point'] = [Point(x, y) for x, y in zip(df['coord_x'].astype(float),
df['coord_y'].astype(float))]

for i in range(df)
#TODO: Fix this, get the closest point from the MULTISTRINGLINE
np = nearest_points(map_2.iloc['geometry'], df.iloc[i]['point'])
df.loc[i, 'closestPoint_Coast'] = np

#TODO generate the distance in KM
df.loc[i, 'distance_To_Coast'] =

return df
``````

The fastest way to do this is to create an image where the value of each pixel is the distance to the coastline, then pluck values from that image for each point. Of course, that means the precision of the distance is only going to be as good as the resolution of the image, so if you want want less than a few meters accuracy, the image would have to be huge.

Using GeoPandas, you can map across the points in parallel calling the shapely `distance` method to the coastline file. Since there is one feature in the coastline file `point.distance(coastlines)` will give you the distance to to coastline for each point. This is still fairly slow though, on a 4 core machine it runs around 340 features per second for me. That'll take some time to rip through 2.5MM.

``````import geopandas as gpd
from shapely.geometry import Point, box
from random import uniform
from tqdm.notebook import tqdm

#single geom for Norway
norway = world[world["name"]=="Norway"].dissolve(by='name').iloc.geometry

#single geom for the coastline
norway.buffer(0.25)).iloc.geometry

def make_point(id):
point = None
while point is None or not norway.contains(point):
point = Point(uniform(norway.bounds,norway.bounds),
uniform(norway.bounds,norway.bounds))
return {"id": id, "geometry": point}

def compute_distance(point):
point['dist_to_coastline'] = point['geometry'].distance(coastline)
return point 