Creating Linestring Geometries in MariaDB with ST_LineStringFromText


Function

  • ST_LineStringFromText(WKT)

Parameters

  • WKT: This is a mandatory parameter representing the textual definition of the linestring geometry in Well-Known Text format. WKT is a standard way to represent spatial data as text.

Functionality

  1. Input
    The function takes the WKT string as input. This string defines the linestring geometry with coordinates, typically in longitude-latitude (X, Y) or another spatial reference system.
  2. Parsing
    ST_LineStringFromText parses the WKT string and extracts the coordinate information.
  3. Creating Linestring
    The function creates a linestring geometry object based on the extracted coordinates. A linestring consists of an ordered sequence of points that define a linear path.

Use Case in SQL Statements

INSERT INTO my_table (name, geometry)
VALUES ('Path A', ST_LineStringFromText('LINESTRING(0 1, 2 3, 4 5)'));

In this example, the ST_LineStringFromText function creates a linestring object from the provided WKT string LINESTRING(0 1, 2 3, 4 5). This linestring is then inserted into the geometry column of the my_table table.

  • You can find similar functions for creating other geometric objects like points and polygons using ST_GeomFromText and other related functions.


Creating a table with a linestring geometry column

CREATE TABLE routes (
  id INT PRIMARY KEY AUTO_INCREMENT,
  name VARCHAR(255) NOT NULL,
  geom LINESTRING NOT NULL
);

This code creates a table named routes with three columns:

  • geom: A LINESTRING data type column to store linestring geometry objects.
  • name: A varchar column to store route names (up to 255 characters).
  • id: An auto-incrementing integer primary key.

Inserting a linestring into the table

INSERT INTO routes (name, geom)
VALUES ('My Running Path', ST_LineStringFromText('LINESTRING(-122.4 37.8, -122.5 37.7, -122.6 37.85)'));

This code inserts a new record into the routes table. It defines a route named "My Running Path" with a linestring geometry created using ST_LineStringFromText. The WKT string defines a path with three coordinates.

Selecting linestring data from the table

SELECT name, ST_AsText(geom) AS wkt_text
FROM routes;

This code retrieves data from the routes table. It selects the name column and uses the ST_AsText function to convert the linestring geometry in the geom column back to a WKT representation stored in a new column named wkt_text.

Updating a linestring in the table

UPDATE routes
SET geom = ST_LineStringFromText('LINESTRING(-122.3 37.9, -122.2 37.8, -122.1 37.7)')
WHERE id = 1;

This code updates the linestring geometry for the route with id = 1 in the routes table. It uses ST_LineStringFromText to create a new linestring object from the provided WKT string.



Using ST_GeomFromText with LINESTRING specification

  • You can achieve the same result as ST_LineStringFromText by specifying "LINESTRING" within the WKT string:
  • ST_GeomFromText is a more generic function that can handle various geometry types.
INSERT INTO routes (name, geom)
VALUES ('Another Path', ST_GeomFromText('LINESTRING(0 0, 10 10, 20 0)'));

Constructing Linestring from Points (For existing point data)

  • Utilize ST_MakeLine to create a linestring object by ordering the points:
  • If you already have point data in your database, you can leverage spatial functions to construct a linestring.
SELECT ST_MakeLine(point1, point2, point3) AS my_linestring
FROM point_table;

This assumes you have a table with point data and columns named point1, point2, and point3. The result will be a new linestring geometry in the my_linestring column.

Leveraging Client-side Libraries (For complex scenarios)

  • These libraries might offer additional functionalities beyond basic MariaDB functions.
  • Popular choices include PHP's MySQLi extension with spatial functions or PostGIS functions within geospatial client tools.
  • For intricate linestring creation involving calculations or manipulations, consider client-side libraries.
  • For complex linestring creation with calculations, explore client-side libraries for enhanced capabilities.
  • When dealing with existing point data, constructing the linestring from points using ST_MakeLine might be more efficient.
  • If you're directly working with WKT strings, ST_LineStringFromText or ST_GeomFromText with "LINESTRING" specification are suitable options.