Working with Lines in MariaDB: ST_LineFromText and Beyond


ST_LineFromText Function

MariaDB's ST_LineFromText function (also known as ST_LineStringFromText) is used within SQL statements to create a LINESTRING geometry object from a Well-Known Text (WKT) representation and a Spatial Reference System Identifier (SRID).

Breakdown

  • SRID (Spatial Reference System Identifier)
    This is a unique identifier that specifies the coordinate system used by the spatial data. It's crucial for ensuring accurate measurements and comparisons between geometries.
  • WKT (Well-Known Text)
    This is a standard text format for representing spatial geometry objects. It defines the geometry using human-readable text.
  • LINESTRING
    This represents a linear geometry in spatial databases, essentially a sequence of ordered vertices (points) that define a line.

SQL Statement Structure

SELECT ST_LineFromText(WKT_Text, SRID) AS line_geom
FROM your_table;
  • line_geom: This is an alias (optional) you can assign to the resulting LINESTRING geometry object for easier reference in your query.
  • SRID: This is the numeric identifier for the spatial reference system used by the WKT coordinates.
  • WKT_Text: This represents the actual WKT string that defines the line geometry. The format typically involves coordinates enclosed in parentheses and separated by commas.

Example

SELECT ST_LineFromText('LINESTRING(0 0, 10 20)', 4326) AS my_line;

This example creates a LINESTRING object from two points: (0, 0) and (10, 20). The SRID of 4326 indicates that the coordinates are in WGS 84, a common geographic coordinate system. The resulting line object is aliased as my_line.

  • You can create lines with more than two points by adding additional coordinate pairs within the WKT string.
  • ST_LineFromText is case-insensitive.


Creating a line from a stored WKT string

CREATE TABLE locations (
  id INT PRIMARY KEY,
  name VARCHAR(255),
  geom GEOMETRY
);

INSERT INTO locations (id, name, geom) VALUES
  (1, 'Shop', ST_LineFromText('LINESTRING(10 15, 20 25)', 4326));

SELECT id, name, ST_AsText(geom) AS wkt_text
FROM locations;

This code creates a table named locations to store information about places with their geometries. It inserts a record for a "Shop" location, defining its geometry using ST_LineFromText with a stored WKT string and SRID 4326. The final query retrieves information about the location and converts the stored geometry back to WKT format using ST_AsText.

Combining lines from multiple WKT strings

SELECT ST_LineFromText(CONCAT('LINESTRING(', point1, ',', point2, ')'), 3857) AS combined_line
FROM your_data_table
WHERE type = 'path_segment';

This example assumes you have a table with data points (point1 and point2) representing path segments. It concatenates these points within a new WKT string using CONCAT and defines the SRID as 3857 (likely a projected coordinate system). ST_LineFromText then creates a combined LINESTRING for each path segment.

Selecting lines within a specific distance

SELECT ST_AsText(geom) AS wkt_text
FROM your_table
WHERE ST_DWithin(geom, reference_point, distance)
  AND geom.geom_type = 'LINESTRING';

This code retrieves WKT representations of lines from a table (your_table) that are within a certain distance (distance) of a reference point (reference_point). The ST_DWithin function checks the spatial relationship between the geometry and the point. Additionally, it filters for geometries of type LINESTRING using the geom_type property.



  1. Using ST_GeomFromText

This function allows you to create a generic geometry object from a WKT string. While it doesn't directly return a LINESTRING, you can achieve the same result by checking the geometry type after creation:

SELECT ST_AsText(geom) AS wkt_text
FROM (
  SELECT ST_GeomFromText(WKT_Text, SRID) AS geom
  FROM your_table
) AS subquery
WHERE geom.geom_type = 'LINESTRING';

This approach involves creating a temporary geometry object using ST_GeomFromText and then filtering for geometries of type LINESTRING using the geom_type property.

  1. Constructing the Line Manually

If you have the individual coordinate points available, you can construct the WKT string yourself and then use ST_LineFromText:

SELECT ST_LineFromText(CONCAT('LINESTRING(', point1_x, ' ', point1_y, ', ', point2_x, ' ', point2_y, ')'), SRID) AS my_line;

This example assumes you have separate columns for X and Y coordinates (point1_x, point1_y, point2_x, point2_y). It constructs the WKT string by concatenating these values and then uses ST_LineFromText to create the LINESTRING.

Choosing the Right Approach

The best approach depends on your specific scenario:

  • If you only have individual coordinate points, constructing the WKT string manually offers more control but might require additional processing.
  • If you need to perform additional filtering or manipulation on the geometry type, using ST_GeomFromText with a subsequent check might be suitable.
  • If you already have complete WKT strings, ST_LineFromText remains the most efficient choice.