O'Reilly logo

Python Geospatial Analysis Essentials by Erik Westra

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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() ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required