1

My code creates random points within polygons using the ArcGIS Pro 3 SDK by creating a random x and y within the extent of the polygon (using a snippet from Esri's Constructing Geometries sample), then checking to see if that point is inside the polygon.

public static class RandomExtension
{
  public static double NextDouble(this Random random, double minValue, double maxValue)
  {
    return random.NextDouble() * (maxValue - minValue) + minValue;
  }

public static Coordinate2D NextCoordinate2D(this Random random, Envelope withinThisExtent) { return new Coordinate2D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax), random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax)); } }

Random randomGenerator = new Random();

mp = MapPointBuilder.CreateMapPoint(randomGenerator.NextCoordinate2D(geometry.Extent), geometry.SpatialReference); if (GeometryEngine.Instance.Contains(geometry, mp)) { \write the point }

This works well for compact polygons, but is much slower for a skinny donut polygon covering a large area (for example, a 300 meter band around the island of Hawaii).

I ran three tests that created 100 random points to show the time difference. The first run created points within the extent of the Hawaiian Islands, the second run created points on the islands, and the third run created points on the shallow-water reefs around the Hawaiian islands. The first run took 0.07 seconds, the second run took 46.6 seconds, and the third run took 233.8 seconds.

ArcGIS Pro map showing three types of polygons used in the test runs

What would be a more efficient way to create random points for a polygon that isn't compact?

Hornbydd
  • 43,380
  • 5
  • 41
  • 81
kenbuja
  • 5,700
  • 2
  • 18
  • 29
  • For me the first question in situations like this is whether optimization is even needed? How often are your polygons skinny donuts vs. more compact? How long does it even take for a skinny donut? Optimization is important when it is really needed, but often times it is time spent chasing marginal improvements. – bixb0012 Nov 30 '22 at 16:13
  • 1
    My group does a lot of work in shallow water areas around islands, such as the reefs surrounding the Hawaiian islands, so this optimization would be important. I updated the question with a couple of examples showing the difference. – kenbuja Nov 30 '22 at 17:09
  • Save your data into PostGIS and have a try with https://postgis.net/docs/ST_GeneratePoints.html. – user30184 Nov 30 '22 at 18:25

1 Answers1

1

Here is an alternative approach, I simulated your reef dataset with a 100m buffer around the Hawaiian islands then clipped out interior.

Fake reef

I then used the Create Random Points tool, you can set the constraining geometry. I sampled 100 times in mainland polygons and then again the "reef" layer as shown below:

Sampled

Sampling mainland polygons tool 0.26 seconds, sampling reef polygons took 0.41 seconds.

So I would suggest calling this geo-processing tool within your code, it's clearly very efficient, rather than "re-inventing the wheel"?

Hornbydd
  • 43,380
  • 5
  • 41
  • 81
  • This is what I ended up doing for simple cases. The ad-in where I'm using this has some additional options for creating random points, such as creating varying numbers of points for selected feature attributes. The Minimum distance option doesn't work properly in this case, so I have to use my existing code in those circumstances. – kenbuja Jan 19 '23 at 20:50