Is there a way to calculate length of linestring from one point to another?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
2
down vote

favorite












If I have one linestring and coordinates of two points on that linestring, what is the easiest way to calculate not shortest distance from those two points, but distance over the given road (given linestring) in PostGIS?



Example of linestring and points A and B on it



So in the given example given linestrin is whole line and I want to calculate length of red line between points A and B.
Any help with this?







share|improve this question


























    up vote
    2
    down vote

    favorite












    If I have one linestring and coordinates of two points on that linestring, what is the easiest way to calculate not shortest distance from those two points, but distance over the given road (given linestring) in PostGIS?



    Example of linestring and points A and B on it



    So in the given example given linestrin is whole line and I want to calculate length of red line between points A and B.
    Any help with this?







    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      If I have one linestring and coordinates of two points on that linestring, what is the easiest way to calculate not shortest distance from those two points, but distance over the given road (given linestring) in PostGIS?



      Example of linestring and points A and B on it



      So in the given example given linestrin is whole line and I want to calculate length of red line between points A and B.
      Any help with this?







      share|improve this question












      If I have one linestring and coordinates of two points on that linestring, what is the easiest way to calculate not shortest distance from those two points, but distance over the given road (given linestring) in PostGIS?



      Example of linestring and points A and B on it



      So in the given example given linestrin is whole line and I want to calculate length of red line between points A and B.
      Any help with this?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 14 at 6:39









      Anastasia

      132




      132




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          4
          down vote



          accepted










          This is one way of doing it



          Find out how big fraction of the line is between the 2 points with ST_LineLocatePoint and multiply that fraction with the whole length.



          SELECT 
          (ST_LineLocatePoint(l,p2)-ST_LineLocatePoint(l,p1) )*st_length(l) dist_along_line,
          st_distance(p1, p2) direct_distance
          FROM
          (
          SELECT 'linestring(1 1, 1 2, 2 2, 5 0, 10 5)'::geometry l,
          'point(1 2)'::geometry p1, 'point(5 3)'::geometry p2
          ) a





          share|improve this answer




















            Your Answer







            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "79"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f292684%2fis-there-a-way-to-calculate-length-of-linestring-from-one-point-to-another%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            4
            down vote



            accepted










            This is one way of doing it



            Find out how big fraction of the line is between the 2 points with ST_LineLocatePoint and multiply that fraction with the whole length.



            SELECT 
            (ST_LineLocatePoint(l,p2)-ST_LineLocatePoint(l,p1) )*st_length(l) dist_along_line,
            st_distance(p1, p2) direct_distance
            FROM
            (
            SELECT 'linestring(1 1, 1 2, 2 2, 5 0, 10 5)'::geometry l,
            'point(1 2)'::geometry p1, 'point(5 3)'::geometry p2
            ) a





            share|improve this answer
























              up vote
              4
              down vote



              accepted










              This is one way of doing it



              Find out how big fraction of the line is between the 2 points with ST_LineLocatePoint and multiply that fraction with the whole length.



              SELECT 
              (ST_LineLocatePoint(l,p2)-ST_LineLocatePoint(l,p1) )*st_length(l) dist_along_line,
              st_distance(p1, p2) direct_distance
              FROM
              (
              SELECT 'linestring(1 1, 1 2, 2 2, 5 0, 10 5)'::geometry l,
              'point(1 2)'::geometry p1, 'point(5 3)'::geometry p2
              ) a





              share|improve this answer






















                up vote
                4
                down vote



                accepted







                up vote
                4
                down vote



                accepted






                This is one way of doing it



                Find out how big fraction of the line is between the 2 points with ST_LineLocatePoint and multiply that fraction with the whole length.



                SELECT 
                (ST_LineLocatePoint(l,p2)-ST_LineLocatePoint(l,p1) )*st_length(l) dist_along_line,
                st_distance(p1, p2) direct_distance
                FROM
                (
                SELECT 'linestring(1 1, 1 2, 2 2, 5 0, 10 5)'::geometry l,
                'point(1 2)'::geometry p1, 'point(5 3)'::geometry p2
                ) a





                share|improve this answer












                This is one way of doing it



                Find out how big fraction of the line is between the 2 points with ST_LineLocatePoint and multiply that fraction with the whole length.



                SELECT 
                (ST_LineLocatePoint(l,p2)-ST_LineLocatePoint(l,p1) )*st_length(l) dist_along_line,
                st_distance(p1, p2) direct_distance
                FROM
                (
                SELECT 'linestring(1 1, 1 2, 2 2, 5 0, 10 5)'::geometry l,
                'point(1 2)'::geometry p1, 'point(5 3)'::geometry p2
                ) a






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Aug 14 at 7:02









                Nicklas Avén

                11.3k12539




                11.3k12539



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fgis.stackexchange.com%2fquestions%2f292684%2fis-there-a-way-to-calculate-length-of-linestring-from-one-point-to-another%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Comments

                    Popular posts from this blog

                    What does second last employer means? [closed]

                    Installing NextGIS Connect into QGIS 3?

                    One-line joke