Mapscript - Sorting labels

Steven Monai stevem at SPATIALMAPPING.COM
Fri Jun 10 11:46:49 EDT 2005


Gribol:

Try sorting your shapefile in ascending order by population, instead of
descending order. My experience with MapServer's labeling is that labels
that are drawn later are given preference to those that were drawn earlier.

If sorting your shapefile in ascending order doesn't help, you could try
defining a number of ANNOTATION layers that are responsible for labeling
cities within specified (and disjoint) population ranges. (e.g. layer1 to
label cities where: POPULATION<10000; layer2 to label cities where:
10000<=POPULATION<20000; layer3 to label: 20000<=POPULATION<40000; etc.)
Then arrange the layers in your mapfile so that layers for annotating small
cities are drawn first. The number of ANNOTATION layers you use will depend
on how tolerant you are for occasional labeling "mistakes" (where a smaller
city's label is drawn in preference to that of a larger city).

Hope this helps,
-SM
--


On Fri, 10 Jun 2005 07:54:42 +0200, Gribol <gribol at FREE.FR> wrote:

>; > Juste a simple exemple :
>; > "Label30" is the label of a town with a population of 3000k.
>; > "Label55" is the label of a town with a population of 5500k.
>; > These two labels are closest (in pixel coordinates) and cannot
>; be displayed
>; > both at the current map extent (because of names collision). These two
>; > labels are matching the filter condition (higher than 2500k).
>; > How to force Mapserver to draw the "Label55" and not "Label30"
>; ? That is my
>; > issue.
>; >
>;
>; I think you may be able to do that with the sortshp utility, see:
>; http://mapserver.gis.umn.edu/utils.html
>;
>; Daniel
>; --
>; ------------------------------------------------------------
>;   Daniel Morissette               dmorissette at dmsolutions.ca
>;   DM Solutions Group              http://www.dmsolutions.ca/
>; ------------------------------------------------------------
>
>Thank you Daniel for your answer.
>But it's not working. In fact, the shapefile is already sorted with a
>descending order on the population field.
>When I display the whole earth, all is fine. I do some zoom in, again it's
>fine. But at a specific scale, when I display all towsn with a population
>number higher than 100000, there is an error, because 2 smalls towns (with
>150000 citizens) are displayed and not a major town with 800000 citizens.
>
>Here is the layer description in the map file :
>
> LAYER
>  NAME "villes"
>  TYPE ANNOTATION
>  DATA "../data/WorldCities.shp"
>  STATUS OFF
>  LABELITEM "name"
>  LABELCACHE ON
>
>  CLASS
>   COLOR 200 10 10
>   SYMBOL 'circle'
>   SIZE 5
>   OUTLINECOLOR 255 255 255
>   LABEL
>    ANGLE 0.0
>    POSITION AUTO
>    ANTIALIAS TRUE
>    TYPE TRUETYPE
>    FONT arial-bold
>    SIZE 10
>    MINDISTANCE 190
>    OFFSET 0 0
>    BUFFER 10
>    COLOR 200 0 0
>    OUTLINECOLOR 255 255 255
>    PARTIALS FALSE
>   END
>    END
>
>  PROJECTION
>   "proj=latlong" "ellps=WGS84" # Geographic
>  END
> END
>
>and here is the php-mapscript code I use :
>
>function DrawCities($Map)
>{
> // Get the layer
> $LayerName = $Map->getLayerByName("villes");
> if($LayerName == NULL) return;
> $LayerName->set("status", MS_ON);
>
> // Get the class
> $ClassVille = $LayerName->getClass(0);
>
> // Compute "step" value according to the current map extent
> $nPas = nCalculPas($Map);
>
> $strFilter = "";
> if($nPas >= 30)
> {
>  $strFilter = "([POPULATION] >= 2000000)";
> }
> else
> if($nPas >= 15)
> {
>  $strFilter = "([POPULATION] >= 1000000)";
> }
> else
> if($nPas >= 5)
> {
>  $strFilter = "([POPULATION] >= 500000)";
> }
> else
> if($nPas >= 2)
> {
>  $strFilter = "([POPULATION] >= 100000)";
> }
> else
> {
>  $strFilter = "([POPULATION] >= 0)";
> }
>
> $LayerName->setFilter($strFilter);
>}
>
>the function "nCalculPas" returns a value according to the current map
>extent. High value means a large map extent. When I zoom in, the map extent
>decreases and this function returns a smallest value.
>
>Marc.



More information about the mapserver-users mailing list