sql server & .net core 使用空间数据
2021/4/26 2:26:46
本文主要是介绍sql server & .net core 使用空间数据,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
使用的库
Microsoft.EntityFrameworkCore.SqlServer Microsoft.EntityFrameworkCore.SqlServer.NetTopologySuite
Startup.cs
public void ConfigureServices(IServiceCollection services) { services.AddEntityFrameworkSqlServer() .AddDbContext(options => options.UseSqlServer(_configuration.GetConnectionString("SqlServer"), //映射到空间数据的数据库中的类型在模型中使用 NTS 类型 x => x.UseNetTopologySuite())); }
建表需要注意:Geography or geometry
By default, spatial properties are mapped to geography columns in SQL Server. To use geometry, configure the column type in your model.
默认的数据类型是geography,如果要使用geometry,需要声明
比如使用geoserver,不识别geography,则需要geometry
[Column(TypeName = "geometry")] public Polygon Shape { get; set; }
参考资料
新增/修改数据
点
//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;
查询数据
概念
GeoJSON:一种地理数据的描述格式。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(); 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(); var polygon = country.Border as NetTopologySuite.Geometries.Polygon; Listres = new List(); res.Add(polygon.Shell.Coordinates); foreach (ILineString interiorRing in polygon.InteriorRings) { res.Add(interiorRing.Coordinates); } foreach(var line in res) { var coordinates = new List(); 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);
经纬度Longitude && Latitude
sql server
Coordinates in NTS are in terms of X and Y values. To represent longitude and latitude, use X for longitude and Y for latitude. Note that this is backwards from the latitude, longitude format in which you typically see these values.
简单来说,X是longitude(经度),Y是latitude(纬度)
geojson
geojson则是相反的
public Position(double latitude, double longitude, double? altitude = null)
示例代码
示例代码
这篇关于sql server & .net core 使用空间数据的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#
- 2024-01-24Advanced .Net Debugging 1:你必须知道的调试工具