Write a file#

Create a synthetic RGB image with CuPy, place it on a map, and save it as a GeoTIFF. This exercise creates its own data and needs no input file.

Prepare the environment#

You need an NVIDIA GPU, CuPy and cugeo-raster built with the nvtiff native feature. Follow the GPU installation recipe using the nvTIFF build. Python extras supply dependencies; the native feature must also be enabled when building the package.

The blocks show one Python session. Lines beginning with >>> or ... are commands; the unprefixed lines are their output. Start Python from the repository root using the environment where you installed the GPU build. Keep the current CuPy device and stream unchanged during this exercise.

Create the pixels#

Make a 256-by-256 RGB image on the GPU. Red increases from left to right, green increases from top to bottom, and blue is their average. Multiply the values by 257 (65535 / 255) to span the UInt16 range from 0 to 65535:

>>> import cupy as cp
>>> rows, cols = cp.indices((256, 256), dtype=cp.uint16)
>>> pixels = cp.stack((cols, rows, (rows + cols) // 2)) * cp.uint16(257)
>>> pixels.shape
(3, 256, 256)

The axes are (bands, rows, columns). These are synthetic colour values, not observations of the location assigned below.

Synthetic RGB gradient, with red increasing to the right and green increasing downward.

The synthetic image displayed as RGB, scaling the full UInt16 range to the screen.#

Define the output grid#

To place the image on a map, we need two pieces of information:

  • a coordinate reference system (CRS) which defines what the map coordinates mean, including their units

  • a transform which maps pixels to positions in that CRS.

For our example, we will use the CRS EPSG:32610, which corresponds to UTM zone 10N, and place the upper-left corner at easting 500000, northing 4200000, with 10-metre square pixels:

>>> import cugeo.raster as raster
>>> crs = raster.CRS.from_epsg(32610)
>>> transform = raster.transform.from_origin(500000, 4200000, 10, 10)

Write the GeoTIFF#

Open the destination in write mode ("w"), passing the array dimensions, sample type and creation options directly to raster.open():

>>> from pathlib import Path
>>> output_path = Path("build/raster-tutorials/synthetic.tif")
>>> output_path.parent.mkdir(parents=True, exist_ok=True)
>>> with raster.open(
...     output_path, "w",
...     driver="GTiff",
...     width=pixels.shape[2], height=pixels.shape[1],
...     count=pixels.shape[0], dtype=pixels.dtype,
...     transform=transform, crs=crs,
...     photometric="RGB", compress="none",
... ) as dst:
...     dst.write(pixels)
...
>>> dst.closed
True

Alongside the CRS and transform defined above, these options describe the file:

Option

Meaning

driver="GTiff"

Write a GeoTIFF.

width=pixels.shape[2], height=pixels.shape[1]

Take the number of columns and rows from the array’s spatial dimensions.

count=pixels.shape[0]

Take the number of bands from the array’s first axis.

dtype=pixels.dtype

Store samples with the same data type as the array.

photometric="RGB"

Interpret the three bands as red, green and blue so image viewers display them as RGB.

compress="none"

Store the pixels without compression.

write() consumes the complete three-band array. The writer uses nvTIFF to encode CUDA input and publishes the file when the context manager closes successfully. Re-running the block replaces the output file. File I/O uses host memory; GPU encoding does not imply GPUDirect Storage.

Inspect the file#

The file has been written when the with block finishes. Inspect its name and size on disk:

>>> print(f"{output_path.name}: {output_path.stat().st_size:,} bytes")
synthetic.tif: 393,708 bytes

You have written a three-band UInt16 RGB GeoTIFF with a 256-by-256 grid, 10-metre pixels and CRS EPSG:32610. The file size includes the encoded pixel data and metadata; it can vary with encoder versions and creation options.

See the write how-to for other creation options and the capability matrix for supported formats and sample types.