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). 
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 (not geom). You need to use a function 
that returns a shortest line between two gemoetries and then you can find 
centroid on the shortest line, for that you can try 
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;

Muhammad Imran


--- On Fri, 11/18/11, Gaurav Singh <[email protected]> wrote:

> From: Gaurav Singh <[email protected]>
> Subject: [postgis-users] min distance between two lines
> To: "[email protected]" 
> <[email protected]>
> 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
> [email protected]
> http://postgis.refractions.net/mailman/listinfo/postgis-users
> 
_______________________________________________
postgis-users mailing list
[email protected]
http://postgis.refractions.net/mailman/listinfo/postgis-users

Reply via email to