My application uses the ArcGIS Silverlight API to select polygons from a map layer. It uses the Geometry.Extent.GetCenter() method to get the centroid of the shape and add a thumbtack image at those coordinates.

The problem I'm having is that for some shapes (crescents, for example), the centroid will actually fall outside the bounds of the shape. In this case, the thumbtack will appear outside of the shape.

Is there any way that I can ensure that the coordinates I choose are actually inside of the polygon?

You are basically looking for an equivalent to IArea.LabelPoint in ArcObjects. The Silverlight API does not have support for this directly, but you can leverage the REST API GeometryService to do that. See Label Points. The downside of using the geometry service is that you need to send a separate request to the server, which may (or may not) be an issue in your scenario.

You might also find this question useful: Algorithm for finding irregular polygon centroid (label point).

Taken from: http://support.esri.com/en/knowledgebase/techarticles/detail/19742

If the center of the polygon's bounding box does not fall inside the polygon itself (one example would be a crescent-shaped polygon), the point is moved in the X direction until it enters into the polygon (let's call this "Point1"). The point is then moved in the same direction, along the X axis, until it exits the polygon (let's call this "Point2"). The centroid is calculated to be halfway between Point1 and Point2, on the same X axis. For complex polygons that have more than one pair of polygon outlines that cross the X-axis, each pair of outlines is compared to see which pair creates the widest length along the X-axis. Then, the centroid is calculated to be halfway between the points where this pair of outlines crosses the X-axis.

The equivalent is somewhere in ArcMap since version 8, but I cannot find the equivalent in ArcObjects. I presume it would use the above method (which I guess you could implement yourself - it maybe as quick as searching through IGeometry5 interfaces..).

Update: It could be IGpDescribeGeometry.Centroid Property

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.