mod_tile 0.9.1
Renders map tiles with Mapnik and serves them using Apache HTTP Server
Loading...
Searching...
No Matches
gen_tile.cpp File Reference
#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.

Macro Definition Documentation

◆ DEG_TO_RAD

#define DEG_TO_RAD   (M_PI / 180)

Conversion factor from degrees to radians.

◆ image_32

#define image_32   image_rgba8

Compatibility alias mapping legacy Mapnik 2 image_32 to Mapnik 3+ image_rgba8.

◆ image_data_32

#define image_data_32   image_rgba8

Compatibility alias mapping legacy Mapnik 2 image_data_32 to Mapnik 3+ image_rgba8.

◆ RAD_TO_DEG

#define RAD_TO_DEG   (180 / M_PI)

Conversion factor from radians to degrees.

◆ RENDER_SIZE

#define RENDER_SIZE   (256 * (METATILE + 1))

Default initial pixel dimension (width and height) for a loaded Mapnik Map.

Function Documentation

◆ check_xyz()

int check_xyz ( int x,
int y,
int z,
struct xmlmapconfig * 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.

Parameters
xTile X coordinate.
yTile Y coordinate.
zTile zoom level.
mapMap configuration containing zoom limits and projection aspect ratios.
Returns
Non-zero (1) if coordinates are valid, 0 if out of bounds.

◆ get_projection()

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.

Parameters
srsMapnik spatial reference system (PROJ) string.
Returns
Heap-allocated projectionconfig struct (caller owns memory).

◆ load_fonts()

void load_fonts ( const char * font_dir,
int recurse )
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.

Parameters
font_dirFilesystem path to the directory containing font files.
recurseNon-zero to search subdirectories recursively, 0 for top-level only.

◆ parameterize_map_max_connections()

void parameterize_map_max_connections ( Map & m,
int num_threads )
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.

Parameters
mMapnik Map instance whose layer datasources will be updated.
num_threadsNumber of active rendering threads configured for this map.

◆ render()

enum protoCmd render ( struct xmlmapconfig * map,
int x,
int y,
int z,
char * options,
metaTile & tiles )
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.

Parameters
mapMap style configuration and Mapnik state for this worker thread.
xTop-left tile X coordinate of the metatile.
yTop-left tile Y coordinate of the metatile.
zTile zoom level.
optionsStyle parameterization options string (used when parameterize_function is set).
tilesOutput metaTile container populated with encoded sub-tile images.
Returns
cmdDone on successful render and encoding, or cmdNotDone if Mapnik throws an exception.

◆ render_init()

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.

Parameters
plugins_dirDirectory path containing Mapnik input datasource plugins (.input).
font_dirDirectory path containing font files for Mapnik's FreeType engine.
font_dir_recurseNon-zero to recursively load fonts from subdirectories of font_dir.

◆ render_thread()

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.

Parameters
argPointer to the shared array of xmlconfigitem definitions (XMLCONFIGS_MAX elements).
Returns
Always returns NULL (runs until process termination).

◆ tile2prjbounds()

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.

Parameters
prjProjection bounds and aspect ratio configuration.
xTop-left tile X coordinate of the metatile.
yTop-left tile Y coordinate of the metatile.
zTile zoom level.
Returns
Projected bounding box covering the metatile area.

Variable Documentation

◆ map_load_mutex

pthread_mutex_t map_load_mutex = PTHREAD_MUTEX_INITIALIZER
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.