<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Wed, Aug 14, 2013 at 6:01 PM, René Fournier <span dir="ltr"><<a href="mailto:m5@renefournier.com" target="_blank">m5@renefournier.com</a>></span> wrote:<br style="font-family:monospace">

<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="word-wrap:break-word"><span style="font-family:monospace">The reason is that each of the rectangles are actually subdivided into 12 smaller, equal rectangles, numbered/lettered A-L. How would you go about doing this? It doesn't seem like something I can compute in the initial query to retrieve the containing rectangle… Any ideas?</span><br>

</div></blockquote><div><br></div><div>Sounds to me like you have data your database isn't modeling. Any particular reason you don't want to store the further divided rectangles?<br><br></div><div>Assuming you have a reason you don't want to, <a href="http://www.postgresql.org/docs/9.2/static/functions-math.html">width_bucket</a> comes to mind:<br>

<br><span style="font-family:courier new,monospace">SELECT width_bucket(ST_X(point), ST_XMin(rect), ST_XMax(rect), 4) AS horizontal_subrect<br>     , width_bucket(ST_Y(point), ST_YMin(rect), ST_YMax(rect), 3) AS vertical_subrect<br>

FROM (SELECT ST_GeomFromText('POLYGON ((0 0,0 100,100 100,100 0,0 0))') AS rect) r<br>   , (SELECT ST_MakePoint(50,50) AS point) p;</span><br><br></div><div>gives<br><br>horizontal_subrect = 3, vertical_subrect = 2. You would need to be able to coordinate that column and row number to a rectangle letter, of course.<br>

<br><br></div><div>Someone else might be able to expound on this a bit more about whether it's a good idea, but if your data is always rectangles, maybe Box2D would be better than geometry.<br><br>SELECT ST_MakeBox2D(ST_MakePoint(0,0), ST_MakePoint(10,10));<br>

<br></div><div>I'm not sure if that gives any performance gains, but I wouldn't be surprised at all if it did.<br></div></div></div></div>