Hi all,<br><br>I am trying to create a fast and efficient way of calculating the number of different order of neighbors for a quadrat vector cell polygon in a grid having a specific value.<br><br>I tried to calculate this using ST_DWithin() but since my cells are quadrat, ST_DWithin does not capture cells in the outer corner when trying to count a high order number of neighbors.<br>
<br>For instance using 3rd order neighbors ST_DWithin only find 44 neighbors while it should have found 48 neighbors. Hence, it misses the 4 corner cells in the 7x7 grid representing the 3rd order neighbors.<br><br>So I was thinking about making quadrat buffers around the cell to do my count. However, this takes a lot of time since it have to do the buffering for each cell polygon in the grid. (some 259,200 of them, 720x360).<br>
<br>This was my query:<br><br>DROP TABLE IF EXISTS cf_spatiallag_n;<br><br>SELECT DISTINCT a.gid, c.gridyear, d.gwcode, count(DISTINCT b.gid)<br>INTO cf_spatiallag_n<br>FROM priogrid a, priogrid b, confsitegrid c, priogridall d<br>
WHERE a.gid != b.gid AND b.gid = c.gid AND a.gid = d.gid AND c.gwcode = d.gwcode AND <a href="http://c.cf">c.cf</a> = 1<br>AND ST_Intersects(ST_Buffer(a.cell, 0.25, 'join=mitre'), b.cell)<br>GROUP BY a.gid, c.gridyear, d.gwcode<br>
ORDER BY c.gridyear, a.gid;<br><br>Any ideas on how i can optimize this query?<br><br>Thanks in advance.<br><br>Andreas<br>