HolesVB - sindizzy/DSW GitHub Wiki

Sample code demonstrating how to create a polygon that contains holes.

Imports DotSpatial.Geometries
Imports DotSpatial.Topology.Geometries

Private Sub btnSelect_Click(sender As Object, e As EventArgs)
	Try
		'Defines a new coordinate array
		Dim coords As Coordinate() = New Coordinate(19) {}
		'Defines a new random number generator
		Dim rnd As New Random()
		'defines a randomly generated center for teh polygon
		Dim center As New Coordinate((rnd.NextDouble() * 360) - 180, (rnd.NextDouble() * 180) - 90)
		For i As Integer = 0 To 18
			'generates random coordinates and adds those coordinates to the array
			coords(i) = New Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 10), center.Y + (i * 10) * Math.PI / 10)
		Next
		'sets the last coordinate equal to the first, this 'closes' the polygon
		coords(19) = New Coordinate(coords(0).X, coords(0).Y)
		'defines a new LingRing from the coordinates
		Dim Ring As New LinearRing(coords)
		'Repeates the process, but generates a LinearRing with a smaller area, this will be the hole in the polgyon
		Dim coordshole As Coordinate() = New Coordinate(19) {}
		For i As Integer = 0 To 19
			coordshole(i) = New Coordinate(center.X + Math.Cos((i * 10) * Math.PI / 20), center.Y + (i * 10) * Math.PI / 20)
		Next
		coordshole(19) = New Coordinate(coordshole(0).X, coordshole(0).Y)
		Dim Hole As New LinearRing(coordshole)
		'This steps addes the hole LinerRing to a ILinearRing Array
		'A Polgyon can contain multiple holes, thus a Array of Hole is required
		Dim Holes As ILinearRing() = New ILinearRing(0) {}
		Holes(0) = Hole
		'This passes the Ring, the polygon shell, and the Holes Array, the holes

		Dim pg As New Polygon(Ring, Holes)
	Catch ex As Exception
		MessageBox.Show(ex.Message)
	End Try
End Sub