如何使用WKT?

时间:2014-06-19 21:13:31

标签: sql sql-server wkt

我正在进行数据库分配(TSQL),并且已经获得了一些“熟知的文本”。

我已经制作了表格:

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),
);

如何将几何点插入表中?

我可以使用它;

(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))

2 个答案:

答案 0 :(得分:1)

您可以使用STGeomFromText方法http://msdn.microsoft.com/en-us/library/bb933823.aspx

将WKT转换为Sql Server Geometry

由于您的下一个问题可能是“这个SRID的内容是什么,然后”,这里是相关的SO问题Geometry column: STGeomFromText and SRID (what is an SRID?)

答案 1 :(得分:0)

这样的事情:

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。