[GRASS-user] Adding only one label per cat

Hamish hamish_b at yahoo.com
Mon Feb 1 03:31:28 EST 2010


Tim wrote:
> I have a vector file that has some islands on it.  I would like to add
> labels to the map, but do not want it to add a label to every island,
> just to the broader category.  To illustrate, say I have a map of
> southeast Asia, I want to put the name of each country on the map,
> centred over the country. But when I try that (or equivalent), I end up
> with "Philippines" written over every single island of the Philippines,
> instead of just over the country itself.  I tried to use v.type and
> convert from centroid to point, and then put the labels on each point,
> but it takes the centroid for each polygon (island) instead of the
> broader category (country) so I ended up with the same problem.  Can
> somebody please let me know how to get around this?

hmmm, an interesting question.


if you don't mind some shell scripting-

MAP=geology
LABEL_COLUMN=label


ITEMS=`v.db.select $MAP -c column=$LABEL_COLUMN`

(
for ITEM in $ITEMS ; do
   v.out.ascii $MAP column=$LABEL_COLUMN where="$LABEL_COLUMN = '$ITEM'" | \
     awk -F'|' \
      'BEGIN { sum_x=0; sum_y=0; i=0 }
       { sum_x += $1; sum_y += $2; i++ }
       END { if(i>0) { printf("%.15g|%.15g|%s\n", sum_x/i, sum_y/i, $4) } }'
done
) | v.in.ascii out=${MAP}_avg_$LABEL_COLUMN columns="x double, y double, $LABEL_COLUMN varchar(255)"

d.vect $MAP -c color=none type=area

d.vect ${MAP}_avg_$LABEL_COLUMN disp=attr attrcol=$LABEL_COLUMN \
  lcolor=black lsize=14 xref=center yref=center bgcolor=white



(of course this assumes that your clusters of points are gregarious)


might be nice to write that one up in python and sent it to the addons
repo...


Hamish



      


More information about the grass-user mailing list