I'm trying to plot a shapefile directly from PostGIS using Python. My shapefile is already stored in PostgreSQL. I know how to connect Python with PostgreSQL, but I just can't find anything that helps me plot my data into a map. I read somewhere that I should connect to PostgreSQL, query my shapefile table, select the geom attribute, store it into a geodataframe and then plot it.
Here's the code I'm using. Any ideas??
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()