calc_distance_on_unit_sphere
- pyhelpers.geom.calc_distance_on_unit_sphere(pt1, pt2, unit='mile', precision=None)[source]
Calculate distance between two points.
- Parameters:
pt1 (shapely.geometry.Point | tuple | numpy.ndarray) – one point
pt2 (shapely.geometry.Point | tuple | numpy.ndarray) – another point
unit (str) – distance unit (for output), defaults to
'miles'
; valid options include'mile'
and'km'
precision (None | int) – decimal places of the calculated result, defaults to
None
- Returns:
distance (in miles) between
pt1
andpt2
(relative to the earth’s radius)- Return type:
float | None
Examples:
>>> from pyhelpers.geom import calc_distance_on_unit_sphere >>> from pyhelpers._cache import example_dataframe >>> example_df = example_dataframe() >>> example_df Longitude Latitude City London -0.127647 51.507322 Birmingham -1.902691 52.479699 Manchester -2.245115 53.479489 Leeds -1.543794 53.797418 >>> london, birmingham = example_df.loc[['London', 'Birmingham']].values >>> london array([-0.1276474, 51.5073219]) >>> birmingham array([-1.9026911, 52.4796992]) >>> arc_len_in_miles = calc_distance_on_unit_sphere(london, birmingham) >>> arc_len_in_miles # in miles 101.10431101941569 >>> arc_len_in_miles = calc_distance_on_unit_sphere(london, birmingham, precision=4) >>> arc_len_in_miles 101.1043
Note
This function is modified from the original code available at [GEOM-CDOUS-1]. It assumes the earth is perfectly spherical and returns the distance based on each point’s longitude and latitude.