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)
				'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)
				'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)
				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
			End Try
		End Sub

Last edited Jul 13, 2010 at 2:35 PM by AlamoMan, version 3