Ticket #606 (assigned defect)

Opened 5 years ago

Last modified 3 months ago

unexpected polygon label placement

Reported by: dgadoury@dmsolutions.ca Assigned to: dmorissette (accepted)
Priority: high Milestone: 5.4 release
Component: MapServer C Library Version: 4.1
Severity: normal Keywords:
Cc: warnock@dmsolutions.ca

Description (Last modified by dmorissette)

When labeling polygons, labels are often placed in unexpected locations
including on the edge or even completely outside the polygon. I will attach two
images to demonstrate.

The first shows Flodida and bordering states, and the FL label falls on the
FL/AL border. AL is not labeled at all even though there would be room for a
label if the FL label was better placed. In this case the label POSITION was set
to CC.

The second image shows Alaska and northwest Canada. The label POSITION in this
case is AUTO and it results in the BC label falling completely outside its
polygon. None of the labels are really where I would want or expect them to be.

From a cartographic viewpoint this is a serious problem.

Chosing a different POSITION may help in this case, but in other areas of the
map it would create new problems. What is really needed is a an assurance that
labels will at least fall completely within the polygon they are associated with.

Attachments

cc_labels.png (5.1 kB) - added by dgadoury@dmsolutions.ca on 03/29/04 16:29:07.
image showing FL label
auto_labels.png (6.0 kB) - added by dgadoury@dmsolutions.ca on 03/29/04 16:30:11.
image showing Alaska & northwest Canada
fl_circle.png (6.5 kB) - added by dgadoury@dmsolutions.ca on 06/16/04 13:43:59.
image showing where circle method would place label in FL
bc_circle.png (6.3 kB) - added by dgadoury@dmsolutions.ca on 06/16/04 13:44:58.
image showing where circle method would place label in BC
Centroid.ZIP (57.2 kB) - added by dgadoury@dmsolutions.ca on 07/12/04 11:57:50.
centroid placement code
bug606.gif (7.9 kB) - added by dmorissette on 11/08/06 13:20:09.
Image showing Monteregie with original BBOX center + scanline algorithms
bug606b.gif (7.3 kB) - added by dmorissette on 11/08/06 13:33:46.
Image showing Monteregie region with original scanline algo (BBOX center disabled)
bug606c.gif (7.8 kB) - added by dmorissette on 11/08/06 13:39:16.
Image showing Monteregie region with weighted scanline algorithm
bug606.patch (1.9 kB) - added by dmorissette on 11/08/06 13:46:40.
mapprimitive.c patch to disable BBOX center and add weight to scanline algorithm

Change History

03/29/04 16:29:07 changed by dgadoury@dmsolutions.ca

  • attachment cc_labels.png added.

image showing FL label

03/29/04 16:30:11 changed by dgadoury@dmsolutions.ca

  • attachment auto_labels.png added.

image showing Alaska & northwest Canada

03/30/04 08:02:53 changed by dmorissette

  • cc set to steve.lime@dnr.state.mn.us.
  • component changed from Build Problems to MapServer C Library.

04/13/04 17:55:56 changed by sdlime

  • status changed from new to assigned.
Dean: MapServer uses a sampling technique to locate a label point within a 
concave polygon. It's sort of like the scanline conversion algorithm used to 
fill polygons. Polygon label generation works like so:

First, try the center of the polygon bbox, if in the polygon return it. If 
outside then try sampling. Sampling uses scan lines and saves the mid-point of 
the longest scanline. At least that's supposed to be how it works. In the 
Alaska/NW Canada image the NT label is placed based on the bbox, and the BC 
label placed based on the scanline. The Florida map suffers from the scanline 
method, the sampling interval is probably too large. This can be changed in the 
source code.

The fix? Probably involves dumping the bbox, or at least making the test more 
rigorous than simply "in". The scanline conversion method should try to 
maximize distance from the edges of the bbox.

There's not enough time to address for 4.2 but I could have a go for 4.3/4.4 
(which should be release before MUM2).

Steve

06/16/04 13:42:40 changed by dgadoury@dmsolutions.ca

I spoke with one of my instructors from Carleton U about label placement in
polygons and he mentioned two other methods. He offered qbasic code if it would
help.

The first is: Calculate the MBR, find the midpoint in Y, then draw a horizontal
line at that Y from the right side of the polygon to the left. The midpoint in X
of that line should be a reasonably selected centroid or label point. 
* This would work nicely for Florida but not for BC, so I guess this method is
no better than the current method. Both will produce both good and bad results
depending on the shape of the polygon.

Other methods try to fit the largest inscribed circle within the polygon, and
the centre of that circle would be the label point. I don't know the nuts and
bolts of how this would work, and it could be computationally expensive or even
impossible, but it sounds like it might produce good results. I did a quick
semi-freehand rendering of where labels would be placed in Florida and BC using
this kind of a method and the results are good. See the attachments.

06/16/04 13:43:59 changed by dgadoury@dmsolutions.ca

  • attachment fl_circle.png added.

image showing where circle method would place label in FL

06/16/04 13:44:58 changed by dgadoury@dmsolutions.ca

  • attachment bc_circle.png added.

image showing where circle method would place label in BC

06/16/04 20:38:28 changed by sdlime

Great! Pass along the code...

Steve

07/09/04 11:14:58 changed by sdlime

Dean: Just a reminder to attach the basic code to the bug.

Steve

07/12/04 10:42:24 changed by dgadoury@dmsolutions.ca

Sorry, I missed the last comment. I have emailed Steve Prashker requesting any
code he can supply and as soon as I receive anything I'll attach it here.

07/12/04 11:57:50 changed by dgadoury@dmsolutions.ca

  • attachment Centroid.ZIP added.

centroid placement code

07/20/05 14:44:02 changed by dgadoury@dmsolutions.ca

Any new ideas or proposals to solve this issue?  

Whenever we try to label polygons and run into this issue we have a tough
decision to make.  We either have to drop the labeling of polygons and give up
some potentially cool styling and info, or We have to live with unruly labels 
(there are just too many features on earth that don't work with the way
MapServer handles polygon labeling ;).

07/20/05 14:45:03 changed by dgadoury@dmsolutions.ca

  • cc set to warnock@dmsolutions.ca.

07/25/05 12:11:32 changed by sdlime

One additional idea might be to use GEOS to compute centroids for polygons. I'd 
have to do some checking on if it has any options that would be particularly 
helpful for placing labels. It would be expensive and would probably be tunable.

Yet another option is playing with the parameters (e.g. density of polygon 
sampling) that are used when generating a label point. It's possible that 
simply tuning those parameters a bit more might really help. I can point 
someone to the DEFINEs if necessary.

Steve

11/08/06 13:20:09 changed by dmorissette

  • attachment bug606.gif added.

Image showing Monteregie with original BBOX center + scanline algorithms

11/08/06 13:30:38 changed by dmorissette

  • status changed from assigned to new.
  • owner changed from mapserverbugs to dmorissette.
  • cc set to assefa@dmsolutions.ca, dmorissette@mapgears.com.
Reassigned bug to myself

11/08/06 13:33:46 changed by dmorissette

  • attachment bug606b.gif added.

Image showing Monteregie region with original scanline algo (BBOX center disabled)

11/08/06 13:39:16 changed by dmorissette

  • attachment bug606c.gif added.

Image showing Monteregie region with weighted scanline algorithm

11/08/06 13:43:17 changed by sdlime

Can we come up with a better way to determine in the bbox result should be used?

Steve

11/08/06 13:46:40 changed by dmorissette

  • attachment bug606.patch added.

mapprimitive.c patch to disable BBOX center and add weight to scanline algorithm

11/08/06 13:52:16 changed by dmorissette

  • status changed from new to assigned.
> Can we come up with a better way to determine in the bbox result should be used?

One idea I had was to calculate the BBOX of the label inside
msPolygonLabelPoint() and test that all 4 corners lie inside the polygon. That
would at least solve the issues with the labels that overlap with the edges of
their polygon. However that leaves two questions unanswered:

1- Is the point-in-polygon calculation expensive? i.e. if we call that 4 times
then do we rule out any benefit over going straight to the scanline algorithm?

2- The scanline algorithm is almost always going to give better looking results
than the BBOX center even if there is no overlap wth the edge of the polygon. IS
this algorithm that expensive (do we know)? Should we at a minimum offer a
mapfile option to allow users to request the use of the scanline algorithm in
all cases (and bypass the BBOX) if they are willing to swallow the little
performance hit.

11/08/06 14:14:45 changed by dmorissette

  • milestone set to 5.0 release.
Setting 5.0 release target milestone

11/08/06 14:24:57 changed by sdlime

In the samples the original scanline method (w/BBOX disabled) look better than 
the weighted scanline, are the names reversed?

What about running some timings on label placement simply using the scanline 
method versus the 4.10 method? I would think the scanline method is pretty fast.

Steve

11/08/06 15:18:00 changed by dmorissette

No, I don't think they are reversed. Attachment 599 is the image with the
original scanline (w/BBOX disabled), it has two labels that are clipped a the
top of the image. This was fixed in attachment 600 by the weighted scanline
method... the only issue with the weighted scanline is that the Montreal label
is shifted up a little bit, a side-effect of the weighted scanline method.
However it still seems to me that the weighted scanline gives a better result.

Note that the issue with the original scanline resultinng in the two clipped
labels in attachment 599 is not really fixed by the weighted method. We'd really
need to find out why the first scanline is so close to the edge of the image and
fix that, in which case the need for the weighted scanline may be less important
(but probably still benefecial).

08/07/07 11:45:06 changed by dmorissette

  • description changed.
  • milestone changed from 5.0 release to 5.2 release.

06/18/08 13:08:55 changed by dmorissette

  • milestone changed from 5.2 release to 5.4 release.