Thursday, 6 May 2010

Random Points in Polygon in JTS

Recently there was a thread on the PostGIS list about how to create "dot-density" maps. Essentially this involves creating a set of N randomly-placed points which lie within a given polygon.

JTS already has a lot of random shape creation functions provided in the TestBuilder (randomPoints, randomPointsInGrid, randomRadialPoints, randomRectilinearWalk, etc). But they aren't currently exposed as a class in the API. This seemed like a good use case to initiate the development of such a class.

So now JTS has a RandomShapeFactory class. The class allows setting an extent using either a rectangular Envelope or a polygonal geometry, and will create sets of N points within the defined extent.

Here's a screenshot of the TestBuilder showing the new function at work:


Update: it's been pointed out that what is really desired is to have the points evenly distributed through the polygon. This will take a bit more thinking... At least the API won't have to change much to support this.

6 comments:

  1. Or neither random nor even, but quasi-random, such as Halton sequences that eventually fill the space as N -> infinity.

    ReplyDelete
  2. Halton Sequences... I love it.

    All in good time...

    ReplyDelete
  3. You have mentioned that JTS now has the new factory. When will that be exposed? I didn't see it in 1.11.

    ReplyDelete
  4. Craig,

    This code was developed after the release of 1.11, so it' only in SVN. Also, I'm in the process of refactoring it to improve the interface. If you need it right now, it's in com.vividsolutions.jts.geom.util.RandomShapeFactory and com.vividsolutions.jtstest.function.CreateRandomGeometryFunctions

    ReplyDelete
  5. I was just curious what happened to the RandomShapeFactory class? I looked in the JTS source (http://jts-topo-suite.svn.sourceforge.net/viewvc/jts-topo-suite/trunk/jts/java/src/com/vividsolutions/jts/geom/util/) and don't see it there. I'd love to use it if it is still available somewhere.

    Thanks,
    David

    ReplyDelete
  6. In case anyone is interested, it appears to me that the class was moved to the package com.vividsolutions.jts.shape.random.RandomPointsBuilder and is available in the 1.12 release.

    ReplyDelete