Using Sql Spatial Data (C#) to find the “visual” center of irregular polygons

…衆ロ難τιáo~ 提交于 2019-12-03 05:43:49
Peet Whittaker

An almost identical question has already been asked here. In your case, the most relevant answer is probably this one. The hard part of that answer is determining where to draw the line that splits the polygon into two equal areas. Therefore, I propose a slightly modified version below for SQL Server:

  1. Use STCentroid to find the centre of mass
  2. If this is inside the polygon (STWithin), then no need to process further; otherwise:
  3. Determine the centroid of the polygon's bounding box (e.g. use STEnvelope to get the polygon's envelope and pass that as the argument to STCentroid)
  4. If this centroid is within polygon (STWithin), it may be good enough (would need testing); otherwise:
  5. Extend the line that connects the polygon's centroid and the envelope's centroid so that it extends to just beyond the polygon's envelope in each direction (may be easier in C# code, or see here for a PostGIS solution)
  6. Determine the intersection points between the extended line and the polygon (STIntersection)
  7. Find the nearest intersection point to the polygon's centroid point from step 1 (see here). This point is an approximation for the "1st cut point" detailed in the linked answer.
  8. Find the nearest intersection point to the intersection point found above in step 7. This point is an approximation for the "2nd cut point" detailed in the linked answer.
  9. The mid-point of the line that connects the two intersection points will be inside the polygon, and should be a reasonable approximation for the "visual centre" of the polygon.

For instance, given the polygon for the DCREHA example above, the following GIF animates the procedure listed above (judged by eye - actual result will differ):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!