Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Distance between 2 POINTs in Postgis in srid 4326 in metres

Tags:

This is probably a simple question, but I'm not very good at PostGIS and don't fully grok all of this.

Basically I have a table (nodes) with a POINT column (point). I have created an index on this column

create index nodes__points on nodes using gist (point) 

The column was created with

select addgeometrycolumn('nodes', 'points', 'POINT', 4326, 2) 

I am using srid 4326 because I'm adding data that's in the form (latitude, longitude). (i.e. the co-ordinate system where the position of Dublin, Ireland is lat=53.353 lon=-6.264 (which I've added with GeomFromText('POINT(-6.264 53.535)'))).

For each point, I want to find all points that are roughly within a 1km box centred on that point (so selcet a.id, count(*) from nodes as a, nodes as b where SOME_DISTANCE_FUNCTION_HERE(a.point, b.point, 1000) group by a.id;. It doesn't have to be exact, just a rough hueristic figure. a 1km bbox is fine, a 1km circle is fine. It doesn't have to be exactly 1km, just that order of magnitude.

The ST_Distance/ST_DWithin/etc. all use the units of the SRID, which for 4326/WGS64 is degrees (so 1 = 1 degree of lattitude/longitude). But I want to use metres.

I tried ST_distance_sphere and st_dwithin which can use metres, but if I do that, the explain shows that the index isn't being used.

How can I get roughly what I want, and use the geographical index?

UPDATE: This is on PostgreSQL 9.1 and PostGIS 2.0 svn build.

like image 280
Amandasaurus Avatar asked Dec 11 '11 14:12

Amandasaurus


People also ask

How do you find the distance between two points in PostGIS?

ST_Distance in PostGIS To construct a basic query in SQL to find the distance between two points, use the ST_Distance function. ST_Distance is flexible in that you can pass in geometry or geography type object (see here for more information on these object types).

What is Srid PostGIS?

Basically, PostGIS opens up the ability to store your data in a single coordinate system such as WGS84 (SRID 4326), and when you need something like Area, Distance, or Length, you use a function to create that column from your data in a projected coordinate system that will give you a local interpretation of your data ...


1 Answers

You could use ST_Transform to use meters, also remeber that not all functions are available with geography types but if you really need speed use ST_DWithin, is the fastest way. Here's an aproximation of conversions between degrees and meters:

| places | degrees    | distance | | ------ | ---------- | -------- | | 0      | 1.0        | 111 km   | | 1      | 0.1        | 11.1 km  | | 2      | 0.01       | 1.11 km  | | 3      | 0.001      | 111 m    | | 4      | 0.0001     | 11.1 m   | | 5      | 0.00001    | 1.11 m   | | 6      | 0.000001   | 0.111 m  | | 7      | 0.0000001  | 1.11 cm  | | 8      | 0.00000001 | 1.11 mm  | 
like image 95
Francisco Valdez Avatar answered Oct 08 '22 05:10

Francisco Valdez