# Distance to the ocean

Given a GeoPosition, what is the recommended method to calculate its distance from the ocean?

EDIT

As an example, Atlanta is 350 km from the Atlantic ocean, but where is that actual location? It would be nice to not have to specify an ocean by name as well.

GeoDistance[Entity["City", {"Atlanta", "Georgia", "UnitedStates"}],
Entity["Ocean", "AtlanticOcean"]]


Quantity[349.84438296049916, "Kilometers"]

GeoGraphics[{Entity["Country", "UnitedStates"],
GeoMarker[Entity["City", {"Atlanta", "Georgia", "UnitedStates"}],
"Alignment" -> Bottom]},
GeoCenter -> Entity["City", {"Atlanta", "Georgia", "UnitedStates"}],
GeoRange -> Quantity[600, "Kilometers"],
GeoBackground -> "Coastlines"]


Probably can make this more elegant, but here's what I've got so far:

atlanta = Entity["City", {"Atlanta", "Georgia", "UnitedStates"}];
nearestOcean = First@GeoNearest[Entity["Ocean"], atlanta];
oceanPoints = Flatten[EntityValue[nearestOcean, "Polygon"][[1]]["Data"], 1];
nearestCoastalPoint = Nearest[oceanPoints, EntityValue[atlanta, "Coordinates"]]
(* {{32.52, -80.8324}} *)


GeoGraphics[
{Entity["Country", "UnitedStates"],
GeoMarker[atlanta, "Alignment" -> Bottom],
GeoMarker[GeoPosition[nearestCoastalPoint], "Alignment" -> Bottom]},
GeoCenter -> atlanta,
GeoRange -> Quantity[600, "Kilometers"],
GeoBackground -> "Coastlines"]


The point doesn't look like it's quite on the coast, but maybe the plot just doesn't show sufficient detail at this scale.

EDIT

The option GeoBackground -> "Coastlines" reduces the precision of the plot. If I try this,

GeoGraphics[
{Entity["Country", "UnitedStates"],
GeoMarker[GeoPosition[nearestCoastalPoint], "Alignment" -> Bottom]},
GeoCenter -> nearestCoastalPoint,
GeoRange -> Quantity[50, "Kilometers"]]


It does indeed seem like this point could be consdidered to be on the coast.

• GeoGraphics[{ Entity["Country", "UnitedStates"] , GeoMarker[atlanta, "Alignment" -> Bottom] , GeoMarker[GeoPosition[nearestCoastalPoint] , "Alignment" -> Bottom], GeoDisk[atlanta, Quantity[350, "Kilometers"]] } , GeoCenter -> atlanta , GeoRange -> Quantity[400, "Kilometers"] , GeoBackground -> "Coastlines" ] suggests otherwise, but this could be a projection issue.
– Syed
Commented Oct 5, 2022 at 17:07
• Thanks @lericr, I will have to study this solution, especially the polygon part further.
– Syed
Commented Oct 5, 2022 at 19:32

Another possibility: Take a position for your city:

a = GeoPosition@Entity["City", {"Atlanta", "Georgia", "UnitedStates"}]
(* GeoPosition[{33.7629, -84.4227}] *)


Then take the points of the US boundary (I'm skipping the nearestOcean computation performed in the accepted solution):

us = First[Entity["Country", "UnitedStates"]["Polygon"]]


Compute the closest of those points:

q = First[Nearest[us, p]]
(* GeoPosition[{31.8109, -81.3837}] *)


Check that the distance is approximately what you obtained initially:

GeoDistance[p, q, UnitSystem -> "Metric"]
(* Quantity[357.609, "Kilometers"] *)


The difference is due to the fact that the original computation was performed with the polygon of Atlanta, while we used a central point.