先上代码
select replace(replace(replace(replace(replace(replace(replace(replace(replace('('||trim(substr(sde.st_astext(t.shape),instr(sde.st_astext(t.shape),'(')))||')','((((','((('),'))))',')))'),', ','`'),'( ','<'),'`','],['),' ',','),'(','['),'<','['),')',']') from tablex t
以上sql,可以实现把
POLYGON (( 120.111 32.1121, 120.32423 32.1213, 120.32423 32.1213))
转换为
[[[120.111,32.1121],[120.32423,32.1213],[120.32423,32.1213]]]
把
MULTIPOLYGON ((( 120.111 32.1121, 120.32423 32.1213, 120.32423 32.1213)),(( 120.111 32.1121, 120.32423 32.1213, 120.32423 32.1213)),(( 120.111 32.1121, 120.32423 32.1213, 120.32423 32.1213)))
转换为
[[[120.111,32.1121],[120.32423,32.1213],[120.32423,32.1213]],[[120.111,32.1121],[120.32423,32.1213],[120.32423,32.1213]],[[120.111,32.1121],[120.32423,32.1213],[120.32423,32.1213]]]
从而实现只要是polygon,都是[点串集合]统一的3层[[[数组
分析一下上面的sql实现
第一步sde.st_astext(t.shape) 不多说,st_geometry函数
第二步trim(substr(sde.st_astext(t.shape),instr(sde.st_astext(t.shape),’(’))) 去除前缀的POLYGON或者MULTIPOLYGON
第三步replace(replace(’(’||trim(substr(sde.st_astext(t.shape),instr(sde.st_astext(t.shape),’(’)))||’)’,’((((’,’(((’),’))))’,’)))’) 在第二步上两头都加上了一层括号,然后把4层括号((((替换为3层(((,实现了不论POLYGON还是MULTIPOLYGON 结果的结构统一性,即如果是MULTIPOLYGON 则4层会替换成3层,如果是POLYGON,则3层还是3层
后续几步都是替换步骤
1.先把原来的’, ‘(逗号后有个空格),替换为’`’(或者其他字符只要不是括号点之类的)
2.然后把’( ‘(括号后有个空格)替换为’<’(或者其他字符只要不是括号点之类的)
3.然后把之前1替换的字符替换成’],[’
4.再把2中替换的’<‘换成’[’,其他括号分别对应换成’[’,’]'即可