Importing spatial data
Take a copy of the TM_WORLD_BORDERS-0.3
directory you downloaded earlier, and place it inside your world_borders
directory. Then create another Python script named import_data.py
. This is where you will place the code to import the data into your database.
We are going to use the OGR library to import the data from the shapefile, and psycopg2
to insert it into the database. So the first two lines in our program should look like the following:
import osgeo.ogr import psycopg2
We next need to open up a connection to the database. The code to do this is identical to the code that we used in the create_table.py
script:
connection = psycopg2.connect(database="world_borders", user="...", password="...") cursor = connection.cursor()
Tip
Don't forget to adjust the keyword parameters to psycopg2.connect()
to match the user account you need to connect to PostgreSQL.
We are now ready to start importing the data from the shapefile. First, though, we are going to delete the existing contents...