Skip to content

Commit 76a3421

Browse files
authored
Merge pull request #150 from tomhel/ruleset
Adds support for zoom level rules
2 parents b35b749 + 78c4e47 commit 76a3421

File tree

6 files changed

+486
-41
lines changed

6 files changed

+486
-41
lines changed

include/mapcache.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,8 @@ typedef struct mapcache_image mapcache_image;
105105
typedef struct mapcache_grid mapcache_grid;
106106
typedef struct mapcache_grid_level mapcache_grid_level;
107107
typedef struct mapcache_grid_link mapcache_grid_link;
108+
typedef struct mapcache_rule mapcache_rule;
109+
typedef struct mapcache_ruleset mapcache_ruleset;
108110
typedef struct mapcache_context mapcache_context;
109111
typedef struct mapcache_dimension mapcache_dimension;
110112
typedef struct mapcache_requested_dimension mapcache_requested_dimension;
@@ -839,6 +841,11 @@ struct mapcache_cfg {
839841
*/
840842
apr_hash_t *grids;
841843

844+
/**
845+
* hashtable containing (pre)defined rulesets
846+
*/
847+
apr_hash_t *rulesets;
848+
842849
/**
843850
* the format to use for some miscelaneaous operations:
844851
* - creating an empty image
@@ -899,9 +906,11 @@ MS_DLL_EXPORT mapcache_cache* mapcache_configuration_get_cache(mapcache_cfg *con
899906
mapcache_grid *mapcache_configuration_get_grid(mapcache_cfg *config, const char *key);
900907
MS_DLL_EXPORT mapcache_tileset* mapcache_configuration_get_tileset(mapcache_cfg *config, const char *key);
901908
mapcache_image_format *mapcache_configuration_get_image_format(mapcache_cfg *config, const char *key);
909+
mapcache_ruleset *mapcache_configuration_get_ruleset(mapcache_cfg *config, const char *key);
902910
void mapcache_configuration_add_image_format(mapcache_cfg *config, mapcache_image_format *format, const char * key);
903911
void mapcache_configuration_add_source(mapcache_cfg *config, mapcache_source *source, const char * key);
904912
void mapcache_configuration_add_grid(mapcache_cfg *config, mapcache_grid *grid, const char * key);
913+
void mapcache_configuration_add_ruleset(mapcache_cfg *config, mapcache_ruleset *ruleset, const char * key);
905914
void mapcache_configuration_add_tileset(mapcache_cfg *config, mapcache_tileset *tileset, const char * key);
906915
void mapcache_configuration_add_cache(mapcache_cfg *config, mapcache_cache *cache, const char * key);
907916

@@ -1063,6 +1072,12 @@ struct mapcache_grid_link {
10631072
mapcache_extent_i *grid_limits;
10641073
int minz,maxz;
10651074

1075+
/**
1076+
* rules (mapcache_rule) for each zoom level
1077+
* index in array = zoom level
1078+
*/
1079+
apr_array_header_t *rules;
1080+
10661081
/**
10671082
* tiles above this zoom level will not be stored to the cache, but will be
10681083
* dynamically generated (either by reconstructing from lower level tiles, or
@@ -1075,6 +1090,46 @@ struct mapcache_grid_link {
10751090
apr_array_header_t *intermediate_grids;
10761091
};
10771092

1093+
/**\class mapcache_rule
1094+
* \brief a zoom level rule
1095+
*/
1096+
struct mapcache_rule {
1097+
/**
1098+
* rule for zoom level
1099+
*/
1100+
int zoom_level;
1101+
/**
1102+
* color of tiles when outside visible extent, ARGB
1103+
*/
1104+
unsigned int hidden_color;
1105+
/**
1106+
* tile to return when outside visible extent
1107+
*/
1108+
mapcache_buffer *hidden_tile;
1109+
/**
1110+
* visible extents, array of mapcache_extent
1111+
*/
1112+
apr_array_header_t *visible_extents;
1113+
/**
1114+
* visible limits, array of mapcache_extent_i
1115+
*/
1116+
apr_array_header_t *visible_limits;
1117+
};
1118+
1119+
/**\class mapcache_ruleset
1120+
* \brief a set of rules
1121+
*/
1122+
struct mapcache_ruleset {
1123+
/**
1124+
* the name of this ruleset
1125+
*/
1126+
char *name;
1127+
/**
1128+
* rules (mapcache_rule)
1129+
*/
1130+
apr_array_header_t *rules;
1131+
};
1132+
10781133
/**\class mapcache_tileset
10791134
* \brief a set of tiles that can be requested by a client, created from a mapcache_source
10801135
* stored by a mapcache_cache in a mapcache_format
@@ -1291,6 +1346,49 @@ MS_DLL_EXPORT mapcache_http_response* mapcache_core_proxy_request(mapcache_conte
12911346
MS_DLL_EXPORT mapcache_http_response* mapcache_core_respond_to_error(mapcache_context *ctx);
12921347

12931348

1349+
/* in ruleset.c */
1350+
1351+
/**
1352+
* \brief allocate and initialize a new ruleset
1353+
* @param pool
1354+
*/
1355+
mapcache_ruleset* mapcache_ruleset_create(apr_pool_t *pool);
1356+
1357+
/**
1358+
* \brief allocate and initialize a new rule
1359+
* @param pool
1360+
*/
1361+
mapcache_rule* mapcache_ruleset_rule_create(apr_pool_t *pool);
1362+
1363+
/**
1364+
* \brief clone a rule
1365+
* @param pool
1366+
* @param rule
1367+
*/
1368+
mapcache_rule* mapcache_ruleset_rule_clone(apr_pool_t *pool, mapcache_rule *rule);
1369+
1370+
/**
1371+
* \brief get rule for zoom level, or NULL if none exist
1372+
* @param ruleset
1373+
* @param zoom_level
1374+
*/
1375+
mapcache_rule* mapcache_ruleset_rule_find(apr_array_header_t *rules, int zoom_level);
1376+
1377+
/**
1378+
* \brief get rule at index, or NULL if none exist
1379+
* @param rules
1380+
* @param idx
1381+
*/
1382+
mapcache_rule* mapcache_ruleset_rule_get(apr_array_header_t *rules, int idx);
1383+
1384+
/**
1385+
* \brief check if tile is within visible extent
1386+
* @param rule
1387+
* @param tile
1388+
*/
1389+
int mapcache_ruleset_is_visible_tile(mapcache_rule* rule, mapcache_tile *tile);
1390+
1391+
12941392
/* in grid.c */
12951393
mapcache_grid* mapcache_grid_create(apr_pool_t *pool);
12961394

@@ -1333,6 +1431,7 @@ int mapcache_grid_get_level(mapcache_context *ctx, mapcache_grid *grid, double *
13331431
* \param tolerance the number of tiles around the given extent that can be requested without returning an error.
13341432
*/
13351433
MS_DLL_EXPORT void mapcache_grid_compute_limits(const mapcache_grid *grid, const mapcache_extent *extent, mapcache_extent_i *limits, int tolerance);
1434+
void mapcache_grid_compute_limits_at_level(const mapcache_grid *grid, const mapcache_extent *extent, mapcache_extent_i *limits_ptr, int tolerance, int zoom_level);
13361435

13371436
/* in util.c */
13381437
MS_DLL_EXPORT int mapcache_util_extract_int_list(mapcache_context *ctx, const char* args, const char *sep, int **numbers,

lib/cache.c

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,18 @@
3030

3131
int mapcache_cache_tile_get(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
3232
int i,rv;
33+
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
3334
#ifdef DEBUG
3435
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_get on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
3536
#endif
37+
38+
/* if tile is outside visible limits, return a blank tile */
39+
if (mapcache_ruleset_is_visible_tile(rule, tile) == MAPCACHE_FALSE) {
40+
tile->encoded_data = mapcache_buffer_create(0, ctx->pool);
41+
mapcache_buffer_append(tile->encoded_data, rule->hidden_tile->size, rule->hidden_tile->buf);
42+
return MAPCACHE_SUCCESS;
43+
}
44+
3645
for(i=0;i<=cache->retry_count;i++) {
3746
if(i) {
3847
ctx->log(ctx,MAPCACHE_INFO,"cache (%s) get retry %d of %d. previous try returned error: %s",cache->name,i,cache->retry_count,ctx->get_error_message(ctx));
@@ -79,9 +88,17 @@ void mapcache_cache_tile_delete(mapcache_context *ctx, mapcache_cache *cache, ma
7988

8089
int mapcache_cache_tile_exists(mapcache_context *ctx, mapcache_cache *cache, mapcache_tile *tile) {
8190
int i,rv;
91+
mapcache_rule *rule = mapcache_ruleset_rule_get(tile->grid_link->rules, tile->z);
8292
#ifdef DEBUG
8393
ctx->log(ctx,MAPCACHE_DEBUG,"calling tile_exists on cache (%s): (tileset=%s, grid=%s, z=%d, x=%d, y=%d",cache->name,tile->tileset->name,tile->grid_link->grid->name,tile->z,tile->x, tile->y);
8494
#endif
95+
96+
/* if tile is outside visible limits return TRUE
97+
a blank tile will be returned on subsequent get call on cache */
98+
if (mapcache_ruleset_is_visible_tile(rule, tile) == MAPCACHE_FALSE) {
99+
return MAPCACHE_TRUE;
100+
}
101+
85102
for(i=0;i<=cache->retry_count;i++) {
86103
if(i) {
87104
ctx->log(ctx,MAPCACHE_INFO,"cache (%s) exists retry %d of %d. previous try returned error: %s",cache->name,i,cache->retry_count,ctx->get_error_message(ctx));

lib/configuration.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ mapcache_cfg* mapcache_configuration_create(apr_pool_t *pool)
127127
cfg->grids = apr_hash_make(pool);
128128
cfg->image_formats = apr_hash_make(pool);
129129
cfg->metadata = apr_table_make(pool,3);
130+
cfg->rulesets = apr_hash_make(pool);
130131

131132
mapcache_configuration_add_image_format(cfg,
132133
mapcache_imageio_create_png_format(pool,"PNG",MAPCACHE_COMPRESSION_FAST),
@@ -241,6 +242,11 @@ mapcache_grid *mapcache_configuration_get_grid(mapcache_cfg *config, const char
241242
return (mapcache_grid*)apr_hash_get(config->grids, (void*)key, APR_HASH_KEY_STRING);
242243
}
243244

245+
mapcache_ruleset *mapcache_configuration_get_ruleset(mapcache_cfg *config, const char *key)
246+
{
247+
return (mapcache_ruleset*)apr_hash_get(config->rulesets, (void*)key, APR_HASH_KEY_STRING);
248+
}
249+
244250
mapcache_tileset *mapcache_configuration_get_tileset(mapcache_cfg *config, const char *key)
245251
{
246252
if(config->mode == MAPCACHE_MODE_NORMAL) {
@@ -265,6 +271,11 @@ void mapcache_configuration_add_grid(mapcache_cfg *config, mapcache_grid *grid,
265271
apr_hash_set(config->grids, key, APR_HASH_KEY_STRING, (void*)grid);
266272
}
267273

274+
void mapcache_configuration_add_ruleset(mapcache_cfg *config, mapcache_ruleset *ruleset, const char * key)
275+
{
276+
apr_hash_set(config->rulesets, key, APR_HASH_KEY_STRING, (void*)ruleset);
277+
}
278+
268279
void mapcache_configuration_add_tileset(mapcache_cfg *config, mapcache_tileset *tileset, const char * key)
269280
{
270281
tileset->config = config;

0 commit comments

Comments
 (0)