sql server & .net core 使用空间数据
使用的库
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()));
}
建表需要注意: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<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);
经纬度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)
示例代码
参考资料
空间数据
空间类型 - geometry (Transact-SQL)
NTS Topology Suite
空间数据 (SQL Server)
sql server & .net core 使用空间数据的更多相关文章
- postgresql && .net core 使用空间数据
这里主要讲遇到的一些报错 增删改查 && 计算部分基本和sql server的空间数据操作一毛一样,感谢微软大大的倾情支持,直接看demo即可(- ̄▽ ̄)- 前往sql server ...
- SQL Server 2008空间数据应用系列九:使用空间工具(Spatial Tools)导入ESRI格式地图数据
转自:http://www.cnblogs.com/beniao/archive/2011/03/22/1989310.html 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Micros ...
- SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息
原文:SQL Server 2008空间数据应用系列十:使用存储过程生成GeoRSS聚合空间信息 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server 2 ...
- SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型
原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...
- 在Windows 上安装SQL Server的一些注意事项
基本来说安装SQL Server 单节点数据库并不是很困难的事情,大多可以通过Next来安装完成.其中要注意以下几点 安装.net3.5 可以参考本Blog的一些安装须知. Windows Serve ...
- visual studio 和 sql server 的激活密钥序列号
VS2010: YCFHQ-9DWCY-DKV88-T2TMH-G7BHP VS2013: BWG7X-J98B3-W34RT-33B3R-JVYW9 VS2015: 专业版:HMGNV-WCYXV- ...
- MICROSOFT SQL SERVER 2012 序列号
MICROSOFT SQL SERVER DEVELOPER 版(开发版) 序列号:YQWTX-G8T4R-QW4XX-BVH62-GP68Y MICROSOFT SQL SERVER ENTERPR ...
- ASP.NET Core 中文文档 第二章 指南(4.5)使用 SQL Server LocalDB
原文:Working with SQL Server LocalDB 作者:Rick Anderson 翻译: 魏美娟(初见) 校对: 孟帅洋(书缘).张硕(Apple).许登洋(Seay) Appl ...
- linux+asp.net core+nginx+sql server
Linux Disibutaion:Ubuntu 16.04.1 LTS Web Server:Nginx.Kestrel 安装.net core sudo sh -c 'echo "deb ...
随机推荐
- 阿里云OSS工具类
[前言] 我们上家公司的存储系统用的是FastDFS(智能一代云平台(二十八):对前后端分离和FastDFS的使用的再理解):现在在职的公司用的是阿里云的OSS(OSS的官方文档),在工作的时候整理一 ...
- Android OkHttp网络连接封装工具类
package com.lidong.demo.utils; import android.os.Handler; import android.os.Looper; import com.googl ...
- thinkphp缩略图
<?php class IndexAction extends Action { public function index() { $Photo = M('Photo'); $list = $ ...
- [RxJS] Use RxJS mergeMap to map and merge high order observables
Like RxJS switchMap() is a shortcut for map() and switch(), we will see in this lesson how mergeMap( ...
- uva 1519 - Dictionary Size(字典树)
题目链接:uva 1519 - Dictionary Size 题目大意:给出n个字符串组成的字典.如今要加入新的单词,从已有单词中选出非空前缀和非空后缀,组成新单词. 问说能组成多少个单词. 解题思 ...
- 【t008】钱币变换问题
Time Limit: 2 second Memory Limit: 32 MB [问题描述] 给定 2*n 个方格,将其排成一行.选择两个相邻的方格,设置为空方格,初始时不放钱币.而其余的方格共放入 ...
- 并发编程--CAS自旋锁
在前两篇博客中我们介绍了并发编程--volatile应用与原理和并发编程--synchronized的实现原理(二),接下来我们介绍一下CAS自旋锁相关的知识. 一.自旋锁提出的背景 由于在多处理器系 ...
- 课后作业11--使用SQL语句创建一个数据库
use master if db_id ('test') is not null--判断test数据库是否存在 drop database [test]--如果存在 删除test go--完成查找删除 ...
- java-synchronized原理
介绍 synchronized是一种独占式的重量级锁,在运行到同步方法或者同步代码块的时候,让程序的运行级别由用户态切换到内核态,把所有的线程挂起,通过操作系统的指令,去调度线程.这样会频繁出现程序运 ...
- TensorFlow 实战(二)—— tf.train(优化算法)
Training | TensorFlow tf 下以大写字母开头的含义为名词的一般表示一个类(class) 1. 优化器(optimizer) 优化器的基类(Optimizer base class ...