I imported ~1000 paths from somewhere into my postgis database to a linestring field.
(EDIT) My table is like this
+---------+---------------+------------------+
| id(int) | name(varchar) | path(LINESTRING) |
+---------+---------------+------------------+
| 123 | foo | 000002... |
| 124 | bar | 000002... |
I had the problem that each of the paths were divided into chunks, and those chunks were mixed up in some cases.
Suppose a linestring that was divided at point number 50 and 70:
- Chunk A: points 1-50
- Chunk B: points 51-70
- Chunk C: points 71-100
When I migrated that into my database, they got mixed, so the resulted linestring could end up like this:
- Chunk A: points 1-50
- Chunk C: points 71-100
- Chunk B: points 51-70
So that produces a jump from 50 to 71 and another one from 100 to 51
(EDIT) When I imported those paths divided in chunks I supposed that they were ordered, but the fact was that some were mixed, and that made some of my linestrings to be with their points ordered like the second example.
I want to be able to reorder those chunks (of points) so I would like to construct a SQL query to detect which paths have mixed points, then I could manually (with a tool made using openlayers) rearrange them.
It would be desirable to have a SQL update query to solve this problem, but I think that detection is easier (I presume there are ~5% or less of paths with errors)
EDIT3: I think the script for detection could check if a path contains a pair of consecutive points too far away. Maybe a SQL that orders paths from the path that contains the longest segment, would be good.
How could I make a function to get the length of the max segment in a linestring?
Here I show an example:
This is how it is in the database
This is how I want it to be fixed
EDIT4: Like I planned on EDIT3, a function could be written to find the longest distance between two consecutive points in a linestring iterating through the points of a linestring using ST_NPoints() and ST_PointN(), then a query could be made to order paths with that longest distance. It's highly probable that the linestrings that have this distance too long present the problem described. That way I would be able to detect them, and fix them manually.
The result from the detection SQL would be something like this:
|ordered by this|
+---------+---------------+------------------+---------------+
| id(int) | name(varchar) | path(LINESTRING) | msbtcp(int) |
+---------+---------------+------------------+---------------+
| 123 | foo | 000002... | 1000 |
| 124 | bar | 000002... | 800 |
*msbtcp would be the result of the function: max_separation_between_two_consecutive_points(path)