For the data to be processed and imported, a few new modules will be used. The pyshapefile module (or pyshp, imported as shapefile) is used to connect to the shapefiles and to read both the geometries and attribute data that they contain. The pygeoif module is a pure Python module that implements a protocol known as the geo_interface.
This protocol allows Python object-level introspection of geospatial data, for example, it converts geospatial data formats into Python objects. It will be used to convert between shapefile geometries stored in binary into WKT geometries that can be inserted into the database using the GeoAlchemy2 ORM:
# The pyshapefile module is used to read shapefiles and
# the pygeoif module is used to convert between geometry types
import shapefile
import pygeoif
To connect to the database and the tables, import the SQLAlchemy ORM and other SQLAlchemy functions:
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String, ForeignKey, Float
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import relationship
To add data to the geometry columns of the database tables, the GeoAlchemy2 Geometry data type will be used:
# The Geometry columns from GeoAlchemy2 extend the SQLAlchemy ORM
from geoalchemy2 import Geometry
To enable the script to find the downloaded shapefiles, use the Tkinter module and its filedialog method, as it is built into Python and is OS-agnostic:
# The built-in Tkinter GUI module allows for file dialogs
from tkinter import filedialog
from tkinter import Tk
Connections to the database will again be created using the create_engine function from SQLAlchemy. This section also generates a session using the session manager, binding it to the engine variable that connects to the database:
# Connect to the database called chapter11 using SQLAlchemy functions
conn_string = 'postgresql://postgres:password@localhost/chapter11'
engine = create_engine(conn_string)
Session = sessionmaker(bind=engine)
session = Session()
The session will allow for queries and commits (that is, writing to the database) to them being managed. We will need to query against database tables inside the for loop, to create the database relationships between the counties, districts, and states.
The database table models are again defined within the script, subclassing from the declarative_base class. These class definitions will match those within the last script.