Create LinearRing and Polygon

Topics: General Topics
Oct 13, 2007 at 2:52 PM
Hi, I would really appreciate it if someone could help with the following:

I have written the function below to create a polygon from a string of vertices. However the IDE gives me the following error on the line indicated: " Value of type '1 Dimensional array of SharpMap.Geometries.Point' cannot be converted to 'System.Collections.Generic.List(Of SharpMap.Geometries.Point)'".

It doesnt seem to like my supplied argument of an array of type Geometries.Point. But i dont understand why?? I'm not very familiar with generics in .net.

Thanks for any help.

Here's my function:

Private Function buildSolidPolygonFromString(ByVal strVertices As String) As Geometries.Polygon
'This function will create solid polygons only. i.e. no donuts.
'Vertices of the polygon are expected in a string of the following format:
'"x1, y1, x2, y2, etc..."

Dim tempCoords() As String = Split(strVertices, ",")
Dim coords((tempCoords.Length / 2) - 1) As Geometries.Point

Dim i As Integer = 0
While i < tempCoords.Length
coords(0).X = CType(tempCoords(i), Double)
i = i + 1
coords(0).Y = CType(tempCoords(i + 1), Double)
i = i + 1
End While

Dim extRing As New Geometries.LinearRing(coords)'<--- ERROR ON THIS LINE
Dim mySolidPolygon As New Geometries.Polygon(extRing)

Return mySolidPolygon

End Function
Oct 13, 2007 at 7:52 PM
In C#, I can do the following:

List<Geometries.Point> pointList = new List<Geometry.Point>(coords);

That will create a List object for you, and populate it with the objects from your coords array. Or, more likely, you can just create a List<Geometry.Point> coords, and then in your loop, create a single point and add it to the list instead of to your array.

I don't do VB.Net. Not sure of the syntax.
Coordinator
Oct 14, 2007 at 3:38 AM
Hi Geord9,

Magnum4610 has a good approach: you can just call Polygon.ExteriorRing.Add(). If you really want to create a LinearRing, you'll need an IEnumerable(Of Point) instance, since a linear ring needs an enumeration instead of a single instance.
Oct 14, 2007 at 7:03 PM

codekaizen wrote:
Hi Geord9,

Magnum4610 has a good approach: you can just call Polygon.ExteriorRing.Add(). If you really want to create a LinearRing, you'll need an IEnumerable(Of Point) instance, since a linear ring needs an enumeration instead of a single instance.


Decided to use Magnum's approach in c# as I couldn't figure it out in vb. Working function is below in case anyones interested.

Thanks alot for help guys!

public SharpMap.Geometries.Polygon buildSolidPolygonFromString(String strVertices)
{
List<SharpMap.Geometries.Point> pointList = new List<SharpMap.Geometries.Point>();
String[] tempCoords = strVertices.Split(new Char[] { ',' });

int i = 0;
while (i < tempCoords.Length)
{
SharpMap.Geometries.Point tempPoint = new SharpMap.Geometries.Point();
tempPoint.X = Double.Parse(tempCoordsi);
i = i + 1;
tempPoint.Y = Double.Parse(tempCoordsi);
i = i + 1;
pointList.Add(tempPoint);
};

SharpMap.Geometries.LinearRing extRing = new SharpMap.Geometries.LinearRing(pointList);
SharpMap.Geometries.Polygon mySolidPolygon = new SharpMap.Geometries.Polygon(extRing);

return mySolidPolygon;
}