<div dir="ltr"><div>Hi All,</div><div><br></div>Just to follow-up - looks like I've figured out the issue:<div>In the count of total number of pixels per building, it seems I was calculating the total number of pixels per tile that intersected with the polygons, not the total number of pixels that actually intersected the polygon. For now, I used a reclassification step, which gets around this issue, but I suspect an ST_Intersection approach would also work. </div><div><br></div><div>Here's the working sql:</div><div><div><br></div><div>--Begin Code</div><div>SELECT *,</div><div> count_undevel/(count_total)::float as PropVeg --Calculates proportion of pixels within each building that are class 1 or 2 based on count_undevel and count_total</div><div>INTO resultsvectorlayers.greenroofs_pctgrn</div><div>FROM(</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>SELECT </div><div><span class="Apple-tab-span" style="white-space:pre">     </span>p.*,</div><div>--Count number of pixels per building with values 1 or 2</div><div><span class="Apple-tab-span" style="white-space:pre">  </span>ST_CountAgg(</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>ST_Reclass(</div><div><span class="Apple-tab-span" style="white-space:pre">                  </span>ST_Clip(rast, p.geom_2263),</div><div><span class="Apple-tab-span" style="white-space:pre">                  </span>1,'[1-2]:1, [3-5]:0', '8BUI',0), 1,TRUE) --reclassifies classes 1 and 2 as 1, reclassifies values 3-5 as 0, and designates 0 as nodata</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>as count_undevel,</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>as count_undevel,</div><div>--Count total number of pixels</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>ST_CountAgg(</div><div><span class="Apple-tab-span" style="white-space:pre">         </span>ST_Clip(</div><div><span class="Apple-tab-span" style="white-space:pre">             </span>ST_Reclass(rast,1,'[0-5]:1','8BUI',1), --reclassify all pixels to non-nodata; when this is clipped, all pixels within polygons are kept as 1, while pixels outside of buildings are set to nodata</div><div><span class="Apple-tab-span" style="white-space:pre">            </span>1, p.geom_2263,NULL,TRUE),</div><div><span class="Apple-tab-span" style="white-space:pre">           </span>1,TRUE) --exclude nodata from countAgg</div><div><span class="Apple-tab-span" style="white-space:pre">               </span>as count_total</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>FROM staging.naip_ndvi_classified_2013 r, staging.nycbldgs p</div><div><span class="Apple-tab-span" style="white-space:pre"> </span>WHERE ST_INTERSECTS(r.rast, p.geom_2263) --and rid = 2</div><div><span class="Apple-tab-span" style="white-space:pre">       </span>group by p.geom_2263, p.gid</div><div>) AS FOO;</div></div><div>--End Code<br><br>On Tuesday, July 12, 2016 at 4:56:11 PM UTC-4, Michael Treglia wrote:<blockquote class="gmail_quote" style="margin: 0;margin-left: 0.8ex;border-left: 1px #ccc solid;padding-left: 1ex;"><div dir="ltr">Hi All,<div><br></div><div>I'm working to do some overlay analyses between raster and vector layers, and having an issue getting an accurate count of pixels per polygon.</div><div><br></div><div>My query is below - basically my raster layer has values ranging 1-5, with some nodata pixels (defined as such, with values of 0). I want to calculate, for each polygon, the proportion of pixels, of the total (including nodata), with values of 1 and 2. </div><div><br></div><div>The first ST_CountAgg statement (yielding 'count_undevel') seems to be working as expected, and reports an accurate number. However, the second one, to create the 'count_total' field consistently gives an over-count.  </div><div><br></div><div>Any guess as to what might be going on/how to correct? I'll gladly share the properties of the data if that would be useful.</div><div><br></div><div>Thanks!</div><div>Mike</div><div><br></div><div><br></div><div><br></div><div><div>SELECT *,</div><div> count_undevel/(count_total)::<wbr>float as PropVeg --Calculates proportion of pixels within each building that are class 1 or 2 based on count_undevel and count_total</div><div>INTO resultsvectorlayers.pctgrn</div><div>FROM(</div><div><span style="white-space:pre">   </span>SELECT </div><div><span style="white-space:pre">      </span>p.*,</div><div>--Count number of pixels per building with values 1 or 2</div><div><span style="white-space:pre">   </span>ST_CountAgg(</div><div><span style="white-space:pre">          </span>ST_Reclass(</div><div><span style="white-space:pre">                   </span>ST_Clip(rast, p.geom_2263),</div><div><span style="white-space:pre">                   </span>1,'[1-2]:1, [3-5]:0', '8BUI',0), 1,TRUE) --reclassifies classes 1 and 2 as 1, reclassifies values 3-5 as 0, and designates 0 as nodata</div><div><span style="white-space:pre">                </span>as count_undevel,</div><div>--Count total number of pixels</div><div><span style="white-space:pre">        </span>ST_CountAgg(</div><div><span style="white-space:pre">          </span>ST_Clip(rast, p.geom_2263),</div><div><span style="white-space:pre">           </span>1,FALSE)</div><div><span style="white-space:pre">              </span>as count_total</div><div><span style="white-space:pre">        </span>FROM staging.naip_ndvi_classified_<wbr>2013 r, staging.nycbldgs p</div><div><span style="white-space:pre">       </span>WHERE ST_INTERSECTS(r.rast, p.geom_2263) --and rid = 2</div><div><span style="white-space:pre">        </span>group by p.geom_2263, p.gid</div><div>) AS FOO;</div></div><div><br></div><div><br></div><div><br></div></div>
</blockquote></div></div>