This project is read-only.

How to disjoint a LineString geom into two seperate LineString geoms?

Topics: Developer Forum
Apr 29, 2009 at 5:01 AM
Edited Apr 29, 2009 at 5:13 AM
Hi,
I got a LineStringA(startPoint, endPoint), and a Point(middlePoint) within LineStringA.
Is there any function could cut this LineString into two pieces like LineStringB(startPoint, middlePoint) and LineStringC(middlePoint, endPoint)?
Or MultiLineString((startPoint, middlePoint), (middlePoint, endPoint))?

Cheers
Apr 30, 2009 at 11:46 AM
Edited Apr 30, 2009 at 9:27 PM
Hi Steven, this functionality isn't built in, but there are tools available within the library which could help.
I would start by creating a new SqlClr project, referencing the MsSqlSpatial dll. 
You could then write functions that may look something like (untested):

[SqlFunction(IsDeterministic = true)]
public static SqlBytes SqlSplitLineString(SqlBytes ewkbline, SqlBytes ewkbpoint)
{
    ILineString line = ConvertGeometry.ToGeometry(ewkbline) as ILineString; 
    IPoint point = ConvertGeometry.ToGeometry(ewkbpoint) as IPoint;  

    GeometryFactory geometryFactory = new GeometryFactory(..);
    IMultiLineString multiline = SplitLineString(geometryFactory, line, point);

    return ConvertGeometry.ToSqlBytes(multiline);

}

private static IMultiLineString SplitLineString(GeometryFactory geometryFactory, ILineString line, IPoint point)
{
    bool found;
    List<ICoordinate> part1 = new List<ICoordinate>();
    List<ICoordinate> part2 = new List<ICoordinate>();


    for(int index = 0;index < line.Coordinates.Count; index++)
    {
        ICoordinate c1, c2;
        c1 = line.Coordinates[index];
        if(!found)
        {
           c2 = line.Coordinates[index +1];
           ILineString sub = geometryFactory.CreateLine(new ICoordinate[]{c1, c2});

            part1.Add(c1);
            if (sub.Intersects(point));
            {
                found = true;
                part1.Add(point.Coordinate);
                part2.Add(point.Coordinate);
            }
        }
        else
        {
            part2.Add(c1);
        }
    }

    return geometryFactory.CreateMultiLineString(new ILineString[]{ geometryFactory.CreateLine(part1), geometryFactory.CreateLine(part2) });

}

or there may be a better way... hth jd