Randomly generate points withing a Geometry

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Randomly generate points withing a Geometry

Justin York
What would be the easiest way to randomly generate points within a given geometry? I was hoping getInteriorPoint() would do so but it always returns the same point.
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Randomly generate points withing a Geometry

michaelm-2
Hi Justin,
> What would be the easiest way to randomly generate points within a
> given geometry? I was hoping getInteriorPoint() would do so but it
> always returns the same point.
Be sure that if getInteriorPoint() had done what you want, somebody
would already have requested that it does return always the same point ;-)
Just joking. Don't know what you really need to achieve but here is how
I would do (not tested) :
env = geometry.getInternalEvelope()
// create a random point in the geometry envelope
x = env.getMinX() + Math.random()*env.getWidth();
y = env.getMinY() + Math.random()*env.getHeight();
test if (x, y) is inside your geometry, if not try another random point

pros : random aspect should be quite good
cons : quite expansive because of test inclusion
May be there is a more efficient solution by hacking
getInteriorPointMethod()

Michaël
> ------------------------------------------------------------------------
>
> _______________________________________________
> jts-devel mailing list
> [hidden email]
> http://lists.refractions.net/mailman/listinfo/jts-devel
>  

_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Randomly generate points withing a Geometry

mbedward
Hi Justin,

If each of your geometries occupy most of its bounding envelope I'd do
the simple method that Michaël suggests.

If a geometry only occupies a very small portion of its envelope a
better way is to stratify the random point generation by dividing up
the envelope into smaller, equal-sized rectangles, noting which of
these overlap the geometry.  Then generate each point using the simple
algorithm within a randomly chosen rectangle.

Hope this helps

Michael
_______________________________________________
jts-devel mailing list
[hidden email]
http://lists.refractions.net/mailman/listinfo/jts-devel
Loading...