Creating Rasters from Scratch in PostGIS, Part 2

2017-03-24
database

Goal

In this post, we’ll learn how to create a multi-band raster from scratch using PostGIS. This ability will be particularly useful as we begin exploring the relatively uncharted waters of the PostGIS Raster Module.

Purpose:

This is a companion article for Creating Rasters from Scratch in PostGIS. I recommend reading it if you haven’t already to learn why you might want to do this as well as for a more detailed explanation of the parameters.

Getting Started

Let’s look at the code from our last post:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-- Insert into our newly created table
-- Insert into that table
INSERT INTO public.test_250(rast)
SELECT ST_AddBand(
-- Make empty raster
ST_MakeEmptyRaster(
1500, -- Raster width x (in pixels)
1500, -- Raster width y (in pixels)
-105.4330444336, -- Upper left X coordinate
40.7170785158, -- Upper left Y coordinate
0.00208333, -- X Cell Resolution (in degrees) (~250m)
0.00208333, -- Y Cell Resolution (in degrees) (~250m)
0, -- X skew
0, -- Y skew
4326 -- SRID (WGS 84)
),
-- We're making a single band raster, but you can add
-- as many bands as you like by adding additional rows
-- to the array.
ARRAY [
ROW(
1, -- Band index: sets this as the first band
'8BUI'::text, -- Pixel Type (string rep of ST_BandPixelType types)
5, -- Initialized pixel value
255 -- Nodata Value
)
]::addbandarg[]
);

In order to add additional bands, all we have to do is add another row to our rasterbandarg[]:

1
2
3
4
5
6
7
INSERT INTO public.test_250(rast)
SELECT ST_AddBand( ST_MakeEmptyRaster(1500, 1500, -105.4330444336, 40.7170785158, 0.00208333, 0.00208333, 0, 0, 4326 ),
ARRAY [
ROW(1, '8BUI'::text, 5, 255),
ROW(1, '8BUI'::text, 10, 255)
]::addbandarg[]
);

Yep. It’s really that easy!