[postgis-users] min distance between two lines

muhammad imran imranserver at yahoo.com
Sun Nov 20 13:47:28 PST 2011


Hi Gaurav,

In principle, st_distance(geom, geom) always returns minimum distance between two geoms and it should work for any geom type (line, polygon, point). Function st_distance(st_centroid(geom), st_centroid(geom)) may return the distance between centroids of geoms. Calculating minimum distance of a geom to more than one geoms is however tricky a bit. It has been quite a long time, I developed a simple function (below) to find two nearest markets (among several markets) to a village location based on minimum (Euclidian) distance. You may have a look into it to get some idea. To find the centriod on minimum distance between two lines, you need to return a line geomtery, whereas st_distance_spheroid(geometry, geometry, spheroid) returns distance as double precision. You need to use a function that returns a shortest line between two gemoetries and then you can find centroid on the shortest line e.g. st_centroid(st_shortestline(geometry, geometry))     

Regards,
Muhammad Imran
PhD Student
ITC, University of Twente

CREATE OR REPLACE FUNCTION "Burkina_Faso"."BF_NN_MARKET_CAL"() RETURNS boolean AS'
DECLARE
    i integer;
     result text;
 
BEGIN
FOR i in (select gid from "Burkina_Faso"."BF_VILLAGE_TAGED") loop
insert into "Burkina_Faso"."BF_NN_MARKET"
select b.gid, b.village,a.gid, a.market, min(st_distance(a.the_geom, b.the_geom))/1000
from "Burkina_Faso"."BF_MARKET" a, "Burkina_Faso"."BF_VILLAGE_TAGED" b
where b.gid=i
group by a.market, b.village, a.gid, b.gid, st_distance(a.the_geom, b.the_geom) 
having st_distance(a.the_geom, b.the_geom)=(select min(st_distance(a.the_geom, b.the_geom)))
order by st_distance(a.the_geom, b.the_geom) asc 
limit 2;
end loop;
    return true;
END;
' LANGUAGE plpgsql;

--- On Fri, 11/18/11, Gaurav Singh <singh09721 at itc.nl> wrote:

> From: Gaurav Singh <singh09721 at itc.nl>
> Subject: [postgis-users] min distance between two lines
> To: "postgis-users at postgis.refractions.net" <postgis-users at postgis.refractions.net>
> Date: Friday, November 18, 2011, 3:33 PM
> Hi All,
> 
> I have two lines and I want to calculate the minimum
> distance between them and finding the centroid between those
> two lines.
> Can you help me telling of ST_Distance_Spheroid is the
> right method to calculate the minimum distance between the
> two lines.
> 
> Thanks,
> Gaurav Singh
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
> 



More information about the postgis-users mailing list