postgis_raster
postgis_raster
postgis : PostGIS raster types and functions
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 1502 | postgis_raster
|
postgis
|
3.6.2 |
GIS
|
GPL-2.0
|
C
|
| Attribute | Has Binary | Has Library | Need Load | Has DDL | Relocatable | Trusted |
|---|---|---|---|---|---|---|
--s-d--
|
No
|
Yes
|
No
|
Yes
|
no
|
no
|
| Relationships | |
|---|---|
| Requires | postgis
|
| Need By | h3_postgis
|
| See Also | pointcloud_postgis
pointcloud
pgrouting
h3
q3c
ogr_fdw
geoip
pg_polyline
|
| Siblings | postgis
postgis_topology
postgis_sfcgal
postgis_tiger_geocoder
address_standardizer
address_standardizer_data_us
|
Packages
| Type | Repo | Version | PG Major Compatibility | Package Pattern | Dependencies |
|---|---|---|---|---|---|
| EXT | PGDG
|
3.6.2 |
18
17
16
15
14
|
postgis |
postgis |
| RPM | PGDG
|
3.6.2 |
18
17
16
15
14
|
postgis36_$v |
- |
| DEB | PGDG
|
3.6.2 |
18
17
16
15
14
|
postgresql-$v-postgis-3 |
- |
| Linux / PG | PG18 | PG17 | PG16 | PG15 | PG14 |
|---|---|---|---|---|---|
el8.x86_64
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
el8.aarch64
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
PGDG 3.6.1
|
el9.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
el9.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
el10.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
el10.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
d12.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
d12.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
d13.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
d13.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
u22.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
u22.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
u24.x86_64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
u24.aarch64
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
PGDG 3.6.2
|
Source
Install
Make sure PGDG repo available:
pig repo add pgdg -u # add pgdg repo and update cacheInstall this extension with pig:
pig install postgis; # install via package name, for the active PG version
pig install postgis_raster; # install by extension name, for the current active PG version
pig install postgis_raster -v 18; # install for PG 18
pig install postgis_raster -v 17; # install for PG 17
pig install postgis_raster -v 16; # install for PG 16
pig install postgis_raster -v 15; # install for PG 15
pig install postgis_raster -v 14; # install for PG 14Create this extension with:
CREATE EXTENSION postgis_raster CASCADE; -- requires postgisUsage
PostGIS Raster extends PostGIS with support for raster (gridded) data stored directly in PostgreSQL. It enables raster analysis, raster/vector interaction, and map algebra operations within SQL.
Setup
CREATE EXTENSION postgis_raster;Loading Raster Data
The raster2pgsql command-line tool imports raster files (GeoTIFF, etc.) into PostgreSQL:
# Load a GeoTIFF as tiled 100x100 rasters, create spatial index, use COPY
raster2pgsql -s 4326 -t 100x100 -I -C -M elevation.tif public.dem | psql mydb
# Append to existing table
raster2pgsql -s 4326 -t 100x100 -a more_data.tif public.dem | psql mydbKey flags:
-s <srid>– Set the SRID-t <width>x<height>– Tile the raster into chunks-I– Create a spatial GiST index-C– Apply raster constraints-M– Vacuum analyze after loading
Querying Raster Data
Raster Metadata
-- Get raster dimensions and pixel size
SELECT rid,
ST_Width(rast) AS width,
ST_Height(rast) AS height,
ST_ScaleX(rast) AS pixel_size_x,
ST_ScaleY(rast) AS pixel_size_y,
ST_NumBands(rast) AS bands,
ST_SRID(rast) AS srid
FROM dem LIMIT 5;Pixel Values
-- Get the value at a specific point
SELECT ST_Value(rast, ST_SetSRID(ST_MakePoint(-73.985, 40.748), 4326)) AS elevation
FROM dem
WHERE ST_Intersects(rast, ST_SetSRID(ST_MakePoint(-73.985, 40.748), 4326));
-- Get value at column/row position (band 1)
SELECT ST_Value(rast, 1, 10, 20) FROM dem WHERE rid = 1;Band Statistics
SELECT (ST_SummaryStats(rast)).*
FROM dem WHERE rid = 1;
-- Returns: count, sum, mean, stddev, min, maxRaster Processing
Clipping Rasters by Vector Geometry
-- Clip raster to a polygon boundary
SELECT ST_Clip(rast, geom) AS clipped_rast
FROM dem, boundaries
WHERE ST_Intersects(rast, geom);Map Algebra
Apply pixel-by-pixel operations:
-- Single-raster map algebra: classify elevation
SELECT ST_MapAlgebra(rast, 1, NULL,
'CASE WHEN [rast] > 100 THEN 1 WHEN [rast] > 50 THEN 2 ELSE 3 END') AS classified
FROM dem;
-- Two-raster map algebra: difference between two DEMs
SELECT ST_MapAlgebra(a.rast, 1, b.rast, 1, '[rast1] - [rast2]') AS diff
FROM dem_old a, dem_new b
WHERE ST_Intersects(a.rast, b.rast);Raster/Vector Interaction
-- Convert raster pixels to vector points
SELECT (ST_PixelAsPoints(rast)).*
FROM dem WHERE rid = 1;
-- Convert raster to polygons (one per unique value)
SELECT (ST_DumpAsPolygons(rast)).*
FROM dem WHERE rid = 1;
-- Intersect raster with vector and get values
SELECT p.name, ST_Value(d.rast, p.geom) AS elevation
FROM dem d, points p
WHERE ST_Intersects(d.rast, p.geom);Resampling and Reprojection
-- Resample to a different pixel size
SELECT ST_Rescale(rast, 0.001, -0.001) FROM dem;
-- Reproject to a different SRID
SELECT ST_Transform(rast, 3857) FROM dem;Exporting Rasters
-- Export as GeoTIFF (binary)
SELECT ST_AsTIFF(rast) FROM dem WHERE rid = 1;
-- Export as PNG
SELECT ST_AsPNG(rast) FROM dem WHERE rid = 1;
-- Export as JPEG
SELECT ST_AsJPEG(rast) FROM dem WHERE rid = 1;Last updated on