[postgis-users] Follow-up: exterior rings in multipolygons

Mr. Puneet Kishor punk.kish at gmail.com
Mon Jul 25 16:23:45 PDT 2011


On Jul 25, 2011, at 4:16 PM, Mr. Puneet Kishor wrote:

> 
> On Jul 25, 2011, at 3:44 PM, Sandro Santilli wrote:
> 
>> On Mon, Jul 25, 2011 at 03:40:34PM -0500, Mr. Puneet Kishor wrote:
>>> Please help me understand the following
>>> 
>>> ====
>>> SELECT 
>>>   ST_Nrings(the_geom) nr, 
>>>   ST_NumInteriorRings(the_geom) nir, 
>>>   ST_AsText(the_geom) WKT 
>>> FROM table 
>>> WHERE objectid = 280;
>>> 
>>> nr  nir  WKT
>>> --  ---  ----------------------------------------------------------------
>>> 4    3  "MULTIPOLYGON(((..),(..),(..),(..)))"
>>> 
>>> ====
>>> 
>>> Looking at the above, I am assuming the first (..) is the exterior ring, and the remaning three (..) are the 3 interior rings. However,
>>> 
>>> ====
>>> SELECT ST_ExteriorRing(the_geom) er  
>>> FROM table 
>>> WHERE objectid = 280;
>>> 
>>> ERROR: ExteriorRing: geom is not a polygon
>>> ====
>>> 
>>> 
>>> So, what is going on? Of course, the docs say that ST_ExteriorRing doesn't work on a MULTIPOLYGON. So, is the above only an implementation anomaly, and I can treat the first ring as an exterior ring? Or, is it really something else?
>> 
>> All your rings are wrapper in another pair of parens, which identify
>> a component of the MULTIPOLYGON, that is a POLYGON. You must extract
>> the Polygon to use ST_ExteriorRing against it. So your query would be:
>> 
>> SELECT ST_ExteriorRing(ST_GeometryN(the_geom,1)) er
>> FROM table
>> WHERE objectid = 280;
>> 
> 
> 
> Thanks. The above works. So, does this mean that given "MULTIPOLYGON(((1),(2),(3),(4)))", 1 indeed is an exterior ring, and 2-4 are interior rings, and the "entity" does have 4 rings kinda like below?
> 
> +-------------------+
> |                   |
> |  +-----+   +---+  |
> |  |     |   | 3 |  |
> |  |  2  |   +---+  |
> |  +-----+          |
> |       +--------+  |
> |       |        |  |
> |   1   |   4    |  |
> |       |        |  |
> |       +--------+  |
> +-------------------+
> 
> 

Pursuing my quest to understand how multipolygons are handled

SELECT 
    ST_Nrings(the_geom) nr, 
    ST_NumInteriorRings(the_geom) nir, 
    ST_AsText(the_geom) wkt_feature,
    ST_AsText(ST_ExteriorRing(ST_GeometryN(the_geom, 1))) wkt_ext_ring
FROM table 
WHERE id = 280;

nr  nir  wkt_feature                          wkt_ext_ring
--  ---  -----------------------------------  --------------
 4    3  MULTIPOLYGON(((..),(..),(..),(..)))  LINESTRING(..)
 

So, the exterior ring is not a polygon. It is actually a LINESTRING. How can that be? And, how do get the geometries of the rest of the rings? The following doesn't work (for, say, ring 2)

	ST_AsText(ST_InteriorRingN(the_geom, 2))

I get "ERROR:  InteriorRingN: geom is not a polygon". And, the following 

	ST_AsText(ST_InteriorRingN(ST_GeometryN(the_geom, ?)))

I get "ERROR:  function st_interiorringn(geometry) does not exist"

For what its worth, this table has only what I would consider polygons, no lines. I loaded it via shp2pgsql without forcing it to load POLYGONS. I believe, by default, it creates MULTIPOLYGONS.




More information about the postgis-users mailing list