GeoFile is a series dedicated to looking at geographical data, its features, and uses. In this article, we'll look at raster data and how you can use shapefiles and rasters to give you other perspectives on your maps by superimposing georeferenced images onto your maps.
If you're familiar with satellite images, thermal imagery, or even perhaps elevation features on a map, you most likely have been exposed to raster data. Raster data can help identify changes on maps, show more detail than what shapefiles can provide, show environmental changes over time, and a whole lot more.
In this article, we'll just cover the surface of what raster data can provide by looking at what raster data is, how to gather information about your raster data with the GDAL utility, how to import and view raster data into a PostgreSQL database using
raster2pgsql, and how to superimpose it onto a shapefile with QGIS.
What is raster data?
Raster data is basically a collection of pixels, or cells, organized into rows and columns with geospatial information that forms tiles. These pixels are sometimes given attribute values, which define geographic features, have assigned SRIDs, and have references to location coordinates, referred to as georeferenced rasters.
At the same time, attribute values can be added, which are referred to as bands that hold the actual raster data. For example, if we take a map of a town and break that up into smaller sections, or tiles, we could record some information about each section, or cell, within its own band.
We see raster data every time we look at satellite imagery on an online map, or when looking at real-life representations of temperature, elevation, or topographical features on a map. Raster data can also be in the form of scanned maps or images that lie over another map to show historical changes, for instance.
PostGIS and GDAL
One of the essential tools for using raster data with PostGIS is GDAL (Geospatial Data Abstraction Library) that comprises a suite of utilities for raster and vector geospatial data.
While you probably will not touch all of the utilities that GDAL comes with, the most popular ones are
gdalinfo allows you retrieve information about the raster, which is usually in the form of a
.tif image file. It's particularly useful when trying to figure out what the SRID of the raster is before importing it into PostgreSQL.
gdal_translate allows you to export your raster data out of PostGIS and into other formats, and it allows you to set the size of your raster to your requirements.
gdalwarp allows you to change the SRID of your raster data, which
gdal_translate doesn't do.
Superimposing a raster onto a shapefile
Raster data is really interesting when we start combining the images with shapefiles. If you're not familiar with shapefiles, see this article which discusses how to import them to your Compose PostgreSQL database and use them.
To give you an example, we'll take a map of Seattle and then change the SRID to 4326 using
gdalwarp. We'll then use
raster2pgsql which comes with PostGIS to import the raster data and use
shp2pgsql to import our shape file into our Compose PostgreSQL database. Then we'll use QGIS to connect to Compose PostgreSQL and view our shapefile with the superimposed Seattle map.
Database set up
We are assuming that you have a Compose PostgreSQL deployment set up and that you have installed the PostGIS extension. If you haven't got a deployment setup, follow the steps in the help documentation.
After you're in your deployment's console, select Browser from the side menu to create a new database. Once you've done that, click on your database and select Extensions from the side menu. Scroll down to PostGIS and click the install button on the left.
Importing the shapefile
After you've created a database, you'll want to download the shapefile that contains the map that we want to use. In this case, we'll use the US Census map for the United States which can be downloaded here. We'll then import that into our PostgreSQL database using the
shp2pgsql command that comes with PostGIS. A detailed tutorial on how to import a shapefile into PostgreSQL can be found here.
Warping the raster
Now that the shapefile has been imported, we want to download the raster data for Seattle. The file that you should download is "f47122e1" located here.
Once you've downloaded the file, you'll need to inspect it so that you know what SRID it uses. It is important to have the same projections for both the shapefile and your raster data. If you don't then you run the risk of skewing the raster data when superimposing it onto the shapefile map.
To inspect the SRID of the raster data, use
gdalinfo followed by the path of your
.tif file like:
This will give you some information like the following:
Driver: GTiff/GeoTIFF Files: /Users/alger/path/seattle/seattle.tif /Users/alger/path/seattle/seattle.tfw Size is 7413, 5517 Coordinate System is: PROJCS["NAD27 / UTM zone 10N", GEOGCS["NAD27", DATUM["North_American_Datum_1927", SPHEROID["Clarke 1866",6378206.4,294.9786982139006, AUTHORITY["EPSG","7008"]], AUTHORITY["EPSG","6267"]], PRIMEM["Greenwich",0], UNIT["degree",0.0174532925199433], AUTHORITY["EPSG","4267"]], PROJECTION["Transverse_Mercator"], PARAMETER["latitude_of_origin",0], PARAMETER["central_meridian",-123], PARAMETER["scale_factor",0.9996], PARAMETER["false_easting",500000], PARAMETER["false_northing",0], UNIT["metre",1, AUTHORITY["EPSG","9001"]], AUTHORITY["EPSG","26710"]] Origin = (500002.512999999977183,5316564.759999999776483) Pixel Size = (10.160000000000000,-10.160000000000000) Metadata: AREA_OR_POINT=Area TIFFTAG_RESOLUTIONUNIT=1 (unitless) TIFFTAG_SOFTWARE=Arc/Info TIFFTAG_XRESOLUTION=0.097999997 TIFFTAG_YRESOLUTION=0.097999997 Image Structure Metadata: COMPRESSION=PACKBITS INTERLEAVE=BAND
What we're really interested in here is this section that tells us that the projection is EPSG 4267:
DATUM["North_American_Datum_1927", ... AUTHORITY["EPSG","4267"]],
Since the shapefile uses EPSG 4326 and our raster data uses EPSG 4267, we can change the raster SRID to use EPSG 4326. GDAL provides us with the
gdalwarp utility that will change the SRID of the raster like:
gdalwarp -t_srs 'EPSG:4326' /Users/alger/path/seattle/seattle.tif /Users/alger/path/seattle/seattle2.tif
This will create another file called
seattle2.tif within your folder.
Now, we'll want to import the file into our Compose PostgreSQL database. This is done with PostGIS's
raster2pgsql command, which is an import/export tool for raster data.
To use the command, write the following in your terminal:
raster2pgsql -s 4326 -F /Users/alger/path/seattle/seattle2.tif -n ColumnName -I -C | psql "sslmode=require host=aws-us-east-2-portal.1.dblayer.com port=98989 dbname=compose user=admin"
When importing a raster, make sure that you set the SRID number to whatever it's encoded in otherwise it defaults to 0 if it doesn't know the SRID. Since we used
gdalinfo and changed the SRID with
gdalwarp to EPSG 4326, we simply use 4326 in the command. The switch
-F is the path of your
-n allows you to set the column name of the raster data, and
-I creates a gist index on the raster column.
-C sets constraints on the raster column. After the configuration has been set up, we use the Compose PostgreSQL connection string to make a connection to our database and enter the deployment's password when prompted.
Let's view with QGIS
If you haven't got a copy of QGIS to view your GIS data, you can download it free here for any platform you're using.
Since your shapefile and raster data has been imported into your Compose PostgreSQL database, we can use QGIS to connect to our database and start to superimpose our raster data onto our map.
You will first want to set up a database connection using Layer > Add Layer > Add PostGIS Layers. Here, you'll set up a new connection by entering your Compose PostgreSQL credentials which are located on your deployment's console.
After you've set up a connection to the databases that contain your raster data and your shapefile, you add the shapefile layer first, giving you:
then you add the raster data which will line up perfectly with the map like:
If we have a closer look, you'll see that the raster data lines up pretty well with the map.
Summing it up
We looked at what raster data is, how to change the SRID so that it aligns with our shapefile, and looked at how the shapefile and the raster data complement each other. While you might not use raster data for every GIS application, they are useful when you want to superimpose data on top of a map; for example, to show differences between two maps, or to add more details to a map. Nevertheless, it's always fun to see the different ways that maps can help not just to get from point A to point B, but they can also tell a story. Try out using raster data in your next application with Compose PostgreSQL.
If you have any feedback about this or any other Compose article, drop the Compose Articles team a line at email@example.com. We're happy to hear from you.