我在Postgis中有很多行(在Geoserver中导入)。我需要为没有步长的固定长度(1km,100米)的每条线创建(在新表中)多边形(或缓冲区中计数点具有固定颜色的缓冲区)。如何创建查询Postgis或可能与Leaflet(Geojson来自geoserver)?
答案 0 :(得分:0)
要剪切线条,您必须使用ST_LINE_Substring
SELECT id,ST_Line_Substring(line,
CASE WHEN i=0 THEN 0 ELSE (i*100)/ST_Length(line) END,
CASE WHEN (i+1)*100<ST_Length(line) THEN ((i+1)*100)/ST_Length(line) ELSE 1 END),50,'endcap=flat')
FROM line,
(SELECT generate_series(0,floor(ST_Length(line)/100)::int) as i FROM line) as q
如果你希望ST_Buffer
在两端都是平坦的,那么将ST_DIFFERENCE
变得容易,如果你想要原始线末端以外的其他东西,它会变得更加复杂。然后我们必须WITH
line_frags AS
(
SELECT l.id,i, ST_Line_Substring(l.line,
CASE WHEN i=0 THEN 0 ELSE (i*100)/ST_Length(l.line) END,
CASE WHEN (i+1)*100<ST_Length(l.line) THEN ((i+1)*100)/ST_Length(l.line) ELSE 1 END) as frag,
CASE WHEN (i+1)*100>ST_Length(l.line) THEN true ELSE false END as the_end
FROM line AS l JOIN (SELECT id,generate_series(0,floor(ST_Length(l.line)/100)::int) as i FROM line AS l) as q ON q.id=l.id)
SELECT
CASE
WHEN f1.i=0 AND f2.i IS NULL
THEN ST_Buffer(f1.frag,50,'endcap=round')
WHEN f2.i IS NOT NULL
THEN ST_DIFFERENCE(ST_Buffer(f1.frag,50,'endcap=round'),ST_BUFFER(f2.frag,50,'endcap=flat'))
ELSE
ST_Buffer(f1.frag,50,'endcap=flat') END as buf
FROM line_frags AS f1
LEFT JOIN line_frags AS f2
ON f1.id=f2.id AND (f1.i=0 and f2.i=1 OR f1.the_end AND f2.i=f1.i-1);
然后缓冲下一个/前一个元素的缓冲区,不要忘记只有一个片段的情况
{{1}}