I have a feeling I'm not understanding you exactly, since in my case there
are no generated corners?  I just move all the points in the line by a given
x/y amount.  Here's my code (sorry it's csharp, I'm using NTS):

        /// <summary>
        /// Moves a line the given distance to either the "left" or
        /// "right" of the line.  The average angle of the line is
calculated
        /// via the endpoints and "left" and "right" are 90 degrees to
either direction.
        /// </summary>
        /// <param name="input">The line to shift.</param>
        /// <param name="toTheRight">If true, move to the right.
        ///                          If false, move to the left.</param>
        /// <param name="distance">How far to shift the line.</param>
        /// <returns>A new line, identical to the input except all points in
it are
        ///          moved to the left or right.</returns>
        public static ILineString ShiftLine(ILineString input, bool
toTheRight,
            double distance)
        {
            if (input.NumPoints < 2)
            {
                throw new ArgumentException("Cannot shift a line with less
than two points: " + input);
            }
            IPoint point0 = input.StartPoint;
            IPoint point1 = input.EndPoint;
            // Calculate the angle of the line.
            double avgAngle = Math.Atan2(point1.Y - point0.Y, point1.X -
point0.X);
            // Now turn to the right or left.
            if (toTheRight)
            {
                avgAngle -= Math.PI / 2;
            }
            else
            {
                avgAngle += Math.PI / 2;
            }
            // Now convert back to an x/y offset.
            double yOffset = Math.Sin(avgAngle) * distance;
            double xOffset = Math.Cos(avgAngle) * distance;
            ICoordinate[] shiftedCoords = new ICoordinate[input.NumPoints];
            for (int i = 0; i < shiftedCoords.Length; i++)
            {
                IPoint p = input.GetPointN(i);
                shiftedCoords[i] = new Coordinate(p.X + xOffset, p.Y +
yOffset);
            }
            return new LineString(shiftedCoords);
        }

As you can see I took a pretty simple approach.  I'm shifting street
centerlines, I'm not sure the behavior will necessarily be what you want if
you are dealing with very non-straight lines.  However this actually
produces better results than my original, more complicated, logic where I
tried to take the angle of every segment separately and compute a real
"average" direction based on how long each segment was relative to the
others.  In one way of looking at it, the start point and end point are the
average slope, assuming you're using reasonably square units (Oh yeah, I
doubt this will work well if you're drawing lines from Austrailia to
Greenland).

Jeff


On Thu, Nov 13, 2008 at 9:09 AM, Andrea Aime <[EMAIL PROTECTED]> wrote:

> Jeff Adams ha scritto:
>
>> Are you talking about shifting the line to a new location (keeping it a
>> line), or doing a half buffer (now you have a polygon)?
>>
>> I wrote code to shift a line to the line's left or right, if that's what
>> you're trying to do I can provide that.  I believe I posted my first
>> solution to this forum, but I later discovered it was flawed and have a
>> better (and much simpler) one now.
>>
>
> Just shift a linestring, but the whole linestring, not just a segment.
> Whether shifting produces rounded corners or not, that's not relevant
> to me, provided the shifted result is connected.
>
> So if you have some code, I would be very glad to take it :)
> Oh, it would eventually end up in GeoTools (or JTS if Martin wants).
>
>
> Cheers
> Andrea
>
> --
> Andrea Aime
> OpenGeo - http://opengeo.org
> Expert service straight from the developers.
> _______________________________________________
> jts-devel mailing list
> [email protected]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>
_______________________________________________
jts-devel mailing list
[email protected]
http://lists.refractions.net/mailman/listinfo/jts-devel

Reply via email to