Gia*_*los 1 c# gis entity-framework
有一项服务给出了某些标准,例如国家/地区和城镇,您会得到一个众所周知的文本,用于定义包含搜索区域的多边形或多边形.
你可能在这里有一个例子.
现在我尝试使用C#和EF6.1将此数据插入到SQL Server数据库中.
代码如下:
1st从服务中获取多边形字符串并将其添加到变量中:
var polygon = GetPolygonFromService(country, state, town);
Run Code Online (Sandbox Code Playgroud)
然后使用它在db中插入:
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();
}
Run Code Online (Sandbox Code Playgroud)
现在当我这样做时,我得到一个错误:
传入的表格数据流(TDS)远程过程调用(RPC)协议流不正确.参数4("@ 3"):提供的值不是数据类型地理的有效实例.检查源数据中的无效值.无效值的示例是具有大于精度的比例的数字类型的数据
经过一些研究和一些测试后我得出结论,这是因为多边形定义中每个点的顺序是多边形定义外部区域而不是内部区域,所以不要试图获得新区域约克,它除了纽约之外还有其他地球.
有没有办法将其转换为正确的方向?
编辑:我之前给出的答案似乎并不是每次都有效.
首先是一个返回一个函数 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());
}
Run Code Online (Sandbox Code Playgroud)
然后使用该功能
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();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
497 次 |
| 最近记录: |