Geometry Functions

Each function that belongs to this group takes a geometry value as its argument and returns some quantitive or qualitive property of the geometry. Some functions restrict their argument type. Such functions return NULL if the argument is of an incorrect geometry type. For example, Area() returns NULL if the object type is neither Polygon nor MultiPolygon.

General Geometry Functions

The functions listed in this ssection do not restrict their argument and accept a geometry value of any type.

GeometryType(g) Returns as a string the name of the geometry type of which the geometry instance g is a member. The name will correspond to one of the instantiable Geometry subclasses.
    mysql> SELECT GeometryType(GeomFromText('POINT(1 1)'));
    +------------------------------------------+
    | GeometryType(GeomFromText('POINT(1 1)')) |
    +------------------------------------------+
    | POINT                                    |
    +------------------------------------------+
    
Dimension(g) Returns the inherent dimension of the geometry value g. The result can be -1, 0, 1, or 2. (The meaning of these values is given in GIS class geometry.)
    mysql> SELECT Dimension(GeomFromText('LineString(1 1,2 2)'));
    +------------------------------------------------+
    | Dimension(GeomFromText('LineString(1 1,2 2)')) |
    +------------------------------------------------+
    |                                              1 |
    +------------------------------------------------+
    
SRID(g) Returns an integer indicating the Spatial Reference System ID for the geometry value g.
    mysql> SELECT SRID(GeomFromText('LineString(1 1,2 2)',101));
    +-----------------------------------------------+
    | SRID(GeomFromText('LineString(1 1,2 2)',101)) |
    +-----------------------------------------------+
    |                                           101 |
    +-----------------------------------------------+
    
Envelope(g) Returns the Minimum Bounding Rectangle (MBR) for the geometry value g. The result is returned as a polygon value.
    mysql> SELECT AsText(Envelope(GeomFromText('LineString(1 1,2 2)')));
    +-------------------------------------------------------+
    | AsText(Envelope(GeomFromText('LineString(1 1,2 2)'))) |
    +-------------------------------------------------------+
    | POLYGON((1 1,2 1,2 2,1 2,1 1))                        |
    +-------------------------------------------------------+
    
The polygon is defined by the corner points of the bounding box:
    POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))
    

The OpenGIS specification also defines the following functions, which MySQL does not yet implement:

Boundary(g) Returns a geometry that is the closure of the combinatorial boundary of the geometry value g.
IsEmpty(g) Returns 1 if the geomtry value g is the empty geometry, 0 if it is not empty, and -1 if the argument is NULL. If the geometry is empty, it represents the empty point set.
IsSimple(g) Currently, this function is a placeholder and should not be used. When implemented, its behavior will be as described in the next paragraph. Returns 1 if the geometry value g has no anomalous geometric points, such as self intersection or self tangency. IsSimple() returns 0 if the argument is not simple, and -1 if it is NULL. The description of each instantiable geometric class given earlier in the chapter includes the specific conditions that cause an instance of that class to be classified as not simple.

Point Functions

A Point consists of its X and Y coordinates, which may be obtained using the following functions:

X(p) Returns the X-coordinate value for the point p as a double-precision number.
    mysql> SELECT X(GeomFromText('Point(56.7 53.34)'));
    +--------------------------------------+
    | X(GeomFromText('Point(56.7 53.34)')) |
    +--------------------------------------+
    |                                 56.7 |
    +--------------------------------------+
    
Y(p) Returns the Y-coordinate value for the point p as a double-precision number.
    mysql> SELECT Y(GeomFromText('Point(56.7 53.34)'));
    +--------------------------------------+
    | Y(GeomFromText('Point(56.7 53.34)')) |
    +--------------------------------------+
    |                                53.34 |
    +--------------------------------------+
    

LineString Functions

A LineString consists of Point values. You can extract particular points of a LineString, count the number of points that it contains, or obtain its length.

EndPoint(ls) Returns the Point that is the end point of the LineString value ls.
    mysql> SELECT AsText(EndPoint(GeomFromText('LineString(1 1,2 2,3 3)')));
    +------------------------------------------------------------+
    | AsText(EndPoint(GeomFromText('LineString(1 1,2 2,3 3)')))  |
    +------------------------------------------------------------+
    | POINT(3 3)                                                 |
    +------------------------------------------------------------+
    
GLength(ls) Returns as a double-precision number the length of the LineString value ls in its associated spatial reference.
    mysql> SELECT GLength(GeomFromText('LineString(1 1,2 2,3 3)'));
    +--------------------------------------------------+
    | GLength(GeomFromText('LineString(1 1,2 2,3 3)')) |
    +--------------------------------------------------+
    |                                  2.8284271247462 |
    +--------------------------------------------------+
    
IsClosed(ls) Returns 1 if the LineString value ls is closed (that is, it sStartPoint() and EndPoint() values are the same). Returns 0 if ls is not closed, and -1 if it is NULL.
    mysql> SELECT IsClosed(GeomFromText('LineString(1 1,2 2,3 3)'));
    +---------------------------------------------------+
    | IsClosed(GeomFromText('LineString(1 1,2 2,3 3)')) |
    +---------------------------------------------------+
    |                                                 0 |
    +---------------------------------------------------+
    
NumPoints(ls) Returns the number of points in the LineString value ls.
    mysql> SELECT NumPoints(GeomFromText('LineString(1 1,2 2,3 3)'));
    +----------------------------------------------------+
    | NumPoints(GeomFromText('LineString(1 1,2 2,3 3)')) |
    +----------------------------------------------------+
    |                                                  3 |
    +----------------------------------------------------+
    
PointN(ls,n) Returns the n-th point in the Linestring value ls. Point numbers begin at 1.
    mysql> SELECT AsText(PointN(GeomFromText('LineString(1 1,2 2,3 3)'),2));
    +-----------------------------------------------------------+
    | AsText(PointN(GeomFromText('LineString(1 1,2 2,3 3)'),2)) |
    +-----------------------------------------------------------+
    | POINT(2 2)                                                |
    +-----------------------------------------------------------+
    
StartPoint(ls) Returns the Point that is the start point of the LineString value ls.
    mysql> SELECT AsText(StartPoint(GeomFromText('LineString(1 1,2 2,3 3)')));
    +-------------------------------------------------------------+
    | AsText(StartPoint(GeomFromText('LineString(1 1,2 2,3 3)'))) |
    +-------------------------------------------------------------+
    | POINT(1 1)                                                  |
    +-------------------------------------------------------------+
    

The OpenGIS specification also defines the following function, which MySQL does not yet implement:

IsRing(ls) Returns 1 if the LineString value ls is closed (thatis, its StartPoint() and EndPoint() values are the same) and is simple (does not pass through the same point more than once). Returns 0 if ls is not a ring, and -1 if it is NULL.

MultiLineString Functions

GLength(mls) Returns as a double-precision number the length of the MultiLineString value mls. The length of mls is equal to the sum of the lengths of its elements.
    mysql> SELECT GLength(GeomFromText('MultiLineString((1 1,2 2,3 3),(4 4,5 5))'));
    +-------------------------------------------------------------------+
    | GLength(GeomFromText('MultiLineString((1 1,2 2,3 3),(4 4,5 5))')) |
    +-------------------------------------------------------------------+
    |                                                   4.2426406871193 |
    +-------------------------------------------------------------------+
    
IsClosed(mls) Returns 1 if the MultiLineString value mls is closed (that is, the StartPoint() and EndPoint() values are the same for each LineString in mls). Returns 0 if mls is not closed, and -1 if it is NULL.
    mysql> SELECT IsClosed(GeomFromText('MultiLineString((1 1,2 2,3 3),(4 4,5 5))'));
    +--------------------------------------------------------------------+
    | IsClosed(GeomFromText('MultiLineString((1 1,2 2,3 3),(4 4,5 5))')) |
    +--------------------------------------------------------------------+
    |                                                                  0 |
    +--------------------------------------------------------------------+
    

Polygon Functions

Area(poly) Returns as a double-precision number the area of the Polygon value poly, as measured in its spatial reference system.
    mysql> SELECT Area(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))'));
    +----------------------------------------------------------------------------+
    | Area(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))')) |
    +----------------------------------------------------------------------------+
    |                                                                          8 |
    +----------------------------------------------------------------------------+
    
NumInteriorRings(poly) Returns the number of interior rings in the Polygon value poly.
    mysql> SELECT NumInteriorRings(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))'));
    +----------------------------------------------------------------------------------------+
    | NumInteriorRings(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))')) |
    +----------------------------------------------------------------------------------------+
    |                                                                                      1 |
    +----------------------------------------------------------------------------------------+
    
ExteriorRing(poly) Returns the exterior ring of the Polygon value poly as a LineString.
    mysql> SELECT AsText(ExteriorRing(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))')));
    +--------------------------------------------------------------------------------------------+
    | AsText(ExteriorRing(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))'))) |
    +--------------------------------------------------------------------------------------------+
    | LINESTRING(0 0,0 3,3 3,3 0,0 0)                                                            |
    +--------------------------------------------------------------------------------------------+
    
InteriorRingN(poly,n) Returns the n-th interior ring for the Polygon value poly as a LineString. Ring numbers begin at 1.
    mysql> SELECT AsText(InteriorRingN(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))'),1));
    +-----------------------------------------------------------------------------------------------+
    | AsText(InteriorRingN(GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))'),1)) |
    +-----------------------------------------------------------------------------------------------+
    | LINESTRING(1 1,1 2,2 2,2 1,1 1)                                                               |
    +-----------------------------------------------------------------------------------------------+
    

The OpenGIS specification also defines the following functions, which MySQL does not yet implement:

Centroid(poly) Returns the mathematical centroid for the Polygon value poly as a Point. The result is not guaranteed to be on the polygon.
PointOnSurface(poly) Returns a Point value that is guaranteed to be on the Polygon value poly.

MultiPolygon Functions

Area(mpoly) Returns as a double-precision number the area of the MultiPolygon value mpoly, as measured in its spatial reference system.
    mysql> SELECT Area(GeomFromText('MultiPolygon(((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1)))'));
    +-----------------------------------------------------------------------------------+
    | Area(GeomFromText('MultiPolygon(((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1)))')) |
    +-----------------------------------------------------------------------------------+
    |                                                                                 8 |
    +-----------------------------------------------------------------------------------+
    

The OpenGIS specification also defines the following functions, which MySQL does not yet implement:

Centroid(mpoly) Returns the mathematical centroid for the MultiPolygon value mpoly as a Point. The result is not guaranteed to be on the MultiPolygon.
PointOnSurface(mpoly) Returns a Point value that is guaranteed to be on the MultiPolygon value mpoly.

GeometryCollection Functions

NumGeometries(gc) Returns the number of geometries in the GeometryCollection value gc.
    mysql> SELECT NumGeometries(GeomFromText('GeometryCollection(Point(1 1),LineString(2 2, 3 3))'));
    +------------------------------------------------------------------------------------+
    | NumGeometries(GeomFromText('GeometryCollection(Point(1 1),LineString(2 2, 3 3))')) |
    +------------------------------------------------------------------------------------+
    |                                                                                  2 |
    +------------------------------------------------------------------------------------+
    
GeometryN(gc,n) Returns the n-th geometry in the GeometryCollection value gc. Geometry numbers begin at 1.
    mysql> SELECT AsText(GeometryN(GeomFromText('GeometryCollection(Point(1 1),LineString(2 2, 3 3))'),1));
    +------------------------------------------------------------------------------------------+
    | AsText(GeometryN(GeomFromText('GeometryCollection(Point(1 1),LineString(2 2, 3 3))'),1)) |
    +------------------------------------------------------------------------------------------+
    | POINT(1 1)                                                                               |
    +------------------------------------------------------------------------------------------+