This project is read-only.

Create random points, then move them around.

This example was a test case for a bug fix on 11/7/2010. Make sure you have downloaded something more recent than changeset e91fbe2d79ca or else the "move" operation listed here will not work. To run this, create two buttons. The first button adds the points. The second button moves the points to a slightly different random location. Pressing the move button sequentially should cause the points to appear to move around randomly. This sample is also part of the "TestViewer" application that is in the "Demo Projects" folder of the source code repository.


        //using DotSpatial.Data;
        //using DotSpatial.Symbology;
        //using DotSpatial.Topology;
        //using System.Drawing;
        IFeatureSet _myPoints = new FeatureSet(FeatureType.Point);
        private void AddPoints()
            // Create the featureset if one hasn't been created yet.
            if (_myPoints == null) _myPoints = new FeatureSet(FeatureType.Point);
            // Assume background layers have been added, and get the current map extents.
            double xmin = map1.Extent.MinX;
            double xmax = map1.Extent.MaxX;
            double ymin = map1.Extent.MinY;
            double ymax = map1.Extent.MaxY;

            // Randomly generate 10 points that are in the map extent
            Random rnd = new Random();
            for(int i = 0; i < 10; i++)
                double x = xmin + rnd.NextDouble() * (xmax - xmin);
                double y = ymin + rnd.NextDouble() * (ymax - ymin);
                Coordinate c = new Coordinate(x, y);

            // Add a layer to the map, and we know it is a point layer so cast it specifically.
            IMapPointLayer pointLayer = map1.Layers.Add(_myPoints) as IMapPointLayer;

            // Control what the points look like through a symbolizer (or pointLayer.Symbology for categories)
            if(pointLayer != null)
                pointLayer.LegendText = "MovingPoints";
                pointLayer.Symbolizer = new PointSymbolizer(Color.Blue,             DotSpatial.Symbology.PointShape.Ellipse, 7);


        private void MovePoints()
            Random rnd = new Random();
            foreach(IFeature feature in _myPoints.Features)
                // Coordinates can be updated geographically like
                // feature.Coordinates[0].X += (rnd.NextDouble() - .5);
                // feature.Coordinates[0].Y += (rnd.NextDouble() - .5);
                // Or controled in pixels with the help of the map
                System.Drawing.Point pixelLocation = map1.ProjToPixel(feature.Coordinates[0]);

                // Control movement in terms of pixels
                int dx = Convert.ToInt32((rnd.NextDouble() - .5) * 50); // shift left or right 5 pixels
                int dy = Convert.ToInt32((rnd.NextDouble() - .5) * 50); // shift up or down 5 pixels
                pixelLocation.X = pixelLocation.X + dx;
                pixelLocation.Y = pixelLocation.Y + dy;

                // Convert the pixel motions back to geographic motions.
                Coordinate c = map1.PixelToProj(pixelLocation);
                feature.Coordinates[0] = c;
            // Refresh the cached representation because we moved points around.


Last edited May 20, 2015 at 6:32 PM by mbayles, version 7