<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
  <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type">
</head>
<body bgcolor="#ffffff" text="#000000">
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.<br>
<br>
First I set 4 columns for the index of the point closest to each corner:<br>
<br>
<i>ALTER TABLE sections ADD COLUMN ne integer;<br>
ALTER TABLE sections ADD COLUMN se integer;<br>
ALTER TABLE sections ADD COLUMN sw integer;<br>
ALTER TABLE sections ADD COLUMN nw integer;<br>
<br>
</i>Then I make sure each polygon  is encoded in the same direction:<br>
<br>
<i>UPDATE SECTIONS set the_geom = forceRHR(the_geom);<br>
</i><br>
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:<br>
<br>
<i>---------------------------------------------------------------------------<br>
<br>
-- Function: line_locate_vertex(geometry, geometry)<br>
<br>
-- DROP FUNCTION line_locate_vertex(geometry, geometry);<br>
<br>
CREATE OR REPLACE FUNCTION line_locate_vertex(geometry, geometry)<br>
  RETURNS integer AS<br>
$BODY$<br>
  DECLARE     <br>
    dist double precision;<br>
    n_dist double precision;<br>
    i integer;<br>
    p integer;<br>
  BEGIN<br>
    dist = 9999999999999;<br>
     <br>
   IF (GeometryType($1) != 'LINESTRING') THEN<br>
    raise notice 'First Geometry type must be a LINESTRING';<br>
    RETURN -1;<br>
   END IF;<br>
   IF (GeometryType($2) != 'POINT') THEN<br>
    raise notice 'Second Geometry type must be a POINT';<br>
    RETURN -1;<br>
   END IF;<br>
<br>
    FOR i IN 1 .. numPoints($1)<br>
    LOOP<br>
        n_dist = distance($2,pointN($1,i));<br>
        IF (n_dist < dist) THEN<br>
            p = i;<br>
            dist = n_dist;<br>
        END IF;<br>
    END LOOP;<br>
<br>
    RETURN p;<br>
  <br>
  END;<br>
$BODY$<br>
  LANGUAGE 'plpgsql' VOLATILE;<br>
---------------------------------------------------------------------------<br>
</i><br>
Finally I find the index for each corner to from the exterior ring of
the polyon to the bounding box corners<br>
<br>
<i>update sections set ne =
line_locate_vertex(exteriorRing(geometryN(the_geom,1)),setSrid(MakePoint(xmax(the_geom),ymax(the_geom)),srid(the_geom)));<br>
update sections set se =
line_locate_vertex(exteriorRing(geometryN(the_geom,1)),setSrid(MakePoint(xmax(the_geom),ymin(the_geom)),srid(the_geom)));<br>
update sections set sw =
line_locate_vertex(exteriorRing(geometryN(the_geom,1)),setSrid(MakePoint(xmin(the_geom),ymin(the_geom)),srid(the_geom)));<br>
update sections set nw =
line_locate_vertex(exteriorRing(geometryN(the_geom,1)),setSrid(MakePoint(xmin(the_geom),ymax(the_geom)),srid(the_geom)));<br>
</i><br>
<br>
This should give you your indexes.  The point is easy to get from there.<br>
<br>
Bruce<br>
<br>
<br>
Armin Burger wrote:
<blockquote cite="mid:49A6F40E.1060402@gmx.net" type="cite">Hello
  <br>
  <br>
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.
  <br>
  <br>
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:
  <br>
  lower-right, lower-left, upper-left, upper-right
  <br>
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?
  <br>
  <br>
Regards
  <br>
  <br>
Armin
  <br>
_______________________________________________
  <br>
postgis-users mailing list
  <br>
<a class="moz-txt-link-abbreviated" href="mailto:postgis-users@postgis.refractions.net">postgis-users@postgis.refractions.net</a>
  <br>
<a class="moz-txt-link-freetext" href="http://postgis.refractions.net/mailman/listinfo/postgis-users">http://postgis.refractions.net/mailman/listinfo/postgis-users</a>
  <br>
  <br>
</blockquote>
</body>
</html>