Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Python GeoSpatial Analysis Essentials

You're reading from   Python GeoSpatial Analysis Essentials Process, analyze, and display geospatial data using Python libraries and related tools

Arrow left icon
Product type Paperback
Published in Jun 2015
Publisher
ISBN-13 9781782174516
Length 200 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
Erik Westra Erik Westra
Author Profile Icon Erik Westra
Erik Westra
Arrow right icon
View More author details
Toc

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

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image