问题
I'm drawing regions (using SqlGeometry
/SqlGeography
and translating them to the WPF LocationCollection
equivalent) on the Bing Maps WPF Control and needed to label them. I got the labels drawn on the regions and attached them to the point found by STCentroid()
. Of course as you imagine this is a problem with the 'U' or 'C' shaped regions where the centroid ends up outside the region, which makes the label incorrect.
Is there a way using SqlGeometry
/SqlGeography
to find the "visual" center or perhaps find the largest circle that can fit in the shape and use that center? I've tried various methods using STPointOnSurface()
but it seems that STPointOnSurface()
always picks a point on the edge like so DCREHA (the label for the bottom dark green region always ends up on the edges:
回答1:
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:
- Use
STCentroid
to find the centre of mass - If this is inside the polygon (
STWithin
), then no need to process further; otherwise: - 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 toSTCentroid
) - If this centroid is within polygon (
STWithin
), it may be good enough (would need testing); otherwise: - 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)
- Determine the intersection points between the extended line and the polygon (
STIntersection
) - 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.
- 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.
- 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):
来源:https://stackoverflow.com/questions/39276401/using-sql-spatial-data-c-to-find-the-visual-center-of-irregular-polygons