How to append points to LINESTRING SQL

五迷三道 提交于 2019-12-25 13:06:16

问题


I have a table with points which is a LINESTRING. I have a row in there which has some points in said column.

I have a second set of points in the form a of a string, I would like to append these points to the existing row. Is there any way to do this in MySQL without selecting the points as text, manually merging the strings then updating points in the row?


回答1:


MYSQL Spatial function does not include any solution for appending a LINESTRING but there is a workaround which i have tried for you.

  1. Get the value

    set @gval = (select ST_AsText(route) from spatial where id =5);

I named the table as 'spatial' and added a column as 'route' which is of datatype linestring

  1. Appended the string by using the replace function and entering your required lat lon (or point)

    set @gval = replace(@gval, ')', ',8.5684875 76.8520767)'); Update spatial set route =GEOMFROMTEXT(@gval) where id=5;

this works good for me.




回答2:


Say I am tracking my position on a cycle ride and sending the lat/lng to a server The server keeps a LINESTRING representing my route up to the current point I would like to add a point every few seconds as I travel, to reflect my route Obviously as the LINESTRING grows in size, it gets increasingly inefficient to convert it to text, add a point, then convert back to spatial data I was wondering if there was a function to efficiently append a point directly to the end of the LINESTRING. I hope that makes sense?

In this case, normally you would

  1. Create a new row with each new data point. That new row should have time, and your POINT.
  2. Then you would create a view (or materialized view) that aggregated those rows of points into a LINESTRING. To do that, see this question.

In MySQL and PostgreSQL you don't really want to do append to a LINESTRING for every point insertion. In MVCC databases, usually UPDATEing a row requires rewriting the entire row. For some cases it may not matter at all, but if you're writing 100+ point routes I wouldn't go that method.

ℹ In PostGIS this is slightly easier as you have spatial aggregates that aren't available to you in MySQL, like ST_MakeLine. So you can do SELECT ST_MakeLine(pt ORDER BY pointTime) FROM table GROUP BY bike_route;. You also have functions like ST_AddPoint which adds a POINT to a LINESTRING (in the event your routes are short or you don't care about write-heavy updates).



来源:https://stackoverflow.com/questions/45799208/how-to-append-points-to-linestring-sql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!