How to use WKT?
我正在做一个数据库分配 (TSQL),并且已经获得了一些”众所周知的文本”可以使用。
我已经制作了表格:
1
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
DROP TABLE geometry_polygons;
GO DROP TABLE geometry_points; GO DROP TABLE geometry_linestrings; GO CREATE TABLE geometry_polygons( id INT PRIMARY KEY NOT NULL, polygon geometry, label VARCHAR(50), area DECIMAL(10,4), ); CREATE TABLE geometry_points( id INT PRIMARY KEY NOT NULL, point geometry, label VARCHAR(501), ); CREATE TABLE geometry_linestrings( id INT PRIMARY KEY NOT NULL, linestring geometry, label VARCHAR(50), LENGTH DECIMAL(10,4), ); |
如何将几何点插入表格中?
我有这个工作;
1
2 3 4 5 6 7 8 |
(POLYGON((175.0305935740471 –39.924665194652604,175.03033608198166 –39.924387504970255,175.0301563739777 –39.92449035313209,175.03041118383408 –39.92474952981466,175.0305935740471 –39.924665194652604)),
POLYGON((175.02037167549133 –39.91957403819063,175.01922369003293 –39.92019116822632,175.02052187919617 –39.921170336469736,175.02149283885956 –39.920446247016564,175.02037167549133 –39.91957403819063)), LINESTRING(175.03047287464142 –39.92471250463434,175.03052115440366 –39.9247659854439,175.03007590770721 –39.924840035726646,175.02978086471558 –39.92509098331137,175.02977550029755 –39.92522262757816,175.03003299236298 –39.92538306868607,175.03039240837097 –39.92549002921579,175.031116604805 –39.92600426019882,175.0300168991089 –39.92684347686768,175.0209134817123 –39.919899060702676,175.02084910869598 –39.91996488785901),POINT(175.031116604805 –39.92600426019882),POINT(175.0300168991089 –39.92684347686768),POINT(175.02427697181705 –39.92248272908731), POINT(175.02696990966797 –39.92459731505649), POINT(175.0277853012085 –39.925181488771976), POINT(175.02287685871124 –39.92140689812598), POINT(175.02042531967163 –39.920339278607294), POINT(175.03031730651853 –39.924552061955026)) |
您可以使用 STGeomFromText 方法将 WKT 转换为 Sql Server Geometry http://msdn.microsoft.com/en-us/library/bb933823.aspx
由于您的下一个问题可能是”什么是 SRID 东西,那么”,这里有相关的 SO 问题 Geometry 专栏:STGeomFromText 和 SRID(什么是 SRID?)
类似这样的:
1
2 3 |
DECLARE @g geometry;
SET @g=geometry::STGeomFromText(‘POLYGON((175.0305935740471 -39.924665194652604,175.03033608198166 -39.924387504970255,175.0301563739777 -39.92449035313209,175.03041118383408 -39.92474952981466,175.0305935740471 -39.924665194652604))’, 4326); INSERT INTO geometry_polygons (polygon) VALUES (@g); |
请注意,您必须指定坐标参考系统,有时称为空间参考系统 (SRS),例如 4326,表示纬度/经度,才能使许多空间功能正常工作。
编辑:您在更新后的问题中添加的几何图形是多边形、线串和点的混合体,因此,如果您想添加它们,您需要使用 GeometryCollection(可以采用多种类型)一口气,或将它们拆分为单独的条目。但是,像这样混合不同的几何类型通常是个坏主意,因为许多空间函数不适用于 GeometryCollections。
- 像这样的东西 declare @g geometry; set @g=geometry::STGeomFromText(‘POLYGON((175.0305935740471 -39.924665194652604,175.03033608198166 -39.924387504970255,175.0301563739777 -39.92449035313209,175.03041118383408 -39.92474952981466,175.0305935740471 -39.924665194652604))’, 4326); insert into geometry_polygons (polygon) values (@g);
- 我有,但我得到”无法将值 NULL 插入列 \\’id\\’,表 \\’master.dbo.geometry_polygons\\’;列不允许空值。插入失败。”
- 是的,您必须为 id 提供一个值,如错误所示。您的问题询问如何插入几何列,我已经回答了,但我将留给您弄清楚如何将其余所需值添加到插入中。
- 非常感谢您的帮助。
- 不客气。我知道 SQL Server 中的空间有点难以理解。不过,总的来说,您应该在开始 SO 之前自己弄清楚,即,我已经尝试过,它不起作用。
来源:https://www.codenong.com/24316323/