This project is read-only.

Moving Lines

Apr 19, 2011 at 6:23 PM


     I've been trying to create a line in my map and then move it. I copied the moving points sample code (which does work) as closely as possible. The line simply does not update it's position on the map. Can anyone help?

Here's the code:

private void AddLine()
            // Create the featureset if one hasn't been created yet.
            if (_myPoints == null) _myPoints = new FeatureSet(FeatureType.Line);
            _myPoints.Projection = map.Projection;

            // Assume background layers have been added, and get the current map extents.

            double xmin = map.ViewExtents.MinX;
            double xmax = map.ViewExtents.MaxX;
            double ymin = map.ViewExtents.MinY;
            double ymax = map.ViewExtents.MaxY;

            // Randomly generate 10 points that are in the map extent
            Coordinate[] coords = new Coordinate[10];
            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);
                coords[i] = new Coordinate(x, y);
            _myPoints.Features.Add(new LineString(coords));

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

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


        public void MoveLine()
            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);

                for (int i = 0; i < 10;i++)
                    // Or controled in pixels with the help of the map
                    System.Drawing.Point pixelLocation = map.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.
                    feature.Coordinates[i] = map.PixelToProj(pixelLocation);

            // Refresh the cached representation because we moved points around.


Apr 25, 2011 at 8:10 PM

So the goal of my coding project was to make a scalebar & colorbar. In MapWinGIS I did these by using drawings and just redrawing them every time the ExtentsChanged event was thrown.

DotSpatial doesn't seem to have drawings like MapWinGIS, so I started trying to used ShapeLayers. I've got the labeling updating correctly for my scalebar, but I can't get the darned thing to stay in the corner I want it in. Moving the scalebar when the ExtentsChanged event is thrown seems like an easy solution, but not luck so far. I tried a polygon layer too.

I noticed that the Print Layout classes actually include a scalebar. If there's something similar I can use in my maps, that would be great. I'll still need to make the colorbar though.

Can anyone shed some insight?