I have created a table loading via raster2psql:
raster2pgsql -a -f Spatial_Grid -F -I -M -T ssa_data -X ssa_data *.tif 
ssa.TERRAIN_GRID

The tif data comes from http://dwtkns.com/srtm/

I load 98 rows of data using above.

So I need elevation data back if I pass to this data a lat and long.
I initially used query: example

SELECT rid, ST_Value(spatial_grid,1,
                ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 
40.0000000968475)')
               ) AS ALTITUDE
FROM TERRAIN_GRID;

This returns the right answer BUT these raster warnings make it difficult to 
work with:
NOTICE:  Attempting to get pixel value with out of range raster coordinates:

So I came up with a way to only check the row that the point is actually 
contained within the row raster first. This eliminates the warnings:

SELECT ST_Value(spatial_grid,1, 
ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 40.0000000968475)')) AS 
ALTITUDE
FROM TERRAIN_GRID
WHERE 
ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(-79.9616664245329,
 40.0000000968475),4326));

So rather than have someone running this query say from python and have to put 
in lat longs in twice I created a function:

create or replace function getAltitude(vLongitude decimal,vLatitude decimal)
            returns integer as $$
declare
            lAltitude ssa.TERRAIN_GRID.spatial_grid%TYPE;
            lPointValue varchar := 'SRID=4326;POINT(' || vLongitude || ' ' || 
vLatitude || ')';
begin
            SELECT ST_Value(spatial_grid,1,
                ST_GEOMFROMEWKT(lPointValue)
               )
            INTO lAltitude
            FROM TERRAIN_GRID
    WHERE 
ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(vLongitude, 
vLatitude),4326));
            return lAltitude;
end;
$$ LANGUAGE plpgsql;

This compiles without error, but when I run it, this happens:
SELECT * FROM getAltitude(-79.96166, 40.0000);
ERROR:  rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of 
characters
CONTEXT:  PL/pgSQL function getaltitude(numeric,numeric) line 6 at SQL statement


Now it appears to be caused by something in the predicate (where clause):
WHERE ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(vLongitude, 
vLatitude),4326));

I say that because if I change the function to:
create or replace function getAltitude(vLongitude decimal,vLatitude decimal)
            returns integer as $$
declare
            lAltitude ssa.TERRAIN_GRID.spatial_grid%TYPE;
            lPointValue varchar := 'SRID=4326;POINT(' || vLongitude || ' ' || 
vLatitude || ')';
begin
            SELECT ST_Value(spatial_grid,1,
                ST_GEOMFROMEWKT(lPointValue)
               )
            INTO lAltitude
            FROM TERRAIN_GRID;
            return lAltitude;
end;
$$ LANGUAGE plpgsql;

It executes.

I cannot find any information on this. Any help on this would be appreciated!
_______________________________________________
postgis-users mailing list
[email protected]
https://lists.osgeo.org/mailman/listinfo/postgis-users

Reply via email to