Google calculated Distance differs PostGIS

808 views
Skip to first unread message

Luca Borzani

unread,
Dec 22, 2011, 5:55:42 AM12/22/11
to Google Maps JavaScript API v3
The problem is:

distance calculated in postgis differs (not slightly) from distance
returned by google maps. This brings to inconsistency in what the user
sees on the map.

Example:

We have two points with SRID 4326
A) POINT(45.53983820000001 10.22295620)
B) POINT(45.53945870 10.22567910)
* you can place the coords in google and see what the result looks
like

Google will give a distance of 220meters, PostGIS of about 304 meters
(or about 305 meters using a spheroid)

Now, I understand routing distance (i.e. google distance) it's not the
same as spatial distance (i.e. calculated with st_distance in postgis)
but still I can't explain why the result differs so much.
How can I make PostGis calculate the same (more or less) distance as
google?

Now I explain why this is an issue.

phase 1)
I let the user indicate pointA on the map adding a marker. Then I draw
a circle, with center pointA, and radius of 250 meters. PointB (assume
it's already loaded on the map, because of a previous query) will be
included (visually) in the circle area, because according to google
pointB is 220 meters away from pointA. So the user confirms the action
and saves to the db.

phase 2)
I want to let the user mark (later, when he logs in again perhaps) a
point on the map and draw a circle as before, this time pointB is not
already placed on the map (so the user does not have a visual clue of
where pointB actually is). I'm going to query PostGis to know if there
are any points that intersects the circle drawn by the user.

assume we have a very simple table with just one row that holds pointB
and a query like this SELECT * FROM mytable where ST_DWithin(:pointA,
pointB, 250.0)

now the problem is that this query won't return any result because
according to postgis pointB is 304 meters away from pointA.
But wait, when the user created pointA he SAW ON THE MAP that PointB
IS in the area of the circle with center PointA and radius 250 meters.

So, I can I fix this? I could use a tolerance in my queries, but what
value should i assign to the tolerance(based on which logic?). 100
meters? Could work sometimes, maybe other times not, it could be too
much or too little.

I'll appreciate any help,thanks

Rossko

unread,
Dec 22, 2011, 10:06:47 AM12/22/11
to Google Maps JavaScript API v3
Is there any reason to think your PostGIS is in the same projection/
coordinate system as Google's map? You may need to be doing some
conversion somewhere.

John Coryat

unread,
Dec 22, 2011, 11:42:22 AM12/22/11
to google-map...@googlegroups.com
What is the exact query you're using in PostGIS to get your distance? 

4326 should be fine. That's what Google uses too.

-John Coryat

Marcelo

unread,
Dec 22, 2011, 12:42:26 PM12/22/11
to Google Maps JavaScript API v3
This is not a PostGIS support group, but you're inverting latitude and
longitude.

PostGIS uses POINT(lon lat)

select ST_Distance_Sphere(
ST_GeomFromText('POINT(10.22295620 45.53983820000001)'),
ST_GeomFromText('POINT(10.22567910 45.53945870)')
)

Result: 216.223944861114

select ST_Distance_Sphere(
ST_GeomFromText('POINT(45.53983820000001 10.22295620)'),
ST_GeomFromText('POINT(45.53945870 10.22567910)')
)

Result: 305.606787846591

--
Marcelo - http://maps.forum.nu
--

John Coryat

unread,
Dec 22, 2011, 3:58:58 PM12/22/11
to google-map...@googlegroups.com
Marcelo,

Good catch...

-John

Luca Borzani

unread,
Dec 23, 2011, 3:18:58 AM12/23/11
to Google Maps JavaScript API v3
Thank You everyone for the help, especially to Marcelo for noticing
the mistake (shame on me!).

On 22 Dic, 21:58, John Coryat <cor...@gmail.com> wrote:
> Marcelo,
>
> Good catch...
>
> -John
Reply all
Reply to author
Forward
0 new messages