Create Random Points in C#

Points represent exact locations, though in many cases they are simply reference locations where the actual shape or area of the feature being represented is much less important than the approximate location of that item. These are also usually features that are numerous. An example would be cities of the united states, where each city is very small compared to the entire country. Since points don't have a size characteristic, generally, no matter how much you zoom in, points are always represented by a symbol that is the same size. In this example the geographic points each are also associated with an independent elevation. This elevation is considered ancillary to the geographic data, and so is stored in an Attribute. If many points should be considered a single entity, a MultiPoint shapefile can be created. Unlike MultiLineStrings and MultiPolygons, however, MultiPoints require a different feature type that Points because of the shapefile specification.

points.jpg

Imports MapWindow.Projections
Imports MapWindow.Data
Imports MapWindow.Geometries

Public Class Form1
    Private Sub BuildPoints()
        ' Setup a new "shapefile" by using the featureset object
        Dim fs As New FeatureSet(FeatureTypes.Point)
        ' 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 99
            ' A coordinate is the simple X and Y location
            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 pt As New Point(c)
            ' 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(pt)

            ' Working with the current feature allows you to control attribute content as well as the feature content.
            currentFeature.DataRow("Elevation") = rnd.[Next](0, 100)
        Next i
        fs.SaveAs("C:\test.shp", True)
    End Sub
End Class

Last edited Feb 26, 2010 at 11:43 PM by Shade1974, version 5

Comments

No comments yet.