Reproject an image#
Find a sample pixel’s longitude and latitude, then reproject the RGB image from its local UTM grid to Equal Earth and compare the two images.
Use the GPU installation from Read a file.
Both cugeo-raster and cugeo-proj need CUDA support; the installation recipe
there builds matching libraries. Start a new Python session from the
repository root and run the sample download snippet
to define sample_dir and source_path. Keep the temporary directory alive
and the current CuPy device and stream unchanged throughout this exercise.
Read the source grid#
Use a with block to open the dataset and close it automatically when the
block ends. The pixel array and metadata collected inside the block remain
available for the following steps.
>>> import cugeo.raster as raster
>>> with raster.open(source_path) as src:
... pixels = src.read()
... source_crs = src.crs
... source_transform = src.transform
... nodata = src.nodata
...
>>> source_crs.to_string()
'EPSG:32618'
Find longitude and latitude#
Find the centre of pixel (500, 500), then use cugeo-proj to transform that position to EPSG:4326. The EPSG registry assigns numeric codes to coordinate reference systems; code 4326 identifies the WGS 84 geographic CRS. In this transform, X represents longitude and Y represents latitude, both measured in degrees rather than distances in metres.
Pass CuPy coordinate arrays to run the transform on the GPU:
>>> import cupy as cp
>>> import cugeo.proj as proj
>>> x, y = raster.transform.xy(source_transform, 500, 500)
>>> geographic_crs = proj.CRS.from_epsg(4326)
>>> with proj.Transformer.from_crs(source_crs, geographic_crs) as to_lonlat:
... longitude, latitude = to_lonlat.transform(cp.asarray([x]), cp.asarray([y]))
...
>>> round(float(longitude[0]), 6), round(float(latitude[0]), 6)
(-77.439751, 24.183849)
The position is approximately 77.44° west, 24.18° north.
Creating the one-element CuPy arrays transfers this coordinate pair from the
host; converting the results to float waits and retrieves the two values.
Transforming a coordinate does not move or resample the image’s pixels. Reprojecting the raster also creates a new pixel grid and samples the source onto it.
Reproject the pixels#
Use Equal Earth (EPSG:8857), a world projection that preserves area while changing shapes and angles. Its effect on the image’s footprint and orientation is easier to see than reprojection to longitude and latitude at this location.
Create a destination array covering the image in Equal Earth coordinates. This grid has 875 columns and 812 rows, with 300-metre spacing and its upper-left corner at X = −7,258,000 m, Y = 3,228,000 m:
>>> target_crs = raster.CRS.from_epsg(8857)
>>> target_transform = raster.transform.from_origin(-7258000, 3228000, 300, 300)
>>> destination = cp.empty((pixels.shape[0], 812, 875), dtype=pixels.dtype)
>>> reprojected, reprojected_transform = raster.warp.reproject(
... pixels, destination,
... src_transform=source_transform, src_crs=source_crs,
... src_nodata=nodata,
... dst_crs=target_crs, dst_transform=target_transform,
... resampling=raster.enums.Resampling.nearest,
... )
>>> reprojected.shape
(3, 812, 875)
>>> reprojected_transform
Affine(300.0, 0.0, -7258000.0,
0.0, -300.0, 3228000.0)
The destination array provides GPU storage and fixes the output dimensions; the destination transform locates those pixels in the target CRS. Each column adds 300 metres to X, and each row subtracts 300 metres from Y. These are distances on the projected map: projection distortion means they do not represent uniform ground distances everywhere.
Nearest-neighbour sampling selects source sample values without averaging
them. src_nodata=0 tells this operation that a source pixel is invalid when
all three bands are zero. Uncovered output pixels are filled with that same
nodata value. cugeo-proj supplies the coordinate transforms and cugeo-raster
samples the GPU pixel arrays.
Both images show the same place, using the original 0–255 RGB display range. Their aspect ratios are preserved so you can compare the footprint and the orientation of the islands.
Continue with the grid how-to to choose a grid matching another raster, or calculate statistics over valid pixels to exclude missing data from a calculation.
To save a pixel array with its CRS and transform, follow Write a file.