PostGIS查询指定范围的数据
发布时间:2020-12-13 16:58:19 所属栏目:百科 来源:网络整理
导读:对于上一篇PostGIS批量导入栅格数据中导入的气温数据,如何查询指定范围的气温呢? 比如,给定了经纬度范围,如何取出给定月份的数据? 下面的SQL代码给出了查询方法: SELECT ST_Union(ST_Clip(rast,geom)) AS rast FROM staging.tmean_19 CROSS JOIN ST_Ma
对于上一篇PostGIS批量导入栅格数据中导入的气温数据,如何查询指定范围的气温呢? SELECT ST_Union(ST_Clip(rast,geom)) AS rast FROM staging.tmean_19 CROSS JOIN ST_MakeEnvelope(3.87,73.67,53.55,135.05,4326) As geom WHERE ST_Intersects(rast,geom) AND month=1;
其中, SELECT ST_AsTIFF(rast,'LZW') FROM ( SELECT ST_Union(ST_Clip(rast,geom)) AS rast FROM staging.tmean_19 CROSS JOIN ST_MakeEnvelope(97.51,37.28,111.55,50.52,4326) As geom WHERE month=1 AND ST_Intersects(rast,geom) ) AS rasttiff;
完整的Python代码如下: # -*- coding: utf-8 -*-
import psycopg2
# Connect to an existing database
conn = psycopg2.connect('host=localhost port=5432 user=postgres password=post1231 dbname=postgis_in_action')
# Open a cursor to perform database operations
cur = conn.cursor()
# Execute SQL query
# cur.execute("SELECT ST_AsTIFF(rast,'LZW') AS rasttiff FROM staging.wsiearth WHERE rid=1;")
# cur.execute("SELECT ST_AsTIFF(ST_Union(rast),'LZW') AS rasttiff FROM staging.tmean_19 WHERE filename='tmean1_19.tif';")
strsql = "SELECT ST_AsTIFF(rast,'LZW') " "FROM (" "SELECT ST_Union(ST_Clip(rast,geom)) AS rast " "FROM " "staging.tmean_19 " "CROSS JOIN " "ST_MakeEnvelope(97.51,4326) As geom " "WHERE month=1 AND ST_Intersects(rast,geom)" ") AS rasttiff" cur.execute(strsql) # Fetch data as Python objects rasttiff = cur.fetchone() # Write data to file if rasttiff is not None: open('/home/theone/Desktop/tmean1.tif','wb').write(str(rasttiff[0])) # Close communication with the database cur.close() conn.close()
我们可以在QGIS中查看结果,并和原图进行对比: (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |