Ejemplo n.º 1
0
public SharpMap.Geometries.MultiLineString SplitLineString(
    SharpMap.Geometries.LineString lineString, 
    System.Double length)
{
    if (lineString == null || lineString.IsEmpty())
        throw new System.ArgumentException("Linestring is null or Empty", "lineString");

    var gf = new NetTopologySuite.Geometries.GeometryFactory();
    var ntsLine = (NetTopologySuite.Geometries.LineString)
                    SharpMap.Converters.NTS.GeometryConverter.ToNTSGeometry(lineString, gf);

    var ret = new SharpMap.Geometries.MultiLineString();
    var lil = new NetTopologySuite.LinearReferencing.LengthIndexedLine(ntsLine);

    double currentLength = 0d;
    while (currentLength  < ntsLine.Length)
    {
        var tmpLine = (NetTopologySuite.Geometries.LineString)
            lil.ExtractLine(currentLength, currentLength + length);
        ret.LineStrings.Add((SharpMap.Geometries.LineString)
            SharpMap.Converters.NTS.GeometryConverter.ToSharpMapGeometry(tmpLine));
        currentLength += length;
    }
    return ret;
}
Ejemplo n.º 2
0
 private static void SharpMapMultiLineStringToSqlGeometry(SqlGeometryBuilder geomBuilder, SMMultiLineString multiLineString)
 {
     geomBuilder.BeginGeometry(OpenGisGeometryType.MultiLineString);
     for (int i = 0; i < multiLineString.NumGeometries; i++)
         SharpMapLineStringToSqlGeometry(geomBuilder, multiLineString.Geometry(i) as SMLineString);
     geomBuilder.EndGeometry();
 }
Ejemplo n.º 3
0
 internal static SharpMap.Geometries.MultiLineString ToSharpMapMultiLineString(GisSharpBlog.NetTopologySuite.Geometries.MultiLineString multiLineString)
 {
     SharpMap.Geometries.MultiLineString collection = new SharpMap.Geometries.MultiLineString();
     foreach (GisSharpBlog.NetTopologySuite.Geometries.LineString lineString in multiLineString.Geometries)
         collection.LineStrings.Add(ToSharpMapLineString(lineString));
     return collection;
 }
Ejemplo n.º 4
0
 private static SMMultiLineString SqlGeometryToSharpMapMultiLineString(SqlGeometry geometry)
 {
     SMMultiLineString multiLineString = new SMMultiLineString();
     for (int i = 0; i < geometry.STNumGeometries(); i++)
         multiLineString.LineStrings.Add((SMLineString)SqlGeometryToSharpMapLineString(geometry.STGeometryN(i)));
     return multiLineString;
 }
Ejemplo n.º 5
0
        /// <summary>
        /// Reads and parses the geometry with ID 'oid' from the ShapeFile
        /// </summary>
        /// <remarks><see cref="FilterDelegate">Filtering</see> is not applied to this method</remarks>
        /// <param name="oid">Object ID</param>
        /// <returns>geometry</returns>
        private SharpMap.Geometries.Geometry ReadGeometry(uint oid)
        {
            brShapeFile.BaseStream.Seek(GetShapeIndex(oid) + 8, 0); //Skip record number and content length
            ShapeType type = (ShapeType)brShapeFile.ReadInt32(); //Shape type
            if (type == ShapeType.Null)
                return null;
            if (_ShapeType == ShapeType.Point || _ShapeType==ShapeType.PointM || _ShapeType==ShapeType.PointZ)
            {
                SharpMap.Geometries.Point tempFeature = new SharpMap.Geometries.Point();
                return new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble());
            }
            else if (_ShapeType == ShapeType.Multipoint || _ShapeType == ShapeType.MultiPointM || _ShapeType == ShapeType.MultiPointZ)
            {
                brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box
                SharpMap.Geometries.MultiPoint feature = new SharpMap.Geometries.MultiPoint();
                int nPoints = brShapeFile.ReadInt32(); // get the number of points
                if (nPoints == 0)
                    return null;
                for (int i = 0; i < nPoints; i++)
                    feature.Points.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));

                return feature;
            }
            else if (	_ShapeType == ShapeType.PolyLine || _ShapeType == ShapeType.Polygon ||
                        _ShapeType == ShapeType.PolyLineM || _ShapeType == ShapeType.PolygonM ||
                        _ShapeType == ShapeType.PolyLineZ || _ShapeType == ShapeType.PolygonZ)
            {
                brShapeFile.BaseStream.Seek(32 + brShapeFile.BaseStream.Position, 0); //skip min/max box

                int nParts = brShapeFile.ReadInt32(); // get number of parts (segments)
                if (nParts == 0)
                    return null;
                int nPoints = brShapeFile.ReadInt32(); // get number of points

                int[] segments = new int[nParts + 1];
                //Read in the segment indexes
                for (int b = 0; b < nParts; b++)
                    segments[b] = brShapeFile.ReadInt32();
                //add end point
                segments[nParts] = nPoints;

                if ((int)_ShapeType%10 == 3)
                {
                    SharpMap.Geometries.MultiLineString mline = new SharpMap.Geometries.MultiLineString();
                    for (int LineID = 0; LineID < nParts; LineID++)
                    {
                        SharpMap.Geometries.LineString line = new SharpMap.Geometries.LineString();
                        for (int i = segments[LineID]; i < segments[LineID + 1]; i++)
                            line.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));
                        mline.LineStrings.Add(line);
                    }
                    if (mline.LineStrings.Count == 1)
                        return mline[0];
                    return mline;
                }
                else //(_ShapeType == ShapeType.Polygon etc...)
                {

                    //First read all the rings
                    List<SharpMap.Geometries.LinearRing> rings = new List<SharpMap.Geometries.LinearRing>();
                    for (int RingID = 0; RingID < nParts; RingID++)
                    {
                        SharpMap.Geometries.LinearRing ring = new SharpMap.Geometries.LinearRing();
                        for (int i = segments[RingID]; i < segments[RingID + 1]; i++)
                            ring.Vertices.Add(new SharpMap.Geometries.Point(brShapeFile.ReadDouble(), brShapeFile.ReadDouble()));
                        rings.Add(ring);
                    }
                    bool[] IsCounterClockWise = new bool[rings.Count];
                    int PolygonCount = 0;
                    for (int i = 0; i < rings.Count;i++)
                    {
                        IsCounterClockWise[i] = rings[i].IsCCW();
                        if (!IsCounterClockWise[i])
                            PolygonCount++;
                    }
                    if (PolygonCount == 1) //We only have one polygon
                    {
                        SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon();
                        poly.ExteriorRing = rings[0];
                        if (rings.Count > 1)
                            for (int i = 1; i < rings.Count; i++)
                                poly.InteriorRings.Add(rings[i]);
                        return poly;
                    }
                    else
                    {
                        SharpMap.Geometries.MultiPolygon mpoly = new SharpMap.Geometries.MultiPolygon();
                        SharpMap.Geometries.Polygon poly = new SharpMap.Geometries.Polygon();
                        poly.ExteriorRing = rings[0];
                        for (int i = 1; i < rings.Count;i++)
                        {
                            if (!IsCounterClockWise[i])
                            {
                                mpoly.Polygons.Add(poly);
                                poly = new SharpMap.Geometries.Polygon(rings[i]);
                            }
                            else
                                poly.InteriorRings.Add(rings[i]);
                        }
                        mpoly.Polygons.Add(poly);
                        return mpoly;
                    }
                }
            }
            else
                throw (new ApplicationException("Shapefile type " + _ShapeType.ToString() + " not supported"));
        }