How can I get the number of polygons that form a geometry in PostgreSQL?
For example U.S map has a lot of islands in addition to its main lands. I want to count these islands plus the main lands.
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It only takes a minute to sign up.Sign up to join this community
Use the geometry accessor ST_NumGeometries.
SELECT ID, ST_NumGeometries(shape) FROM myTable
If your geometries contain geometry types other than polygons, you will need to break them up and keep only the polygons before counting. But from your question this would not be required.
According to the documentation (chapter 7.3), there are several geometry accessors which might meet your criteria, depending on whether the geometry is a collection, a polygon, or a multi-polygon: