What are the options to find the center point of a polygon?

711
2
10-22-2021 12:08 AM
DavidMrázek
Occasional Contributor II

Good day,
I know there are several ways to find the center point of a polygon. One is Centroid (the one I have built)
The second option is MeanCenter (the one I have built).
Are there any more?

This is MeanCenter:

 

 

 

protected override async void OnClick()
        {
         var mapView = MapView.Active;
            if (mapView != null)
            {
await QueuedTask.Run(async () =>
                {
var pathCenter = @"D:\MeanCenter.shp";
                    object[] listOfParame = { dissFeat };
                    await StartGp("stats.MeanCenter", listOfParame);
 });
            }
        }
private static async Task StartGp(string funcArc, object[] parame)
        {
            var parameters = Geoprocessing.MakeValueArray(parame);
            // FunctionPart(funcArc, parameters);
            await Geoprocessing.ExecuteToolAsync(funcArc, parameters).ConfigureAwait(false);
        }

 

 

 

Here is Centroid method:

 

 

protected override async void OnClick()
        {
 var mapView = MapView.Active;
            if (mapView != null)
            {
 await QueuedTask.Run(async () =>
                {
 var featToPoly = @"D:\featToPoly.shp";
                    object[] paraList = { dissFeat, featToPoly };
                    await StartGp("management.FeatureToPolygon", paraList);
                    var featurePolyogn = MapView.Active.Map.FindLayers("featToPoly").First() as FeatureLayer;
                    var fc = featurePolyogn.GetTable() as FeatureClass;
                     using (var cursor = fc.Search())
                    {
                        while (cursor.MoveNext())
                        {
                            using (var feature = cursor.Current as Feature)
                            {
                                if (feature == null) continue;
                               Polygon polygon = feature.GetShape() as Polygon;

                                using (PolygonBuilder pb = new PolygonBuilder(polygon))
                                {
                                    Polygon myPolygon = pb.ToGeometry();
                                    await CreateFcWithAttributesAsync("Center_Point", EnumFeatureClassType.POINT);
                                    var pointLayer = MapView.Active.Map.FindLayers("Center_Point").First() as FeatureLayer; 
                                    var mapPoint = GeometryEngine.Instance.Centroid(myPolygon);
                                    var createOperation = new EditOperation();
                                    createOperation.Create(pointLayer, mapPoint);
                                    createOperation.Execute();
                                    }

                                });
                            }
                        }
                    }
                });
            }
        }
private static async Task StartGp(string funcArc, object[] parame)
        {
            var parameters = Geoprocessing.MakeValueArray(parame);
            // FunctionPart(funcArc, parameters);
            await Geoprocessing.ExecuteToolAsync(funcArc, parameters).ConfigureAwait(false);
        }

 

 

 

Thank you all!

 

0 Kudos
2 Replies
ABishop
MVP Regular Contributor

@DavidMrázek 

Feature to Point with "Inside" unchecked in the geoprocessing tool.

Amanda Bishop, GISP
0 Kudos
John_Jones
New Contributor III

Slightly off topic, but just a note that if you are using an approach like your Centroid approach on multiple features you likely want to hoist the edit operation out of the inner loop and create all the new centroid points in one operation as so...

  var featToPoly = @"D:\featToPoly.shp";
  object[] paraList = { dissFeat, featToPoly };
  await StartGp("management.FeatureToPolygon", paraList);
  var featurePolyogn = MapView.Active.Map.FindLayers("featToPoly").First() as FeatureLayer;
  var fc = featurePolyogn.GetTable() as FeatureClass;
  await CreateFcWithAttributesAsync("Center_Point", EnumFeatureClassType.POINT);
  var pointLayer = MapView.Active.Map.FindLayers("Center_Point").First() as FeatureLayer; 
  var createOperation = new EditOperation();
  using (var cursor = fc.Search())
  {
    while (cursor.MoveNext())
    {
      using (var feature = cursor.Current as Feature)
      {
        if (feature == null) continue;
        var polygon = feature.GetShape() as Polygon;

        var mapPoint = GeometryEngine.Instance.Centroid(polygon);
        createOperation.Create(pointLayer, mapPoint);
      }
    }
  }
  await createOperation.ExecuteAsync();