Sunday, 6 September 2026

MultiLineString: Representing Multiple Paths with Arrays of LineStrings

  

Imagine you’re trying to model a bus route, river, or road. A simple LineString works fine only if the path is continuous, meaning it’s one long, unbroken line of connected points.

 

Example

-----------------------------------------
Case 1: LineString (One Connected Line)
-----------------------------------------
Bus Route A:
      ●─────●─────●─────●

Data: [(0,0), (5,0), (10,0), (15,0)]


-----------------------------------------
Case 2: MultiLineString (Multiple Lines)
-----------------------------------------
Bus Route B:
      ●─────●     (Gap here)     ●─────●─────●

Data: [
         [(0,0), (5,0)],          -- first segment
         [(10,0), (15,0), (20,0)] -- second segment
      ]

   

Another example is each metro line can be a separate LineString. You can group all the lines into a MultiLineString for the entire map.

 

    


Image credits goes to https://commons.wikimedia.org/wiki/File:Metro_Map_2025_-_Bengaluru_City.pdf.

 

Find the below working application.

CREATE DATABASE IF NOT EXISTS demo_db;

CREATE TABLE demo_db.transit_routes (
    route_name String,
    route MultiLineString
) ENGINE = Memory();

   

Insert some data into transit_routes table.

 

INSERT INTO demo_db.transit_routes VALUES
(
    'Blue Line',
    [
        [(0, 0), (10, 0), (10, 10), (0, 10)],
        [(1, 1), (2, 2), (3, 3)]
    ]
);

   

Select or view the route.

 

SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes;

krishna :) SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes;

SELECT
    route_name,
    route,
    toTypeName(route) AS type
FROM demo_db.transit_routes

Query id: b3347d00-9761-4d16-8e21-3665bbe3ddb7

   ┌─route_name─┬─route───────────────────────────────────────────────┬─type────────────┐
1.  Blue Line   [[(0,0),(10,0),(10,10),(0,10)],[(1,1),(2,2),(3,3)]]  MultiLineString 
   └────────────┴─────────────────────────────────────────────────────┴─────────────────┘

1 row in set. Elapsed: 0.002 sec.

 


Previous                                                    Next                                                    Home

No comments:

Post a Comment