public static class RandomExtension
{
///
/// Generate a random double number between the min and max values.
///
/// Instance of a random class.
/// The min value for the potential range.
/// The max value for the potential range.
/// Random number between min and max
/// The random result number will always be less than the max number.
public static double NextDouble(this Random random, double minValue, double maxValue)
{
return random.NextDouble() * (maxValue - minValue) + minValue;
}
///
/// /Generate a random coordinate (only x,y values) within the provided envelope.
///
/// Instance of a random class.
/// Area of interest in which the random coordinate will be created.
/// A coordinate with random values (only x,y values) within the extent.
public static Coordinate2D NextCoordinate2D(this Random random, Envelope withinThisExtent)
{
return new Coordinate2D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax),
random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax));
}
///
/// /Generate a random coordinate 3D (containing x,y,z values) within the provided envelope.
///
/// Instance of a random class.
/// Area of interest in which the random coordinate will be created.
/// A coordinate with random values 3D (containing x,y,z values) within the extent.
public static Coordinate3D NextCoordinate3D(this Random random, Envelope withinThisExtent)
{
return new Coordinate3D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax),
random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax), 0);
}
}
=======================
protected override async void OnClick()
{
// to work in the context of the active display retrieve the current map
Map activeMap = MapView.Active.Map;
// retrieve the first point layer in the map
var pointFeatureLayer = activeMap.GetLayersAsFlattenedList().OfType().Where(
lyr => lyr.ShapeType == ArcGIS.Core.CIM.esriGeometryType.esriGeometryPoint).FirstOrDefault();
if (pointFeatureLayer == null)
return;
// first generate some random points
await ConstructSamplePoints(pointFeatureLayer);
// activate the button completed state to enable the polyline button
FrameworkApplication.State.Activate("geometry_points_constructed");
}
///
/// Create random sample points in the extent of the spatial reference
///
/// Point geometry feature layer used to the generate the points.
/// Task{bool}
private Task<bool> ConstructSamplePoints(FeatureLayer pointFeatureLayer)
{
// create a random number generator
var randomGenerator = new Random();
// the database and geometry interactions are considered fine-grained and must be executed on
// the main CIM thread
return QueuedTask.Run(() =>
{
// start an edit operation to create new (random) point features
var createOperation = new EditOperation()
{
Name = "Generate points",
SelectNewFeatures = false
};
// get the feature class associated with the layer
var featureClass = pointFeatureLayer.GetTable() as FeatureClass;
// define an area of interest. Random points are generated in the allowed
// confines of the allow extent range
var areaOfInterest = MapView.Active.Extent;
MapPoint newMapPoint = null;
// retrieve the class definition of the point feature class
var classDefinition = featureClass.GetDefinition() as FeatureClassDefinition;
// store the spatial reference as its own variable
var spatialReference = classDefinition.GetSpatialReference();
// create 20 new point geometries and queue them for creation
for (int i = 0; i < 20; i++)
{
// generate either 2D or 3D geometries
if (classDefinition.HasZ())
newMapPoint = MapPointBuilder.CreateMapPoint(randomGenerator.NextCoordinate3D(areaOfInterest), spatialReference);
else
newMapPoint = MapPointBuilder.CreateMapPoint(randomGenerator.NextCoordinate2D(areaOfInterest), spatialReference);
// queue feature creation
createOperation.Create(pointFeatureLayer, newMapPoint);
}
// execute the edit (feature creation) operation
return createOperation.ExecuteAsync();
});
}