[postgis-users] order of points in ST_ConvexHull

Bruce Rindahl rindahl at lrcwe.com
Thu Feb 26 13:07:39 PST 2009

I use this method for arbitrary polygons to find the point closest to 
the corners of the bounding rectangle.  This is used to find the closest 
distance to the sides of an (almost) square polygon called a section.

First I set 4 columns for the index of the point closest to each corner:

/ALTER TABLE sections ADD COLUMN ne integer;
ALTER TABLE sections ADD COLUMN se integer;
ALTER TABLE sections ADD COLUMN sw integer;
ALTER TABLE sections ADD COLUMN nw integer;

/Then I make sure each polygon  is encoded in the same direction:

/UPDATE SECTIONS set the_geom = forceRHR(the_geom);
Then I create a function that takes a linestring and a point and finds 
the closest vertex to the point and returns the index number:


-- Function: line_locate_vertex(geometry, geometry)

-- DROP FUNCTION line_locate_vertex(geometry, geometry);

CREATE OR REPLACE FUNCTION line_locate_vertex(geometry, geometry)
  RETURNS integer AS
    dist double precision;
    n_dist double precision;
    i integer;
    p integer;
    dist = 9999999999999;
   IF (GeometryType($1) != 'LINESTRING') THEN
    raise notice 'First Geometry type must be a LINESTRING';
    RETURN -1;
   END IF;
   IF (GeometryType($2) != 'POINT') THEN
    raise notice 'Second Geometry type must be a POINT';
    RETURN -1;
   END IF;

    FOR i IN 1 .. numPoints($1)
        n_dist = distance($2,pointN($1,i));
        IF (n_dist < dist) THEN
            p = i;
            dist = n_dist;
        END IF;

    RETURN p;
Finally I find the index for each corner to from the exterior ring of 
the polyon to the bounding box corners

/update sections set ne = 
update sections set se = 
update sections set sw = 
update sections set nw = 

This should give you your indexes.  The point is easy to get from there.


Armin Burger wrote:
> Hello
> I need to find a possibility to identify upper-left, upper-right, etc. 
> corners of +/- rectangular polygons. I.e. polygons with guranteed just 
> 4 corners, but with a shape that is typically between a rectangle and 
> a rhomb. The polygons define the geometry of image boundaries ("image 
> footprints"). But it cannot be guaranteed which point in the polygon 
> corresponds to which corner since the order of points during geometry 
> creation is unknown.
> One idea was to use ST_ConvexHull(geometry) since for this very simple 
> polygons the convex hull seems to be identical with the geometry. It 
> looked to me that the order in this convex hull was:
>   lower-right, lower-left, upper-left, upper-right
> Does anybody know if this order is always like that or can this order 
> change? Would anybody know another method to identify which point of 
> the polygon corresponds to which corner?
> Regards
> Armin
> _______________________________________________
> postgis-users mailing list
> postgis-users at postgis.refractions.net
> http://postgis.refractions.net/mailman/listinfo/postgis-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20090226/e0e7a712/attachment.html>

More information about the postgis-users mailing list