> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wherobots.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Raster Writers

<Note>
  Sedona writers are available in Scala. Java and Python have the same APIs.
</Note>

## Write Raster DataFrame to raster files

To write a Sedona Raster DataFrame to raster files, you need to (1) first convert the Raster DataFrame to a binary DataFrame using `RS_AsXXX` functions and (2) then write the binary DataFrame to raster files using Sedona's built-in `raster` data source.

### Write raster DataFrame to a binary DataFrame

You can use the following RS output functions (`RS_AsXXX`) to convert a Raster DataFrame to a binary DataFrame. Generally the output format of a raster can be different from the original input format. For example, you can use `RS_FromGeoTiff` to create rasters and save them using `RS_AsArcGrid`.

| Function                                                                 | Description                                           |
| ------------------------------------------------------------------------ | ----------------------------------------------------- |
| [RS\_AsGeoTiff](/reference/wherobots-db/raster-data/output/RS_AsGeoTiff) | Convert a raster to GeoTiff binary format             |
| [RS\_AsArcGrid](/reference/wherobots-db/raster-data/output/RS_AsArcGrid) | Convert a raster to Arc Info ASCII Grid binary format |
| [RS\_AsPNG](/reference/wherobots-db/raster-data/output/RS_AsPNG)         | Convert a raster to PNG binary format                 |

### Write a binary DataFrame to raster files

Introduction: You can write a Sedona binary DataFrame to external storage using Sedona's built-in `raster` data source. Note that: `raster` data source does not support reading rasters. Please use Spark built-in `binaryFile` and Sedona RS constructors together to read rasters.

Available options:

* rasterField:
  * Default value: the `binary` type column in the DataFrame. If the input DataFrame has several binary columns, please specify which column you want to use. You can use one of the `RS_As*` functions mentioned above to convert the raster objects to binary raster file content to write.
  * Allowed values: the name of the to-be-saved binary type column
* fileExtension
  * Default value: `.tiff`
  * Allowed values: any string values such as `.png`, `.jpeg`, `.asc`
* pathField
  * No default value. If you use this option, then the column specified in this option must exist in the DataFrame schema. If this option is not used, each produced raster image will have a random UUID file name.
  * Allowed values: any column name that indicates the paths of each raster file
* useDirectCommitter
  * Default value: `true`. If set to `true`, the output files will be written directly to the target location. If set to `false`, the output files will be written to a temporary location and finally be committed to their target location. It is usually slower to write large amount of raster files with `useDirectCommitter` set to `false`, especially when writing to object stores such as S3.
  * Allowed values: `true` or `false`

The schema of the Raster dataframe to be written can be one of the following two schemas:

```html theme={"system"}
root
 |-- raster_binary: binary (nullable = true)
```

or

```html theme={"system"}
root
 |-- raster_binary: binary (nullable = true)
 |-- path: string (nullable = true)
```

SQL example 1:

```scala theme={"system"}
// Assume that df contains a raster column named "rast"
df.withColumn("raster_binary", expr("RS_AsGeoTiff(rast)"))\
  .write.format("raster").mode("overwrite").save("my_raster_file")
```

SQL example 2:

```scala theme={"system"}
// Assume that df contains a raster column named "rast" and a string column named "path"
df.withColumn("raster_binary", expr("RS_AsGeoTiff(rast)"))\
  .write.format("raster")\
  .option("rasterField", "raster_binary")\
  .option("pathField", "path")\
  .option("fileExtension", ".tiff")\
  .mode("overwrite")\
  .save("my_raster_file")
```

The produced file structure will look like this:

```html theme={"system"}
my_raster_file
- part-00000-6c7af016-c371-4564-886d-1690f3b27ca8-c000
	- test1.tiff
	- .test1.tiff.crc
- part-00001-6c7af016-c371-4564-886d-1690f3b27ca8-c000
	- test2.tiff
	- .test2.tiff.crc
- part-00002-6c7af016-c371-4564-886d-1690f3b27ca8-c000
	- test3.tiff
	- .test3.tiff.crc
- _SUCCESS
```

To read it back to Sedona Raster DataFrame, you can use the following command (note the `*` in the path):

```scala theme={"system"}
sedona.read.format("binaryFile").load("my_raster_file/*")
```

Then you can create Raster type in Sedona like this `RS_FromGeoTiff(content)` (if the written data was in GeoTiff format).

The newly created DataFrame can be written to disk again but must be under a different name such as `my_raster_file_modified`

### Write Geometry to Raster dataframe

You can use [RS\_AsRaster](/reference/wherobots-db/raster-data/operators/RS_AsRaster) to convert a Geometry to a Raster dataset. Unlike `RS_Clip`, which extracts a subset of an existing raster while preserving its original values, `RS_AsRaster` generates a new Raster where the Geometry is rasterized onto a raster grid.
