Postgresql 如何从IPython笔记本中查询存储的PostGIS光栅数据
测试数据Postgresql 如何从IPython笔记本中查询存储的PostGIS光栅数据,postgresql,pandas,sqlalchemy,postgis,geoalchemy,Postgresql,Pandas,Sqlalchemy,Postgis,Geoalchemy,测试数据 (159KB) 目标三: 使用raster2pgsql将光栅加载到PostGIS中,并在QGIS中可视化 在我的IPython笔记本中,连接到PostGIS并将光栅加载到NumPy阵列中 在我的IPyhton笔记本中,使用Pandas加载一个时间序列,其中一个像素的光栅具有存储在PostGIS中的不同时间步长 迄今为止的进程 我已经使用raster2pgsql命令将一个光栅图像导入PostGIS光栅,并使用DB管理器在QGIS中将其可视化: raster2pgsql -s 4326
- (159KB)
raster2pgsql
将光栅加载到PostGIS中,并在QGIS中可视化raster2pgsql
命令将一个光栅图像导入PostGIS光栅,并使用DB管理器在QGIS中将其可视化:
raster2pgsql -s 4326 -d -I -C -M -R -l 4 D:\Downloads\raster//modis.tif -F -t 100x100 public.ndvi | psql -U postgres -d rastertest -h localhost -p 5432
但是如何从IPython笔记本中访问/查询此光栅
我发现了这个,是关于炼金术和地炼金术的。其中提到它也支持PostGIS光栅。看起来很有趣!但是使用我不知道如何将其应用于光栅数据
我想我可以使用以下代码连接到我的PostGIS数据库,其中postgres=user
,password=admin
和database=rastertest
:
from sqlalchemy import create_engine
engine = create_engine('postgresql://postgres:admin@localhost/rastertest', echo=True)
但是。。
非常感谢您的建议。您应该使用psycopg模块从python连接到postgres db。一些代码示例:
import psycopg2
def connect_db():
try:
conn = psycopg2.connect("dbname='rastertest' user='admin' host='localhost' password='password'")
conn.set_session(autocommit=True) #if you want your updates to take effect without being in a transaction and requiring a commit, for a beginner, I would set this to True
return conn
except:
print "I am unable to connect to the database"
return None
def get_raster(raster_id,conn):
query= "SELECT ST_AsText(geom) from raster_table where id={}".format(raster_id)
conn.cursor().execute(query)
res = cur.fetchall()
return res[0][0]
也许光栅的文本表示法是可以使用的。
或者,在此处查看是否有任何函数返回您想要的numpy数组,并相应地替换get_光栅中的查询。(可能是这样)我不熟悉PostGIS,但如果最终它只是简单的旧SQL,您可能想尝试
ipython SQL
(),它允许您连接到数据库并使用%SQL和%%SQL magic在ipython笔记本中编写查询,完全避免ORM路径。你想在ipython笔记本中可视化光栅吗?你的帖子看起来很有趣。一定会试试的!