#### Z and M coordinates

0 %
Z and M coordinates
Details
// Explore More Tutorials

# Z and M coordinates

01/08/2019

Beginner
10 min.
Geometries can optionally have Z (3rd dimension) and M (measure) values associated with each point in addition to X and Y coordinates

#### You will learn

You will learn the basics of multidimensional coordinate systems that introduce Z (3rd dimension) and M (measure) coordinates to geometries’ points.

Step 1: Introducing Z and M coordinates

All geometries have at least two coordinate dimensions: the X and Y defining 2D coordinates of each point. For geographic spatial reference systems, these are the latitude and longitude of a point.

Geometries can optionally have Z and / or M values associated with each point in the geometry. The Z value introduces the third dimension coordinate. The Z value usually is used to indicate the height, or elevation. The measure value M can be used to record additional non-coordinate values at various points within a geometry. The M value is most often used in geometries with the linear reference system, e.g. where M stores a distance value from a defined starting point.

The following dimension types are supported by SAP HANA:

• 2D dimension (X, Y)
• 2D dimension with measure (X, Y, M)
• 3D dimension (X, Y, Z)
• 3D dimension with measure (X, Y, Z, M)

Important to note that these Z and M coordinate values can be used to store additional information, but are not considered when computing spatial relations or set operations.

Step 2: Optional coordinates in geometries' constructors

In Well-known text (WKT) standard geometries with 3D and / or measure value are designated by “Z” and / or “M” after the geometry type. Below are examples for points.

A point with the third dimension Z of the value 2:

``````SELECT NEW ST_Point('POINT Z(0 0 2)').ST_asWKT() from dummy;
``````

A point with the measure value M equal 0.5:

``````SELECT NEW ST_Point('POINT M(0 0 0.5)').ST_asWKT() from dummy;
``````

A point with both the third dimension Z and measure M:

``````SELECT NEW ST_Point('POINT ZM(0 0 2 0.5)').ST_asWKT() from dummy;
``````

The Z and M values can be used with other geometries too.

``````SELECT NEW ST_GeometryCollection('GeometryCollection Z(LineString Z(5 10 20, 10 12 25, 15 10 13), Polygon Z((10 -5 4, 15 5 6, 5 5 7, 10 -5 4)), Point Z(10 15 12))').ST_asWKT() from dummy;
``````
Step 3: Storing and retrieving spatial data with Z and M

What is important to understand is that Z and M do not change the geometry’s dimensionality. The result of each statement below will still be `0` for `ST_Point` objects.

``````SELECT NEW ST_Point('POINT Z(0 0 2)').ST_Dimension() as Dimensions from dummy;
SELECT NEW ST_Point('POINT M(0 0 0.5)').ST_Dimension() as Dimensions from dummy;
SELECT NEW ST_Point('POINT ZM(0 0 2 0.5)').ST_Dimension() as Dimensions from dummy;
``````

To find the number of coordinates stored within each point of a geometry use the `ST_CoordDim` method. The following values may be returned:

• 2 - The geometry contains only two coordinates (X and Y)
• 3 - The geometry contains one additional coordinate (either Z or M) for each point
• 4 - The geometry contains two additional coordinates (both Z and M) for each point
``````SELECT NEW ST_LineString('LineString ZM(5 10 20 0, 10 12 25 0, 15 10 13 0)').ST_CoordDim() as CoordDims from dummy;
-- Result is 4
``````

Methods similar to retrieving X and Y are used to retrieve the value of either Z or M of geometries:
- `ST_Z()` and `ST_M()` - only for points,
- `ST_Zmin()`, `ST_Zmax()`, `ST_Mmin()`, `ST_Mmax()` - for all geometries.

``````SELECT NEW ST_LineString('LineString ZM(5 10 20 0, 10 12 25 0, 15 10 13 0)').ST_Zmin() as Z from dummy;
-- Result is 13

SELECT NEW ST_LineString('LineString ZM(5 10 20 0, 10 12 25 0, 15 10 13 0)').ST_Zmax() as Z from dummy;
-- Result is 25

SELECT NEW ST_Point('Point ZM(5 10 20 0)').ST_M() as M from dummy;
-- Result is 0
``````
Step 4: Processing spatial data with Z and M

As mentioned above these additional coordinate Z and M values are used to store and retrieve additional information, but are not considered during spatial computations or set operations.

For example the result of the line string calculation from one of the previous tutorials will still be `5`, and not `13` if Z value of the string’s end point is `12`. As a fun fact: (5, 12, 13) is another example of the Pythagorean triple, same as (3, 4, 5).

``````SELECT NEW ST_LineString('LineString Z(0 0 0, 4 3 12)').ST_Length() as StringLength FROM dummy;
-- Result is still 5, same as in the following
-- SELECT NEW ST_LineString('LineString (0 0, 4 3)').ST_Length() FROM dummy;
``````

Another modified example from a previous tutorial.

``````SELECT NEW ST_Point ('POINT Z(1 1 100)').ST_Within(NEW ST_Point('POINT Z(0 0 0.5)').ST_Buffer(2)) as WITHIN FROM dummy;
-- Result is True as point (1,1) is still within a buffer around the point (0,0) even though they have different "height" location defined by Z
``````

The method `ST_Buffer()` applied to a point with 3 dimensions flattens a calculated geometry, making it 2 dimensional: X and Y only. And the method `ST_Within` does not take Z dimension into consideration.

``````SELECT NEW ST_Point('POINT Z(0 0 0.5)').ST_Buffer(2).ST_CoordDim() from DUMMY;
-- Result is 2 (coordinates dimensions of an output polygon)
``````
Which of the following coordinates can be used in SAP HANA?
×