How to create a legend

    Many years ago I participated in brevet: bicycle marathons, where one by one and leisurely (but with a time limit) you need to travel from 200 to 1200 kilometers. A meditative lesson, when there is time to enjoy nature, sing all the songs, consider the legend and the route map ... Which are depressing: often this is an ordinary Excel table with random design, and the map is optional. St. Petersburg club “Baltic Star” is more advanced than many: the participants of its brevet always received a card - albeit a dark gray one, with a dark gray route and a checkpoint on black callouts. Not very comfortable. And since I became interested in OpenStreetMap, I felt that I was able to improve the design of legends. And he embarked on a second attempt (the first, six years ago, ashamed to recall).


    The main characteristic of OpenStreetMap: unlimited possibilities and a black hole instead of ready-made solutions. To map a route, you need to make not only a route, but also a map. Without exception, all existing styles based on OSM are either not suitable for displaying a route, or are blatantly disgusting, and it is better to lay on them a thick sheet of cardboard. Therefore, I do the usual actions: install mapnik, postgis, osm2pgsql, osmosis, download a 25-gigabyte planet, cut out an area, load it into the database. There is data, now we need to draw it up.


    In 2010, MapBox invented the CartoCSS format and released the beautiful TileMill editor : in fact, the CartoCSS → Mapnik XML converter, the interface to the map reader plus a style editor with highlighting. This radically changed the preparation of map styles: now you do not need to write XML with your hands or learn even more intricate "simplifying" analogues: the main merit of MapBox was not even the CartoCSS format, but the detailed documentation for each of its keys and a convenient multi-platform installer. Now, indeed, anyone can create a card style from scratch to an impressive card.

    Textbooks are on a GIS-Lab and on a geek magazine, but they will only help deal with the buttons. There is no difficulty in coloring the lines from the database, but it is important to know exactly what to display on the map, and to perfectly understand the specifics of the source data. In OpenStreetMap this is not easy: for example, go and draw up the built-up territories. OSM is a map for editors, not for users, the data model in it is completely creepy in places. Nobody has normally solved the problem of generalization so far, so on a small scale you still have to draw every bend of a small rivulet and every path of a railway sorting station. So yes, the main thing is to know exactly which objects to draw (less is better, a universal principle), and plunge into the wiki and in examples in order to understand how to select them from the data mash.

    The goal is clear: to make a style for printing on a black and white printer, on a scale of 9-11 (2-5 km in centimeter). The main roads are important, their coverage is useless, and the maximum of settlements. And horizontally. With the latter, it is interesting: what decision do stylists come to after digging in the Internet for half an hour? Of course, take the SRTM, process gdal_contour and flop on the card as is. It is not difficult, there are dozens of instructions, and the output is something like this:

    Using such horizontals is impossible. There are so many of them that they turn into a uniform, uninformative background. In addition, cropped to 60 latitudes. When I asked what to do, at the GIS Lab forum they came up with the idea: why SRTM on such a small scale, when you can take a less accurate relief, but covering the whole globe? In the end, I found the gorgeous GMTED2010, connected several of its squares and additionally smoothed it in the SAGA GIS. Contours need to be prepared in gdal_contour, because it correctly directs the lines in the resulting vector file, allowing you to draw bergstrikes: small serifs showing the direction of the slope. I added them “because I can”, but later I realized that without berths the contours do not make sense: how to distinguish a ravine from an embankment if only horizontals divisible by 100 m are signed?

    The need to overlay the route line led to the removal of all signatures away from the roads. Small-scale printing requires the signature of even villages where only large cities are usually indicated. And the dots per inch density of the printer allowed us to reduce font sizes by 1-2 points relative to other web cards. This post is in shtosmedraws attention to details that make the resulting Veloroad mapostyle the best for its task. Here I will make the most important remark to future map authors: never, hear, never draw metro stations at station = subway points . Dozens of newcomers drag underground platforms under the station halls every week, because on the map these underground stations are marked at a convenient scale of 12+, and the metro entrances themselves are only 18+. Some details of my style have already begun to penetrate into strangers, and I hope this aspect also spreads.

    Lyrical digression: drawing under the renderer is a scourge of the OpenStreetMap project. Most participants, including 99% of newcomers, perceive the map editor as a graphic editor, and fill in the area: the territory of the city is covered with landuse = residential instead of the invisible place = * , because it becomes pleasantly gray, garbage cans mean amenity = recycling , because the right waste_disposal is not displayed on It comes to the point of absurdity: parks are denoted by a circuit from highway = trunk , because it is green. One Indian masterfully mastered the "vector editor" to perfection by adding volume to the map (this is a two-dimensional picture):

    The Veloroad style can be compared with others on the demo site. After it, viewing alternatives (including Yandex maps) is difficult: blood and tears flow from the eyes, an endless void discourages. There are only two styles, comparable to or superior to it: it is MapSurfer with divine arrangement of signatures and with an abundance of data. All of them, as well as pictures, tracks, public transport, etc. are included in the list of layers of .


    What is the point in a map style that cannot be shown to all your friends? You can’t boast, it helps to move towards the goal. In addition, since I got such an awesome style, I need to give people the opportunity to use it in any form. So, having studied the offers, I took a ten-dollar server in DigitalOcean ($ 10 for 30 gigabytes of SSD! Blows the brain) and deployed a tile server on it. Deployment instructions mass:

    As you might guess, there are many links, because none of them are exhaustive. Technologies change faster than they are described: style has long moved to CartoCSS, renderd got an alternative in the form of Tirex, many packages have long been in the system repositories. No matter how ordinary your OS is (i.e. ubuntu), anyway, when setting up the server (starting with mod_tile) you have to play a trick, and the quality of the server settings will depend on your ability to find and process information.

    I downloaded a small region, only 17 gigabytes, but updated it with minute diffs of the whole planet. This gave an increase of 650 megabytes per day. Every ten days the disk space ran out, it was necessary to recreate the database on the local machine and upload it to the server: its resources were not enough to run osm2pgsql. On the fourth fill, patience ran out, and I sat down for programming. I uploaded the result to github : script for trimming planetary diff on the polygon and osm2pgsql database. It is imperfect, but the daily gain fell fourfold. Once a month, you can start refilling. The ideal option, as one Frenchman noted in the mailing list, would be to teach osm2pgsql to read the augmented diffs and not to store intermediate data in the database (they occupy about 60% of the volume).

    Despite the fact that the base was entirely North-Western, Central and Far Eastern districts, every second asked to add his city, unexpectedly far from the borders of the crop. The mapper and Self-Perfection admin helped to cope with the requests, sharing the burden of paying for the server, so I increased the base by another 10 gigabytes. Now the map lacks only the Southern and North Caucasian districts, but the demand from there is small. Ah, and there is no Crimea either.

    I don’t remember anymore who in our chat talked about retin tiles. At that moment, I suddenly realized: on my laptop, because of the excessive number of pixels, I look at all sites on a scale of 133%. The text looks good, but the maps are solid soap (google maps is a nice exception). I’ve already gotten to know how to configure a map - why not try making 2: 1 tiles for HiDPI mode? One small problem: the only demonstration of such tiles in my memory worked on Tirex, and I had an older renderd. In general, my first commit to a C ++ project followed. On « Veloroad the Retina » I sometimes I go just like that, without the need: to clear lines and high-quality fonts resting the eyes, and it seems that raster maps have a future.


    We return to the task. The data is uploaded to the database, the map is configured, the style is written, you need to get a map of the specified rectangle in vector and raster formats at 300 dpi for printing on page A5. Simple operation, where is the tool? Oh, for rendering a picture with a map-reader scripts as much as half a dozen - from a simple generate_image, where the coordinates of the angles of the area should be written in the source, to nik2img, which can have different projections and formats. That's just nobody accepts millimeters, and having tried to request a snapshot of the map with a given zoom at a given point for verification, I did not get what I see on There is also a problem with resolution: there is a scale_factor parameter, but if you change it, only the lines become thicker, covering everything around. What the hell, how do they use this ?!

    And again, do it yourself. I sat down for a python and made a cool one in a weekNik4 script . Quite simple, except for the sheets outlined by the schemes and the ragged source mapnik. The main thing is reliability and ten kilobytes of illustrated documentation. If you know what you want to get, the solution is elementary. From the simplest --url osm.xml screenshot.png

    to the finest settings for paper size, margins, layers and dpi. No surprises, not a single mysterious parameter. I sketched an interactive bbox selectionand for a month I made cards from the command line. In addition, specially for cyclists who prefer closed and very expensive programs to free, he prepared casts of maps in his style on several scales and converted them for OziExplorer. The largest one, on the 11th scale, had a size of 21 × 22 thousand pixels: Nik4 can circumvent the map’s limitations by gluing the final map from fragments. True, img2ozf did not chew on this monster, only z10 (16k × 16k).

    The main task was to get a vector map in SVG format. Oddly enough, the dpi value affects the file: the mapnik places the letters in the signatures on the pixel grid, so kerning suffers at low dpi. Another problem with signatures is that cairo does not know how (according to the map authors, I did not check) to combine elements into groups, so each word consists of separate letters, plus their highlight (halo). The map’s signature placement algorithm is dull, like a log, so after exporting it would be nice to shift the signatures from the roads and the route, but this is 2 × the number of letters of the curves that very annoying to highlight. Back to python: the mapnik-group-text script collects the letters of signatures and their halo into groups.

    Early June for poster contestat the conference “State of the Map EU” unloaded a huge SVG with the surroundings of Karlsruhe in the style of veloroad. Editing files larger than 100 megabytes in Inkscape is difficult, so I didn’t move the signatures, I just added a text box. The poster took fifth place in the vote, but was the first to disappear from the exhibition: no wonder, it was the most practical there. I managed to be the second: I stole the bright "Pop Art", which now hangs on my wall.


    The command line is fine, but normal users need a GUI. I’m not doing this whole system for myself, but for cyclists: they should receive legends and maps with a minimum of effort, so that it is not much more complicated than before, with Excel and OziExplorer screenshots. The above-mentioned snapshots already allow you to take screenshots, but you can still simplify it, according to the popular “click on the button, you get the result” algorithm. I used to turn to php (web) or perl (cgi) for simple sites, but since I started to write Python scripts, I went to study cgi-programming on it.

    Service " Get Veloroad"- the most epic, in my opinion, mapping application on the Russian Internet this year. For those who need a map snapshot, at least. In fact, this is the web interface to Nik4: the same size, margin, and format options, plus a few parametric fields: GPX layer and linear scale. Unlike tiles, additional styles do not require a place for a cache, so I added several styles to choose from: the same veloroad, but without forced Russification, and the standard style. In other words, now you do not need to take screenshots or glue tiles : just select the desired area, image size, png format, and press the button. Participants of the "people's cards", waiting for the weekly handouts of printed cards, then have to think about what they spend time on.

    But no, it's still messy. Another feature of the map-reader is in the way: if you tell him that you want a 360 dpi picture, the size of the resulting SVG will be quadrupled, as if there were no dpi, and the cairo library in the width and height fields, for some reason, writes the sizes in pixels with a suffix "Pt", increasing the picture by 1.25 times. Of course, if you export such an SVG to PDF, the page size will be surprising. So the file needs to be scaled. I was already preparing to recount the numbers in each attribute of each tag, as I discovered a discrepancy between the width / height and the viewBox attribute. I read the specification, and here it is: you don’t need to process all the XML, just recalculate these six numbers. Conversely, to notepad and geometric constructions!

    Suppose it turned out, but there is one more task: when you export a picture with fields to a centimeter, vector objects are not cut off at the border, but stick out well, therefore it is not enough to expand the viewBox, you still need to close the fields with a white frame. Ideally, it would be nice to crop objects yourself, but this is a difficult and not burning task (and cropping does not cancel the frame, because thick strokes will stick out). Just a couple of days of calculating the coordinates, and the svg-resize script is ready and implemented in Get Veloroad: now, downloading the map for A5 with 7 mm margins in SVG, after exporting the file to PDF, you get exactly this: white margins and the exact page size. Of course, this does not apply to downloading PDF directly from the site.

    Automatic resizing along the way solved another problem: a set of graphic elements for creating a brevet (start, finish and KP markers, name, logo and arrows) when importing to a sheet no longer needs to be scaled, just ungroup, pull it to the right places and correct the text. Making a route map has become faster and more enjoyable, and the whole process is built on open source programs.


    The card is not important for shavers: having only it in your hands, it is easy to get lost at some tangled intersection. The most important thing is an understandable legend, a verbal description of the route with a clear direction of movement at each turn and the distances between points. Step-by-step automatic descriptions will not work: after 10 hours of pedaling, I don’t want to guess which of the right turns I had in mind, but go by the sign “Kukuevo 50 ►” . Before me, legends were printed from Excel, they were too lengthy, and because A4 sheet cannot be attached to the steering wheel, an accidental fly could close a paragraph. Each new route author drew a table from scratch, sometimes even calculating the opening and closing times of the control box on a calculator.


    Building a legend takes a lot of time. Even without going to the track, but simply opening the Google panorama in one window, the OSRM site in another window for calculating distances, you can spend half a day searching for unique signs of rotation, formulating unambiguous phrases, formatting the table. This can be facilitated by formalizing the process so that traveling through panoramas takes up not 80, but 80% of the time. So I made a legend template in LibreOffice, using the open PT Sans font. Of course, it’s not good to force people to install left-handed programs, but it is better to rely on open formats right away than to upload files in proprietary xls depending on the Microsoft font, which is forbidden to be distributed under license.

    Then everything is simple, an animated picture that has eaten into the brain helpsabout simplification of tables. He removed the grid, but highlighted even lines with light gray to make it easier to track on the road: light turn, dark turn. Removed extra columns by entering the necessary information (CP number, for example) directly into the text. I made the description of the turn itself obligatory, and some people strive to leave only mileage and direction: bikers will not pass the pointer now. The text is printed in tenth size, kilometers - even larger. Only keywords are highlighted: “left”, “right”, “KP”. All legends are collected in an archive, from where you can copy pieces of routes, and see how to correctly designate a particular situation (T-junction, road numbers in Finland, springs, etc.).

    On the second sheet of the table, time stamps for each turn are automatically calculated: when the volunteers need to be at the point to catch the first moose, and when the point needs to be closed according to the ACP regulation. There are standards for brevet passage: from 13.5 hours per 200 km to 90 per 1200. Up to 600 km, time can be easily calculated by dividing the distance by 15 km / h. The speed of the "moose", the fastest cyclists, is considered to be 32–34 km / h in the first stage, and then in descending order. Of course, on long brevets you should not forget about the date. The calculated values ​​are copied to the description of the KP and finish. When finished, the legend needs to be exported to PDF.


    Now we have PDF legends and one or more PDFs with maps, all in A5 format, as cyclists will use them. A4 sheets are usually downloaded to printers, so you need to not only combine several files into one, but also glue the pages in pairs, turning them if necessary. Windows users, which, judging by the file formats, are all marathon runners, have to dodge here: although you can shuffle pages in PDF Split and Merge , gluing them is a much more difficult task. At first I made a template from two columns, and split the picture with the map, but this approach was suitable only for short brevets, the legend of which fit on one sheet of A5. How to be

    For Linux, all repositories have the magic pdftk and pdfjam programs that solve any PDF preparation tasks that may come to mind. I’ve used them for a month, but you can’t tell normal people to install a virtual machine only to prepare a couple of documents. And pdfjam, by the way, pulls up dependencies by half a gigabyte. Without creating a web interface can not do. First, I decided to cut the corner: I placed four PDF fill forms on the page and a line for shuffling pages in pdftk (“A1 B A2 C” - first page of document A, document B, second page ... well, that's clear). Using the button, the cgi script called pdftk and pdfjam, and returned the finished PDF. It is often crooked because it is difficult to prevent mistakes in the pdftk line from the first try. And the instructions for the authors were supplemented with three paragraphs of the format description, plus a link to PDFsam and its description, as a fallback for the busiest. Not a thing.

    Formally, the problem has been solved: it’s enough for the authors to install two programs and one font, and we go through the whole process from start to finish. There would be a fairy tale end, to unsubscribe in PM and goodbye, already wrote the text of the letter ... but no, I can not. Inaccurate. Convincing himself to learn at least Flask, created a new directory and launched vim. For a week I looked at the documentation of the framework (very good, by the way), pulled a couple of lines from the textbook on the Habré , lazily wrote templates and copied pastes from cgi-crypt. At a certain point: “bah, it's almost ready” - and in two evenings I quickly finished, tested, uploaded to the server, finished again and reloaded. It turned out awesome: you only need to be able to press a button, but to distinguish between left and right; instead of the line for pdftk - a visual editor, all documents are validated, and you can use pictures as pages. Of course, ImageMagick could not do without it: they create page thumbnails, and raster maps are converted to PDF. With the help of identify, the necessity of turning the pages is determined, therefore in the interface there are no changes in orientation, only a revolution. The system, however, is locked to a password, because the disk is not rubber, and resources to other processes are more important.

    So what?

    The archive of maps and legends, short and detailed instructions, and all the links are collected on the page for authors , a link to which I sent to the six authors with whom I interacted this year. By the end of the year there are only five brevet left, the legends for which I will make myself to build up the base. I hope to participate in one of them. I consider the project completed, although its success will be clear only next year: will the authors use my services, or will they return to Excel and OziExplorer?

    No one needs printed cards now, they reminded me of OpenStreetMap. Tourists take navigators with them, seasoned tourists buy an atlas with maps “for the general staff”, and ordinary people need just web maps on their smartphones. St. Petersburg's weekly Friday night bike rides publish route maps rather as a tribute to tradition: the substrates they use are unreadable, but no one complains: just follow the host and hundreds of red beacons, you won’t get lost. When was the last time I opened the atlases in the pocket of the passenger seat of my car? I do not remember.

    But there is a lesson for which the ability to quickly print cards is vital. In the literal sense: this is humanitarian aid and rescue work. In Africa, Indonesia, Haiti, and Pakistan, thousands of doctors, rescuers, volunteers simply draw and update OpenStreetMap, which they can then print and use in their work. In third world countries, you cannot rely on batteries and an Internet feed. To collect cartographic data, they use “bypass sheets”: small fragments of the map on a very large scale, on top of which they draw the missing objects. Honestly, in the same primitive way, data is collected in Russia, and in Germany, and in the UK. However, there is still no convenient way to print bypass sheets for future use. In the process of preparing printed cards, I learned a lot of relevant technologies, and this will help me in the future to optimize the printing process of bypass sheets, already without multi-gigabyte servers and complex components. Soon all these people will be enough to click on the button.

    Also popular now: