Select to view content in your preferred language

How to get the centroid of a "C" or "U" shape like polygon?

2195
8
09-15-2016 07:15 PM
Joo_PinTan
Deactivated User

Anyone know how to get the centroid of a "C", or "U" shape like polygon and the point need to be inside the polygon some where near the centre?

0 Kudos
8 Replies
DanPatterson_Retired
MVP Emeritus

in most Arc* products this is referred to as a LabelPoint, so look for some incarnation of that term

ie 

The point at which the label is located. The labelPoint is always located within or on a feature.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

For the Java API, getLabelPointForPolygon in the GeometryEngine class.

0 Kudos
DanPatterson_Retired
MVP Emeritus

like I said Joshua... they have to look for labelpoint stuff ... grief

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

In Feature to Point Tool, choose "INSIDE" as the point location.



Think Location
0 Kudos
Joo_PinTan
Deactivated User

I need to use ArcGis Runtime SDK for Java API to code it. Feature to point is not available for me.

JayantaPoddar
MVP Esteemed Contributor

Oops... My mistake. Didn't notice the Space where it's posted.



Think Location
0 Kudos
DanPatterson_Retired
MVP Emeritus

did you look for labelpoint or not? it would be in a polygon class or methods section for your api

0 Kudos
Joo_PinTan
Deactivated User
protected boolean determineCentroid(Geometry polygon, Point centroid) {
  if (polygon.getType() == Geometry.Type.POLYGON) {
    Point tempCentroid;

    //Get centroid of the polygon
    Envelope env = new Envelope();
    polygon.queryEnvelope(env);
    tempCentroid = env.getCenter();

    //If tempCentroid is within the polygon then centroid is valid, else look for nearest vertex
    if (GeometryEngine.within(polygon, tempCentroid, featureServiceTable.getSpatialReference()))
      centroid.setXY(tempCentroid.getX(), tempCentroid.getY());
    else {
      Proximity2DResult proximity2DResult = GeometryEngine.getNearestCoordinate(polygon, tempCentroid, true);
      centroid.setXY(proximity2DResult.getCoordinate().getX(), proximity2DResult.getCoordinate().getY());
    }

    return true;
  }
  
  return false;
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I did something as shown above to look for the centroid of a polygon whereby the centre is empty. Although the result it is not really plotted at the "centre" of the polygon but it did solved my problem.

0 Kudos