Search code examples
mysqllinegisqgissegment

Convert points to line segments


In MySQL, I have two tables to store GPS trackpoint and second table to store chainage values. I wish to generate line segments by using both tables above. Please see the attached images for both tables:

Table:Trackpoint

r_id    chainage    latitude    longitude
5246    0   12.251324   105.984187
5246    5   12.251354   105.984224
5246    6   12.251357   105.984219
5246    8   12.25137    105.984231
5246    9   12.251374   105.984222
5246    9   12.251374   105.984217
5246    9   12.251374   105.984217
5246    10  12.251373   105.984217
5246    17  12.251307   105.984206
5246    38  12.251117   105.984186
5246    42  12.251082   105.984184
5246    60  12.250926   105.984155
5246    88  12.250668   105.98414
5246    125 12.250338   105.984167
5246    129 12.250311   105.984186
5246    133 12.250278   105.984209
5246    138 12.250246   105.984233
5246    189 12.249824   105.984432
5246    275 12.249232   105.984933
5246    389 12.248444   105.985601
5246    475 12.247789   105.986025
5246    493 12.247635   105.986089
5246    503 12.247557   105.986123
5246    622 12.246541   105.986484

Table: Chainage

R_ID    Start_Chainage  End_Chainage    Condition
5246    0               30              Good
5246    10              50              Bad
5246    50              100             Fair
5246    100             140             Good
5246    140             230             Bad
5246    230             500             Poor
5246    500             627             Good

**How do I generate a view, which able to draw a line in QGIS. similar this

road_line_condition.

I have tried this: , but the result is not as expected. It shows each section is not connected, and many lines of each section. the result from query below:

SELECT
  `t1`.`id`             AS `id`,
  `t1`.`chainage`       AS `chainage`,
  `t2`.`start_chainage` AS `start_chainage`,
  `t2`.`end_chainage`   AS `end_chainage`, 
  `t2`.`condition`    AS `Condition`,
   ST_GeomFromText(CONCAT("LineString(", GROUP_CONCAT(T1.longitude,' ',T1.latitude SEPARATOR ','), ")", ""),4326) AS line_wkt

FROM (`trackpoint` `t1`
   LEFT JOIN `Chainage` `t2`
     ON (`t1`.`id` = `t2`.`R_id`))
WHERE t1.`chainage`>=t2.`start_chainage` AND t1.`chainage`<=t2.`end_chainage`
GROUP BY t2.`end_chainage`

Apologies for my English.
Thanks


Solution

  • You did not write what's wrong with the results, but I suspect the order of the points is random. You need to order points inside group concat, something like:

    GROUP_CONCAT(
       CONCAT(T1.longitude,' ',T1.latitude)
       ORDER BY chainage 
       SEPARATOR ',')
    

    Note that there are some points with duplicate chainage = 9, this data does not allow one to determine what's the proper order for them.