[postgis-users] ERROR: rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters
Regina Obe
lr at pcorp.us
Sun Aug 5 07:44:20 PDT 2018
Couple of things
1) No need for ST_Envelope, raster support ST_Intersects against
geometries.
So doing this should be sufficient and should be faster because it can use
the spatial index on the raster, your ST_Envelope won't be able to use the
raster spatial index
SELECT ST_Value(spatial_grid,1,
ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 40.0000000968475)')) AS
ALTITUDE
FROM TERRAIN_GRID
WHERE ST_Intersects(spatial_grid,ST_SetSRID(ST_MakePoint(-79.9616664245329,
40.0000000968475),4326));
2) I would also rewrite your function as an sql function since it's a
bit shorter and may perform better. I put in a limit 1 because on occasion,
your point might be in two tiles and you just want one answer back I
presume.
create or replace function getAltitude(vLongitude decimal,vLatitude decimal)
returns integer as $$
WITH p AS (SELECT ST_SetSRID(ST_MakePoint(vLongitude,
vLatitude),4326) AS IPointValue)
SELECT
ST_Value(spatial_grid,1,IPointValue)::integer
FROM TERRAIN_GRID, p ON
(ST_Intersects(spatial_grid, IPointValue) )
LIMIT 1;
$$ LANGUAGE sql;
3)
regarding this issue - 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
I'm not sure what is causing that. I suspect you might have a bad raster
tile which perhaps for whatever reason you aren't hitting without the WHERE
clause
Could possibly be a bug in ST_Envelope raster function as well. I rarely
use ST_Envelope for raster.
Can you try the following and see if it triggers the error. That would
hopefully give you a clue which record is faulty if it is a bad record.
SELECT ST_Envelope(spatial_grid)
FROM TERRAIN_GRID;
SELECT spatial_grid::geometry
FROM TERRAIN_GRID;
From: postgis-users [mailto:postgis-users-bounces at lists.osgeo.org] On Behalf
Of Michael.CTR.Lazlo at faa.gov
Sent: Friday, August 03, 2018 9:38 AM
To: postgis-users at lists.osgeo.org
Subject: [postgis-users] ERROR: rt_raster_from_hexwkb: Raster HEXWKB input
must have an even number of characters
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.9616664245
329, 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!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20180805/8c798069/attachment.html>
More information about the postgis-users
mailing list