RT_Convolution takes a raster column and a convolution kernel, and returns a raster column with the convolution kernel applied to each band of the input raster. Convolution is a spatial operation that applies a kernel (filter) to an input raster to produce an output raster where each pixel value is a weighted sum of its neighboring pixels.
You can specify one of the predefined kernels:
| Kernel type | Description |
|---|---|
| Gradient | Gradient kernels can be used for directional gradient detection in 45-degree increments. Available kernels are:
|
| Laplacian | Laplacian kernels can be used for edge enhancement. They are commonly applied after smoothing to reduce sensitivity to noise. Available kernels are:
|
| Line detection | Line detection kernels can be used for line detection in specific orientations. Better results may be achieved if smoothing is applied before edge detection. Available kernels are:
|
| Sobel | Sobel kernels are used for edge detection. Available kernels are:
|
| Sharpening | Sharpening (high-pass) kernels accentuate local differences between a pixel and its neighbors. A high-pass filter calculates the focal sum statistic for each cell of the input using a weighted kernel neighborhood. It brings out the boundaries between features, thus sharpening edges between objects. Available kernels are:
|
| Smoothing | Smoothing (low-pass) kernels smooth the data by reducing local variation and removing noise. The low-pass filter calculates the average (mean) value for each neighborhood. The effect is that the high and low values within each neighborhood are averaged out, reducing the extreme values in the data. Available kernels are:
|
| Point spread | The point spread kernel portrays the distribution of light from a point source through a lens. This will introduce a slight blurring effect. The available kernel is:
|
You can also define a custom kernel. Use custom as the kernel name and provide the number of columns, number of rows,
and an array of kernel values.
| Function | Syntax |
|---|---|
| Python | convolution(raster, kernel |
| SQL | RT |
| Scala | convolution(raster, kernel |
For more details, go to the GeoAnalytics Engine API reference for convolution.
Examples
from geoanalytics.raster import functions as RT
pixels = list(range(16))
df = spark.createDataFrame([[pixels]], ["pixels"]) \
.withColumn("raster", RT.create_raster("pixels", 4, 4, "int8"))
# Apply a predefined kernel
r1 = df.select(RT.convolution("raster", "smooth_3x3").alias("convolution"))
r1.withColumn("smooth_pixels", RT.band_values("convolution", 1)).show(truncate=False)
# Apply a custom kernel
kernel = [1, 1, 1,
1, 1, 1,
1, 1, 1]
r2 = df.select(RT.convolution("raster", "custom", 3, 3, kernel).alias("convolution"))
r2.withColumn("custom_pixels", RT.band_values("convolution", 1)).show(truncate=False)+-------------------------+-----------------------------------------------------------------------------------------------------+
|convolution |smooth_pixels |
+-------------------------+-----------------------------------------------------------------------------------------------------+
|SqlRaster(1x4x4, Float32)|[0.9375, 1.75, 2.5, 3.0625, 3.25, 5.0, 6.0, 6.75, 6.25, 9.0, 10.0, 10.75, 6.1875, 8.75, 9.5, 10.0625]|
+-------------------------+-----------------------------------------------------------------------------------------------------+
+-----------------------+------------------------------------------------------------------------------------------------+
|convolution |custom_pixels |
+-----------------------+------------------------------------------------------------------------------------------------+
|SqlRaster(1x4x4, Int16)|[10.0, 18.0, 24.0, 28.0, 27.0, 45.0, 54.0, 60.0, 51.0, 81.0, 90.0, 96.0, 42.0, 66.0, 72.0, 76.0]|
+-----------------------+------------------------------------------------------------------------------------------------+Version table
| Release | Notes |
|---|---|
2.2.0 | Python, SQL, and Scala functions introduced |