Create a Random MultiLineString

A MultiLineString is a kind of ogc feature that represents a continuous set of points connected by linear segments, but can have several, discontinuous parts associated with a single entity. They are usually used to represent roads, railroads or rivers. Creating a new Shapefile that contains some randomly created MultiLineStrings is shown below. Features represent a coupling of geometric shapes with non-geometric attributes. In this case the elevation value corresponds to an attribute of the integer data type. Each feature has exactly one measured elevation value, regardless of the number of coordinates in the geometric shape.


using MapWindow.Data;
using MapWindow.Geometries;
using MapWindow.Projections;
/// <summary>
/// MultiLineString represents several, disconnected lines.  MultiPoint
/// and MultiPolygon work the same way, so this example covers all of these.
/// For MultiPoint, make sure the feature specification is multi-point or else
/// the shapefile may not save correctly.  The other multi-formats are supported
/// as multi-part lines/polygons in the shapefile.
/// </summary>
private void BuildMultiLineString()
    // Setup a new "shapefile" by using the featureset object
    FeatureSet fs = new FeatureSet(FeatureTypes.Line);
    // You can control things like the projection with this object
    fs.Projection = KnownCoordinateSystems.Projected.UtmNad1983.NAD1983UTMZone11N;
    // The DataTable is a standard .Net DataTable, so you can add columns the normal way and use with DataGrid controls
    fs.DataTable.Columns.Add("Elevation", typeof(int));

    // Set up the specs for creating random points.  If you already know the point values from a file this is not necessary.
    Random rnd = new Random(DateTime.Now.Millisecond);
    const int YMIN = 0;
    const int YSPAN = 1000;
    const int XMIN = 0;
    const int XSPAN = 1000;

    LineString[] strings = new LineString[10];

    // In a loop we are creating 100 different points using the box established above.
    for (int i = 0; i < 10; i++)
        // An array of coordinates that defines a single, continuous line
        Coordinate[] coords = new Coordinate[10];
        for (int j = 0; j < 10; j++)
            // A coordinate is the simple X and Y location
            coords[j] = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);

        Coordinate c = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);
        // A point has geoemtry capabilities like testing intersection with polygons etc.
        LineString ls = new LineString(coords);

        // collect all the LineStrings we create in a single array.
        strings[i] = ls;

    MultiLineString mls = new MultiLineString(strings);
    // A feature also has attributes related to the featureset
    IFeature currentFeature = fs.AddFeature(mls);

    // Working with the current feature allows you to control attribute content as well as the feature content.
    currentFeature.DataRow["Elevation"] = rnd.Next(0, 100);

    fs.SaveAs(@"C:\test.shp", true);

Last edited Feb 27, 2010 at 12:47 AM by Shade1974, version 5


No comments yet.