Questions tagged [haversine]

The haversine formula is an equation important in navigation, giving great-circle distances between two points on a sphere from their longitudes and latitudes.

haversine
Filter by
Sorted by
Tagged with
1160 votes
49 answers
1.2m views

Calculate distance between two latitude-longitude points? (Haversine formula)

How do I calculate the distance between two points specified by latitude and longitude? For clarification, I'd like the distance in kilometers; the points use the WGS84 system and I'd like to ...
Robin Minto's user avatar
  • 15.2k
188 votes
13 answers
241k views

Measuring the distance between two coordinates in PHP

Hi I have the need to calculate the distance between two points having the lat and long. I would like to avoid any call to external API. I tried to implement the Haversine Formula in PHP: Here is ...
maxdangelo's user avatar
  • 3,103
159 votes
12 answers
241k views

Haversine formula in Python (bearing and distance between two GPS points)

Problem I would like to know how to get the distance and bearing between two GPS points. I have researched on the haversine distance. Someone told me that I could also find the bearing using the same ...
avitex's user avatar
  • 2,528
59 votes
7 answers
45k views

Fast Haversine Approximation (Python/Pandas)

Each row in a Pandas dataframe contains lat/lng coordinates of 2 points. Using the Python code below, calculating the distances between these 2 points for many (millions) of rows takes a very long ...
Nyxynyx's user avatar
  • 62.5k
41 votes
9 answers
58k views

Using the Haversine Formula in Javascript

I'm trying to use the Haversine Distance Formula (as found here: http://www.movable-type.co.uk/scripts/latlong.html) but I can't get it to work, please see the following code function test() { ...
Creights's user avatar
  • 907
31 votes
8 answers
13k views

CLLocation Category for Calculating Bearing w/ Haversine function

I'm trying to write a category for CLLocation to return the bearing to another CLLocation. I believe I'm doing something wrong with the formula (calculous is not my strong suit). The returned bearing ...
Nick's user avatar
  • 8,493
25 votes
5 answers
33k views

Haversine formula with php

I want to use this formula with php. I have a database with some values of latitute and longitude saved. I want to find, with a certain value of latitude and longitude in input, all the distances (...
user avatar
25 votes
1 answer
39k views

Pandas Latitude-Longitude to distance between successive rows [duplicate]

I have the following in a Pandas DataFrame in Python 2.7: Ser_Numb LAT LONG 1 74.166061 30.512811 2 72.249672 33.427724 3 67.499828 37.937264 4 84.253715 ...
edesz's user avatar
  • 12.1k
22 votes
1 answer
23k views

Querying MySQL for latitude and longitude coordinates that are within a given mile radius

I currently have a MySQL table that is structured as follows: id name lon lat ----- ----- ----------- ----------- 1 Mark -76.316528 40.036027 2 ...
Lance's user avatar
  • 4,756
16 votes
3 answers
62k views

How to transform a distance from degrees to metres?

I'm using OpenLayers with an ordinary mercator map and I'm trying to sample a bounding box by finding a grid of points in latlong. The bbox is expressed in latlon, e.g. 48.1388,-15.3616,55.2057,-3....
Mulone's user avatar
  • 3,633
16 votes
1 answer
19k views

Vectorised Haversine formula with a pandas dataframe

I know that to find the distance between two latitude, longitude points I need to use the haversine function: def haversine(lon1, lat1, lon2, lat2): lon1, lat1, lon2, lat2 = map(radians, [lon1, ...
user3755536's user avatar
13 votes
3 answers
10k views

How to filter a django model with latitude and longitude coordinates that fall within a certain radius

I have the following model. class Location(models.Model): name = models.CharField(max_length = 128, blank = True) address =models.CharField(max_length = 200, blank= True) latitude = ...
deadlock's user avatar
  • 7,178
12 votes
2 answers
3k views

Spring Query: Haversine formula with pageable

I'm trying to use the Haversine formula to find entities near to a location in a Spring Data JPA Query with Pageable but I dont get it done. My first approach looks like this @Query("SELECT m, (...
krinklesaurus's user avatar
11 votes
6 answers
26k views

Haversine distance calculation between two points in Laravel

I'm working on a Laravel application in which I need to find all the products within a certain radius of the user's coordinates. Products have a one-to-many relationship with users so that users can ...
Robke22's user avatar
  • 111
9 votes
2 answers
6k views

Interpreting sklearn haversine outputs to kilometers

I can't figure out how to interpret the outputs of the haversine implementations in sklearn (version 20.2) The documentation says,"Note that the haversine distance metric requires data in the form of ...
flyingmeatball's user avatar
9 votes
2 answers
4k views

Pandas Dataframe: join items in range based on their geo coordinates (longitude and latitude)

I got a dataframe that contains places with their latitude and longitude. Imagine for example cities. df = pd.DataFrame([{'city':"Berlin", 'lat':52.5243700, 'lng':13.4105300}, {'...
Matthias's user avatar
  • 5,644
8 votes
3 answers
8k views

Calculating Great-Circle Distance with SQLite

Here is my problem, I have a SQLite table with locations and latitudes / longitudes. Basically I need to: SELECT location, HAVERSINE(lat, lon) AS distance FROM location ORDER BY distance ASC; ...
Alix Axel's user avatar
  • 153k
8 votes
3 answers
5k views

Is Google Maps calculating the wrong distance?

I've managed to get two functions working to calculate distances between two point on a google map. One that I gleaned(stole) from various places on the web and the other using the GLatLng....
gargantuan's user avatar
  • 8,908
7 votes
5 answers
5k views

Python calculate lots of distances quickly

I have an input of 36,742 points which means if I wanted to calculate the lower triangle of a distance matrix (using the vincenty approximation) I would need to generate 36,742*36,741*0.5 = 1,349,974,...
mptevsion's user avatar
  • 947
6 votes
4 answers
6k views

Pairwise haversine distance calculation

I have two arrays with lat and long. I want to calculate distance between every pair of lat and long with every other pair of lat and long in the array. Here are my two arrays. lat_array array([ 0....
Neil's user avatar
  • 8,057
6 votes
1 answer
1k views

Vectorize haversine distance computation along path given by list of coordinates

I have a list of coordinates and can calculate a distance matrix among all points using the haversine distance metric. Coordinates come a as numpy.array of shape (n, 2) of (latitude, longitude) ...
Stefan's user avatar
  • 42.5k
6 votes
3 answers
4k views

Distance-based JOIN given Latitude/Longitude

Given the following tables: table A (id, latitude, longitude) table B (id, latitude, longitude) how do I build an efficient T-SQL query that associates each row in A with the closest row in B? The ...
Marsellus Wallace's user avatar
6 votes
1 answer
3k views

Why Manhattan Distance with haversine formula for geolocalizations is not accurate? [Python]

I want to compute the "MANHATTAN DISTANCE" also called "CITY BLOCK DISTANCE" among pairs of coordinates with LAT, LNG. Following this post Manhattan Distance for two geolocations I had computed the ...
piezzoritro's user avatar
6 votes
2 answers
3k views

Haversine Formula Implementation on Arduino

I'm working on creating a geofence using GPS & Arduino. I want to implement the Haversine Formula to find the distance between two points, in order to compute it with the radius. The problem is I'...
user3286992's user avatar
6 votes
2 answers
4k views

What earth radius should I use to calculate distances near the Poles?

I'm monitoring a GPS unit which is on it's way from Cape Discovery in Canada, to the North Pole. I need to keep track of the distance travelled and distance remaining each day, so I'm using the ...
gargantuan's user avatar
  • 8,908
5 votes
3 answers
6k views

Distance between two locations isn't right

I have used the algorithm on http://www.movable-type.co.uk/scripts/latlong.html to find the distance between two points. My two points are long1 = 51.507467; lat1 = -0.08776; long2 = 51.508736; ...
Ally's user avatar
  • 2,215
5 votes
2 answers
8k views

Using the Haversine formula with PostgreSQL and PDO

On my site I'm trying to get locations nearby. I'm trying to use the Haversine formula for this. http://en.wikipedia.org/wiki/Haversine_formula MySQL Great Circle Distance (Haversine formula) ...
PeeHaa's user avatar
  • 72.1k
5 votes
1 answer
14k views

Calculate the distance between two coordinates with Python [closed]

I have a map where they find several points (lat/long) and want to know the distance that exists between them. So, given a set of lat/long coordinates, how can I compute the distance between them in ...
V. Andy's user avatar
  • 61
5 votes
1 answer
2k views

Efficient computation of minimum of Haversine distances

I have a dataframe with >2.7MM coordinates, and a separate list of ~2,000 coordinates. I'm trying to return the minimum distance between the coordinates in each individual row compared to every ...
Walt Reed's user avatar
  • 1,376
5 votes
2 answers
6k views

VBA haversine formula

I am trying to implement Haversine formula into excel function. Its looks like this: Public Function Haversine(Lat1 As Variant, Lon1 As Variant, Lat2 As Variant, Lon2 As Variant) Dim R As Integer, ...
banshe's user avatar
  • 75
5 votes
2 answers
3k views

Is it possible to implement the Haversine formula in Objective-C and call it from SQLite?

As I understand, SQLite doesn't have the math functions to properly implement the Haversine formula in straight SQL. I'm thinking this should be possible using an external function, with the ...
brianegge's user avatar
  • 29.6k
5 votes
1 answer
1k views

Haversine formula Unity

So I'm trying to use the haversine formula in Unity to get the distance between two different points (latitude and longitud given). The code is working (no errors) but I keep gettting a wrong result. ...
Manuel Estévez's user avatar
5 votes
2 answers
3k views

Equirectangular approximation in PHP

I'm trying to calculate the distance between two latitude/longitude coordinates using the equirectangular approximation formula in PHP, but I get different results than the haversine formula (which I ...
Martijn's user avatar
  • 3,724
5 votes
5 answers
7k views

How to measure distance using Haversine formula with MySQL?

I get Latitude and Longitudes from Google Maps Reverse-Geocoding API and then I need something like this: mysql_query("SELECT users.*, ".mysql_distance_column($lat,$lng)." FROM users ORDER BY ...
Neo's user avatar
  • 11.3k
5 votes
2 answers
255 views

Product Lookup By Postal/Zip Code | Haversine Algorithm | Performance

I have an application that searches for items based on a postal code. When searching for the postal code, I return all products that are from that City/Neighborhood (done by parsing the postal/zip ...
Mark's user avatar
  • 4,833
5 votes
1 answer
16k views

The result by haversine formula is meter o kmeter?

I use the haversine formula to calculate the distance among the points. The result of this formula is in meter or in kmeter? http://en.wikipedia.org/wiki/Haversine_formula Anyone can help me?
doflamingo's user avatar
4 votes
1 answer
10k views

Calculating distance and velocity between time ordered coordinates

I have a csv containing locations (latitude,longitude) for a given user denoted by the id field, at a given time (timestamp). I need to calculate the distance and the velocity between a point and the ...
andrewr's user avatar
  • 783
4 votes
1 answer
2k views

How can I introduce the radius in query radius-BallTree sklearn, with units of radians or km?

I'm working with latitude and longitude data. I've used BallTree because I have many rows (32000 rows) in the dataset. If I built the tree with haversine distance: model_BTree = BallTree(np.array(...
DataScienceMA DMS's user avatar
4 votes
2 answers
3k views

Join operation in Haversine formula

I am implementing Haversine formula in PHP as the follows $result=mysqli_query($mysqli,"SELECT *,( 6371 * acos( cos( radians({$lat}) ) * cos( radians( `latitude` ) ) * cos( radians( `longitude` ) -...
Ashif Shereef's user avatar
4 votes
2 answers
459 views

MySQL geospacial search using haversine formula returns null on same point

I'm trying to implement a geospacial search in a php application. Currently, I'm using the following query to find points within 10km of a given latitude and longitude: SELECT * FROM ( SELECT *,...
Tudor Ravoiu's user avatar
  • 2,130
4 votes
1 answer
2k views

GeoCoordinate.GetDistanceTo using wrong radius for Earth?

I decompiled System.Device.Location.GeoCoordinate.GetDistanceTo(...), and found it uses the Haversine formula, which should specify the Earth's radius as one of the steps of calculation. The radius ...
David S.'s user avatar
  • 6,016
4 votes
2 answers
370 views

Finding two farthest objects between array of coordinates in PHP

I want to find two farthest objects (from each other) in my $user_devices array. Every object of $user_devices has attributes: id, name, imei and coordinates. Ex.: $user_devices = array( 'id' =&...
senti's user avatar
  • 73
4 votes
0 answers
11k views

How to calculate distance between two latitude and longitude [duplicate]

I would like to calculate distance between 2 points of latitude and longitude i have read about Haversine Formula and Spherical Law of Cosines for calculate distance between two points but my main ...
androidXP's user avatar
  • 1,722
3 votes
2 answers
4k views

Haversine and Laravel

I'm attempting to compare a users location (Sent via params in the URL) to offers.offer_lat and offers.offer_long (in my DB) by a distance argument (set in miles) I'm going crazy right now, a lot of ...
ExohJosh's user avatar
  • 1,852
3 votes
2 answers
4k views

Haversine formula using SQL server to find closest venue - vb.net

I am grabbing a postcode from a form. I can then convert this postcode to lng,lat coordinates as I have these stored in a table. SELECT lng, lat from postcodeLngLat WHERE postcode = 'CV1' I have ...
Tom's user avatar
  • 12.9k
3 votes
2 answers
3k views

How does atan appear in haversine formula?

I am kinda new on this. I am about to convert math equation to code lately. So I started practicing on simple formulas such as Fibonacci numbers and haversine formula. But I am kinda confused with ...
choz's user avatar
  • 17.6k
3 votes
1 answer
483 views

Geographic coordinate distance calculation with haversine formula gives the wrong output

I'm creating a distance calculator in c# using the haversine equation to calculate the distance between longitudes and latitudes but it is giving the wrong output can anyone see why? the first long ...
kian5749's user avatar
3 votes
2 answers
2k views

Trying to get distance using longitude and latitude, but keep running to an error: 'Series' object has no attribute 'radians'

I have a csv file that contains longitude, latitude and other data. Use pd.read_csv read data then put longitude and latitude into a dataframe. Defined a new DF: longLat = gps[["Longitude", "Latitude"]...
Katrina's user avatar
  • 274
3 votes
3 answers
472 views

Scalability Location Distance Search Over 100,000 LatLng Positions Across USA

Scenario = 1) Delivery Offices spread out across the USA each specifying their own maximum delivery radius limit in miles. 2) A target address geo converted to LatLng is the delivery destination. ...
Martin Sansone - MiOEE's user avatar
3 votes
2 answers
1k views

Haversine's formula in sqlalchemy

I'm having the following lines in my code query = "SELECT id, " \ "( 3959 * acos( cos( radians(37) ) * cos( radians( %(lat)i ) ) * " \ "cos( radians( %(lng)i ) - radians(-122) ) + ...
Nigel's user avatar
  • 1,705

1
2 3 4 5
9