I am working on a hotel application (using ruby on rails), and I am trying to calculate the distance between an hotel and the sea.
I have gathered the geometry of the shoreline points in a geojson file and I am now trying to calculate the distance using the rgeo gem.
My findings are the following:
when using a simple_mercator_factory, I obtain an wrong value and when using a spherical_factory the distance between 2 point is correct, but I calculating the distance between a point and a line yields an error.
How can I calculate this distance ?
Try using
geographic_factorywhich uses spherical calculations and can handle both point-to-point and point-to-line distance calculations.Parse the shoreline points from the GeoJSON file and convert them to RGeo objects using the geographic_factory:
Create an RGeo point for the hotel location using the geographic_factory:
Calculate the minimum distance between the hotel location and the shoreline points:
Now, min_distance will hold the shortest distance between the hotel and the shoreline in meters.
edit: you'll need to add
rgeo-geojson