一般来说,在.net 里面要创建sqlpolygon实例,对组成polygon的点的顺序有着严格的要求,必须是逆时针方向的,例如:
string wktString = "POLYGON((-74.00751113891601 40.73412061435751,-74.02193069458008 40.705758069466754,-73.96940231323242 40.72891738296314,-74.00442123413086 40.70107318388675,-74.00751113891601 40.73412061435751))";
SqlChars polyText = new SqlChars(wktString);
SqlGeography polygon = SqlGeography.STPolyFromText(polyText, 4326);
但如果点时顺时针的话,下面的语句会抛出异常 :
string wktString = "POLYGON((-74.00751113891601 40.73412061435751,-73.96940231323242 40.72891738296314,-74.00442123413086 40.70107318388675,-74.02193069458008 40.705758069466754,-74.00751113891601 40.73412061435751))";
SqlChars polyText = new SqlChars(wktString);
SqlGeography polygon = SqlGeography.STPolyFromText(polyText, 4326);
解决的办法是:
(1) 下载 sqlspatialtools 最新version
地址:http://sqlspatialtools.codeplex.com/SourceControl/list/changesets
(2) 添加引用sqlspatialtools.dll 到项目中
(3) 使用MakeValidGeographyFromText()方法去返回一个SqlGeography
SqlGeography polygon = SQLSpatialTools.Functions.MakeValidGeographyFromText(wktString, 4326);