问题
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.
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
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
- Create a new row with each new data point. That new row should have time, and your
POINT
. - 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 UPDATE
ing 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