gpt4 book ai didi

sql - 空间记录的迭代查询

转载 作者:行者123 更新时间:2023-12-02 01:45:54 25 4
gpt4 key购买 nike

我在 SQL Server 中创建了以下查询

declare @x XML
set @x='<Dataset_Extent>
<EXTENT_TYPE>Bounding_Polygon</EXTENT_TYPE>
<Vertex>
<LON>66.91292909247741</LON>
<LAT>30.27001012181008</LAT>
<X>299232</X>
<Y>3350549</Y>
<COL>1</COL>
<ROW>1</ROW>
</Vertex>
<Vertex>
<LON>66.99456841960638</LON>
<LAT>30.27128639618252</LAT>
<X>307089.5</X>
<Y>3350549</Y>
<COL>15715</COL>
<ROW>1</ROW>
</Vertex>
<Vertex>
<LON>66.99700791329992</LON>
<LAT>30.1509623521339</LAT>
<X>307089.5</X>
<Y>3337207.5</Y>
<COL>15715</COL>
<ROW>26683</ROW>
</Vertex>
<Vertex>
<LON>66.91546772378466</LON>
<LAT>30.14969219541345</LAT>
<X>299232</X>
<Y>3337207.5</Y>
<COL>1</COL>
<ROW>26683</ROW>
</Vertex>
<Center>
<LON>66.9549932872921</LON>
<LAT>30.21048776638499</LAT>
<X>303160.75</X>
<Y>3343878.25</Y>
<COL>7858</COL>
<ROW>13342</ROW>
</Center>
</Dataset_Extent>';
declare @wkt varchar(8000);
select @wkt=CONVERT(varchar(7000),@x.query('distinct-values(
for $lon in /Dataset_Extent/Vertex/LON/text()
for $lat in /Dataset_Extent/Vertex/LAT/text()
return ($lon cast as xs:string?,$lat cast as xs:string?,","))
'));

--concatenate the word POLYGON
set @wkt='POLYGON(('+@wkt +'))';
print @wkt

这给了我以下输出:

POLYGON((66.91292909247741 66.99456841960638 30.27128639618252 66.99700791329992 66.91546772378466 30.27001012181008,30.1509623521339 30.14969219541345))

每个 Lon/lat 对的顺序不正确,我要求按以下格式输出:

POLYGON((66.91292909247741 30.27001012181008,66.99456841960638 30.27128639618252,66.99700791329992 30.1509623521339,66.91546772378466 30.14969219541345,66.91292909247741 30.27001012181008))

起始顶点 Lon/Lat 也应在末端重复以使多边形闭合。

我需要怎么做才能解决这些问题?

最佳答案

这将完成工作...

DECLARE @wkt NVARCHAR(MAX)
SELECT @wkt = COALESCE(@wkt + ',' + char(13), '')
+ concat(convert(varchar, x.query('./LON/text()')),' ', convert(varchar,x.query('./LAT/text()')))
from @x.nodes('/Dataset_Extent/Vertex') as t(x)

select @wkt = COALESCE(@wkt + ',' + char(13), '')
+ concat(convert(varchar, x.query('./LON/text()')),' ', convert(varchar,x.query('./LAT/text()')))
from @x.nodes('/Dataset_Extent/Vertex[1]') as t(x)

set @wkt='POLYGON(('+@wkt +'))';
print @wkt

不是很漂亮,但我不知道如何子查询第一个节点...祝你好运!如果可以的话,我尽量避免使用 xpath :)

关于sql - 空间记录的迭代查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25672554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com