Skip to content

Pitch: Use Terracotta to Serve Raster Data

Abhineet Tamrakar edited this page Jun 28, 2020 · 1 revision

The GCBM spatial output is a list of tiled, DEFLATE compressed GeoTIFF files. In this form, the raster data cannot be used in my project's web application.

Also, the associated non-spatial data is not embedded into this raster data. It is present in a seperate SQLite database.

To be able to create an interactive front-end, I need an easy way to be able to access this raster data along with its associated non-spatial data.

Terracotta

Terracotta is a tile server written in Python. It takes some raster data and then serves it through an easy to use HTTP API. This API can solve the problems that I mentioned above.

It can serve the various tiles of a raster in the form of PNGs, which can then be easily rendered in a browser. The nature of the API also allows us easily overlay these images onto a map, as shown in this preview.

The API has the ability to return any arbitrary metadata associated with a particular raster. This can solve my problem of accessing non-spatial data on the front-end.

Terracotta & GCBM Output

Terracotta is not exactly a library. It is a command line app. It is used to serve and explore a directory of spatial data. You pass it a filename pattern as an argument. It parses the directory based on it, categorizes the raster data and then starts a server.

This make it a great tool to explore GCBM's spatial data. For example, following are the instructions for using it with the sample data available here.

First, rename the files so that they follow a pattern that Terracotta can understand. In bash:

$ for f in *.*; do echo "$(echo $f | sed 's/_//g' | sed -r 's/(.*)([0-9][0-9][0-9][0-9])(.*)/\1_\2\3/')"; done

Next, start a Terracotta server by executing:

$ terracotta serve -r ./{name}_{year}.tif'

Finally, connect this server to Terracotta's Preview interface:

$ terracotta connect localhost:5000

This will open an interface in your web browser, which you can then use to view raster data overlayed onto a map.

Terracotta & My Project

As mentioned above, Terracotta is not a library. So, how will I use it in my project?

For that, I plan on doing two thins:

First, I'll use Terracotta's Python API and generate a database of raster data. This will allow me to add the non-spatial data as metadata. And also instead of having Terracotta parse a directory, this would allow me to pass it just a single database.

Second, even though Terracotta is not advertised as a library, its code still follows a well organized and modular structure. This means that all of its features can be invoked programmatically by calling the right function (which would be this one in my case) with the right arguments.

Conclusion

I also looked at other solutions like MapServer, before settling on Terracotta. None of them offered the combined benifts of Python, HTTP API, Raster Optimization Tools, etc.

So, in conclusion, Terracotta seemed to be the most pragmatic choice for my project.