Working with Raster data
Raster data plays a crucial role in many geospatial applications, providing a powerful tool for visualizing and analyzing spatial patterns, trends, and relationships.
CARTO offers comprehensive support for raster data, empowering users to harness its capabilities for a wide range of analytics and visualization tasks.
This documentation guide delves into the process of working with raster data in CARTO, guiding you through the steps of preparing, uploading, analyzing, and visualizing raster data to extract meaningful insights from your spatial data.
Prepare your raster data
Before you can leverage the power of raster data in CARTO, it's essential to prepare your raster files for optimal performance and compatibility with the platform's raster table format. This section outlines the key considerations and steps involved in preparing your raster data for seamless integration with CARTO.
We'll elaborate on topics like resampling methods, specifying a nodata value, determining an appropriate block size, creating overviews for efficient access, and converting to Cloud-Optimized GeoTIFF (COG) format to enhance performance and scalability.
By following these guidelines, you'll ensure that your raster data is properly formatted and optimized for seamless integration with CARTO's raster capabilities.
For the following steps, we'll be using different GDAL tools that will allow us to prepare our raster to be imported in BigQuery.
Get information about the raster file
Use gdalinfo
to get information and metadata about your file that will be useful for debugging and preparation:
NODATA values
From the information above, we can observe a few things that might be wrong with our file. For example, the minimum value is -32767
which is the larger minimum number that can be represented with the Int16
type. This value is often used as NODATA
value in rasters. Other usual NODATA
values that might be distorting your data are -9999
, -999
, or -3.4e38
.
Setting the correct NODATA
value is a needed step in order to have our raster table correctly generated:
After this, checking our new raster_file_nodata_ok.tif
stats should produce the correct output:
Reproject your raster to EPSG:4326
In some cases, it is advisable to reproject your raster to EPSG:4326 before converting it into a Cloud Optimized GeoTiff. This is easily done with gdalwarp
Conversion to Cloud Optimized GeoTIFF
CARTO requires that raster files are in the Cloud Optimized GeoTIFF (COG) format. We will use GDAL's gdalwarp
tool to transform our raster to this projection using the Google Maps tiling scheme:
Resampling methods
Sometimes, the transformation introduces alterations our our data. For example, different resampling methods might produce different stats in our transformed raster. In this case, using the RESAMPLING=NEAREST
option produced a COG with the same stats as the original file:
Overviews
CARTO supports overviews by storing different resolutions on of the raster in the same table and making Maps API request them depending on the zoom level of the map. To ensure that they're generated when creating our COG, use the -co OVERVIEWS=IGNORE_EXISTING
option.
Alpha band
In terms of adding an alpha band to your COG, -co ADD_ALPHA=NO
is the safer general option. However, in some cases it's advisable to convert your NO_DATA values to an alpha band and use -co ADD_ALPHA=YES
instead.
gdalwarp
supports many other options when creating a COG. Take a look at the complete documentation of the COG driver to see all of them.
Upload your raster file
Once your raster data has been prepared for upload, you can utilize CARTO's Raster Loader utility to seamlessly integrate your raster data into the CARTO platform. This section guides you through the process of installing the Raster Loader, authenticating with your CARTO account, and executing the raster upload commands to effectively store your raster data in BigQuery as a table in the CARTO raster format.
Installation of Raster Loader
Raster Loader is a Python utility that can upload a COG raster file to BigQuery as a CARTO raster table.
The raster-loader
library can be installed from pip
like:
However, it's much advisable to use a virtual environment:
Find more exhaustive information about the installation of Raster Loader in the documentation.
Authentication
In order to create raster tables in BigQuery using Raster Loader, you will need to be authenticated in Google Cloud. Run this command:
Execution
Find a complete guide and reference at the Raster Loader documentation.
The basic command to upload a COG to BigQuery as a CARTO raster table is:
You could also add some options, like the resulting table name or overwrite/append existing tables with --overwrite
or --append
options, for example:
Options for raster bands
By default, Raster Loader will upload the first band in the raster file, but it's possible to specify a different band with a command like:
We could also assign a name to the band, like:
Uploading multiple bands, with (optionally) custom names would look like:
In this case, the order of the bands and their names is determinant: the first band will receive the first name; the second band will get the second name, etc.
Options for very large files
For large raster files, you can use the --chunk_size
flag to specify the number of rows to upload at once, and preventing BigQuery from showing you an exception like the following, due to excessive operations in the destination table:
The default chunk size is 1000 rows.
For example, the following command uploads the raster in chunks of 2000 rows:
Analysis with raster tables using the CARTO Analytics Toolbox
Once your raster data is stored as a BigQuery table, you can leverage the power of CARTO's Analytics Toolbox to perform advanced spatial analyses involving raster and vector data sources.
This section covers some key use cases for analyzing raster data, demonstrating how to extract raster values, perform spatial intersections, and aggregate raster data values using SQL and the procedures provided by the raster module in the CARTO Analytics Toolbox.
Check the complete SQL reference for the raster module.
Get values from the pixels that intersect with a specific point
Get values from the pixels that intersect with points from a table
Get a calculated value from different bands for each pixel that intersects with points on table
Get all pixels that intersect with a polygon and their band's values
Intersect with a polygon table and get aggregated data
Raster visualization with deck.gl
To effectively visualize and gain insights from your raster data, we'll explore the integration of CARTO's Maps API with deck.gl.
This section delves into the process of creating an interactive raster visualization using deck.gl, showcasing how to load a layer that displays raster data extracted from a CARTO raster table.
Find the complete reference in deck.gl's CARTO module documentation.
The following snippet shows how to load a layer coming from a CARTO raster table in BigQuery:
The most relevant part is the return
expression within the getFillColor
property:
getFillColor
expects an array of numbers that range from 0 to 255, to specify an RGBa color, following the format [r, g, b, [a]]
.
This provides a lot of flexibility when it comes to visualizing values from your raster, but you should make sure that the values will range from 0 to 255.
In this example, we're visualizing photovoltaic potential in Spain. The values of the band_1
are normalized so they range between 0-255, and the value is passed to the red channel, so the brighter the red, the higher the photovoltaic potential.
Last updated