Performing queries on rasters using PostgreSQL

With a raster loaded into PostgreSQL, you can query it using Python. The Python library for working with PostgreSQL is psycopg2. The following code will connect to the database where you loaded the TIF:

import psycopg2connection = psycopg2.connect(database="pythonspatial",user="postgres", password="postgres")cursor = connection.cursor()

The previous code imports psycopg2. It then makes a connection passing the database name, username, and password. Lastly, it gets a cursor object so that you can execute queries.

To see the raster in PostgreSQL, you can execute a select all, as shown in the following code:

cursor.execute("SELECT * from bigi") #Big I is the name of the intersection where I-25 and ...

Get Mastering Geospatial Analysis with Python now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.