[postgis-users] How to insert polygon

Paragon Corporation lr at pcorp.us
Tue Oct 12 17:22:05 PDT 2010


Puthick,

You have made an unfortunate, but we wonder why not more common mistake.

PostgreSQL has built in geometry types it has had since the beginning of its
existence.  These go by the names 

polygon, lseg, box, etc. and you have unfortunatly created you sitelocation
field using this native PostgreSQL type instead of a PostGIS geometry type.

This is NOT a PostGIS geometry type - which is simply of type GEOMETRY
regardless of what type of geometry it is.
PostGIS just has 3 data types (geometry, geography, in PostGIS 2.0 -- also
raster) and the subtypes are internally managed in PostGIS or with table
constraints.


To create a postgis geometry type field  -- please follow the help
instructions

http://www.postgis.org/documentation/manual-1.5/AddGeometryColumn.html

A short-hand would be to create as an unconstrainted geometry

CREATE TABLE siteloc(siteid serial primary key, sitelocation geometry);

Add data to your table.

--then force constraints on with 
http://www.postgis.org/documentation/manual-1.5/Populate_Geometry_Columns.ht
ml

SELECT populate_geometry_columns('public.siteloc'::regclass);



Leo and Regina
http://www.postgis.us

-----Original Message-----
From: postgis-users-bounces at postgis.refractions.net
[mailto:postgis-users-bounces at postgis.refractions.net] On Behalf Of Puthick
Hok
Sent: Tuesday, October 12, 2010 7:36 PM
To: postgis-users at postgis.refractions.net
Subject: [postgis-users] How to insert polygon

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
_______________________________________________
postgis-users mailing list
postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users





More information about the postgis-users mailing list