Skip to content

Commit

Permalink
Support different map files per zoom level. This requires appending a…
Browse files Browse the repository at this point in the history
… dot

and zoom level to the mapfile parameter in the config file, e.g.
"mapfile.12=something.xml" - any zoom level for which no special map
file is set will use the one defined with mapfile=...
  • Loading branch information
woodpeck committed Aug 13, 2021
1 parent 8dbaa59 commit 7e7c3e7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 20 deletions.
46 changes: 38 additions & 8 deletions backend-mapnik/metatilehandler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define MERCATOR_WIDTH 40075016.685578488
#define MERCATOR_OFFSET 20037508.342789244

MetatileHandler::MetatileHandler(const std::string& tiledir, const std::string& stylefile, unsigned int tilesize, double scalefactor, int buffersize, unsigned int mtrowcol, const std::string& imagetype) :
MetatileHandler::MetatileHandler(const std::string& tiledir, const std::map<std::string, std::string>& stylefiles, unsigned int tilesize, double scalefactor, int buffersize, unsigned int mtrowcol, const std::string& imagetype) :
mTileWidth(tilesize),
mTileHeight(tilesize),
mMetaTileRows(mtrowcol),
Expand All @@ -45,7 +45,35 @@ MetatileHandler::MetatileHandler(const std::string& tiledir, const std::string&
mScaleFactor(scalefactor),
mTileDir(tiledir)
{
load_map(mMap, stylefile);
for (unsigned int i=0; i<=MAXZOOM; i++)
{
mPerZoomMap[i]=NULL;
}

for (auto itr = stylefiles.begin(); itr != stylefiles.end(); itr++)
{
if (itr->first == "")
{
load_map(mMap, itr->second);
debug("load %s without zoom restrictions", itr->second.c_str());
}
else if (itr->first.at(0) != '.')
{
throw std::invalid_argument("malformed mapfile config postfix '" + itr->first + "'");
}
else
{
char *endptr;
long int num = strtol(itr->first.c_str()+1, &endptr, 10);
if (*endptr || (num<0) || (num>MAXZOOM))
{
throw std::invalid_argument("malformed mapfile config postfix '" + itr->first + "'");
}
mPerZoomMap[num] = new mapnik::Map;
debug("load %s for zoom %d", itr->second.c_str(), num);
load_map(*(mPerZoomMap[num]), itr->second);
}
}

fourpow[0] = 1;
twopow[0] = 1;
Expand Down Expand Up @@ -99,6 +127,7 @@ const NetworkResponse *MetatileHandler::handleRequest(const NetworkRequest *requ
rr.south = (twopow[z] - y - mtr) * MERCATOR_WIDTH / twopow[z] - MERCATOR_OFFSET;
rr.scale_factor = mScaleFactor;
rr.buffer_size = mBufferSize;
rr.zoom = z;

// we specify the bbox in epsg:3857, and we also want our image returned
// in this projection.
Expand Down Expand Up @@ -250,6 +279,7 @@ const RenderResponse *MetatileHandler::render(const RenderRequest *rr)
{
debug(">> MetatileHandler::render");
char init[255];
mapnik::Map *map = mPerZoomMap[rr->zoom] ? mPerZoomMap[rr->zoom] : &mMap;

sprintf(init, "+init=epsg:%d", rr->srs);
// commented out - rely on proper SRS specification in map.xml
Expand Down Expand Up @@ -281,21 +311,21 @@ const RenderResponse *MetatileHandler::render(const RenderRequest *rr)
}

mapnik::box2d<double> bbox(west, south, east, north);
mMap.resize(rr->width, rr->height);
mMap.zoom_to_box(bbox);
map->resize(rr->width, rr->height);
map->zoom_to_box(bbox);
if (rr->buffer_size > -1)
{
mMap.set_buffer_size(rr->buffer_size);
map->set_buffer_size(rr->buffer_size);
}
else if (mMap.buffer_size() < 128)
else if (map->buffer_size() < 128)
{
mMap.set_buffer_size(128);
map->set_buffer_size(128);
}

debug("width: %d, height:%d", rr->width, rr->height);
RenderResponse *resp = new RenderResponse();
resp->image = new mapnik::image_32(rr->width, rr->height);
mapnik::agg_renderer<mapnik::image_32> renderer(mMap, *(resp->image), rr->scale_factor, 0u, 0u);
mapnik::agg_renderer<mapnik::image_32> renderer(*map, *(resp->image), rr->scale_factor, 0u, 0u);
try
{
renderer.apply();
Expand Down
3 changes: 2 additions & 1 deletion backend-mapnik/metatilehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class MetatileHandler : public RequestHandler
{
public:

MetatileHandler(const std::string& tiledir, const std::string& stylefile, unsigned int tilesize, double scalefactor, int buffersize, unsigned int mtrowcol, const std::string & imagetype);
MetatileHandler(const std::string& tiledir, const std::map<std::string,std::string>& stylefiles, unsigned int tilesize, double scalefactor, int buffersize, unsigned int mtrowcol, const std::string & imagetype);
~MetatileHandler();
const NetworkResponse *handleRequest(const NetworkRequest *request);
void xyz_to_meta(char *path, size_t len, const char *tile_dir, int x, int y, int z) const;
Expand All @@ -67,6 +67,7 @@ class MetatileHandler : public RequestHandler
double mScaleFactor;
std::string mTileDir;
mapnik::Map mMap;
mapnik::Map *mPerZoomMap[MAXZOOM+1];
};

#endif
Expand Down
16 changes: 5 additions & 11 deletions backend-mapnik/renderd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ bool RenderDaemon::loadMapnikWrapper(const char *configfile)

char linebuf[255];
std::string tiledir;
std::string mapfile;
std::map<std::string, std::string> mapfiles;
std::string stylename;
unsigned int tilesize = 256;
unsigned int mtrowcol = 8;
Expand Down Expand Up @@ -85,9 +85,9 @@ bool RenderDaemon::loadMapnikWrapper(const char *configfile)
{
tiledir.assign(eq);
}
else if (!strcmp(line, "mapfile"))
else if (!strncmp(line, "mapfile", 7))
{
mapfile.assign(eq);
mapfiles.insert(std::pair<std::string, std::string>(line+7, eq));
}
else if (!strcmp(line, "scalefactor"))
{
Expand Down Expand Up @@ -137,18 +137,12 @@ bool RenderDaemon::loadMapnikWrapper(const char *configfile)
}
fclose(f);

if (mapfile.empty())
if (mapfiles.empty())
{
warning("cannot add %s: missing mapfile option", configfile);
return rv;
}

if (access(mapfile.c_str(), R_OK) == -1)
{
warning("cannot add %s: map file '%s' not accessible", configfile, mapfile.c_str());
return rv;
}

if (tiledir.empty())
{
warning("cannot add %s: missing tiledir option", configfile);
Expand All @@ -169,7 +163,7 @@ bool RenderDaemon::loadMapnikWrapper(const char *configfile)

try
{
mHandlerMap[stylename] = new MetatileHandler(tiledir, mapfile, tilesize,
mHandlerMap[stylename] = new MetatileHandler(tiledir, mapfiles, tilesize,
scalefactor, buffersize, mtrowcol, imagetype);
mHandlerMap[stylename]->setStatusReceiver(this);
debug("added style '%s' from map %s", stylename.c_str(), configfile);
Expand Down
1 change: 1 addition & 0 deletions backend-mapnik/renderrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class RenderRequest
int buffer_size;
unsigned int srs;
unsigned int bbox_srs;
unsigned int zoom;
};

#endif
6 changes: 6 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
tirex (0.6.4) focal; urgency=medium

* allow adding a dot and zoom level after mapfile parameter to override map file for specific zoom levels

-- Frederik Ramm <frederik.ramm@geofabrik.de> Fri, 13 Aug 2021 11:55:10 +0200

tirex (0.6.3) bionic; urgency=medium

* modify TMS backend to take template URL parameter
Expand Down

0 comments on commit 7e7c3e7

Please sign in to comment.