I am relatively new to Python and programming and trying to create a shapefile using multiple WKBs queried from a PostGIS database. I am using psycopg2 to query the database and using shapely and fiona for format conversion. The output I am looking for is a shapefile with multiple polygons (rows) based on how many WKBs were queried. However, I am getting an error: "TypeError: 'Polygon' object is not iterable".
import psycopg2
from shapely.wkb import loads
from shapely.geometry import MultiPolygon, mapping
import fiona
from fiona.crs import from_epsg
def main():
try:
print("Connecting to database...\n")
db_connection = psycopg2.connect(host = "server.com", dbname = "database", user = "postgres", password = "postgres")
print("Connected to database\n")
except:
print("Unable to connect to database")
db_cursor = db_connection.cursor()
try:
db_cursor.execute("SELECT feature FROM table LIMIT 10")
except:
print("Unable to query")
def wkb_to_shapefile(db_wkb, db_wkb_list, schema):
for row in db_wkb:
db_wkb_list.append(row)
image_wkb = db_wkb_list[0][10]
load_wkb = loads(image_wkb, hex = True)
print(load_wkb)
geometry = list(load_wkb)
print(geometry)
with fiona.open("output/shapefile.shp", "w", crs = from_epsg(4326), driver = "ESRI Shapefile", schema = schema) as output:
output.write({"properties": {"feature": ""},
"geometry": mapping(geometry)})
db_rows_list = []
db_rows = db_cursor.fetchall()
shapefile_schema = {"geometry": "MultiPolygon", "properties": {"feature": ""}}
wkb_to_shapefile(db_rows, db_rows_list, shapefile_schema)
db_cursor.close()
db_connection.close()
if __name__ == "__main__":
main()