創建pgraster的overview表以及overviewconstrants表約束

創建pgraster的overview表以及overviewconstrants表約束

練習postgis官方文檔中有關pgraster部分的內容。記錄下筆記。

在pgraster導入的時候raster2pgsql這個插件使用的時候,忘記使用-l這個參數來建立overview

這個時候需要手動在寫sql添加

1.創建一個overview表

 

CREATE TABLE temp_overviewtable AS SELECT
ST_AddBand(
  ST_MakeEmptyRaster(1024, 1024, 0, 0, 4),
  1, '8BSI'::text, -129, NULL
) r2;

 

2.創建與raster表字段對應關係,使用函數AddOverviewConstraints來實現

 

select addoverviewconstraints('temprature','rast','temp_overviewtable','r2','6')

 

3.創建完成,查詢語句查看:

select * from raster_columns
select * from raster_overviews

select st_astiff(st_union(rast)) as rasttiff from public.temprature where filename='wc2.0_2.5m_tmax_01.tif' limit 10

4.關於導出pgraster查看效果:有輸入了,當然想看一下輸出是很什麼樣的,而postgis恰恰是去圖形化的,壓根沒有圖形可視化的窗口,所以想看效果只能通過寫代碼看。

怎麼寫代碼,些什麼代碼?從官網上看有php,aps.net,java的,還有各plpython的方式的這個要求的安裝plpython關於這種方式自己去官網試試吧。

5.我選了csdn上的一篇博文:https://blog.csdn.net/theonegis/article/details/55100365

來試,總是報錯TypeError: a bytes-like object is required, not 'str'。修改了其中的輸出的代碼。見下面代碼的粉色部分


import psycopg2
conn=psycopg2.connect('host=localhost port=5432 user=postgres password=12345 dbname=name')

cur=conn.cursor()

#這裏可以把sql拿出來去pg裏面試一下看看是不是可用,是不是需要很久的時間,爲了效率我這裏限制只導出第一幅

cur.execute("select st_astiff(st_union(rast)) as rasttiff from public.temprature where filename='wc2.0_2.5m_tmax_01.tif' limit 1")

rasttiff=cur.fetchone()

#http://initd.org/psycopg/docs/cursor.html可以來psycopg官網看看相關的參數
if rasttiff is  not None:

#輸出位置的選擇,output是我的輸出文件名
    open(r'E:\data\temprature\output.tif','wb').write(rasttiff[0])

cur.close()
conn.close()

6.找到我的e盤的對應位置,看我的圖出來了哈哈。

 

具體參考原文如下:

http://postgis.net/docs/RT_AddOverviewConstraints.html

Synopsis

boolean AddOverviewConstraints(name ovschema, name ovtable, name ovcolumn, name refschema, name reftable, name refcolumn, int ovfactor);

boolean AddOverviewConstraints(name ovtable, name ovcolumn, name reftable, name refcolumn, int ovfactor);

Description

Adds constraints on a raster column that are used to display information in the raster_overviews raster catalog.

The ovfactor parameter represents the scale multiplier in the overview column: higher overview factors have lower resolution.

When the ovschema and refschema parameters are omitted, the first table found scanning the search_path will be used.

Availability: 2.0.0

Examples

CREATE TABLE res1 AS SELECT
ST_AddBand(
  ST_MakeEmptyRaster(1000, 1000, 0, 0, 2),
  1, '8BSI'::text, -129, NULL
) r1;

CREATE TABLE res2 AS SELECT
ST_AddBand(
  ST_MakeEmptyRaster(500, 500, 0, 0, 4),
  1, '8BSI'::text, -129, NULL
) r2;

SELECT AddOverviewConstraints('res2', 'r2', 'res1', 'r1', 2);

-- verify if registered correctly in the raster_overviews view --
SELECT o_table_name ot, o_raster_column oc,
       r_table_name rt, r_raster_column rc,
       overview_factor f
FROM raster_overviews WHERE o_table_name = 'res2';
  ot  | oc |  rt  | rc | f
------+----+------+----+---
 res2 | r2 | res1 | r1 | 2
(1 row)
		

See Also

Section 5.2.2, “Raster Overviews”DropOverviewConstraintsST_CreateOverviewAddRasterConstraints

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章