Sedona loader are available in Scala, Java and Python and have the same APIs.
Raster format.
By default, these functions uses lon/lat order.
Loading raster using the raster loader
Theraster loader reads raster data from binary files as out-of-database (out-db) rasters then splits that raster data into smaller tiles.
- Scala
- Java
- Python
rast: The raster data inRasterformat. This is an out-db raster tile that references to the original raster data file.x: The 0-based x-coordinate of the tile. This column only presents when retile is not disabled.y: The 0-based y-coordinate of the tile. This column only presents when retile is not disabled.
option("retile", "false"), or specify the tile size manually using options such as option("tileWidth", "256") and option("tileHeight", "256").
The options for the raster loader are as follows:
retile: Enables tiling. Default istrue.tileWidth: The width of the tile. If not specified, the size of internal tiles will be used.tileHeight: The height of the tile. If not specified, will usetileWidthiftileWidthis explicitly set, otherwise the size of internal tiles will be used.padWithNoData: Pad the right and bottom of the tile with NODATA values if the tile is smaller than the specified tile size. Default isfalse.autoRescale: Whether to rescale the pixel values using the scale and offset values in the GeoTIFF file. Default isfalse.
If the internal tiling scheme of raster data does not conform to tiling, the
raster loader will throw an error. You can disable automatic tiling using option("retile", "false"), or specify the tile size manually to workaround this issue. A better solution is to translate the raster data into COG format using gdal_translate or other tools.raster loader also works with Spark generic source file options, such as option("pathGlobFilter", "*.tif*") and option("recursiveFileLookup", "true"). For instance, you can load all the .tif files recursively in a directory using
raster loader uniquely handles paths ending in / by performing a recursive search for files. This is equivalent to omitting / from the path and setting option("recursiveFileLookup", "true").
The DataFrame loaded by the raster loader will be automatically repartitioned by default, this is for evenly distributing the workload of processing raster tiles to the entire cluster. The number of partitions is proportional to the number of executor CPU cores in the cluster. You can disable auto repartitioning by setting the Spark session configuration spark.sedona.raster.load.autoRepartition to false. If you want to manually specify the number of partitions, you can set the Spark session configuration spark.sedona.raster.load.numPartitions to the desired number of partitions.
Loading raster using binaryFile loader (Deprecated)
Step 1: Load raster to a binary DataFrame
You can load any type of raster data using the code below. Then use the RS constructors below to create a Raster DataFrame.Step 2: Create a raster type column
After loading the raster data files usingbinaryFile loader, you can either use RS_FromPath to load the raster as an out-db raster, or use one of RS_FromGeoTiff, RS_FromArcInfoAsciiGrid and RS_FromNetCDF to load the binary data of the raster file as an in-db raster.
Loading raster files as out-db raster using RS_FromPath
We can drop the content binary column to avoid reading the content of the file entirely when using RS_FromPath to load out-db rasters.
Loading raster content as in-db raster using RS_FromGeoTiff
We’ll use the content binary column to load in-db raster. This requires loading the entire raster file into memory.
Raster Loading Functions
RS_FromArcInfoAsciiGrid
Introduction: Returns a raster geometry from an Arc Info Ascii Grid file. Format:RS_FromArcInfoAsciiGrid(asc: ARRAY[Byte])
SQL example:
RS_FromGeoTiff
Introduction: Returns a raster geometry from a GeoTiff file. Format:RS_FromGeoTiff(content: ARRAY[Byte], autoRescale: Boolean = true)
contentis a byte array that contains the content of the GeoTiff file.autoRescaleis an optional parameter that specifies whether to rescale the pixel values using the scale and offset values in the GeoTiff file. The default value istrue.
RS_FromPath
You can load rasters from paths. Rasters loaded in this way are called “out-db” rasters. Out-db rasters hold references to raster files instead of holding the actual pixel data. Out-db rasters can be used interchangeably with ordinary rasters. The only difference is that out-db rasters will load raster files in a deferred manner. Pixel data won’t be loaded until pixel values were accessed by functions such asRS_Value or RS_BandAsArray. It is more appropriate to load large raster files as out-db rasters.
Introduction: Returns an out-db raster from path to image file. Currently, it supports loading GeoTiff files (*.tiff or *.tif) and Arc Info Ascii Grid files (*.asc).
Additional parameters for configuring the Hadoop file system can be passed in as a ; delimited string. For example, fs.s3a.access.key=xxx;fs.s3a.secret.key=xxx. To load
GeoTiff files without automatic rescaling, please add raster.reader.auto-rescale=false to the parameters.
RS_FromPath will load the metadata of the raster file immediately when eagerLoadMetadata is set to true, and report any errors encountered
reading the raster file, otherwise it will only keep the path to raster file without loading it, until the metadata of the raster is actually needed.
The default value of eagerLoadMetadata is false.
Format: RS_FromPath(path: String)
Format: RS_FromPath(path: String, params: String)
Format: RS_FromPath(path: String, params: String, eagerLoadMetadata: Boolean)
SQL example:
RS_MakeEmptyRaster
Introduction: Returns an empty raster geometry. Every band in the raster is initialized to0.0.
Format:
- NumBands: The number of bands in the raster. If not specified, the raster will have a single band.
- BandDataType: Optional parameter specifying the data types of all the bands in the created raster.
Accepts one of:
- “D” - 64 bits Double
- “F” - 32 bits Float
- “I” - 32 bits signed Integer
- “S” - 16 bits signed Short
- “US” - 16 bits unsigned Short
- “B” - 8 bits unsigned Byte
- Width: The width of the raster in pixels.
- Height: The height of the raster in pixels.
- UpperleftX: The X coordinate of the upper left corner of the raster, in terms of the CRS units.
- UpperleftY: The Y coordinate of the upper left corner of the raster, in terms of the CRS units.
- Cell Size (pixel size): The size of the cells in the raster, in terms of the CRS units.
- NumBands: The number of bands in the raster. If not specified, the raster will have a single band.
- BandDataType: Optional parameter specifying the data types of all the bands in the created raster.
Accepts one of:
- “D” - 64 bits Double
- “F” - 32 bits Float
- “I” - 32 bits signed Integer
- “S” - 16 bits signed Short
- “US” - 16 bits unsigned Short
- “B” - 8 bits Byte
- Width: The width of the raster in pixels.
- Height: The height of the raster in pixels.
- UpperleftX: The X coordinate of the upper left corner of the raster, in terms of the CRS units.
- UpperleftY: The Y coordinate of the upper left corner of the raster, in terms of the CRS units.
- ScaleX: The scaling factor of the cells on the X axis
- ScaleY: The scaling factor of the cells on the Y axis
- SkewX: The skew of the raster on the X axis, effectively tilting them in the horizontal direction
- SkewY: The skew of the raster on the Y axis, effectively tilting them in the vertical direction
- SRID: The SRID of the raster. Use 0 if you want to use the default Cartesian coordinate system. Use 4326 if you want to use WGS84.
If any other value than the accepted values for the bandDataType is provided, RS_MakeEmptyRaster defaults to double as the data type for the raster.
RS_MakeRaster
Introduction: Creates a raster from the given array of pixel values. The width, height, geo-reference information, and the CRS will be taken from the given reference raster. The data type of the resulting raster will be DOUBLE and the number of bands of the resulting raster will bedata.length / (refRaster.width * refRaster.height).
Format: RS_MakeRaster(refRaster: Raster, bandDataType: String, data: ARRAY[Double])
- refRaster: The reference raster from which the width, height, geo-reference information, and the CRS will be taken.
- bandDataType: The data type of the bands in the resulting raster. Please refer to the
RS_MakeEmptyRasterfunction for the accepted values. - data: The array of pixel values. The size of the array cannot be 0, and should be multiple of width * height of the reference raster.
RS_AsInDB
Introduction: Converts an out-of-database (out-db) raster to an in-database (in-db) raster, facilitating raster data management within the database. This function is useful for scenarios where raster data initially stored outside the database needs to be managed within the database, enhancing data integrity and access efficiency. Format:RS_BandPath
Introduction: Retrieves the file path of an out-of-database (out-db) raster, providing a link to the external raster file it references. Primarily used with out-db rasters to access their storage location. Useful in scenarios involving out-db rasters, where only the raster path and geo-referencing metadata are stored in the database. Format:RS_FromNetCDF
Introduction: Returns a raster geometry representing the given record variable short name from a NetCDF file. This API reads the array data of the record variable in memory along with all its dimensions Since the netCDF format has many variants, the reader might not work for your test case, if that is so, please report this using the public forums. This API has been tested for netCDF classic (NetCDF 1, 2, 5) and netCDF4/HDF5 files. This API requires the name of the record variable. It is assumed that a variable of the given name exists, and its last 2 dimensions are ‘lat’ and ‘lon’ dimensions respectively. If this assumption does not hold true for your case, you can choose to pass the lonDimensionName and latDimensionName explicitly. You can use RS_NetCDFInfo to get the details of the passed netCDF file (variables and its dimensions). Format 1:RS_FromNetCDF(netCDF: ARRAY[Byte], recordVariableName: String)
Format 2: RS_FromNetCDF(netCDF: ARRAY[Byte], recordVariableName: String, lonDimensionName: String, latDimensionName: String)
SQL Example:
RS_NetCDFInfo
Introduction: Returns a string containing names of the variables in a given netCDF file along with its dimensions. Format:RS_NetCDFInfo(netCDF: ARRAY[Byte])
SQL Example:

