Sunday, 8 April 2018

Returning raster values from multiple raster tables via Lat/Lon in PostGIS?


I am new to PostGIS and am unsure if I'm thinking of this correctly, but I'd like to return values from multiple raster tables in a PostGIS database in one go.


The purpose is to query legend values for a webmap based on the lat/long values of a cursor click. So, if there are multiple raster tables in the database e.g., raster1 and raster2, I would like to shoot lat and lon to the DB and return the values from both rasters in some sort of dictionary.



How would I go about doing so with an SQL command? Would I need to do some sort of spatial join? Multiple select statements?



Answer



I have no knowledge on how the GeoDjango raster model works but querying a raster table goes roughly the same way as a geometry table. I would take the following approach, provided that you know the names of your raster tables and each raster has only 1 band:


WITH mypoint AS (
SELECT ST_SetSrid(ST_MakePoint(,),) geom
)
SELECT
ST_Value(a.rast, geom) AS value1,
ST_Value(b.rast, geom) AS value2,
ST_Value(c.rast, geom) AS value3

FROM mypoint p
LEFT JOIN table1 a ON (ST_Intersects(p.geom, a.rast))
LEFT JOIN table2 b ON (ST_Intersects(p.geom, b.rast))
LEFT JOIN table3 c ON (ST_Intersects(p.geom, c.rast))

Documentation and examples on ST_Value: https://postgis.net/docs/RT_ST_Value.html


Side note: make sure the raster table is tiled and indexed (it also works without but can make a large difference in speed)


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...