空间记录的迭代查询

时间:2014-09-04 18:24:22

标签: sql sql-server

我在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也应在末尾重复以使多边形闭合。

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

2 个答案:

答案 0 :(得分:1)

这将完成这项工作......

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

答案 1 :(得分:1)

/Dataset_Extent/Vertex上粉碎XML并使用for xml path技巧构建字符串。

declare @wkt varchar(8000);

set @wkt = 'POLYGON((' +
           stuff((select ','+T.X.value('(LON/text())[1]', 'varchar(50)')+
                         ' '+T.X.value('(LAT/text())[1]', 'varchar(50)')
                  from @x.nodes('/Dataset_Extent/Vertex') as T(X)
                  for xml path('')), 1, 1, '')+
           ','+@x.value('(/Dataset_Extent/Vertex/LON/text())[1]', 'varchar(50)')+
           ' '+@x.value('(/Dataset_Extent/Vertex/LAT/text())[1]', 'varchar(50)')+
           '))';

对于一张桌子,它看起来就像这样。

select  'POLYGON((' +
        stuff((select ','+T.X.value('(LON/text())[1]', 'varchar(50)')+
                      ' '+T.X.value('(LAT/text())[1]', 'varchar(50)')
               from X.XMLData.nodes('/Dataset_Extent/Vertex') as T(X)
               for xml path('')), 1, 1, '')+
        ','+X.XMLData.value('(/Dataset_Extent/Vertex/LON/text())[1]', 'varchar(50)')+
        ' '+X.XMLData.value('(/Dataset_Extent/Vertex/LAT/text())[1]', 'varchar(50)')+
        '))'
from XMLFiles as X;