<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 12 (filtered medium)">
<!--[if !mso]>
<style>
v\:* {behavior:url(#default#VML);}
o\:* {behavior:url(#default#VML);}
w\:* {behavior:url(#default#VML);}
.shape {behavior:url(#default#VML);}
</style>
<![endif]-->
<style>
<!--
/* Font Definitions */
@font-face
{font-family:Calibri;
panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
{font-family:Tahoma;
panose-1:2 11 6 4 3 5 4 4 2 4;}
/* 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:blue;
text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
{mso-style-priority:99;
color:purple;
text-decoration:underline;}
span.EmailStyle17
{mso-style-type:personal;
font-family:"Calibri","sans-serif";
color:windowtext;}
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 Section1
{size:8.5in 11.0in;
margin:1.0in 1.0in 1.0in 1.0in;}
div.Section1
{page:Section1;}
-->
</style>
<!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="3074" />
</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=blue vlink=purple>
<div class=Section1>
<p class=MsoNormal><span style='color:#1F497D'>Hi,<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> Sorry for the
noise, but the function probably should be:<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>BEGIN<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> xmin =
ST_X(geom) - dough; --
establish lower left for buffer<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> ymin =
ST_Y(geom) - dough; --<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> delta = dough *
2; --<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> WHILE i <
itermax LOOP<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> i
= i + 1;<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> pointx
= xmin + delta * random(); -- generate random point value<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> pointy
= ymin + delta * random();<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> returnpoint
= ST_SetSRID( ST_MakePoint( pointx, pointy ), ST_SRID(geom) );<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> EXIT WHEN<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> ST_DWithin(
returnpoint, geom, dough ) -- test to see if inside outer ring<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> AND
NOT<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> ST_DWithin(
returnpoint, geom, nut ); --test to see if inside inner ring<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> END LOOP;<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> IF i >
itermax THEN<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> RAISE
NOTICE 'Reached maximum iterations without placing point inside donut.';<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> END IF;<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'> RETURN
returnpoint;<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>END;<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>Even so, the question remains :)…
<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'>Steve<o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<div>
<p class=MsoNormal><!--[if gte vml 1]><v:shapetype id="_x0000_t75" coordsize="21600,21600"
o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f"
stroked="f">
<v:stroke joinstyle="miter" />
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0" />
<v:f eqn="sum @0 1 0" />
<v:f eqn="sum 0 0 @1" />
<v:f eqn="prod @2 1 2" />
<v:f eqn="prod @3 21600 pixelWidth" />
<v:f eqn="prod @3 21600 pixelHeight" />
<v:f eqn="sum @0 0 1" />
<v:f eqn="prod @6 1 2" />
<v:f eqn="prod @7 21600 pixelWidth" />
<v:f eqn="sum @8 21600 0" />
<v:f eqn="prod @7 21600 pixelHeight" />
<v:f eqn="sum @10 21600 0" />
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect" />
<o:lock v:ext="edit" aspectratio="t" />
</v:shapetype><v:shape id="_x0000_s1027" type="#_x0000_t75" alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png"
style='position:absolute;margin-left:1.5pt;margin-top:0;width:67.5pt;height:91.5pt;
z-index:2;visibility:visible;mso-wrap-style:square;mso-wrap-distance-left:9pt;
mso-wrap-distance-top:0;mso-wrap-distance-right:9pt;
mso-wrap-distance-bottom:0;mso-position-horizontal:absolute;
mso-position-horizontal-relative:text;mso-position-vertical:absolute;
mso-position-vertical-relative:text'>
<v:imagedata src="cid:image002.png@01CC9313.CDEC2A10" o:href="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" />
<w:wrap type="square"/>
</v:shape><![endif]--><![if !vml]><img width=90 height=122
src="cid:image002.png@01CC9313.CDEC2A10" align=left hspace=12
alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" v:shapes="_x0000_s1027"><![endif]><span
style='font-size:14.0pt;font-family:"Arial","sans-serif";color:#006C56'>Stephen
Mather<br>
</span><span style='font-family:"Arial","sans-serif";color:#006C56'>Geographic
Information Systems (GIS) Manager<br>
</span><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>(216) 635-3243<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>svm@clevelandmetroparks.com<br>
</span><span style='color:#1F497D'><a href="http://www.clemetparks.com/"><span
style='font-size:9.0pt;font-family:"Arial","sans-serif";color:blue'>clevelandmetroparks.com</span></a><o:p></o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
</div>
<p class=MsoNormal><span style='color:#1F497D'><o:p> </o:p></span></p>
<div>
<div style='border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in'>
<p class=MsoNormal><b><span style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'>From:</span></b><span
style='font-size:10.0pt;font-family:"Tahoma","sans-serif"'> postgis-users-bounces@postgis.refractions.net
[mailto:postgis-users-bounces@postgis.refractions.net] <b>On Behalf Of </b>Stephen
V. Mather<br>
<b>Sent:</b> Tuesday, October 25, 2011 11:04 AM<br>
<b>To:</b> postgis-users@postgis.refractions.net<br>
<b>Subject:</b> [postgis-users] ST_DWithin-- unexpected behavior?<o:p></o:p></span></p>
</div>
</div>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>Hi All,<o:p></o:p></p>
<p class=MsoNormal>
Running 1.3.5 still :). Found some unexpected (to me), but pleasant
behavior with ST_DWithin. What we’re doing is creating a function that we
can use to randomize a point within a minimum and maximum bounds. Our
original approach was to generate the geometry of the difference of two buffers
(a donut, if you will) and then place a random point within that. Better
to test with ST_DWithin directly then buffer, so we rewrote:<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>BEGIN<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
xmin = ST_XMin(geom) - dough;<o:p></o:p></p>
<p class=MsoNormal>
deltax = ST_XMax(geom) - xmin + dough;<o:p></o:p></p>
<p class=MsoNormal>
ymin = ST_YMin(geom) - dough;<o:p></o:p></p>
<p class=MsoNormal>
deltay = ST_YMax(geom) - ymin + dough;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
WHILE i < itermax LOOP<o:p></o:p></p>
<p class=MsoNormal>
i = i + 1;<o:p></o:p></p>
<p class=MsoNormal>
pointx = xmin + deltax * random();<o:p></o:p></p>
<p class=MsoNormal>
pointy = ymin + deltay * random();<o:p></o:p></p>
<p class=MsoNormal>
returnpoint = ST_SetSRID( ST_MakePoint( pointx, pointy ), ST_SRID(geom) );<o:p></o:p></p>
<p class=MsoNormal>
EXIT WHEN<o:p></o:p></p>
<p class=MsoNormal><b>
ST_DWithin( returnpoint, geom, dough )<o:p></o:p></b></p>
<p class=MsoNormal><b>
AND NOT<o:p></o:p></b></p>
<p class=MsoNormal><b>
ST_DWithin( returnpoint, geom, nut );</b><o:p></o:p></p>
<p class=MsoNormal>
END LOOP;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
IF i > itermax THEN<o:p></o:p></p>
<p class=MsoNormal>
RAISE NOTICE 'Reached maximum iterations without placing point inside donut.';<o:p></o:p></p>
<p class=MsoNormal>
END IF;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
RETURN returnpoint;<o:p></o:p></p>
<p class=MsoNormal>END;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>The puzzling part is that even if the parameters are passed
out of order, i.e. the inner ring is passed for the outer ring and vice versa,
we get a fine result. It’s a nice unexpected behavior, but unexpected
none-the-less. Anyone have any thoughts on why this works? Full
function below:<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><!--[if gte vml 1]><v:shape id="cmp-ms-90x122.png" o:spid="_x0000_s1026"
type="#_x0000_t75" alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png"
style='position:absolute;margin-left:1.5pt;margin-top:0;width:67.5pt;height:91.5pt;
z-index:1;visibility:visible'>
<v:imagedata src="cid:image002.png@01CC9313.CDEC2A10" o:title="cmp-ms-90x122" />
<w:wrap type="square"/>
</v:shape><![endif]--><![if !vml]><img width=90 height=122
src="cid:image002.png@01CC9313.CDEC2A10" align=left hspace=12
alt="http://www.clemetparks.com/images/esig/cmp-ms-90x122.png" v:shapes="cmp-ms-90x122.png"><![endif]><span
style='font-size:14.0pt;font-family:"Arial","sans-serif";color:#006C56'>Stephen
Mather<br>
</span><span style='font-family:"Arial","sans-serif";color:#006C56'>Geographic
Information Systems (GIS) Manager<br>
</span><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>(216) 635-3243<o:p></o:p></span></p>
<p class=MsoNormal><span style='font-size:9.0pt;font-family:"Arial","sans-serif";
color:#006C56'>svm@clevelandmetroparks.com<br>
</span><a href="http://www.clemetparks.com/"><span style='font-size:9.0pt;
font-family:"Arial","sans-serif"'>clevelandmetroparks.com</span></a><o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>CREATE OR REPLACE FUNCTION ST_Donut<o:p></o:p></p>
<p class=MsoNormal>
( geom Geometry,<o:p></o:p></p>
<p class=MsoNormal>
dough DOUBLE PRECISION,<o:p></o:p></p>
<p class=MsoNormal>
nut DOUBLE PRECISION,<o:p></o:p></p>
<p class=MsoNormal>
itermax INTEGER)<o:p></o:p></p>
<p class=MsoNormal>
RETURNS Geometry<o:p></o:p></p>
<p class=MsoNormal>
AS $$<o:p></o:p></p>
<p class=MsoNormal>
<o:p></o:p></p>
<p class=MsoNormal>DECLARE<o:p></o:p></p>
<p class=MsoNormal>
xmin DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal>
ymin DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
deltax DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal>
deltay DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
pointx DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal>
pointy DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
returnpoint Geometry;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
i INTEGER := 0;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
nut1 DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal>
dough1 DOUBLE PRECISION;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>BEGIN<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
xmin = ST_XMin(geom) - dough;<o:p></o:p></p>
<p class=MsoNormal>
deltax = ST_XMax(geom) - xmin + dough;<o:p></o:p></p>
<p class=MsoNormal>
ymin = ST_YMin(geom) - dough;<o:p></o:p></p>
<p class=MsoNormal>
deltay = ST_YMax(geom) - ymin + dough;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
WHILE i < itermax LOOP<o:p></o:p></p>
<p class=MsoNormal>
i = i + 1;<o:p></o:p></p>
<p class=MsoNormal>
pointx = xmin + deltax * random();<o:p></o:p></p>
<p class=MsoNormal>
pointy = ymin + deltay * random();<o:p></o:p></p>
<p class=MsoNormal>
returnpoint = ST_SetSRID( ST_MakePoint( pointx, pointy ), ST_SRID(geom) );<o:p></o:p></p>
<p class=MsoNormal>
EXIT WHEN<o:p></o:p></p>
<p class=MsoNormal>
ST_DWithin( returnpoint, geom, dough )<o:p></o:p></p>
<p class=MsoNormal>
AND NOT<o:p></o:p></p>
<p class=MsoNormal>
ST_DWithin( returnpoint, geom, nut );<o:p></o:p></p>
<p class=MsoNormal>
END LOOP;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
IF i > itermax THEN<o:p></o:p></p>
<p class=MsoNormal>
RAISE NOTICE 'Reached maximum iterations without placing point inside donut.';<o:p></o:p></p>
<p class=MsoNormal>
END IF;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>
RETURN returnpoint;<o:p></o:p></p>
<p class=MsoNormal>END;<o:p></o:p></p>
<p class=MsoNormal><o:p> </o:p></p>
<p class=MsoNormal>$$ LANGUAGE plpgsql;<o:p></o:p></p>
</div>
</body>
</html>