Note: this page refers to the mapwindow.dll library which was absorbed and refactored into DotSpatial. Please check out DotSpatial at

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.


using MapWindow.Data;
using MapWindow.Geometries;
using MapWindow.Projections;

private void BuildPoints()
    // Setup a new "shapefile" by using the featureset object
    FeatureSet fs = 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", 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;

    // In a loop we are creating 100 different points using the box established above.
    for (int i = 0; i < 100; i++)
        // A coordinate is the simple X and Y location
        Coordinate c = new Coordinate(XMIN + rnd.NextDouble() * XSPAN, YMIN + rnd.NextDouble() * YSPAN);
        // A point has geoemtry capabilities like testing intersection with polygons etc.
        Point pt = 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.
        IFeature currentFeature = 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);
    fs.SaveAs(@"C:\test.shp", true);

Last edited Jul 14, 2011 at 12:20 AM by danames, version 7


No comments yet.