<html 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=Windows-1252">
<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:Aptos;
panose-1:2 11 0 4 2 2 2 2 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
{margin:0in;
font-size:11.0pt;
font-family:"Aptos",sans-serif;
mso-ligatures:standardcontextual;}
span.EmailStyle17
{mso-style-type:personal-compose;
font-family:"Aptos",sans-serif;
color:windowtext;}
.MsoChpDefault
{mso-style-type:export-only;
font-size:11.0pt;}
@page WordSection1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
{page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">BTW If this question is too specific to SQL and I should seek aid elsewhere, please let me know.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I am able to return a layer of rowids associated with features whose geotiff band value at the centroid location is above a particular threshold. I then turn back around and delete all such features manually in Python. But it would be
nice to do this with a delete from query.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">The working select query is: select rowid, gdal_get_pixel_value(path/to/raster.tif, ndvi_band_num, ‘georef’, ST_X(cnt), ST_Y(cent)) as ndvi from (select ST_Centroid(geom) as cnt from layer_name) where ndvi < 600<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">So, collect feature centroids as a subexpression so gdal_get_pixel_value() has the inputs it needs.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Unfortunately, attempts to mutate this into a delete from query has failed.<o:p></o:p></p>
<p class="MsoNormal"> sql_query = f"delete from {label_name} where ndvi < 600 from \<o:p></o:p></p>
<p class="MsoNormal"> (gdal_get_pixel_value('{raster_fp}', {ndvi_band_num}, 'georef', ST_X(cnt), ST_Y(cnt)) as ndvi from \<o:p></o:p></p>
<p class="MsoNormal"> (select ST_Centroid(geom) as cnt from {label_name}))"<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Per tradition, the SQL parser doesn’t provide much insight, merely “near "from": syntax error”.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Simpler queries like “delete from layer_name where ST_Area(GEOMETRY)” just work, without needing to pass through rowids around from subexpressions. So I hope it is just a matter of SQL spellcraft on my side that’s lacking, but I do know
some database engines don’t allow deleting from tables that source that table in subexpressions, so AFAIK this may just not be possible.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Ideas?<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">Best,<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">Jesse<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">Lead Computer Scientist<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">Science Systems and Applications, Inc.<o:p></o:p></span></p>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">Dr Compton Tucker Team<o:p></o:p></span></p>
</div>
</div>
<p class="MsoNormal"><span style="font-size:12.0pt;mso-ligatures:none">NASA Goddard Space Flight Center</span><o:p></o:p></p>
</div>
</body>
</html>