user2898914
user2898914

Reputation: 115

How to make a polygon with a hole

I'm using postgresSQL with postgis and I'm trying to create a polygon with a hole. I read a lot of users asking for something similar, but I can't do it for myself. My code is

insert into distritos(nombre,geom) values ('Distrito6',ST_MakePolygon(ST_GeomFromText('LINESTRING((-15.66486 27.91996, -15.60610 27.91820, -15.60359 27.97169, -15.66586 27.97144,-15.66486 27.91996),(-15.65753 27.95894, -15.61610 27.95995, -15.61459 27.93157, -15.65477 27.27.93007,-15.65753 27.95894))',4258)));

but it doesn't work. What do I have to do?

Upvotes: 1

Views: 2610

Answers (1)

John Powell
John Powell

Reputation: 12571

A Polygon has a double parenthesis at the start and end, and inner rings are delineated by using single pairs of parenthesis, between commas, ie, ),( whereas a Linestring only has one set of parenthesis at the beginning and end. You are actually using the syntax for a MultiLinestring, which is probably where your issue is. You can fix you query like this:

insert into distritos(nombre, geom) values
      ('Distrito6', ST_GeomFromText('POLYGON((-15.66486 27.91996,
           -15.60610 27.91820, -15.60359 27.97169, -15.66586 27.97144,-15.66486 27.91996),
      (-15.65753 27.95894, -15.61610 27.95995, -15.61459 27.93157,
         -15.65477 27.93007,-15.65753 27.95894))',4258));

Also, you do not need to use ST_MakePolygon, as if you are using ST_GeomFromText, you can create the polygon directly by using POLYGON instead of LINESTRING in your example above. ST_MakePolygon is probably more useful when you have arrays of Linestrings representing inner rings.

The Wikipedia WKT article details this very clearly, with WKT and accompanying graphics.

NOTE: You also have an error in you linestring, you have a repeated 27. in 27.27.93007.

Upvotes: 1

Related Questions