ArcGIS Pro程式碼建立一個隨機點
阿新 • • 發佈:2022-03-23
public static class RandomExtension { /// <summary> /// Generate a random double number between the min and max values. /// </summary> /// <param name="random">Instance of a random class.</param> /// <param name="minValue">The min value for the potential range.</param>/// <param name="maxValue">The max value for the potential range.</param> /// <returns>Random number between min and max</returns> /// <remarks>The random result number will always be less than the max number.</remarks> public static double NextDouble(this Random random, doubleminValue, double maxValue) { return random.NextDouble() * (maxValue - minValue) + minValue; } /// <summary> /// /Generate a random coordinate (only x,y values) within the provided envelope. /// </summary> /// <param name="random">Instance of a random class.</param>/// <param name="withinThisExtent">Area of interest in which the random coordinate will be created.</param> /// <returns>A coordinate with random values (only x,y values) within the extent.</returns> public static Coordinate2D NextCoordinate2D(this Random random, Envelope withinThisExtent) { return new Coordinate2D(random.NextDouble(withinThisExtent.XMin, withinThisExtent.XMax), random.NextDouble(withinThisExtent.YMin, withinThisExtent.YMax)); } /// <summary> /// /Generate a random coordinate 3D (containing x,y,z values) within the provided envelope. /// </summary> /// <param name="random">Instance of a random class.</param> /// <param name="withinThisExtent">Area of interest in which the random coordinate will be created.</param> /// <returns>A coordinate with random values 3D (containing x,y,z values) within the extent.</returns> 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<FeatureLayer>().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"); } /// <summary> /// Create random sample points in the extent of the spatial reference /// </summary> /// <param name="pointFeatureLayer">Point geometry feature layer used to the generate the points.</param> /// <returns>Task{bool}</returns> 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(); }); }