码迷,mamicode.com
首页 > 数据库 > 详细

sql server & .net core 使用空间数据

时间:2019-02-20 20:13:38      阅读:403      评论:0      收藏:0      [点我收藏+]

标签:tab   red   cto   shel   trie   list   hub   write   ase   

使用的库

Microsoft.EntityFrameworkCore.SqlServer
Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddEntityFrameworkSqlServer()
             .AddDbContext<YourDbContext>(options => options.UseSqlServer(_configuration.GetConnectionString("SqlServer"),
              //映射到空间数据的数据库中的类型在模型中使用 NTS 类型
              x => x.UseNetTopologySuite()));
}

新增/修改数据

//srid=4326:wgs84
var geometryFactory = NtsGeometryServices.Instance.CreateGeometryFactory(srid: 4326);
var currentLocation = geometryFactory.CreatePoint(new Coordinate(x, y));

线

//LinearRing的点必须形成一个封闭的线串,而LineString则不需要
var line = new NetTopologySuite.Geometries.LineString(new Coordinate[]
{
    new Coordinate(10,0),
    new Coordinate(10,10),
    new Coordinate(0,10),
    new Coordinate(0,0),
    //new Coordinate(10,0),
});
//设置坐标系
line.SRID = srid;

var geom = new NetTopologySuite.Geometries.Polygon(
    new LinearRing(new Coordinate[]
    {
        //逆时针绘制
        new Coordinate(10,0),
        new Coordinate(10,10),
        new Coordinate(0,10),
        new Coordinate(0,0),
        new Coordinate(10,0),
    }));
//设置坐标系
geom.SRID = srid;

查询数据

概念

GepJSON:一种地理数据的描述格式。GeoJSON可以描述的对象包括:几何体,要素和要素集。(相关资料)
使用GepJSON需要引用一个新库

GeoJSON.Net

WKT(Well-known text):一种文本标记语言,用于表示矢量几何对象、空间参照系统及空间参照系统之间的转换。它的二进制表示方式,亦即WKB(well-known-binary)则胜于在传输和在数据库中存储相同的信息。该格式由开放地理空间联盟(OGC)制定。WKT可以表示的几何对象包括:点,线,多边形,TIN(不规则三角网)及多面体。(相关资料)
Geometry/geojson/WKT 三者可以互转

//Geometry to GeoJSON
Position position = new Position(x, y);
GeoJSON.Net.Geometry.Point point = new GeoJSON.Net.Geometry.Point(position);
var s = JsonConvert.SerializeObject(point);
//Geometry to wkt
var s2= NetTopologySuite.IO.WKTWriter.ToPoint(city.Location.Coordinate);

结果

"GeoJSON结果:{\"type\":\"Point\",\"coordinates\":[10.0,100.0]},WKT结果:POINT(100 10)"

线

//Geometry to GeoJSON
var coordinates = new List<IPosition>();
foreach (var item in road.Line.Coordinates)
{
    coordinates.Add(new Position(item.X, item.Y, item.Z));
}
GeoJSON.Net.Geometry.LineString line = new GeoJSON.Net.Geometry.LineString(coordinates);
var s= JsonConvert.SerializeObject(line);
//Geometry to wkt
var s2 = NetTopologySuite.IO.WKTWriter.ToLineString(road.Line.CoordinateSequence);

结果

"GeoJSON结果:{\"type\":\"LineString\",\"coordinates\":[[0.0,10.0,\"NaN\"],[10.0,10.0,\"NaN\"],[10.0,0.0,\"NaN\"],[0.0,0.0,\"NaN\"]]},WKT结果:LINESTRING(10 0, 10 10, 0 10, 0 0)"

//Geometry to GeoJSON
var lines = new List<GeoJSON.Net.Geometry.LineString>();
var polygon = country.Border as NetTopologySuite.Geometries.Polygon;
List<Coordinate[]> res = new List<Coordinate[]>();
res.Add(polygon.Shell.Coordinates);
foreach (ILineString interiorRing in polygon.InteriorRings)
{
    res.Add(interiorRing.Coordinates);
}
foreach(var line in res)
{
    var coordinates = new List<IPosition>();
    foreach (var item in line)
    {
        coordinates.Add(new Position(item.X, item.Y, item.Z));
    }
    lines.Add(new GeoJSON.Net.Geometry.LineString(coordinates));
}
GeoJSON.Net.Geometry.Polygon jsonPolygon = new GeoJSON.Net.Geometry.Polygon(lines);
var s = JsonConvert.SerializeObject(jsonPolygon);
//Geometry to wkt
//点和线的是静态方法,面的是方法_(:з」∠)_
var writer = new NetTopologySuite.IO.WKTWriter();
var s2 = writer.Write(country.Border);

结果

"GeoJSON结果:{\"type\":\"Polygon\",\"coordinates\":[[[0.0,10.0,\"NaN\"],[10.0,10.0,\"NaN\"],[10.0,0.0,\"NaN\"],[0.0,0.0,\"NaN\"],[0.0,10.0,\"NaN\"]]]},WKT结果:POLYGON ((10 0, 10 10, 0 10, 0 0, 10 0))"

计算

计算点与点之间的距离

var distance= NetTopologySuite.Operation.Distance.DistanceOp.Distance(point1, point2);

点是否包含在面以内

var prepGeom = NetTopologySuite.Geometries.Prepared.PreparedGeometryFactory.Prepare(geom);
var isContain = prepGeom.Contains(point);

示例代码

https://github.com/zLulus/NotePractice/blob/dev3/Website/DotNetCore/CoreWebsite/Controllers/SqlServerUseGeometryController.cs

参考资料

https://docs.microsoft.com/zh-cn/ef/core/modeling/spatial
https://docs.microsoft.com/zh-cn/sql/t-sql/spatial-geometry/spatial-types-geometry-transact-sql?view=sql-server-2017
https://nettopologysuite.github.io
https://docs.microsoft.com/zh-cn/sql/relational-databases/spatial/spatial-data-sql-server?view=sql-server-2017

sql server & .net core 使用空间数据

标签:tab   red   cto   shel   trie   list   hub   write   ase   

原文地址:https://www.cnblogs.com/Lulus/p/10408633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!