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
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
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
 Westra Westra
Author Profile Icon Westra
Westra
Arrow right icon
View More author details
Toc

Querying spatial data


Let's write another Python program to perform various queries against the contents of our database. Start by creating another Python file named query_data.py and place it into the world_borders directory. We start by importing the psycopg2 library, opening up a connection to our database, and creating a database cursor:

import psycopg2
connection = psycopg2.connect(database="world_borders", user="...", password="...")
cursor = connection.cursor()

This should all be familiar from the create_table.py program we created earlier.

Let's now perform a simple (non-spatial) database query, just to see how it works. Add the following to the end of your program:

cursor.execute("SELECT id,name FROM borders ORDER BY name")
for row in cursor:
    print row

When you run your query_data.py program, you should see a list of the record IDs and associated names, taken from your borders table:

(1264, 'Afghanistan')
(1237, 'Albania')
(1235, 'Algeria')
...

Notice that you use cursor.execute...

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 $15.99/month. Cancel anytime
Visually different images