Skip to content

Commit 198b3dc

Browse files
committed
new doc
1 parent 658b2f3 commit 198b3dc

File tree

9 files changed

+150
-0
lines changed

9 files changed

+150
-0
lines changed

201802/20180226_06.md

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
## PostgreSQL raster(栅格数据) st_value 优化举例
2+
3+
### 作者
4+
digoal
5+
6+
### 日期
7+
2018-02-26
8+
9+
### 标签
10+
PostgreSQL , postgis , 栅格 , 波段 , st_value , raster , 优化 , st_intersects , gist 索引 , arcgis
11+
12+
----
13+
14+
## 背景
15+
栅格数据是由点阵组成的图像化数据,点阵具备空间属性(边界)。点阵由像素组成,所以每个像素也具备空间属性,同时每个像素内填充数值,数值可以代表温度、灰度、颜色系数、属性、湿度等等。
16+
17+
![pic](20180226_06_pic_002.png)
18+
19+
每个栅格中可以有多个波段,每个波段可以代表一个图层,多个波段可以组合叠加计算。
20+
21+
![pic](20180226_06_pic_003.gif)
22+
23+
![pic](20180226_06_pic_004.gif)
24+
25+
![pic](20180226_06_pic_005.gif)
26+
27+
![pic](20180226_06_pic_006.gif)
28+
29+
使用栅格数据,可以实现具备空间、业务属性的数据分析,可视化等。
30+
31+
例如热力图、绿化率图、道路、温度分布等等。
32+
33+
arcgis栅格数据组织形式:
34+
35+
```
36+
任何有效的栅格格式
37+
38+
像素数组
39+
40+
一个或多个波段
41+
42+
以文件形式存储在磁盘或地理数据库中
43+
44+
70 多种支持的格式
45+
46+
可用作影像服务
47+
48+
可在许多地理处理和分析工具中用作数据源
49+
50+
可在“影像分析”窗口中使用
51+
```
52+
53+
由于栅格数据可大可小,当提取某个像素的VALUE时,可能会有一定的性能问题。
54+
55+
## 使用st_value提取raster指定像素VALUE
56+
https://postgis.net/docs/RT_ST_Value.html
57+
58+
当栅格文件非常大时,st_value可能要执行很长时间,虽然只提取一个像素的值。(此性能与栅格文件本身的数据组织,检索方法有关。)
59+
60+
## 使用st_setvalue设置raster指定像素value
61+
http://postgis.net/docs/RT_ST_SetValue.html
62+
63+
## 栅格背景知识
64+
可以从arcgis或postgis的手册中,学习栅格的知识。
65+
66+
http://resources.arcgis.com/zh-cn/help/main/10.2/#/na/009t00000004000000/
67+
68+
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//009t00000007000000
69+
70+
## 优化方法
71+
### 业务层优化
72+
1、切割raster,将一个大的raster,切割为若干小的raster
73+
74+
2、边界转geometry, 建立边界的表达式索引
75+
76+
3、判断输入的geometry point是否与被查询raster相交(st_intersects),相交则求st_value,否则不计算st_value
77+
78+
### 数据库内核层优化
79+
4、当然,还有一种优化方法是对栅格文件本身建立较好的数据模型,便于快速检索。这种方法对业务透明
80+
81+
## 小结
82+
优化思路:
83+
84+
1、降低计算量,使用切割的方法。
85+
86+
2、降低计算量或IO放大,使用表达式索引。
87+
88+
3、降低精度,对raster进行分层,类似这样的思路
89+
90+
```
91+
SELECT
92+
ST_AsText(ST_Union(pixpolyg)) As shadow
93+
FROM
94+
(
95+
SELECT ST_Translate(
96+
ST_MakeEnvelope(
97+
ST_UpperLeftX(rast),
98+
ST_UpperLeftY(rast),
99+
ST_UpperLeftX(rast) + ST_ScaleX(rast)*2,
100+
ST_UpperLeftY(rast) + ST_ScaleY(rast)*2,
101+
0
102+
),
103+
ST_ScaleX(rast)*x,
104+
ST_ScaleY(rast)*y
105+
) As pixpolyg,
106+
ST_Value(rast, 2, x, y) As b2val
107+
FROM
108+
dummy_rast
109+
CROSS JOIN
110+
generate_series(1,1000,2) As x
111+
CROSS JOIN
112+
generate_series(1,1000,2) As y
113+
WHERE rid = 2
114+
AND x <= ST_Width(rast)
115+
AND y <= ST_Height(rast)
116+
) As foo
117+
WHERE
118+
ST_Intersects(
119+
pixpolyg,
120+
ST_GeomFromText('POLYGON((3427928 5793244,3427927.75 5793243.75,3427928 5793243.75,3427928 5793244))',0)
121+
)
122+
AND b2val != 254;
123+
```
124+
125+
![pic](20180226_06_pic_001.gif)
126+
127+
## 参考
128+
https://stackoverflow.com/questions/31799824/optimizing-st-intersects-in-postgresqlpostgis
129+
130+
http://postgis.17.x6.nabble.com/raster-loading-and-ST-Value-performance-td4999924.html
131+
132+
http://postgis.net/docs/RT_ST_SetValue.html
133+
134+
https://postgis.net/docs/RT_ST_Value.html
135+
136+
http://postgis.17.x6.nabble.com/ST-value-slow-td5010865.html
137+
138+
http://resources.arcgis.com/zh-cn/help/main/10.2/#/na/009t00000005000000/
139+
140+
http://resources.arcgis.com/zh-cn/help/main/10.2/index.html#//009t00000007000000
141+
142+
http://postgis.net/docs/manual-2.4/RT_reference.html
143+
144+
[《PostgreSQL multipolygon 空间索引查询过滤精简优化 - IO,CPU放大优化》](../201711/20171122_03.md)
145+
146+
[《PostgreSQL 空间切割(st_split, ST_Subdivide)功能扩展 - 空间对象网格化 (多边形GiST优化)》](../201710/20171005_01.md)
147+
148+
[《PostgreSQL 空间st_contains,st_within空间包含搜索优化 - 降IO和降CPU(bound box) (多边形GiST优化)》](../201710/20171004_01.md)

201802/20180226_06_pic_001.gif

28.7 KB
Loading

201802/20180226_06_pic_002.png

59 KB
Loading

201802/20180226_06_pic_003.gif

12.8 KB
Loading

201802/20180226_06_pic_004.gif

8.54 KB
Loading

201802/20180226_06_pic_005.gif

6.16 KB
Loading

201802/20180226_06_pic_006.gif

20.1 KB
Loading

201802/readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### 文章列表
22
----
3+
##### 20180226_06.md [《PostgreSQL raster(栅格数据) st_value 优化举例》](20180226_06.md)
34
##### 20180226_05.md [《PostgreSQL SELECT 的高级用法(CTE, LATERAL, ORDINALITY, WINDOW, SKIP LOCKED, DISTINCT, GROUPING SETS, ...)》](20180226_05.md)
45
##### 20180226_04.md [《PostgreSQL 11 preview - 支持 数组外键约束》](20180226_04.md)
56
##### 20180226_03.md [《PostgreSQL 对称加密、非对称加密用法介绍》](20180226_03.md)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ digoal's|PostgreSQL|文章|归类
2929

3030
### 未归类文档如下
3131
----
32+
##### 201802/20180226_06.md [《PostgreSQL raster(栅格数据) st_value 优化举例》](201802/20180226_06.md)
3233
##### 201802/20180226_05.md [《PostgreSQL SELECT 的高级用法(CTE, LATERAL, ORDINALITY, WINDOW, SKIP LOCKED, DISTINCT, GROUPING SETS, ...)》](201802/20180226_05.md)
3334
##### 201802/20180226_04.md [《PostgreSQL 11 preview - 支持 数组外键约束》](201802/20180226_04.md)
3435
##### 201802/20180226_03.md [《PostgreSQL 对称加密、非对称加密用法介绍》](201802/20180226_03.md)

0 commit comments

Comments
 (0)