Vicky
Vicky

Reputation: 51

How to create a polygon in postgresql

I have picked some random points from google map and wanted to create as a polygon, I have created a table and trying to create the polygon.

CREATE TABLE public."place"
(
    id integer NOT NULL,
    polygon geometry(polygon,4326),
    count double precision,
    PRIMARY KEY (id)
);

INSERT INTO public."place" VALUES (1, ST_GeomFromText('POLYGON((9.693588094112373 52.39414543838985, 9.694328501691626 52.39397380868952, 9.694174756472517 52.39362874161061, 9.693445118144536 52.393639872848816))', 4326), 34);

The table is created but, the error is

ERROR: geometry contains non-closed rings HINT: "...18144536 52.393639872848816))" <-- parse error at position 166 within geometry

I am not sure how to rectify it. Any suggestions?

Upvotes: 0

Views: 2143

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31143

For a polygon to be valid all rings within must be closed, meaning the first and last point must be the same coordinates. If they don’t match this error is thrown since PostGIS assumes you made an error.

Add the first point as an additional point in the end and the ring will be considered closed and a polygon can be created:

INSERT INTO public."place" VALUES (1, ST_GeomFromText('POLYGON((9.693588094112373 52.39414543838985, 9.694328501691626 52.39397380868952, 9.694174756472517 52.39362874161061, 9.693445118144536 52.393639872848816, 9.693588094112373 52.39414543838985))', 4326), 34);

Upvotes: 4

Related Questions