我尝试使用Python直接从PostGIS绘制shapefile。我的shapefile已经存储在PostgreSQL中。我知道如何将Python与PostgreSQL连接起来,但我找不到任何有助于我将数据绘制成地图的工具。我在某个地方读到应该连接到PostgreSQL,查询shapefile表,选择geom属性,将其存储到geodataframe中,然后绘制它。在
这是我使用的代码。有什么想法吗??在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()