• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

gisoracle

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

ArcGIS Pro代码创建一个随机点

 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, double minValue, 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();
            });

        }

 

posted on 2022-03-23 11:50  gisai  阅读(151)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3