[postgis-users] Point (x%) on a linestring

Chapman, Martin MChapman at sanz.com
Thu Aug 12 15:17:51 PDT 2004


Try something like this.  The following code is from the OGR Lib.  You
could retrieve your geometry from the database and then apply this
algorithm to your points.  You will obviously have to convert the code
to your own.

void OGRLineString::Value( double dfDistance, OGRPoint * poPoint )

{
    double      dfLength = 0;
    int         i;

    if( dfDistance < 0 )
    {
        StartPoint( poPoint );
        return;
    }

    for( i = 0; i < nPointCount-1; i++ )
    {
        double      dfDeltaX, dfDeltaY, dfSegLength;

        dfDeltaX = paoPoints[i+1].x - paoPoints[i].x;
        dfDeltaY = paoPoints[i+1].y - paoPoints[i].y;
        dfSegLength = sqrt(dfDeltaX*dfDeltaX + dfDeltaY*dfDeltaY);

        if( dfLength <= dfDistance && dfLength + dfSegLength >=
dfDistance )
        {
            double      dfRatio;

            dfRatio = (dfDistance - dfLength) / dfSegLength;

            poPoint->setX( paoPoints[i].x * (1 - dfRatio)
                           + paoPoints[i+1].x * dfRatio );
            poPoint->setY( paoPoints[i].y * (1 - dfRatio)
                           + paoPoints[i+1].y * dfRatio );

            if( getCoordinateDimension() == 3 )
                poPoint->setZ( padfZ[i] * (1 - dfRatio)
                               + padfZ[i] * dfRatio );
                
            return;
        }
    }
    
    EndPoint( poPoint );
}

Martin

-----Original Message-----
From: Alex Martins Daher [mailto:aldaher at cpqd.com.br] 
Sent: Thursday, August 12, 2004 2:10 PM
To: PostGIS Users Discussion
Subject: [postgis-users] Point (x%) on a linestring


Hi all!

I have a multilinestring like this one:
MULTILINESTRING((-51.4933730667105 -29.1662071724963,-51.4933280072013
-29.1666495955688))

And I want to know what is the point on the linestring that is x% from
the start. For instance, the centroid of that geometry is 50% from the
start.

Is there any function that can do that: I pass the geometry anda the
percentage and returns the point (geometry) that is x% from the start.

I took a look at that docs but couldn't find anything.

Regards,
Alex
_______________________________________________
postgis-users mailing list postgis-users at postgis.refractions.net
http://postgis.refractions.net/mailman/listinfo/postgis-users



More information about the postgis-users mailing list