Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7836931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T14:23:45+00:00 2026-06-02T14:23:45+00:00

I imported ~1000 paths from somewhere into my postgis database to a linestring field.

  • 0

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:

  1. Chunk A: points 1-50
  2. Chunk B: points 51-70
  3. Chunk C: points 71-100

When I migrated that into my database, they got mixed, so the resulted linestring could end up like this:

  1. Chunk A: points 1-50
  2. Chunk C: points 71-100
  3. 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
a bad way

This is how I want it to be fixed
a good (fixed) way

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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T14:23:47+00:00Added an answer on June 2, 2026 at 2:23 pm

    This sounds a bit convoluted, however if you’re just after the max distance between two consecutive points in a linestring:

    CREATE OR REPLACE FUNCTION max_distance_in_linestring(line geometry) RETURNS float as $BODY$
    DECLARE
        i integer;
        n integer;
        d float;
        m float;
    BEGIN
        d := 0;
        n := ST_NPoints(line);
        i := 2;
        LOOP
            EXIT WHEN i >= n;
            m := ST_Distance(ST_PointN(line,i-1),ST_PointN(line,i));
            -- use for lon,lats:
            -- m := ST_Distance(ST_PointN(line,i-1)::geography,ST_PointN(line,i)::geography);
            IF m > d THEN
                d := m;
            END IF;
            i := i + 1;
        END LOOP;
        RETURN d;
    END;
    $BODY$
    LANGUAGE plpgsql;
    
    SELECT max_distance_in_linestring('LINESTRING(0 0, 1 1, 2 2)'::geometry);
    SELECT max_distance_in_linestring('LINESTRING(0 0, 4 3, 2 2)'::geometry);
    

    you may want to re-cast the ST_PointN calls to ::geography for getting distance in meters.

    The SQL would be like this:

    SELECT
      name, path 
    FROM
      paths
    ORDER BY
      max_distance_in_linestring(path) DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I imported a table from a CSV file into MySQL using this query: LOAD
I imported data from MS-Access into a SQL database using the import/export wizard. Now,
I imported data into a table from a backup I was given. Now I
Imported into a MySQL table is a report that contains a user generated field
I imported an excel file into my database using Tasks -> Import -> Excel
I imported my (PHP) old site's database tables into Django. By default it created
I've imported a DataTable from a SQL Database using SqlDataAdapter and Fill-Method. My datatable
I imported about 60 posts from my old blogger blog into my new wordpress
I imported a List of Strings from a file and put them into an
I imported rules from IIRF into IIS URL Rewrite, and most seemed to work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.