RandomPointsVB - sindizzy/DSW GitHub Wiki
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.
[Image:MovingPoint2.png)(Image_MovingPoint2.png)
'using DotSpatial.Data;
'using DotSpatial.Symbology;
'using DotSpatial.Topology;
'using System.Drawing;
Private Sub AddPoints()
' Create the featureset if one hasn't been created yet.
If _myPoints Is Nothing Then
_myPoints = New FeatureSet(FeatureType.Point)
End If
' Assume background layers have been added, and get the current map extents.
Dim xmin As Double = map1.Extents.Minimum.X
Dim xmax As Double = map1.Extents.Maximum.X
Dim ymin As Double = map1.Extents.Minimum.Y
Dim ymax As Double = map1.Extents.Maximum.Y
' Randomly generate 10 points that are in the map extent
Dim rnd As New Random()
For i As Integer = 0 To 9
Dim x As Double = xmin + rnd.NextDouble() * (xmax - xmin)
Dim y As Double = ymin + rnd.NextDouble() * (ymax - ymin)
Dim c As New Coordinate(x, y)
_myPoints.Features.Add(c)
Next
' Add a layer to the map, and we know it is a point layer so cast it specifically.
Dim pointLayer As IMapPointLayer = TryCast(map1.Layers.Add(_myPoints), IMapPointLayer)
' Control what the points look like through a symbolizer (or pointLayer.Symbology for categories)
If pointLayer IsNot Nothing Then
pointLayer.LegendText = "MovingPoints"
pointLayer.Symbolizer = New PointSymbolizer(Color.Blue, PointShape.Ellipse, 7)
End If
End Sub
Private Sub MovePoints()
Dim rnd As New Random()
For Each feature As IFeature In _myPoints.Features
' Coordinates can be updated geographically like
' feature.Coordinates[0](0).X += (rnd.NextDouble() - .5);
' feature.Coordinates[0](0).Y += (rnd.NextDouble() - .5);
' Or controled in pixels with the help of the map
Dim pixelLocation As Point = map1.ProjToPixel(feature.Coordinates(0))
' Control movement in terms of pixels
Dim dx As Integer = Convert.ToInt32((rnd.NextDouble() - 0.5) * 50)
' shift left or right 5 pixels
Dim dy As Integer = Convert.ToInt32((rnd.NextDouble() - 0.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.
Dim c As Coordinate = map1.PixelToProj(pixelLocation)
feature.Coordinates(0) = c
Next
' Refresh the cached representation because we moved points around.
map1.MapFrame.Invalidate()
map1.Invalidate()
End Sub