[postgis-users] [Raster] Finding pixels intersecting an extent (polygon)

Jean Marchal jean.d.marchal at gmail.com
Fri Feb 6 09:10:12 PST 2015


For the posterity, I modified very slightly the query by changing the
returnband argument of ST_Intersection to return only one band.

WITH foo AS (
SELECT
ST_AsRaster(
ST_GeomFromText('POLYGON ((-52.54178994517749 46.99199259385565,
-52.54178994517749 46.996897959881, -52.53436080387823 46.996897959881,
-52.53436080387823 46.99199259385565, -52.54178994517749
46.99199259385565))', 4269),
rast,
'8BUI',
touched := True
) AS rast
FROM elev
LIMIT 1
)
SELECT
ST_Intersection(
elev.rast,
foo.rast,
                'BAND1'
) AS rast
FROM elev
JOIN foo
ON ST_Intersects(elev.rast, foo.rast)

Thanks again,

Jean

2015-02-05 15:29 GMT-08:00 Jean Marchal <jean.d.marchal at gmail.com>:

> It worked! Thanks a lot!
>
> Jean
>
> 2015-02-05 15:18 GMT-08:00 Bborie Park <dustymugs at gmail.com>:
>
> Try something like:
>>
>> WITH foo AS (
>> SELECT
>> ST_AsRaster(
>> ST_GeomFromText('POLYGON ((-52.54178994517749 46.99199259385565,
>> -52.54178994517749 46.996897959881, -52.53436080387823 46.996897959881,
>> -52.53436080387823 46.99199259385565, -52.54178994517749
>> 46.99199259385565))', 4269),
>> rast,
>> '8BUI',
>> touched := True
>> ) AS rast
>> FROM elev
>> LIMIT 1
>> )
>> SELECT
>> ST_Intersection(
>> elev.rast,
>> foo.rast
>> ) AS rast
>> FROM elev
>> JOIN foo
>> ON ST_Intersects(elev.rast, foo.rast)
>>
>> The idea is to explicitly convert the geometry into a raster using one of
>> the elev rasters as a reference. The "touched := True" slightly changes the
>> behavior of the rasterization.
>>
>> From the docs:
>>
>> The optional touched parameter defaults to false and maps to the GDAL
>> ALL_TOUCHED rasterization option, which determines if pixels touched by
>> lines or polygons will be burned.
>>
>> On Thu, Feb 5, 2015 at 2:45 PM, Jean Marchal <jean.d.marchal at gmail.com>
>> wrote:
>>
>>> Bborie,
>>>
>>> I am running PostGIS 2.1.5. Here is the output of the
>>> postgis_full_version():
>>>
>>> "POSTGIS="2.1.5 r13152" GEOS="3.3.3-CAPI-1.7.4" PROJ="Rel. 4.7.1, 23
>>> September 2009" GDAL="GDAL 1.9.0, released 2011/12/29" LIBXML="2.8.0"
>>> LIBJSON="UNKNOWN" RASTER"
>>>
>>> My query looks like this:
>>>
>>> SELECT ST_Intersection(ST_GeomFromText('POLYGON ((-52.54178994517749
>>> 46.99199259385565, -52.54178994517749 46.996897959881, -52.53436080387823
>>> 46.996897959881, -52.53436080387823 46.99199259385565, -52.54178994517749
>>> 46.99199259385565))', 4269), rast) as rast
>>> FROM elev
>>>
>>> David,
>>>
>>> This query does not returns all the polygons that intersect my polygon.
>>> I think it returns only those where the centroid is inside the polygon or
>>> is it covered based? (like 50% of the pixel intersect with the polygon).
>>>
>>> Thanks for your rapid answers!
>>>
>>> Jean
>>>
>>> 2015-02-05 14:31 GMT-08:00 David Haynes <haynesd2 at gmail.com>:
>>>
>>> select ST_Clip(r.rast,p.geom) as rast
>>>> from polygon p inner join raster r on ST_intersects(r.rast, p.geom)
>>>>
>>>> This returns a raster which has all pixels inside the polygon
>>>>
>>>> On Thu, Feb 5, 2015 at 4:05 PM, Jean Marchal <jean.d.marchal at gmail.com>
>>>> wrote:
>>>>
>>>>> Hi list,
>>>>>
>>>>> I am trying to return all the pixels in a raster that intersect (not
>>>>> just touch) an extent (say a rectangle). I tried ST_Clip and
>>>>> ST_Intersection(raster, geom) but they don't return all the pixels that
>>>>> intersect my extent polygon. Do I have to vectorize the raster first using
>>>>> ST_PixelAsPolygons or there is a better / more efficient way to proceed?
>>>>>
>>>>> Ultimately the goal is to fetch the resulting raster in R.
>>>>>
>>>>> Thanks,
>>>>>
>>>>> Jean
>>>>>
>>>>> _______________________________________________
>>>>> postgis-users mailing list
>>>>> postgis-users at lists.osgeo.org
>>>>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>>>>>
>>>>
>>>>
>>>> _______________________________________________
>>>> postgis-users mailing list
>>>> postgis-users at lists.osgeo.org
>>>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>>>>
>>>
>>>
>>> _______________________________________________
>>> postgis-users mailing list
>>> postgis-users at lists.osgeo.org
>>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>>>
>>
>>
>> _______________________________________________
>> postgis-users mailing list
>> postgis-users at lists.osgeo.org
>> http://lists.osgeo.org/cgi-bin/mailman/listinfo/postgis-users
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20150206/273481a9/attachment.html>


More information about the postgis-users mailing list