Top 5 Recent Articles
ARTICLES CATEGORIES
- Algorithms (22)
- All (399)
- Biography (1)
- Blog (44)
- Business Requirements (1)
- Commentary (1)
- Conversion (2)
- Customers (2)
- Data Models (1)
- Education (2)
- GeoRaptor (13)
- GPS (1)
- Image Processing (2)
- Import Export (8)
- Licensing (2)
- LiDAR (1)
- Linear Referencing (4)
- Manifold GIS (3)
- Mapping (1)
- MySQL Spatial (7)
- Networking and Routing (including Optimization) (5)
- Open Source (18)
- Oracle Spatial and Locator (194)
- Partitioning (1)
- PostGIS (36)
- Projections (1)
- Published Articles (1)
- qGIS (1)
- Recommendations (1)
- Services (1)
- Software Change Log (1)
- Source Code (37)
- Space Curves (9)
- Spatial Database Functions (109)
- Spatial DB comparison (1)
- Spatial XML Processing (11)
- SQL Server Spatial (92)
- Standards (3)
- Stored Procedure (17)
- Tessellation or Gridding (10)
- Tools (2)
- Topological Relationships (1)
- Training (2)
- Triangulation (2)
STNumRings: Counting number of polygon rings
Here is an “ease of use” function that allows you to count the number of rings of a polygon or multipolygon geometry object in SQL Server Spatial.
USE [GISDB] -- Change this to your database GO -- Function: STNumRings -- CREATE FUNCTION dbo.STNumRings(@p_geometry geometry) RETURNS INT AS BEGIN DECLARE @v_GeometryType VARCHAR(1000), @v_geom geometry, @v_geomn INT = 0, @v_ringn INT = 0; BEGIN IF ( @p_geometry IS NULL ) RETURN 0; SET @v_GeometryType = @p_geometry.STGeometryType(); IF ( @v_GeometryType IN ('Point','MultiPoint','LineString','MultiLineString' ) ) RETURN 0; IF ( @v_GeometryType IN ('Polygon','CurvePolygon') ) BEGIN SET @v_ringn = 1 + @p_geometry.STNumInteriorRing(); END; IF ( @v_GeometryType = 'MultiPolygon' ) BEGIN SET @v_geomn = 1; WHILE ( @v_geomn <= @p_geometry.STNumGeometries() ) BEGIN SET @v_geom = @p_geometry.STGeometryN(@v_geomn); SET @v_ringn = @v_ringn + 1 + @v_geom.STNumInteriorRing(); SET @v_geomn = @v_geomn + 1; END; END; IF ( @v_GeometryType = 'GeometryCollection' ) BEGIN SET @v_geomn = 1; WHILE ( @v_geomn <= @p_geometry.STNumGeometries() ) BEGIN SET @v_ringn = @v_ringn + dbo.STNumRings(@p_geometry.STGeometryN(@v_geomn)); SET @v_geomn = @v_geomn + 1; END; END; RETURN @v_ringn; END; END GO
Some tests.
SELECT 'Point' AS gtype, dbo.STNumRings(geometry::STGeomFromText('POINT(0 0)',0)) AS numRings UNION ALL SELECT 'MultiPoint' AS gtype, dbo.STNumRings(geometry::STGeomFromText('MULTIPOINT((0 0),(20 0))',0)) AS numRings UNION ALL SELECT 'LineString' AS gtype, dbo.STNumRings(geometry::STGeomFromText('LINESTRING(0 0,20 0,20 20,0 20,0 0)',0)) AS numRings UNION ALL SELECT 'MultiLineString' AS gtype, dbo.STNumRings(geometry::STGeomFromText('MULTILINESTRING((0 0,20 0,20 20,0 20,0 0),(10 10,10 11,11 11,11 10,10 10),(5 5,5 7,7 7,7 5,5 5))',0)) AS numRings UNION ALL SELECT 'Polygon Outer' AS gtype, dbo.STNumRings(geometry::STGeomFromText('POLYGON((0 0,20 0,20 20,0 20,0 0))',0)) AS numRings UNION ALL SELECT 'Polygon' AS gtype, dbo.STNumRings(geometry::STGeomFromText('POLYGON((0 0,20 0,20 20,0 20,0 0),(10 10,10 11,11 11,11 10,10 10),(5 5,5 7,7 7,7 5,5 5))',0)) AS numRings UNION ALL SELECT 'MultiPolygon' AS gtype, dbo.STNumRings(geometry::STGeomFromText('MULTIPOLYGON (((0 0,20 0,20 20,0 20,0 0),(10 10,10 11,11 11,11 10,10 10),(5 5,5 7,7 7,7 5,5 5)), ((80 80, 100 80, 100 100, 80 100, 80 80)), ((110 110, 150 110, 150 150, 110 150, 110 110)))',0)) AS numRings UNION ALL SELECT 'CurvePolygon' AS gtype, dbo.STNumRings(geometry::STGeomFromText('CURVEPOLYGON(COMPOUNDCURVE((0 -23.43778, 0 23.43778),CIRCULARSTRING(0 23.43778, -45 23.43778, -90 23.43778),(-90 23.43778, -90 -23.43778),CIRCULARSTRING(-90 -23.43778, -45 -23.43778, 0 -23.43778)))',0)) AS numRings UNION ALL SELECT 'GeometryCollection' AS gtype, dbo.STNumRings(geometry::STGeomFromText('GEOMETRYCOLLECTION(CURVEPOLYGON(COMPOUNDCURVE((0 -23.43778, 0 23.43778),CIRCULARSTRING(0 23.43778, -45 23.43778, -90 23.43778),(-90 23.43778, -90 -23.43778),CIRCULARSTRING(-90 -23.43778, -45 -23.43778, 0 -23.43778))), COMPOUNDCURVE(CIRCULARSTRING(-90 -23.43778, -45 -23.43778, 0 -23.43778), (0 -23.43778, 0 23.43778)))',0)) AS numRings;
Resulting in:
gtype | numRings |
---|---|
Point | 0 |
MultiPoint | 0 |
LineString | 0 |
MultiLineString | 0 |
Polygon Outer | 1 |
Polygon | 3 |
MultiPolygon | 5 |
CurvePolygon | 1 |
GeometryCollection | 1 |
I hope this is useful to someone.
Documentation
- MySQL Spatial General Functions
- Oracle LRS Objects
- Oracle Spatial Exporter (Java + pl/SQL)
- Oracle Spatial Object Functions
- Oracle Spatial Object Functions (Multi Page)
- PostGIS pl/pgSQL Functions
- SC4O Oracle Java Topology Suite (Java + pl/SQL)
- SQL Server Spatial General TSQL Functions
- SQL Server Spatial LRS TSQL Functions