[postgis-users] Polygon from a set of points

Brent Wood pcreso at pcreso.com
Tue Jun 12 13:46:19 PDT 2007


--- Leonardo Mateo <leonardomateo at gmail.com> wrote:

> On 6/12/07, Kevin Neufeld <kneufeld at refractions.net> wrote:
> > Have you tried using convexhull(geometry)? Given a collection of points,
> > it'll return a polygon where every point is guaranteed to be inside the
> > polygon.
> > ie.
> > SELECT convexhull(collect(makepoint(x,y))) FROM mytable;
> That's the point, I don't have them on a table. I mean, (x,y) pairs
> will came on plain text. Must I insert them on a temp table? There's
> no chance to make a polygon from (x,y) pairs "on the fly"?
> Sorry if my question is too dumb, I don't have experience with all this
> stuff.


Get your XY data into a table first, as a set of points, then you an use
PostGIS functions/operators such as convexhull on the data.

a simple way (but slow for large datasets) is a bash script (use cygwin on
Windows if you don't have Linux)

Cheers,

  Brent Wood

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#! /bin/bash

DB=mydb

# create the table
# note: add in any other columns you need 

psql $DB -c "create table my_points
              ( id serial primary key);"


# note, 4326 is the SRID for standard Lat/Long degrees, if your XY coords
# are in a different projection/units, you should use the appropriate SRID.

psql $DB -c "select addgeometrycolumn 
              ('','my_points', 'the_geom', 4326, 'POINT', 2);"

# assume your XY points are in whitespace separated columns in a file called 
# myfile.txt, read them & insert a point feature for each coordinate pair

while read X Y ; do
  psql $DB -c "insert into my_points values
                ( default,
                  geomfromtext('POINT($X $Y), 4326') );"
done < myfile.txt



More information about the postgis-users mailing list