c# - 多面体中点的反向方向

标签 c# entity-framework gis

有一个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

经过一些研究和一些测试后,我得出的结论是,发生这种情况是因为多边形定义中每个点的顺序使得多边形定义了外部区域而不是内部区域,所以与其尝试获取纽约地区,它得到除纽约以外的地球其他地区。

有没有办法将其转换为正确的方向?

最佳答案

编辑:我之前给出的答案似乎并不是每次都有效。

我找到了使用这个 post 的解决方案及其提议 link

首先是一个返回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/

相关文章:

python - 重新格式化人口普查标题

python - 如何从 basemap 中删除美国大陆以外(墨西哥和加拿大境内)的轮廓颜色?

c# - Entity Framework 6 添加/更新 - 编辑时出错并尝试将编辑的实体添加为新实体

c# - 实体类型 ApplicationUser 不是当前上下文模型的一部分,具有自定义用户存储的 DB First

c# - 删除实体而不在通用存储库模式 Entity Framework 中获取它

c# - Xamarin.Forms Shell 在视觉层次结构之外导航

c# - 使用距离和引用点计算经度和纬度

c# - WPF:网格中的 ScrollViewer

c# - 如何更改组合框控件中的下拉按钮?

c# - 如何在 Windows 窗体应用程序中触发自动注销?