Create Random Line Strings

A LineString is a kind of ogc feature that represents a continuous set of points connected by linear segments. A single LineString will not be broken into multiple parts. They are usually used to represent roads, railroads or rivers. Creating a new Shapefile that contains some randomly created lines 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.


Imports MapWindow.Data
Imports MapWindow.Geometries
Imports MapWindow.Projections
Private Sub BuildLines()
    ' Setup a new "shapefile" by using the featureset object
    Dim fs As 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", GetType(Integer))
    ' Set up the specs for creating random points.  If you already know the point values from a file this is not necessary.
    Dim rnd As New Random(DateTime.Now.Millisecond)
    Const  YMIN As Integer = 0
    Const  YSPAN As Integer = 1000
    Const  XMIN As Integer = 0
    Const  XSPAN As Integer = 1000
    ' In a loop we are creating 100 different points using the box established above.
    For i As Integer = 0 To 9
        ' An array of coordinates that defines a single, continuous line
        Dim coords As Coordinate() = New Coordinate(9) {}
        For j As Integer = 0 To 9
            ' A coordinate is the simple X and Y location
            coords(j) = New Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN)
        Dim c As New Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN)
        ' A point has geoemtry capabilities like testing intersection with polygons etc.
        Dim ls As New LineString(coords)
        ' A feature also has attributes related to the featureset
        ' Features can be created directly, passing the point into the constructor, but there is a glitch
        ' right now that may not update the DataRow property of the feature correctly once it is added.
        Dim currentFeature As IFeature = fs.AddFeature(ls)
        ' 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)
End Sub

Last edited Feb 27, 2010 at 12:32 AM by Shade1974, version 6


No comments yet.