Source code editor What Is Ajax

↑

After populating spatial columns with values, you are ready to query and analyze them. MySQL provides a set of functions to perform various operations on spatial data. These functions can be grouped into four major categories according to the type of operation they perform:

Functions that convert geometries between various formats

Functions that provide access to qualitative or quantitative properties of a geometry

Functions that describe relations between two geometries

Functions that create new geometries from existing ones

Spatial analysis functions can be used in many contexts, such as:

Any interactive SQL program, such as

**mysql**or MySQL Query BrowserApplication programs written in any language that supports a MySQL client API

MySQL supports the following functions for converting geometry values between internal format and either WKT or WKB format:

Converts a value in internal geometry format to its WKB representation and returns the binary result.

SELECT AsBinary(g) FROM geom;

Converts a value in internal geometry format to its WKT representation and returns the string result.

mysql>

mysql>`SET @g = 'LineString(1 1,2 2,3 3)';`

+--------------------------+ | AsText(GeomFromText(@g)) | +--------------------------+ | LINESTRING(1 1,2 2,3 3) | +--------------------------+`SELECT AsText(GeomFromText(@g));`

Converts a string value from its WKT representation into internal geometry format and returns the result. A number of type-specific functions are also supported, such as

`PointFromText()`

and`LineFromText()`

. See Section 16.4.2.1, “Creating Geometry Values Using WKT Functions”.Converts a binary value from its WKB representation into internal geometry format and returns the result. A number of type-specific functions are also supported, such as

`PointFromWKB()`

and`LineFromWKB()`

. See Section 16.4.2.2, “Creating Geometry Values Using WKB Functions”.

Each function that belongs to this group takes a geometry value as its argument and returns some quantitative or qualitative 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`

.

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

Returns the inherent dimension of the geometry value

. The result can be –1, 0, 1, or 2. The meaning of these values is given in Section 16.2.2, “Class`g`

`Geometry`

”.mysql>

+------------------------------------------------+ | Dimension(GeomFromText('LineString(1 1,2 2)')) | +------------------------------------------------+ | 1 | +------------------------------------------------+`SELECT Dimension(GeomFromText('LineString(1 1,2 2)'));`

Returns the Minimum Bounding Rectangle (MBR) for the geometry value

. The result is returned as a`g`

`Polygon`

value.The polygon is defined by the corner points of the bounding box:

POLYGON((MINX MINY, MAXX MINY, MAXX MAXY, MINX MAXY, MINX MINY))

mysql>

+-------------------------------------------------------+ | AsText(Envelope(GeomFromText('LineString(1 1,2 2)'))) | +-------------------------------------------------------+ | POLYGON((1 1,2 1,2 2,1 2,1 1)) | +-------------------------------------------------------+`SELECT AsText(Envelope(GeomFromText('LineString(1 1,2 2)')));`

Returns as a string the name of the geometry type of which the geometry instance

is a member. The name corresponds to one of the instantiable`g`

`Geometry`

subclasses.mysql>

+------------------------------------------+ | GeometryType(GeomFromText('POINT(1 1)')) | +------------------------------------------+ | POINT | +------------------------------------------+`SELECT GeometryType(GeomFromText('POINT(1 1)'));`

Returns an integer indicating the Spatial Reference System ID for the geometry value

.`g`

In MySQL, the SRID value is just an integer associated with the geometry value. All calculations are done assuming Euclidean (planar) geometry.

mysql>

+-----------------------------------------------+ | SRID(GeomFromText('LineString(1 1,2 2)',101)) | +-----------------------------------------------+ | 101 | +-----------------------------------------------+`SELECT SRID(GeomFromText('LineString(1 1,2 2)',101));`

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

Returns a geometry that is the closure of the combinatorial boundary of the geometry value

.`g`

Returns 1 if the geometry value

is the empty geometry, 0 if it is not empty, and –1 if the argument is`g`

`NULL`

. If the geometry is empty, it represents the empty point set.Currently, this function is a placeholder and should not be used. If implemented, its behavior will be as described in the next paragraph.

Returns 1 if the geometry value

has no anomalous geometric points, such as self-intersection or self-tangency.`g`

`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. (See Section 16.2.1, “The Geometry Class Hierarchy”.)

A `Point`

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

Returns the X-coordinate value for the point

as a double-precision number.`p`

mysql>

mysql>`SET @pt = 'Point(56.7 53.34)';`

+----------------------+ | X(GeomFromText(@pt)) | +----------------------+ | 56.7 | +----------------------+`SELECT X(GeomFromText(@pt));`

Returns the Y-coordinate value for the point

as a double-precision number.`p`

mysql>

mysql>`SET @pt = 'Point(56.7 53.34)';`

+----------------------+ | Y(GeomFromText(@pt)) | +----------------------+ | 53.34 | +----------------------+`SELECT Y(GeomFromText(@pt));`

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.

Returns the

`Point`

that is the endpoint of the`LineString`

value.`ls`

mysql>

mysql>`SET @ls = 'LineString(1 1,2 2,3 3)';`

+-------------------------------------+ | AsText(EndPoint(GeomFromText(@ls))) | +-------------------------------------+ | POINT(3 3) | +-------------------------------------+`SELECT AsText(EndPoint(GeomFromText(@ls)));`

Returns as a double-precision number the length of the

`LineString`

valuein its associated spatial reference.`ls`

mysql>

mysql>`SET @ls = 'LineString(1 1,2 2,3 3)';`

+----------------------------+ | GLength(GeomFromText(@ls)) | +----------------------------+ | 2.8284271247462 | +----------------------------+`SELECT GLength(GeomFromText(@ls));`

`GLength()`

is a non-standard name. It corresponds to the OpenGIS`Length()`

function.Returns the number of

`Point`

objects in the`LineString`

value.`ls`

mysql>

mysql>`SET @ls = 'LineString(1 1,2 2,3 3)';`

+------------------------------+ | NumPoints(GeomFromText(@ls)) | +------------------------------+ | 3 | +------------------------------+`SELECT NumPoints(GeomFromText(@ls));`

Returns the

-th`N`

`Point`

in the`Linestring`

value. Points are numbered beginning with 1.`ls`

mysql>

mysql>`SET @ls = 'LineString(1 1,2 2,3 3)';`

+-------------------------------------+ | AsText(PointN(GeomFromText(@ls),2)) | +-------------------------------------+ | POINT(2 2) | +-------------------------------------+`SELECT AsText(PointN(GeomFromText(@ls),2));`

Returns the

`Point`

that is the start point of the`LineString`

value.`ls`

mysql>

mysql>`SET @ls = 'LineString(1 1,2 2,3 3)';`

+---------------------------------------+ | AsText(StartPoint(GeomFromText(@ls))) | +---------------------------------------+ | POINT(1 1) | +---------------------------------------+`SELECT AsText(StartPoint(GeomFromText(@ls)));`

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

Returns as a double-precision number the length of the

`MultiLineString`

value. The length of`mls`

is equal to the sum of the lengths of its elements.`mls`

mysql>

mysql>`SET @mls = 'MultiLineString((1 1,2 2,3 3),(4 4,5 5))';`

+-----------------------------+ | GLength(GeomFromText(@mls)) | +-----------------------------+ | 4.2426406871193 | +-----------------------------+`SELECT GLength(GeomFromText(@mls));`

`GLength()`

is a non-standard name. It corresponds to the OpenGIS`Length()`

function.Returns 1 if the

`MultiLineString`

valueis closed (that is, the`mls`

`StartPoint()`

and`EndPoint()`

values are the same for each`LineString`

in). Returns 0 if`mls`

is not closed, and –1 if it is`mls`

`NULL`

.mysql>

mysql>`SET @mls = 'MultiLineString((1 1,2 2,3 3),(4 4,5 5))';`

+------------------------------+ | IsClosed(GeomFromText(@mls)) | +------------------------------+ | 0 | +------------------------------+`SELECT IsClosed(GeomFromText(@mls));`

Returns as a double-precision number the area of the

`Polygon`

value, as measured in its spatial reference system.`poly`

mysql>

mysql>`SET @poly = 'Polygon((0 0,0 3,3 0,0 0),(1 1,1 2,2 1,1 1))';`

+---------------------------+ | Area(GeomFromText(@poly)) | +---------------------------+ | 4 | +---------------------------+`SELECT Area(GeomFromText(@poly));`

Returns the exterior ring of the

`Polygon`

valueas a`poly`

`LineString`

.mysql>

->`SET @poly =`

mysql>`'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';`

+-------------------------------------------+ | AsText(ExteriorRing(GeomFromText(@poly))) | +-------------------------------------------+ | LINESTRING(0 0,0 3,3 3,3 0,0 0) | +-------------------------------------------+`SELECT AsText(ExteriorRing(GeomFromText(@poly)));`

Returns the

-th interior ring for the`N`

`Polygon`

valueas a`poly`

`LineString`

. Rings are numbered beginning with 1.mysql>

->`SET @poly =`

mysql>`'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';`

+----------------------------------------------+ | AsText(InteriorRingN(GeomFromText(@poly),1)) | +----------------------------------------------+ | LINESTRING(1 1,1 2,2 2,2 1,1 1) | +----------------------------------------------+`SELECT AsText(InteriorRingN(GeomFromText(@poly),1));`

Returns the number of interior rings in the

`Polygon`

value.`poly`

mysql>

->`SET @poly =`

mysql>`'Polygon((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1))';`

+---------------------------------------+ | NumInteriorRings(GeomFromText(@poly)) | +---------------------------------------+ | 1 | +---------------------------------------+`SELECT NumInteriorRings(GeomFromText(@poly));`

Returns as a double-precision number the area of the

`MultiPolygon`

value, as measured in its spatial reference system.`mpoly`

mysql>

->`SET @mpoly =`

mysql>`'MultiPolygon(((0 0,0 3,3 3,3 0,0 0),(1 1,1 2,2 2,2 1,1 1)))';`

+----------------------------+ | Area(GeomFromText(@mpoly)) | +----------------------------+ | 8 | +----------------------------+`SELECT Area(GeomFromText(@mpoly));`

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

Returns the

-th geometry in the`N`

`GeometryCollection`

value. Geometries are numbered beginning with 1.`gc`

mysql>

mysql>`SET @gc = 'GeometryCollection(Point(1 1),LineString(2 2, 3 3))';`

+----------------------------------------+ | AsText(GeometryN(GeomFromText(@gc),1)) | +----------------------------------------+ | POINT(1 1) | +----------------------------------------+`SELECT AsText(GeometryN(GeomFromText(@gc),1));`

Returns the number of geometries in the

`GeometryCollection`

value.`gc`

mysql>

mysql>`SET @gc = 'GeometryCollection(Point(1 1),LineString(2 2, 3 3))';`

+----------------------------------+ | NumGeometries(GeomFromText(@gc)) | +----------------------------------+ | 2 | +----------------------------------+`SELECT NumGeometries(GeomFromText(@gc));`

Section 16.5.2, “`Geometry`

Functions”, discusses several functions that construct new geometries from existing ones. See that section for descriptions of these functions:

`Envelope(`

)`g`

`StartPoint(`

)`ls`

`EndPoint(`

)`ls`

`PointN(`

,`ls`

)`N`

`ExteriorRing(`

)`poly`

`InteriorRingN(`

,`poly`

)`N`

`GeometryN(`

,`gc`

)`N`

OpenGIS proposes a number of other functions that can produce geometries. They are designed to implement spatial operators.

These functions are not implemented in MySQL. They may appear in future releases.

Returns a geometry that represents all points whose distance from the geometry value

is less than or equal to a distance of`g`

.`d`

Returns a geometry that represents the convex hull of the geometry value

.`g`

Returns a geometry that represents the point set difference of the geometry value

with`g1`

.`g2`

Returns a geometry that represents the point set intersection of the geometry values

with`g1`

.`g2`

Returns a geometry that represents the point set symmetric difference of the geometry value

with`g1`

.`g2`

Returns a geometry that represents the point set union of the geometry values

and`g1`

.`g2`

The functions described in these sections take two geometries as input parameters and return a qualitative or quantitative relation between them.

MySQL provides several functions that test relations between minimal bounding rectangles of two geometries `g1`

and `g2`

. The return values 1 and 0 indicate true and false, respectively.

Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of

contains the Minimum Bounding Rectangle of`g1`

. This tests the opposite relationship as`g2`

`MBRWithin()`

.mysql>

mysql>`SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');`

mysql>`SET @g2 = GeomFromText('Point(1 1)');`

----------------------+----------------------+ | MBRContains(@g1,@g2) | MBRContains(@g2,@g1) | +----------------------+----------------------+ | 1 | 0 | +----------------------+----------------------+`SELECT MBRContains(@g1,@g2), MBRContains(@g2,@g1);`

Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries

and`g1`

are disjoint (do not intersect).`g2`

Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries

and`g1`

are the same.`g2`

Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries

and`g1`

intersect.`g2`

Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries

and`g1`

overlap. The term`g2`

*spatially overlaps*is used if two geometries intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.Returns 1 or 0 to indicate whether the Minimum Bounding Rectangles of the two geometries

and`g1`

touch. Two geometries`g2`

*spatially touch*if the interiors of the geometries do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.Returns 1 or 0 to indicate whether the Minimum Bounding Rectangle of

is within the Minimum Bounding Rectangle of`g1`

. This tests the opposite relationship as`g2`

`MBRWithin()`

.mysql>

mysql>`SET @g1 = GeomFromText('Polygon((0 0,0 3,3 3,3 0,0 0))');`

mysql>`SET @g2 = GeomFromText('Polygon((0 0,0 5,5 5,5 0,0 0))');`

+--------------------+--------------------+ | MBRWithin(@g1,@g2) | MBRWithin(@g2,@g1) | +--------------------+--------------------+ | 1 | 0 | +--------------------+--------------------+`SELECT MBRWithin(@g1,@g2), MBRWithin(@g2,@g1);`

The OpenGIS specification defines the following functions. They test the relationship between two geometry values `g1`

and `g2`

.

The return values 1 and 0 indicate true and false, respectively.

Currently, MySQL does not implement these functions according to the specification. Those that are implemented return the same result as the corresponding MBR-based functions. This includes functions in the following list other than `Distance()`

and `Related()`

.

These functions may be implemented in future releases with full support for spatial analysis, not just MBR-based support.

Returns 1 or 0 to indicate whether

completely contains`g1`

. This tests the opposite relationship as`g2`

`Within()`

.Returns 1 if

spatially crosses`g1`

. Returns`g2`

`NULL`

if`g1`

is a`Polygon`

or a`MultiPolygon`

, or ifis a`g2`

`Point`

or a`MultiPoint`

. Otherwise, returns 0.The term

*spatially crosses*denotes a spatial relation between two given geometries that has the following properties:The two geometries intersect

Their intersection results in a geometry that has a dimension that is one less than the maximum dimension of the two given geometries

Their intersection is not equal to either of the two given geometries

Returns 1 or 0 to indicate whether

is spatially disjoint from (does not intersect)`g1`

.`g2`

Returns as a double-precision number the shortest distance between any two points in the two geometries.

Returns 1 or 0 to indicate whether

is spatially equal to`g1`

.`g2`

Returns 1 or 0 to indicate whether

spatially intersects`g1`

.`g2`

Returns 1 or 0 to indicate whether

spatially overlaps`g1`

. The term`g2`

*spatially overlaps*is used if two geometries intersect and their intersection results in a geometry of the same dimension but not equal to either of the given geometries.Returns 1 or 0 to indicate whether the spatial relationship specified by

exists between`pattern_matrix`

and`g1`

. Returns –1 if the arguments are`g2`

`NULL`

. The pattern matrix is a string. Its specification will be noted here if this function is implemented.Returns 1 or 0 to indicate whether

spatially touches`g1`

. Two geometries`g2`

*spatially touch*if the interiors of the geometries do not intersect, but the boundary of one of the geometries intersects either the boundary or the interior of the other.Returns 1 or 0 to indicate whether

is spatially within`g1`

. This tests the opposite relationship as`g2`

`Contains()`

.