我试图使用Python直接从PostGIS绘制shapefile。我的shapefile已存储在PostgreSQL中。我知道如何将Python与PostgreSQL连接起来,但我无法找到任何可以帮助我将数据绘制到地图中的内容。我在某处读到了我应该连接到PostgreSQL,查询我的shapefile表,选择geom属性,将其存储到地理数据帧中然后绘制它。
这是我正在使用的代码。任何想法??
import psycopg2
import geopandas as gpd
import matplotlib.pyplot as plt
try:
conn = psycopg2.connect("dbname='strokes' user='postgres' host='localhost' password='admin'")
except:
print "I am unable to connect to the database"
cur = conn.cursor()
cur.execute("CREATE INDEX bassin_index ON bassin USING GIST(geom)")
cur.execute("SELECT st_astext(geom) AS wkt, fid_limite, codebassin FROM bassin")
rows = cur.fetchall()
rows_list=[]
for geom,fid_limite,codebassin in cursor:
data={'codebassin':codeb,'fid_limite':fidlim,'geom':geo}
rows_list.append(data)
gdf=gpd.GeoDataFrame(rows_list).set_index('codebassin')
gdf.head()
gdf.plot(column='fid_limite', scheme='QUANTILES', k=5, colormap='gray')
conn.commit()
conn.close()