Solved

MapnikRasterizer: How to rotate Text around Placement Point?

  • 9 December 2016
  • 6 replies
  • 12 views

Userlevel 2
Badge +17

Hello,

I need to render texts onto a raster. The MapnikRasterizer (Text Symbolizer) works as expected in almost all cases, but if you set the Orientation parameter, the text will be rotated around the center of the text itself, not around the specified placement point.

In this image, the red circle is the placement point and it illustrates the result that the same string "text ----- string" has been rotated with 0, 30, 60, and 90 degrees.

The requirement is to rotate the text around the placement point - the red circle in the image above. How can I do that?

icon

Best answer by redgeographics 9 December 2016, 11:11

View original

6 replies

Userlevel 5
Badge +25

There's alignment options in the MapnikRasterizer's Text style parameters, but they take effect after the orientation has been applied and are thus useless. There's also the DX and DY parameters but I suspect those are measured in pixels and again are applied after orientation.

So far the only quick fix (and a dirty fix...) I've been able to find is offsetting your text points before putting them in the MapnikRasterizer...

Badge +3

rotate displacement ?

Userlevel 2
Badge +17

There's alignment options in the MapnikRasterizer's Text style parameters, but they take effect after the orientation has been applied and are thus useless. There's also the DX and DY parameters but I suspect those are measured in pixels and again are applied after orientation.

So far the only quick fix (and a dirty fix...) I've been able to find is offsetting your text points before putting them in the MapnikRasterizer...

Hi @redgeographics, thanks for your answer. Offsetting would be a workaround, but I'm afraid that it might be difficult to compute appropriate offset amount, since the size of text rendering area could not be calculated exactly.

 

Userlevel 2
Badge +17

rotate displacement ?

Hi @gio, thanks for your answer. I found the "rotate-displacement" parameter in the native Mapnik documentation. It seems to be fit to the requirement, but I could not figure out how to set the parameter to the MapnikRasterizer. How can you apply this parameter?
Userlevel 2
Badge +11

Hi @takashi,

unfortunately, rotate displacement is a Mapnik v.3 functionality, we ship Mapnik 2.2.0 with FME. We plan to upgrade to a newer version soon, but a concrete release is not determined yet.

Meanwhile, I found a possible workaround - we can pad the labels with spaces on the left - as many as there are characters plus 2-4 more for an offset. Here is how the output looks like (a single point and twelve labels for it):

The trick here is the space in StringPadder - simple space, which (invisibly) appears after pressing the space button, does not work, so we have to use one of the other spaces such as no-break space or soft-hyphen.

The standard Windows Character Map (I assume, other platforms should have similar apps), or this page - http://alumnus.caltech.edu/~leif/namedchar.html offer a few spaces of different types and lengths.

I suspect, this might not work with all fonts, but for now, I think, it might work as a temporary solution.

The wokspace is attached: mapnik.fmw

Dmitri

 

Userlevel 2
Badge +17

Hi @takashi,

unfortunately, rotate displacement is a Mapnik v.3 functionality, we ship Mapnik 2.2.0 with FME. We plan to upgrade to a newer version soon, but a concrete release is not determined yet.

Meanwhile, I found a possible workaround - we can pad the labels with spaces on the left - as many as there are characters plus 2-4 more for an offset. Here is how the output looks like (a single point and twelve labels for it):

The trick here is the space in StringPadder - simple space, which (invisibly) appears after pressing the space button, does not work, so we have to use one of the other spaces such as no-break space or soft-hyphen.

The standard Windows Character Map (I assume, other platforms should have similar apps), or this page - http://alumnus.caltech.edu/~leif/namedchar.html offer a few spaces of different types and lengths.

I suspect, this might not work with all fonts, but for now, I think, it might work as a temporary solution.

The wokspace is attached: mapnik.fmw

Dmitri

 

Hi @dmitribagh, thanks for the tip. This can be very helpful when the possible vertical alignment was limited to Middle. However, every alignment should be possible in the actual requirement and this method seems not to be applicable to Top and Bottom alignments.I finally found a workaround today.
  1. Once render the text with a specific alignment (e.g. Middle-Middle) and no rotation.
  2. Create the Data MRB Extents area from the raster.
  3. Offset the area depending on the required alignment.
  4. Rotate the area around the original placement point.
  5. Replace the area with its center point.
  6. Render the text at the center point, with Middle-Middle alignment and the specified orientation angle.
Workspace (FME 2016.1.3): mapnik-rotate-displacement.fmw

 

Result - Alignment: Left-Bottom

 

 

It's dirty and also inefficient, but sufficient to my project.

 

I accept @redgeographics's answer, since my workaround has been inspired from their suggestion - offsetting. Thanks everyone!

Reply