You can also store polygons using PostGIS. The following code will create a new table with a single polygon:
from shapely.geometry import Polygon
connection = psycopg2.connect(database="pythonspatial",user="postgres", password="postgres")
cursor = conectionn.cursor()
cursor.execute("CREATE TABLE poly (id SERIAL PRIMARY KEY, location GEOMETRY)")
a=Polygon([(-106.936763,35.958191),(-106.944385,35.239293),
(-106.452396,35.281908),(-106.407844,35.948708)])
cursor.execute("INSERT INTO poly (location)
VALUES (ST_GeomFromText('{}'))".format(a.wkt))
connection.commit()
The previous code is almost identical to the Point and Line examples. Make the database connection and then get a cursor. Use execute() to create the table. Import shapely, construct your geometry and insert it into the table. Lastly, commit() the changes.
The previous examples selected everything from the database and drew the geometry in the Jupyter Notebook. The following code will skip those steps and instead return to the area of the polygon:
cur.execute("SELECT id, ST_Area(location::geography) from poly")
cur.fetchall()
Using ST_Area() and the geometry cast to geography, the previous code should return the following value in meters squared:
[(1, 3550790242.52023)]
Now that you know there is a polygon in the table, you can learn how to search for a point within a polygon.