|
mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
|
#include <climits>#include <cmath>#include <cstdio>#include <cstdlib>#include <cstring>#include <dirent.h>#include <exception>#include <glib.h>#include <map>#include <mapnik/agg_renderer.hpp>#include <mapnik/color.hpp>#include <mapnik/datasource.hpp>#include <mapnik/datasource_cache.hpp>#include <mapnik/font_engine_freetype.hpp>#include <mapnik/image.hpp>#include <mapnik/image_util.hpp>#include <mapnik/image_view.hpp>#include <mapnik/image_view_any.hpp>#include <mapnik/layer.hpp>#include <mapnik/load_map.hpp>#include <mapnik/map.hpp>#include <mapnik/params.hpp>#include <mapnik/pixel_types.hpp>#include <mapnik/version.hpp>#include <pthread.h>#include <string>#include <sys/time.h>#include <unistd.h>#include <utility>#include <mapnik/box2d.hpp>#include "cache_expire.h"#include "g_logger.h"#include "gen_tile.h"#include "metatile.h"#include "parameterize_style.hpp"#include "protocol.h"#include "render_config.h"#include "renderd.h"#include "request_queue.h"#include "store.h"Classes | |
| struct | projectionconfig |
| Spatial reference system (SRS) bounds and grid aspect ratio for a map projection. More... | |
| struct | xmlmapconfig |
| Per-thread runtime configuration and Mapnik state for a single configured map style. More... | |
Macros | |
| #define | DEG_TO_RAD (M_PI / 180) |
| Conversion factor from degrees to radians. | |
| #define | RAD_TO_DEG (180 / M_PI) |
| Conversion factor from radians to degrees. | |
| #define | RENDER_SIZE (256 * (METATILE + 1)) |
| Default initial pixel dimension (width and height) for a loaded Mapnik Map. | |
| #define | image_data_32 image_rgba8 |
| Compatibility alias mapping legacy Mapnik 2 image_data_32 to Mapnik 3+ image_rgba8. | |
| #define | image_32 image_rgba8 |
| Compatibility alias mapping legacy Mapnik 2 image_32 to Mapnik 3+ image_rgba8. | |
Functions | |
| struct projectionconfig * | get_projection (const char *srs) |
| Determine projected world bounds and grid aspect ratios from a PROJ/SRS string. | |
| static void | load_fonts (const char *font_dir, int recurse) |
| Register FreeType font files from a directory with Mapnik's font engine. | |
| static void | parameterize_map_max_connections (Map &m, int num_threads) |
| Scale Mapnik layer datasource connection pools to match the configured thread count. | |
| static int | check_xyz (int x, int y, int z, struct xmlmapconfig *map) |
| Validate whether tile coordinates (x, y, z) fall within valid bounds for map. | |
| mapnik::box2d< double > | tile2prjbounds (struct projectionconfig *prj, int x, int y, int z) |
| Compute the projected bounding box for a metatile anchored at (x, y, z). | |
| static enum protoCmd | render (struct xmlmapconfig *map, int x, int y, int z, char *options, metaTile &tiles) |
| Render a metatile using Mapnik and slice it into individual encoded tiles. | |
| void | render_init (const char *plugins_dir, const char *font_dir, int font_dir_recurse) |
| Initialize global Mapnik datasource plugins and FreeType fonts. | |
| void * | render_thread (void *arg) |
| Worker thread entry point that initializes per-thread Mapnik instances and processes render requests. | |
Variables | |
| static pthread_mutex_t | map_load_mutex = PTHREAD_MUTEX_INITIALIZER |
| Mutex serializing Mapnik XML stylesheet loading and font registration across render threads. | |
| #define DEG_TO_RAD (M_PI / 180) |
Conversion factor from degrees to radians.
| #define image_32 image_rgba8 |
Compatibility alias mapping legacy Mapnik 2 image_32 to Mapnik 3+ image_rgba8.
| #define image_data_32 image_rgba8 |
Compatibility alias mapping legacy Mapnik 2 image_data_32 to Mapnik 3+ image_rgba8.
| #define RAD_TO_DEG (180 / M_PI) |
Conversion factor from radians to degrees.
| #define RENDER_SIZE (256 * (METATILE + 1)) |
Default initial pixel dimension (width and height) for a loaded Mapnik Map.
|
static |
Validate whether tile coordinates (x, y, z) fall within valid bounds for map.
Checks that z is within [map->minzoom, map->maxzoom] and that x and y lie within [0, (2^z * aspect) - 1]. Logs an informational message if out of bounds.
| x | Tile X coordinate. |
| y | Tile Y coordinate. |
| z | Tile zoom level. |
| map | Map configuration containing zoom limits and projection aspect ratios. |
| struct projectionconfig * get_projection | ( | const char * | srs | ) |
Determine projected world bounds and grid aspect ratios from a PROJ/SRS string.
Recognizes Web Mercator (+proj=merc +a=6378137 +b=6378137), Plate Carree (+proj=eqc ...), and British National Grid (+proj=tmerc ... OSGB36). Falls back to Web Mercator bounds with a warning if the SRS string is unrecognized.
| srs | Mapnik spatial reference system (PROJ) string. |
|
static |
Register FreeType font files from a directory with Mapnik's font engine.
Verifies that font_dir can be opened, registers all discovered font faces (optionally recursing into subdirectories), and logs each registered face name and source file path at G_LOG_LEVEL_DEBUG.
| font_dir | Filesystem path to the directory containing font files. |
| recurse | Non-zero to search subdirectories recursively, 0 for top-level only. |
|
static |
Scale Mapnik layer datasource connection pools to match the configured thread count.
Iterates through every layer in m; if a datasource does not already specify max_size, sets max_size to num_threads + 2 and recreates the datasource instance so concurrent render threads do not starve waiting for database connections.
| m | Mapnik Map instance whose layer datasources will be updated. |
| num_threads | Number of active rendering threads configured for this map. |
|
static |
Render a metatile using Mapnik and slice it into individual encoded tiles.
Selects either the base Mapnik Map or a cached parameterized Map variant, renders the full metatile extent into an RGBA buffer via mapnik::agg_renderer, and extracts each sub-tile view into tiles encoded in map->output_format.
| map | Map style configuration and Mapnik state for this worker thread. |
| x | Top-left tile X coordinate of the metatile. |
| y | Top-left tile Y coordinate of the metatile. |
| z | Tile zoom level. |
| options | Style parameterization options string (used when parameterize_function is set). |
| tiles | Output metaTile container populated with encoded sub-tile images. |
| void render_init | ( | const char * | plugins_dir, |
| const char * | font_dir, | ||
| int | font_dir_recurse ) |
Initialize global Mapnik datasource plugins and FreeType fonts.
Must be called once during renderd startup before spawning worker threads.
| plugins_dir | Directory path containing Mapnik input datasource plugins (.input). |
| font_dir | Directory path containing font files for Mapnik's FreeType engine. |
| font_dir_recurse | Non-zero to recursively load fonts from subdirectories of font_dir. |
| void * render_thread | ( | void * | arg | ) |
Worker thread entry point that initializes per-thread Mapnik instances and processes render requests.
Each worker thread maintains its own xmlmapconfig array (because mapnik::Map is not safe for concurrent rendering across threads). After loading all configured XML stylesheets and storage backends, the thread loops indefinitely pulling items from render_request_queue, rendering metatiles, saving them to storage, optionally sending HTCP cache purges, and replying to the requesting client.
| arg | Pointer to the shared array of xmlconfigitem definitions (XMLCONFIGS_MAX elements). |
| mapnik::box2d< double > tile2prjbounds | ( | struct projectionconfig * | prj, |
| int | x, | ||
| int | y, | ||
| int | z ) |
Compute the projected bounding box for a metatile anchored at (x, y, z).
Converts top-left-origin slippy map tile coordinates into bottom-left-origin projected coordinates (box2d<double>). Automatically clamps the metatile span to the world grid dimensions at very low zoom levels where 2^z < METATILE.
| prj | Projection bounds and aspect ratio configuration. |
| x | Top-left tile X coordinate of the metatile. |
| y | Top-left tile Y coordinate of the metatile. |
| z | Tile zoom level. |
|
static |
Mutex serializing Mapnik XML stylesheet loading and font registration across render threads.
Prevents thread-safety issues inside Mapnik/libxml2 and FreeType during concurrent startup initialization.