jca*_*er2 37 entity-framework geospatial
我想从纬度和经度双倍构造一个DbGeography点.
我知道我可以将我的双打转换为字符串并使用该DbGeography.FromText方法.
var latitude = 50.0d;
var longitude = 30.0d;
var pointString = string.Format(
"POINT({0} {1})",
longitude.ToString(),
latitude.ToString());
var point = DbGeography.FromText(pointString);
Run Code Online (Sandbox Code Playgroud)
但是将我的双打转换为字符串似乎是浪费,以便DbGeography可以再次将它们解析为双倍.
我试着像这样直接构建DbGeography:
var point = new DbGeography()
{
Latitude = 50,
Longitude = 30
};
Run Code Online (Sandbox Code Playgroud)
但纬度和经度属性是只读的.(这是有道理的,因为DbGeography类比单个点处理更多)
所述DbGeography类还提供了一种FromBinary采用一个字节阵列的方法.我不确定如何武装我的纬度和经度加倍到正确格式化的字节数组.
是否有一种更简单的方法来构建纬度和经度的DbGeography实例比顶部的代码加倍?
Jon*_*amy 50
简而言之,没有.
SqlGeography有一个合适的方法:
Microsoft.SqlServer.Types.SqlGeography.Point(latitude, longitude, srid);
Run Code Online (Sandbox Code Playgroud)
...但是你无论如何都必须转换为DbGeography.如果您对此感兴趣,请参阅我之前关于转换的答案:DbGeography到SqlGeography(和返回)
也就是说,我完全赞同Raphael Althaus,你应该创建一个静态的方法来让你的生活更轻松:
public static DbGeography CreatePoint(double lat, double lon, int srid = 4326)
{
string wkt = String.Format("POINT({0} {1})", lon, lat);
return DbGeography.PointFromText(wkt, srid);
}
Run Code Online (Sandbox Code Playgroud)
然后所有用法都可以通过该方法.
编辑
@Korayem提出了一个很好的建议,即自从最初回答这个问题以来,我确实已经完成了自己.大多数人使用SRID 4326,因此我们可以通过将其作为参数的默认值来使静态方法更容易使用.
| 归档时间: |
|
| 查看次数: |
19710 次 |
| 最近记录: |