I'm trying to clip a raster using a polygon but I end up with a result that doesn't seem right and where some raster pixels are outside the polygon used for the clip.

See image below where polygon is the green outline and each pixels centroid is red:

Polygon is shown as green, raster is greyscale and pixel centroids are red

I'm using the following SQL statement:

select st_union(st_clip(a.rast,b.geom, true))
from temp_jonyp2.dhm a,
    select wkb_geometry geom 
    from i.gdk_bygning 
    where id=347910
    ) b
where st_intersects(a.rast,b.geom)

I'm running PostgreSQL 9.6 and PostGIS 2.4.

Output of postgis_full_version is:

POSTGIS="2.4.0 r15853" PGSQL="96" GEOS="3.5.0-CAPI-1.9.0 r4084" PROJ="Rel. 4.8.0, 6 March 2012" GDAL="GDAL 1.11.4, released 2016/01/25" LIBXML="2.9.1" LIBJSON="0.11" RASTER

This question might relate to PostGIS polygon clip inaccurate using ST_Clip and ST_Union from tiled raster, but I can't figure out if the issue has been resolved for the version of PostGIS I'm running.

How do I clip a raster with a polygon so that only pixels inside the polygon are included?

UPDATE: From Vince's suggestion in the comments I tried with a negative buffer. This did shave of the pixels touching the polygon in the areas where the clip looks right, but didn't help with the "wrong" parts. See image: With negative buffer

  • 1
    Did you check your projection for both inputs and if needed, did you reproject and try to rerun this clip? – whyzar Oct 16 '17 at 14:22
  • 2
    Have you tried using ST_Within in lieu of ST_Intersects? – Vince Oct 16 '17 at 14:23
  • 2
  • 1
    ST_Intersects seems to be returning pixels where the centroid intersects. – HeikkiVesanto Oct 17 '17 at 10:00
  • 2
    It does work fine for me returning those pixels that intersect the centroid (postgis_version 2.1 USE_GEOS=1 USE_PROJ=1 USE_STATS=1). So my query is just: select ST_Union(ST_Clip(r.rast, g.geom) from raster r join vector g on st_intersects(r.rast, g.geom) – HeikkiVesanto Oct 17 '17 at 10:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.