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.
What would be a more efficient way to create random points for a polygon that isn't compact?


