[GIS] How to create polygon with DBGeography Class in Csharp

cpolygon

I want to create a polygon with csharp 4.5 via DBGeometry class.

I found example on csharp to create a point via System.Data.Spatial namespace. (Requires Reference System.Data.Entity)

This code is working :

var text="POINT(29.11, 40.11)";
DbGeography.PointFromText(text, 4326);  //I can generate a point with this code

But i want to create Polygon object and these codes dont work . Please help me about which prototype – notation should i use to create a polygon or multipolygon object ? And i can not found examples on Microsoft web site. It contains only prototypes, not examples 🙁

//Not Work
var polyb = DbGeography.MultiPolygonFromText("POLYGON((29.11 40.11, 29.11 40.0 , 29.30 40.0, 29.30 40.11, 29.11 40.11)", 4326); --

//Not Work
//var polyc = DbGeography.MultiPolygonFromText("POLYGON((29.11, 40.11), (29.11, 40.0), (29.30, 40.0), (29.30, 40.11), (29.11, 40.11))", 4326);--

//Not work
//var reVal3 = DbSpatialServices.Default.GeographyPolygonFromText("POLYGON(127.652000002563
-26.244400002062321, 127.652000002563 -26.194399997591972,127.652000002563 -26.244400002062321)", 4326);

Summary
How can i create polygon with csharp DBGeometry class from text method ?

Best Answer

The DbGeography uses well known text when creating from text

Your last try was almost correct, was missing a '( )' and more points to create an actual polygon, it should be:

System.Data.Spatial.DbGeography.PolygonFromText("POLYGON((127.652 -26.244,127.652 -26.194,[some other points],127.652 -26.244))", 4326);
  • Notice the 2 '(' at each side.
  • Remember that it is Longitude,Latitude and not the other way around like all other places..
  • You must 'close off' the polygon, which means the start point and end point need to be the same