<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=us-ascii"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Century Schoolbook";}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:#0563C1;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:#954F72;
        text-decoration:underline;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        margin-bottom:.0001pt;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
span.EmailStyle17
        {mso-style-type:personal;
        font-family:"Century Schoolbook";
        color:#0070C0;}
span.EmailStyle18
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:#1F497D;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1272861602;
        mso-list-type:hybrid;
        mso-list-template-ids:-1013128164 67698705 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;}
@list l0:level1
        {mso-level-text:"%1\)";
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level2
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level3
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level4
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level5
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level6
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
@list l0:level7
        {mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level8
        {mso-level-number-format:alpha-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:left;
        text-indent:-.25in;}
@list l0:level9
        {mso-level-number-format:roman-lower;
        mso-level-tab-stop:none;
        mso-level-number-position:right;
        text-indent:-9.0pt;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-US link="#0563C1" vlink="#954F72"><div class=WordSection1><p class=MsoNormal><span style='color:#1F497D'>Couple of things<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoListParagraph style='text-indent:-.25in;mso-list:l0 level1 lfo1'><![if !supportLists]><span style='color:#1F497D'><span style='mso-list:Ignore'>1)<span style='font:7.0pt "Times New Roman"'>      </span></span></span><![endif]><span style='color:#1F497D'>No need for ST_Envelope, raster support ST_Intersects against geometries.<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'>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<o:p></o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal>SELECT ST_Value(spatial_grid,1, ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 40.0000000968475)')) AS ALTITUDE<o:p></o:p></p><p class=MsoNormal>FROM TERRAIN_GRID<o:p></o:p></p><p class=MsoNormal>WHERE ST_Intersects(spatial_grid,ST_SetSRID(ST_MakePoint(-79.9616664245329, 40.0000000968475),4326));<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoListParagraph style='text-indent:-.25in;mso-list:l0 level1 lfo1'><![if !supportLists]><span style='mso-list:Ignore'>2)<span style='font:7.0pt "Times New Roman"'>      </span></span><![endif]>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.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>create or replace function getAltitude(vLongitude decimal,vLatitude decimal)<o:p></o:p></p><p class=MsoNormal>            returns integer as $$<o:p></o:p></p><p class=MsoNormal>               WITH p AS (SELECT ST_SetSRID(ST_MakePoint(vLongitude, vLatitude),4326) AS IPointValue)<o:p></o:p></p><p class=MsoNormal>                              SELECT ST_Value(spatial_grid,1,IPointValue)::integer<o:p></o:p></p><p class=MsoNormal>                                             FROM TERRAIN_GRID, p ON (ST_Intersects(spatial_grid, IPointValue) )<o:p></o:p></p><p class=MsoNormal style='text-indent:.5in'>               LIMIT 1;<o:p></o:p></p><p class=MsoNormal>$$ LANGUAGE sql;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>3)<o:p></o:p></p><p class=MsoNormal><span style='color:#1F497D'>regarding this issue - </span>ERROR:  rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters<o:p></o:p></p><p class=MsoNormal>CONTEXT:  PL/pgSQL function getaltitude(numeric,numeric) line 6 at SQL statement<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>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<o:p></o:p></p><p class=MsoNormal>Could possibly be a bug in ST_Envelope raster function as well.  I rarely use ST_Envelope for raster.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>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.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>SELECT ST_Envelope(spatial_grid)<o:p></o:p></p><p class=MsoNormal>FROM TERRAIN_GRID;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>SELECT spatial_grid::geometry<o:p></o:p></p><p class=MsoNormal>FROM TERRAIN_GRID;<o:p></o:p></p><p class=MsoListParagraph><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoListParagraph><span style='color:#1F497D'><o:p> </o:p></span></p><p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p><div style='border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt'><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0in 0in 0in'><p class=MsoNormal><b>From:</b> postgis-users [mailto:postgis-users-bounces@lists.osgeo.org] <b>On Behalf Of </b>Michael.CTR.Lazlo@faa.gov<br><b>Sent:</b> Friday, August 03, 2018 9:38 AM<br><b>To:</b> postgis-users@lists.osgeo.org<br><b>Subject:</b> [postgis-users] ERROR: rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters<o:p></o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I have created a table loading via raster2psql:<o:p></o:p></p><p class=MsoNormal>raster2pgsql -a -f Spatial_Grid -F -I -M -T ssa_data -X ssa_data *.tif ssa.TERRAIN_GRID<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>The tif data comes from <a href="http://dwtkns.com/srtm/">http://dwtkns.com/srtm/</a><o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I load 98 rows of data using above.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>So I need elevation data back if I pass to this data a lat and long.<o:p></o:p></p><p class=MsoNormal>I initially used query: example<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>SELECT rid, ST_Value(spatial_grid,1,<o:p></o:p></p><p class=MsoNormal>                ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 40.0000000968475)')<o:p></o:p></p><p class=MsoNormal>               ) AS ALTITUDE<o:p></o:p></p><p class=MsoNormal>FROM TERRAIN_GRID;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>This returns the right answer BUT these raster warnings make it difficult to work with:<o:p></o:p></p><p class=MsoNormal>NOTICE:  Attempting to get pixel value with out of range raster coordinates:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>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:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>SELECT ST_Value(spatial_grid,1, ST_GEOMFROMEWKT('SRID=4326;POINT(-79.9616664245329 40.0000000968475)')) AS ALTITUDE<o:p></o:p></p><p class=MsoNormal>FROM TERRAIN_GRID<o:p></o:p></p><p class=MsoNormal>WHERE ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(-79.9616664245329, 40.0000000968475),4326));<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>So rather than have someone running this query say from python and have to put in lat longs in twice I created a function:<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>create or replace function getAltitude(vLongitude decimal,vLatitude decimal)<o:p></o:p></p><p class=MsoNormal>            returns integer as $$<o:p></o:p></p><p class=MsoNormal>declare<o:p></o:p></p><p class=MsoNormal>            lAltitude ssa.TERRAIN_GRID.spatial_grid%TYPE;<o:p></o:p></p><p class=MsoNormal>            lPointValue varchar := 'SRID=4326;POINT(' || vLongitude || ' ' || vLatitude || ')';<o:p></o:p></p><p class=MsoNormal>begin<o:p></o:p></p><p class=MsoNormal>            SELECT ST_Value(spatial_grid,1,<o:p></o:p></p><p class=MsoNormal>                ST_GEOMFROMEWKT(lPointValue)<o:p></o:p></p><p class=MsoNormal>               )<o:p></o:p></p><p class=MsoNormal>            INTO lAltitude<o:p></o:p></p><p class=MsoNormal>            FROM TERRAIN_GRID<o:p></o:p></p><p class=MsoNormal>    WHERE ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(vLongitude, vLatitude),4326));<o:p></o:p></p><p class=MsoNormal>            return lAltitude;<o:p></o:p></p><p class=MsoNormal>end;<o:p></o:p></p><p class=MsoNormal>$$ LANGUAGE plpgsql;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>This compiles without error, but when I run it, this happens:<o:p></o:p></p><p class=MsoNormal>SELECT * FROM getAltitude(-79.96166, 40.0000);<o:p></o:p></p><p class=MsoNormal>ERROR:  rt_raster_from_hexwkb: Raster HEXWKB input must have an even number of characters<o:p></o:p></p><p class=MsoNormal>CONTEXT:  PL/pgSQL function getaltitude(numeric,numeric) line 6 at SQL statement<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>Now it appears to be caused by something in the predicate (where clause):<o:p></o:p></p><p class=MsoNormal>WHERE ST_Contains(ST_Envelope(spatial_grid),ST_SetSRID(ST_MakePoint(vLongitude, vLatitude),4326));<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I say that because if I change the function to:<o:p></o:p></p><p class=MsoNormal>create or replace function getAltitude(vLongitude decimal,vLatitude decimal)<o:p></o:p></p><p class=MsoNormal>            returns integer as $$<o:p></o:p></p><p class=MsoNormal>declare<o:p></o:p></p><p class=MsoNormal>            lAltitude ssa.TERRAIN_GRID.spatial_grid%TYPE;<o:p></o:p></p><p class=MsoNormal>            lPointValue varchar := 'SRID=4326;POINT(' || vLongitude || ' ' || vLatitude || ')';<o:p></o:p></p><p class=MsoNormal>begin<o:p></o:p></p><p class=MsoNormal>            SELECT ST_Value(spatial_grid,1,<o:p></o:p></p><p class=MsoNormal>                ST_GEOMFROMEWKT(lPointValue)<o:p></o:p></p><p class=MsoNormal>               )<o:p></o:p></p><p class=MsoNormal>            INTO lAltitude<o:p></o:p></p><p class=MsoNormal>            FROM TERRAIN_GRID;<o:p></o:p></p><p class=MsoNormal>            return lAltitude;<o:p></o:p></p><p class=MsoNormal>end;<o:p></o:p></p><p class=MsoNormal>$$ LANGUAGE plpgsql;<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>It executes.<o:p></o:p></p><p class=MsoNormal><o:p> </o:p></p><p class=MsoNormal>I cannot find any information on this. Any help on this would be appreciated!<o:p></o:p></p></div></div></body></html>