有一个service给定某些条件,例如国家/地区和城镇,您会得到一个众所周知的文本,该文本定义了包含搜索区域的多边形或多多边形。
你可能有一个例子here .
现在我正尝试使用 C# 和 EF6.1 将这些数据插入到 SQL Server 数据库中。
代码应该是这样的:
1st 从服务中获取多边形字符串并将其添加到变量中:
var polygon = GetPolygonFromService(country, state, town);
然后用它插入数据库:
using(DataContext data = new DataCVontext())
{
var location = new Location
{
Country = country,
State = state,
Town = town,
GeoGraphy = DbGeography.FromText(polygon)
};
data.Locations.Add(location);
data.SaveChanges();
}
现在当我这样做时,我得到一个错误:
The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Parameter 4 ("@3"): The supplied value is not a valid instance of data type geography. Check the source data for invalid values. An example of an invalid value is data of numeric type with scale greater than precision
经过一些研究和一些测试后,我得出的结论是,发生这种情况是因为多边形定义中每个点的顺序使得多边形定义了外部区域而不是内部区域,所以与其尝试获取纽约地区,它得到除纽约以外的地球其他地区。
有没有办法将其转换为正确的方向?
最佳答案
编辑:我之前给出的答案似乎并不是每次都有效。
首先是一个返回DbGeography
public static DbGeography CreatePolygon(string wktString)
{
var sqlGeography = SqlGeography.STGeomFromText(new SqlChars(wktString), 4326).MakeValid();
var invertedSqlGeography = sqlGeography.ReorientObject();
if (sqlGeography.STArea() > invertedSqlGeography.STArea())
{
sqlGeography = invertedSqlGeography;
}
return DbGeography.FromText(sqlGeography.ToString());
}
然后使用函数
var polygon = GetPolygonFromService(country, state, town);
using(DataContext data = new DataCVontext())
{
var location = new Location
{
Country = country,
State = state,
Town = town,
GeoGraphy = CreatePolygon(polygon)
};
data.Locations.Add(location);
data.SaveChanges();
}
关于c# - 多面体中点的反向方向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26737862/