Categories:

Geospatial Functions

ST_NPOINTS , ST_NUMPOINTS¶

Returns the number of points in a GEOGRAPHY or GEOGRAPHY object.

Syntax¶

ST_NPOINTS( <geography_or_geometry_expression> )
Copy

Arguments¶

geography_or_geometry_expression

The argument must be an expression of type GEOGRAPHY or GEOMETRY.

Returns¶

Returns a value of type INTEGER.

Usage Notes¶

  • Each Polygon loop lists the starting point twice (once as the start, once as the end). ST_NPOINTS counts both occurrences. For example, given a triangular Polygon, ST_NPOINTS returns 4, not 3.

  • ST_NUMPOINTS is an alias for ST_NPOINTS.

    Note

    In some other systems, ST_NUMPOINTS behaves differently from ST_NPOINTS and returns the number of points for LineString / MultiLineString objects only.

Examples¶

GEOGRAPHY Examples¶

This shows the number of points in a simple Polygon.

create table geospatial_table_01 (g1 GEOGRAPHY, g2 GEOGRAPHY);
insert into geospatial_table_01 (g1, g2) values 
    ('POLYGON((0 0, 3 0, 3 3, 0 3, 0 0))', 'POLYGON((1 1, 2 1, 2 2, 1 2, 1 1))');
Copy
SELECT ST_NPOINTS(g1) 
    FROM geospatial_table_01;
+----------------+
| ST_NPOINTS(G1) |
|----------------|
|              5 |
+----------------+
Copy

GEOMETRY Examples¶

The following example demonstrates how to use the ST_NPOINTS function.

CREATE OR REPLACE TABLE geometry_shapes (g GEOMETRY);
INSERT INTO geometry_shapes VALUES
    ('POINT(66 12)'),
    ('MULTIPOINT((45 21), (12 54))'),
    ('LINESTRING(40 60, 50 50, 60 40)'),
    ('MULTILINESTRING((1 1, 32 17), (33 12, 73 49, 87.1 6.1))'),
    ('POLYGON((17 17, 17 30, 30 30, 30 17, 17 17))'),
    ('MULTIPOLYGON(((-10 0,0 10,10 0,-10 0)),((-10 40,10 40,0 20,-10 40)))'),
    ('GEOMETRYCOLLECTION(POLYGON((-10 0,0 10,10 0,-10 0)),LINESTRING(40 60, 50 50, 60 40), POINT(99 11))')
    ;

SELECT ST_NPOINTS(g), ST_ASWKT(g) FROM geometry_shapes;
Copy
+---------------+-------------------------------------------------------------------------------------------------+
| ST_NPOINTS(G) | ST_ASWKT(G)                                                                                     |
|---------------+-------------------------------------------------------------------------------------------------|
|             1 | POINT(66 12)                                                                                    |
|             2 | MULTIPOINT((45 21),(12 54))                                                                     |
|             3 | LINESTRING(40 60,50 50,60 40)                                                                   |
|             5 | MULTILINESTRING((1 1,32 17),(33 12,73 49,87.1 6.1))                                             |
|             5 | POLYGON((17 17,17 30,30 30,30 17,17 17))                                                        |
|             8 | MULTIPOLYGON(((-10 0,0 10,10 0,-10 0)),((-10 40,10 40,0 20,-10 40)))                            |
|             8 | GEOMETRYCOLLECTION(POLYGON((-10 0,0 10,10 0,-10 0)),LINESTRING(40 60,50 50,60 40),POINT(99 11)) |
+---------------+-------------------------------------------------------------------------------------------------+
Copy