Page Nav

HIDE

Breaking News:

latest

Ads Place

Can We Transform Words into Geographies?

https://ift.tt/P5KVEBM Placing text on a map in a non-conventional way Europe’s borders with text from Wikipedia. Image by author. Maps...

https://ift.tt/P5KVEBM

Placing text on a map in a non-conventional way

Europe’s borders with text from Wikipedia. Image by author.

Maps usually have labels everywhere. These help us identify what is being represented. Pieces of text which tell us what things are, from the name of a restaurant or a street to the name of a country. However, a label is not embedded in the map, strictly speaking, it is just linked to a “geography” and gives us information about it (for example, the word “Italy” written inside Italy’s borders). But what if we changed that?

If you think about it, when you type a character, your computer is taking a couple of (bezier) curves which connect some points and plots them on a grid of pixels. Then this grid is rendered on your screen. The points which define each glyph are just preserving a relative distance among them, which lets us understand the character being written. Technically, we can assign the points some coordinates and then think about them as “geographies” which can be visualized on a map.

Source: wikimedia.org (under CC license)

From Idea to Implementation

This turned out to be much simpler than it seemed at first. The freetype library allows us to extract the contours of each glyph from a font file. We iterate over the characters in our text and travel the contours for each one forming Linestring objects (shapely library). Then we merge them all together in a MultiLinestring. Finally, we project all the points to the map centring the text at the desired coordinates. The tricky thing here is to place each character next to the previous one and account for the spacing between characters.

Below you have a short piece of code which plots text on a map given some coordinates, scale and inclination in degrees. Dependencies can be installed with:

pip install cartoframes shapely freetype

If you run that piece of code you will get the following interactive map.

Moving on to the next level

Alright, but what’s the point of doing this? Aren’t conventional labels good enough? Yes, they are good enough for 99.9% of use cases. But please, bear with me. Let’s me show you how can you twist the text and make it follow custom paths.

What we are doing here? Basically we are first generating a path of points (a spiral in the left, and a hilbert curve in the right), we then split the text in chunks and then call a new function which fits each chunk between two points. For example, if we have a path with the points A, B and C and the text “Hello World”, we fit first the word “Hello” between A and B, and then “World” between B and C. I think this is pretty neat, because you can take any .tff font file you want and use it to generate geographies in a snap (note that not only the basemap and the color, but also the font from this second map is different).

To go even further, we can take a generic LineString, decompose it and use it as a path to insert text. To get a good result it’s important to apply an interpolation to the LineString so that each pair of points (A,B) are evenly spaced.

Below you can see the names of the survivors of the first ever voyage around the world, which was led by Fernando de Magallanes in the XVI century. Of 256 people who started the expedition in august 1519, only 18 came back alive more than three years later 🫢 (spoiler alert, Magallanes wasn’t one of them)

To finish, I share with you this other map (click!) where I’ve taken the boundaries of the countries in Europe and their respective Wikipedia pages. You can see each country’s border as a description of itself ðŸ¤¯.

Here is the code I wrote for this post, in case you want to take a look.

Click on the image to visualize the live map!
Image by author. Click on it to visualize the live map!

Can We Transform Words into Geographies? was originally published in Towards Data Science on Medium, where people are continuing the conversation by highlighting and responding to this story.



from Towards Data Science - Medium https://ift.tt/XYvf60g
via RiYo Analytics

No comments

Latest Articles