MistyD
MistyD

Reputation: 17223

Huge Difference in result when calculating distance b/w two points using longitude-latitude

I am attempting to calculate the distance between two Positions using SRID(32148) Here are my two points

Point-1:
Lat:46.489767 Long:-119.043221
Point-2:
Lat:47.610902 Long:-122.336422

This website states that the distance in miles b/w these two points is 173.388 however from the code below the result I am getting is 0.002161632093865483

This is the code that I am using

    employeeloc = modelEmployee.objects.filter(user__first_name="adam")[0].location
    employerloc = modelEmployer.objects.filter(user__first_name="adam")[0].location
    meters = employerloc.distance(employeeloc)
    #Caluclate in miles
    dobj = Distance(m=meters)
    mi = dobj.mi

This is a little more detail with debugging results attached enter image description here Any suggestions on why my result is so different ?

Update:

I tried transforming the position using the following code using SRID 4326. However the results are still incorrect

enter image description here

Upvotes: 0

Views: 125

Answers (1)

mlinth
mlinth

Reputation: 3108

You appear to have used the lon / lat coordinates as SRID(32148) ones; you need to transform them.

This incorrect query gives your result 3.47m, because the coordinates don't match the SRID:

select 
st_distance(
st_setsrid(st_point(-122.336422,47.610902),32148),
st_setsrid(st_point(-119.043221,46.489767),32148))
-- 3.47880964046985

This query gives you the 173.71 mi result you expect:

select 
st_distance(
st_transform(st_setsrid(st_point(-122.336422,47.610902),4326),32148),
st_transform(st_setsrid(st_point(-119.043221,46.489767),4326),32148))

--279558.106935732m (=173.71mi)

And that is similar to the result of this query:

select 
st_distance(
st_setsrid(st_point(-122.336422,47.610902),4326)::geography,
st_setsrid(st_point(-119.043221,46.489767),4326)::geography)

--279522.55326056 m (= 173.69 mi)

Upvotes: 2

Related Questions