[postgis-users] How to insert polygon

Puthick Hok hputhick at gmail.com
Tue Oct 12 16:35:50 PDT 2010


Hi,

I started using Postgis a couple months ago. Now I have a problem 
inserting a polygon. The table definition is like this.

Table "public.siteloc"
    Column    |  Type   | Modifiers
--------------+---------+-----------
 siteid       | integer | not null
 sitelocation | polygon | not null
Indexes:
    "siteloc_pkey" PRIMARY KEY, btree (siteid)
    "sitelocation_sp_index" gist (sitelocation)

When I ran the following SQL statement:

INSERT INTO siteloc (siteid, sitelocation) VALUES (18, 
ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', -1));

I got this error:

ERROR:  column "sitelocation" is of type polygon but expression is of 
type geometry
LINE 1: INSERT INTO siteloc (siteid, sitelocation) VALUES (18, ST_Ge...
                                     ^
HINT:  You will need to rewrite or cast the expression.

When I tried to cast it with this statement:

INSERT INTO siteloc (siteid, sitelocation) VALUES (18, CAST( 
ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', -1) AS polygon) );

or

INSERT INTO siteloc (siteid, sitelocation) VALUES (18, 
ST_GeomFromText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', -1)::polygon);

I got this error:

ERROR:  cannot cast type geometry to polygon
LINE 1: ...romText('POLYGON((0 0, 4 0, 4 4, 0 4, 0 0))', -1)::polygon);

I tried google, it did not help. Is there a problem defining my field 
'sitelocation' as 'polygon' rather 'geometry'.

Please help me.
Puthick



More information about the postgis-users mailing list